AVAudioFormat initStandardFormatWithSampleRate是否返回错误的流描述?

时间:2014-12-18 17:54:25

标签: macos core-audio osx-yosemite extaudiofile

当尝试使用ExtAudioFileCreateWithURL编写PCM音频文件时,我得到一个'fmt?'使用双声道音频格式时错误(Mac OS X,SDK 10.10)

我用

创建音频格式
[[AVAudioFormat alloc] initStandardFormatWithSampleRate:sampleRate channels:channelCount]

并在我的应用程序中将它用作AUGraph的音频格式。

AUGraph适用于1ch和2ch两种格式,但是当尝试使用ExtAudioFileCreateWithURL编写音频文件时,它只适用于1ch,2ch导致'fmt?'错误。

运行以下测试时...

AVAudioFormat* ch1 = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100. channels:1];
AVAudioFormat* ch2 = [[AVAudioFormat alloc] initStandardFormatWithSampleRate:44100. channels:2];
NSLog(@"ch1: %@", [ch1 description]);
NSLog(@"ch1 mBitsPerChannel: %d", ch1.streamDescription->mBitsPerChannel);
NSLog(@"ch1 mChannelsPerFrame: %d", ch1.streamDescription->mChannelsPerFrame);
NSLog(@"ch1 mBytesPerFrame: %d", ch1.streamDescription->mBytesPerFrame);
NSLog(@"ch1 mBytesPerPacket: %d", ch1.streamDescription->mBytesPerPacket);
NSLog(@"ch1 mFramesPerPacket: %d", ch1.streamDescription->mFramesPerPacket);
NSLog(@"ch2: %@", [ch2 description]);
NSLog(@"ch2 mBitsPerChannel: %d", ch2.streamDescription->mBitsPerChannel);
NSLog(@"ch2 mChannelsPerFrame: %d", ch2.streamDescription->mChannelsPerFrame);
NSLog(@"ch2 mBytesPerFrame: %d", ch2.streamDescription->mBytesPerFrame);
NSLog(@"ch2 mBytesPerPacket: %d", ch2.streamDescription->mBytesPerPacket);
NSLog(@"ch2 mFramesPerPacket: %d", ch2.streamDescription->mFramesPerPacket);

...这是控制台中的输出:

ch1: <AVAudioFormat 0x600000084560:  1 ch,  44100 Hz, Float32>
ch1 mBitsPerChannel: 32
ch1 mChannelsPerFrame: 1
ch1 mBytesPerFrame: 4
ch1 mBytesPerPacket: 4
ch1 mFramesPerPacket: 1
ch2: <AVAudioFormat 0x6000000845b0:  2 ch,  44100 Hz, Float32, non-inter>
ch2 mBitsPerChannel: 32
ch2 mChannelsPerFrame: 2
ch2 mBytesPerFrame: 4
ch2 mBytesPerPacket: 4
ch2 mFramesPerPacket: 1

mBytesPerFrame和mBytesPerPacket字段不应该有区别吗?或者我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

我想我发现了自己的错误:

使用ExtAudioFileCreateWithURL编写PCM文件需要交错kExtAudioFileProperty_FileDataFormat。自AVAudioFormat以来 -initStandardFormatWithSampleRate:channelLayout:返回Core Audio的标准 deinterleaved 32位浮点格式,我收到'fmt?'错误。

顺便说一句,将标准格式更改为interleaved会导致mBytesPerFrame和mBytesPerPacket值为8,当然......