Does AVAudioFormat initStandardFormatWithSampleRate return incorrect stream description?

643 views Asked by At

When trying to write a PCM audio file with ExtAudioFileCreateWithURL, I get an 'fmt?' err when using a 2-channel audio format (Mac OS X, SDK 10.10)

I create the audio format with

[[AVAudioFormat alloc] initStandardFormatWithSampleRate:sampleRate channels:channelCount]

and also use it as audio format for the AUGraph in my app.

The AUGraph works fine with both, 1ch and 2ch formats, but when trying to write an audio file with ExtAudioFileCreateWithURL, it only works with 1ch, 2ch result in the 'fmt?' error.

When running the following test...

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);

... this is the output in the console:

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

Shouldn't there be a difference in the mBytesPerFrame and mBytesPerPacket fields? Or do I miss something here?

1

There are 1 answers

0
Zaggo On BEST ANSWER

I guess I found my mistake:

Writing PCM files with ExtAudioFileCreateWithURL requires the kExtAudioFileProperty_FileDataFormat to be interleaved. Since AVAudioFormat's -initStandardFormatWithSampleRate:channelLayout: returns Core Audio’s standard deinterleaved 32-bit floating point format, I got the 'fmt?'error.

And btw, changing the standard format to interleaved, results in mBytesPerFrame and mBytesPerPacket of values of 8, of course...