Search code examples
ioscore-audio

How to specify bitrate for an AudioStreamBasicDescription in coreaudio?


I'm recording some PCM audio using an Audio Unit. In the callback, when I have 30 seconds of audio, I want to write to disk an 8000Hz AAC encoded file with a 12kb/s bitrate.

This is the AudioStreamBasicDescription that I use, but my output ends up being 40 kb/s. My question is, can I change some parameters to lower the bitrate, and if so, which parameters do I modify?

    // specify the M4A
    AudioStreamBasicDescription outputFormat = {0};

    outputFormat.mSampleRate         = 8000.0;
    outputFormat.mFormatID           = kAudioFormatMPEG4AAC;
    outputFormat.mFormatFlags        = kMPEG4Object_AAC_Main;
    outputFormat.mChannelsPerFrame   = 1;

Solution

  • See the field mBitsPerChannel in the AudioStreamBasicDescription.

    The number of bits for one audio sample. For example, for linear PCM audio using the kAudioFormatFlagsCanonical format flags, calculate the value for this field as follows:

    mBitsPerChannel = 8 * sizeof (AudioSampleType);
    

    Set this field to 0 for compressed formats.

    https://developer.apple.com/library/ios/documentation/MusicAudio/Reference/CoreAudioDataTypesRef/#//apple_ref/c/tdef/AudioStreamBasicDescription