2

I've got an AudioBuffer with its void *mData full of freshly-rendered audio samples using Apple's CoreAudio Audio Unit API, but I have a problem getting the samples in the right format. The ASBD of said buffer is as follows:

Float64 mSampleRate        44100
UInt32  mFormatID          1819304813
UInt32  mFormatFlags       41
UInt32  mBytesPerPacket    4
UInt32  mFramesPerPacket   1
UInt32  mBytesPerFrame     4
UInt32  mChannelsPerFrame  2
UInt32  mBitsPerChannel    32
UInt32  mReserved          0

I got this by debugging the application and executing an AudioUnitGetProperty(rioUnit, kAudioUnitProperty_StreamFormat, ...) call. The mFormatFlags field implies the following flags (I don't know of any formal decode method, I just got it by trying out different combinations of kAudioUnitFlags until I got 41):

kAudioFormatFlagIsNonInterleaved | kAudioFormatFlagIsPacked | kAudioFormatFlagIsFloat

Which type of data should I cast the buffer with? I've already tried with Float32, SInt32, but they're not it.

I intend to do a conversion to SInt16 afterwards, but I can't do it if I don't get the right format for the samples first.

Thanks in advance.

Carlos Vega
  • 211
  • 3
  • 9

3 Answers3

3

In my experience, iOS will not deliver floating point data to you directly. Instead you should ask for SInt16 (and thus, set mBitsPerChannel to 16 as well) and then manually convert the integer data to floats by dividing each number by 32767.

Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
1

Based on that ASBD, the data is stereo non-interleaved 32 bit floats, which is the canonical format for Audio Units on Mac OS X.

You should be able to cast the mData field to a float * and get one channel of audio data. The full stereo audio should be contained in an AudioBufferList with two buffers each containing one channel.

Why did casting to Float32 not work?

sbooth
  • 16,646
  • 2
  • 55
  • 81
0

Check this code :

   - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
   fromConnection:(AVCaptureConnection *)connection

       //calback function

        const AudioStreamBasicDescription *audioDescription = CMAudioFormatDescriptionGetStreamBasicDescription(CMSampleBufferGetFormatDescription(sampleBuffer));

        int sampleRate        = (int)audioDescription ->mSampleRate;
        int channelsPerFrame  = (int)audioDescription ->mChannelsPerFrame;
        UInt32 formatFlag     =  audioDescription ->mFormatFlags;

        if (formatFlag & kAudioFormatFlagIsFloat) {
          NSLog(@"IS FLOAT");

        } else if ( formatFlag & kAudioFormatFlagIsSignedInteger) {
          NSLog(@"IS Signed Integer");
        }

}