Search code examples
c++macoscore-audioaudio-processingbitrate

Packet size (mBytesPerPacket) or the bitrate for AAC files in Core Audio


I want to configure a AudioStreamBasicDescription with constant bit rate AAC type.

AudioStreamBasicDescription clientFormat = {0};
clientFormat.mSampleRate         = 44100.0;
clientFormat.mFormatID           = kAudioFormatMPEG4AAC;
clientFormat.mFormatFlags        = kMPEG4Object_AAC_Main;
clientFormat.mChannelsPerFrame   = 2;
clientFormat.mBytesPerPacket     = 0;
clientFormat.mBytesPerFrame      = 0;
clientFormat.mFramesPerPacket    = 1024;
clientFormat.mBitsPerChannel     = 0;
clientFormat.mReserved           = 0;

For mBytesPerPacketthe Apple documentation says:

The number of bytes in a packet of audio data. To indicate variable packet size, set this field to 0. For a format that uses variable packet size, specify the size of each packet using an AudioStreamPacketDescription structure.

I want to have it as a constant, so I have to plug there a non-zero value (the desired size), but everything besides 0 fails.

Any help on this?


Solution

  • It turns out that we can set codec properties right on the AudioConverter.

    So I did:

    AudioConverterSetProperty(acRef, kAudioCodecPropertyBitRateControlMode,
                                                      sizeof(control_mode), &control_mode);
    
    AudioConverterSetProperty(acRef, kAudioCodecPropertyCurrentTargetBitRate,
                                                      sizeof(_bitRate), &_bitRate);