我本来打算在核心音频转换服务,在学习的核心音频和我被这个例子在他们的袭击示例代码 :
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 在输入时,一个指针到的分组的数量来写。 在输出时,指针指向的数据包数量实际写入..
但在代码中这两个参数都给出,帧数..这怎么可能? 我知道与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
,并使用数据包,而不是框架..但它是同样的事情)