核心音频:怎能一个分组=一个字节时清楚地一个分组= 4个字节(core audio: how can

2019-07-04 05:23发布

我本来打算在核心音频转换服务,在学习的核心音频和我被这个例子在他们的袭击示例代码 :

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 ,并使用数据包,而不是框架..但它是同样的事情)

Answer 1:

我认为你是对的,按照该定义AudioFile.h头文件, AudioFileWritePackets应该被写入作为第三个参数的音频数据的字节数,并在学习的Core Audio例如framecount变量定义为数据包的数量,而不是字节数。

我试图实施例出来,并得到完全相同的输出(framecount * 4) 0 ,甚至-1作为第三个参数AudioFileWritePackets函数调用。 所以对我来说它似乎是在.h文件中定义的功能并不完全正常工作(不要求第三个参数),并在该示例中,本书的作者都没有注意到这个错误要么 - 我可能是错的。



文章来源: core audio: how can one packet = one byte when clearly one packet = 4 bytes