我使用ExtAudioFileWriteAsync在使用设备录制时编写音频文件,但是一旦我完成录制,我尝试用ExtAudioFileRead函数读取它,我得到的样本与我写的样本不一样...任何人都知道为什么这样做发生?
写作:
self.audioManager.inputBlock = ^(float *data, UInt32 numFrames, UInt32 numChannels) {
for (int i = 0; i < numFrames*numChannels; i++) {
printf("write*%f\n", data[i]);
}
UInt32 numIncomingBytes = numFrames*numChannels*sizeof(float);
UInt32 *outputBuffer =(UInt32*)malloc(numIncomingBytes);
memcpy(outputBuffer, recordedData, numIncomingBytes);
AudioBufferList outgoingAudio;
outgoingAudio.mNumberBuffers = 1;
outgoingAudio.mBuffers[0].mNumberChannels = numChannels;
outgoingAudio.mBuffers[0].mDataByteSize = numIncomingBytes;
outgoingAudio.mBuffers[0].mData = self.outputBuffer;
if( 0 == pthread_mutex_trylock( &outputAudioFileLock ) )
{
ExtAudioFileWriteAsync(outputFile, numFrames, &outgoingAudio);
}
pthread_mutex_unlock( &outputAudioFileLock );
};
[self.audioManager play];
阅读:
UInt32 *outputBuffer = (UInt32 *)malloc(numFrames*numChannels*sizeof(float));
AudioBufferList convertedData;
convertedData.mNumberBuffers = 1;
convertedData.mBuffers[0].mNumberChannels = numChannels;
convertedData.mBuffers[0].mDataByteSize = numFrames*numChannels*sizeof(float);
convertedData.mBuffers[0].mData = outputBuffer;
NSMutableArray *samplesArray = [[NSMutableArray alloc]init];
while (numFrames > 0) {
ExtAudioFileRead(inputFile, &numFrames, &convertedData);
if (numFrames > 0) {
AudioBuffer audioBuffer = convertedData.mBuffers[0];
float *samples = (float *)audioBuffer.mData;
for (int i = 0; i < frameCount*numChannels; i++) {
printf("read*%f\n", samples[i]);
}
}
}
顺便说一下,我正在使用Novocaine项目来获取设备音频。我可以使用Novocaine代码或任何其他播放器重现已保存的音频。
编写ExtAudioFileRef输出时:
ExtAudioFileCreateWithURL(audioFileRef, kAudioFileM4AType, &outputFileDesc, NULL, kAudioFileFlags_EraseFile, &outputFile);
其中outputFileDesc是
AudioStreamBasicDescription outputFileDesc = {44100.0,kAudioFormatMPEG4AAC,0,0,1024,0,thisNumChannels,0,0}; outputFileDesc.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
阅读ExtAudioFileRef inputFile:
ExtAudioFileOpenURL(audioFileRef, &inputFile):
在两种情况下(写作和阅读)都采用相同的格式:
AudioStreamBasicDescription outputFormat;
_outputFormat.mSampleRate = self.samplingRate;
_outputFormat.mFormatID = kAudioFormatLinearPCM;
_outputFormat.mFormatFlags = kAudioFormatFlagIsFloat;
_outputFormat.mBytesPerPacket = 4*self.numChannels;
_outputFormat.mFramesPerPacket = 1;
_outputFormat.mBytesPerFrame = 4*self.numChannels;
_outputFormat.mChannelsPerFrame = self.numChannels;
_outputFormat.mBitsPerChannel = 32;
ExtAudioFileSetProperty(outputFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &_outputFormat);
ExtAudioFileSetProperty(inputFile, kExtAudioFileProperty_ClientDataFormat, sizeof(AudioStreamBasicDescription), &_outputFormat);
顺便说一下,即使读取样本不等于写入样本,两个信号的平均值也非常相似。但我不完全明白为什么不完全平等!
知道我做错了吗?
答案 0 :(得分:1)
听起来有一个或两个ExtaudioFileRefs的隐式格式转换,你会看到转换后的不同样本。您有三种格式:audio_in_format,file_format和audio_out_format。如果audio_in_format与file_format不同,则写入ExtAudioFileRef将创建一个音频转换器,以便在写入磁盘之前将输入音频转换为file_format。如果file_format与audio_out_format不同,读取ExtAudioFileRef也会创建一个转换器。
观点: 你把你的写作ExtAudioFileRef命名为“outputFile”,并且读取ExtAudioFileRef“inputFile”是令人困惑的。我会使用像audioWriter和audioReader这样的东西。