gpt4 book ai didi

ios - objective-c 中的音频文件格式问题

转载 作者:行者123 更新时间:2023-11-29 12:14:35 24 4
gpt4 key购买 nike

我使用 AVAudioRecorder 编写了一个音频 WAV(录制了我的声音)文件。最终的文件格式是 WAV 文件。文件已成功保存,我可以听到自己的声音。我想将此文件发送到后端服务器(网络服务)。但是我的服务器只接受 WAV 格式的数据和 FMT 信息。它不接受我的 wav 文件,因为我的 wav 文件信息包含 FLLR、数据、FMT。我已经在 Riffpad 工具中检查了我的 WAV 文件信息。它显示 FLLR、数据、FMT。但我只想要数据和 fmt。因为我的服务器端只接受数据和 FMT。那么请建议如何以编程方式删除我的 wav 文件中的 FLLR?

enter image description here

我的记录源代码:

  NSError *error;

// Recording settings
NSMutableDictionary *settings = [NSMutableDictionary dictionary];
[settings setValue: [NSNumber numberWithInt:kAudioFormatLinearPCM] forKey:AVFormatIDKey];
[settings setValue: [NSNumber numberWithFloat:22050] forKey:AVSampleRateKey];
[settings setValue: [NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey]; // mono
[settings setValue: [NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
[settings setValue: [NSNumber numberWithBool:NO] forKey:AVLinearPCMIsBigEndianKey];
//[settings setValue: [NSNumber numberWithInt:16] forKey:AudioSampleType];




// File URL
NSURL *url = [NSURL fileURLWithPath:FILEPATH];

//NSLog(@"Url file path ::: %@",url);

// Create recorder
recorder = [[AVAudioRecorder alloc] initWithURL:url settings:settings error:&error];
if (!recorder)
{
// NSLog(@"Error establishing recorder: %@", error.localizedFailureReason);
return;
}

最佳答案

感谢上帝和您的支持 friend 。是的,我终于解决了我的问题。不知道方法对不对。但是我的问题已经解决了。我使用上面的代码录制了语音并保存了我的音频。然后我将再次使用以下代码导出音频。我从 https://developer.ibm.com/answers/questions/180732/seems-watson-text-to-speech-service-returns-a-wav.html 得到这段代码

-(NSData*) stripAndAddWavHeader:(NSData*) wav {
unsigned long wavDataSize = [wav length] - 44;

NSData *WaveFile= [NSMutableData dataWithData:[wav subdataWithRange:NSMakeRange(44, wavDataSize)]];

NSMutableData *newWavData;
newWavData = [self addWavHeader:WaveFile];

return newWavData;
}


- (NSMutableData *)addWavHeader:(NSData *)wavNoheader {

int headerSize = 44;
long totalAudioLen = [wavNoheader length];
long totalDataLen = [wavNoheader length] + headerSize-8;
long longSampleRate = 22050.0;
int channels = 1;
long byteRate = 8 * 44100.0 * channels/8;



Byte *header = (Byte*)malloc(44);
header[0] = 'R'; // RIFF/WAVE header
header[1] = 'I';
header[2] = 'F';
header[3] = 'F';
header[4] = (Byte) (totalDataLen & 0xff);
header[5] = (Byte) ((totalDataLen >> 8) & 0xff);
header[6] = (Byte) ((totalDataLen >> 16) & 0xff);
header[7] = (Byte) ((totalDataLen >> 24) & 0xff);
header[8] = 'W';
header[9] = 'A';
header[10] = 'V';
header[11] = 'E';
header[12] = 'f'; // 'fmt ' chunk
header[13] = 'm';
header[14] = 't';
header[15] = ' ';
header[16] = 16; // 4 bytes: size of 'fmt ' chunk
header[17] = 0;
header[18] = 0;
header[19] = 0;
header[20] = 1; // format = 1
header[21] = 0;
header[22] = (Byte) channels;
header[23] = 0;
header[24] = (Byte) (longSampleRate & 0xff);
header[25] = (Byte) ((longSampleRate >> 8) & 0xff);
header[26] = (Byte) ((longSampleRate >> 16) & 0xff);
header[27] = (Byte) ((longSampleRate >> 24) & 0xff);
header[28] = (Byte) (byteRate & 0xff);
header[29] = (Byte) ((byteRate >> 8) & 0xff);
header[30] = (Byte) ((byteRate >> 16) & 0xff);
header[31] = (Byte) ((byteRate >> 24) & 0xff);
header[32] = (Byte) (2 * 8 / 8); // block align
header[33] = 0;
header[34] = 16; // bits per sample
header[35] = 0;
header[36] = 'd';
header[37] = 'a';
header[38] = 't';
header[39] = 'a';
header[40] = (Byte) (totalAudioLen & 0xff);
header[41] = (Byte) ((totalAudioLen >> 8) & 0xff);
header[42] = (Byte) ((totalAudioLen >> 16) & 0xff);
header[43] = (Byte) ((totalAudioLen >> 24) & 0xff);

NSMutableData *newWavData = [NSMutableData dataWithBytes:header length:44];
[newWavData appendBytes:[wavNoheader bytes] length:[wavNoheader length]];
return newWavData;
}

关于ios - objective-c 中的音频文件格式问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32312508/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com