我正在学习核心音频中的核心音频转换服务,我被他们的示例代码中的这个示例所震惊:
while(1)
{
// wrap the destination buffer in an AudioBufferList
AudioBufferList convertedData;
convertedData.mNumberBuffers = 1;
convertedData.mBuffers[0].mNumberChannels = mySettings->outputFormat.mChannelsPerFrame;
convertedData.mBuffers[0].mDataByteSize = outputBufferSize;
convertedData.mBuffers[0].mData = outputBuffer;
UInt32 frameCount = packetsPerBuffer;
// read from the extaudiofile
CheckResult(ExtAudioFileRead(mySettings->inputFile,
&frameCount,
&convertedData),
"Couldn't read from input file");
if (frameCount == 0) {
printf ("done reading from file");
return;
}
// write the converted data to the output file
CheckResult (AudioFileWritePackets(mySettings->outputFile,
FALSE,
frameCount,
NULL,
outputFilePacketPosition / mySettings->outputFormat.mBytesPerPacket,
&frameCount,
convertedData.mBuffers[0].mData),
"Couldn't write packets to file");
// advance the output file write location
outputFilePacketPosition += (frameCount * mySettings->outputFormat.mBytesPerPacket);
}
请注意如何frameCount
定义为packetsPerBuffer
..packetsPerBuffer
在这里定义:
UInt32 outputBufferSize = 32 * 1024; // 32 KB is a good starting point
UInt32 sizePerPacket = mySettings->outputFormat.mBytesPerPacket;
UInt32 packetsPerBuffer = outputBufferSize / sizePerPacket;
难倒我的部分AudioFileWritePackets
被称为..在文档AudioFileWritePackets 第三和第五个参数定义为:
inNumBytes 正在写入的音频数据的字节数。
ioNumPackets 在输入上,一个指向要写入的数据包数量的指针。在输出时,指向实际写入的数据包数量的指针。
然而在代码中,两个参数都给出了 frameCount .. 这怎么可能?我知道 PCM 数据 1 帧 = 1 个数据包:
// define the ouput format. AudioConverter requires that one of the data formats be LPCM
audioConverterSettings.outputFormat.mSampleRate = 44100.0;
audioConverterSettings.outputFormat.mFormatID = kAudioFormatLinearPCM;
audioConverterSettings.outputFormat.mFormatFlags = kAudioFormatFlagIsBigEndian | kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioConverterSettings.outputFormat.mBytesPerPacket = 4;
audioConverterSettings.outputFormat.mFramesPerPacket = 1;
audioConverterSettings.outputFormat.mBytesPerFrame = 4;
audioConverterSettings.outputFormat.mChannelsPerFrame = 2;
audioConverterSettings.outputFormat.mBitsPerChannel = 16;
但相同的 lPCM 格式也清楚地表明每个数据包有 4 个字节(= 每帧 4 个字节)..
那么这是如何工作的呢?(这同样适用于同一章中使用AudioConverterFillComplexBuffer
代替的另一个示例ExtAudioFileRead
,并使用数据包而不是帧..但它是同一回事)