Curnelious
Curnelious

Reputation: 1

Audio buffer size is incorrect on a mac

After a while,developing with the audio unit, i know there is some bug that is well known to devs who works with audio buffers in low level .

The bug is that the buffer size on a mac is wrong and show 512, instead of 1024 , where the same software on a iDevice , shows 1024 .

Question is , there is a way to solve that so i can get on a mac also 1024 bits buffer? its a little bit hard to work like that, because simulation is different from device.

This is the callback function where i check the buffer from the mic input :

static OSStatus recordingCallback(void *inRefCon,
                                  AudioUnitRenderActionFlags *ioActionFlags,
                                  const AudioTimeStamp *inTimeStamp,
                                  UInt32 inBusNumber,
                                  UInt32 inNumberFrames,
                                  AudioBufferList *ioData)
{

    AudioBuffer buffer;
    buffer.mNumberChannels = 1;
    buffer.mDataByteSize = inNumberFrames * 2;  
    buffer.mData = NULL; 

    AudioBufferList bufferList;
    bufferList.mNumberBuffers = 1;
    bufferList.mBuffers[0] = buffer;
    OSStatus status;
    status = AudioUnitRender(audioUnit,
                             ioActionFlags,
                             inTimeStamp,
                             inBusNumber,
                             inNumberFrames,
                             &bufferList);


     int16_t *q = (int16_t *)(&bufferList)->mBuffers[0].mData;


//  inNumberFrames, is 512 on a mac ,and 1024 on a device 

(speaker output callback is the same by the way=512 bits).

A part of the setting of the audio unit :

AudioStreamBasicDescription audioFormat;
audioFormat.mSampleRate         = 44100.00;//44100.00;
audioFormat.mFormatID           = kAudioFormatLinearPCM;
audioFormat.mFormatFlags        = kAudioFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked;
audioFormat.mFramesPerPacket    = 1;
audioFormat.mChannelsPerFrame   = 1;
audioFormat.mBitsPerChannel     = 16;
audioFormat.mBytesPerPacket     = 2;
audioFormat.mBytesPerFrame      = 2;

Well i found here that someone say you have to set on the mac (retina is mine) the sample rate in the midi tool , i check it out but its 44100 in there . the chanels are 24 bit, i was trying with 16 bits but no good result . still 512 bits buffer size on my mac. I have a mac retina display 2012.

Upvotes: 2

Views: 618

Answers (1)

GW.Rodriguez
GW.Rodriguez

Reputation: 1181

There are a few things to consider: 1) audio units are made to have the buffer sizes set by the host 2) you can set a max buffer side via overriding AUBase::SetMaxFramesPerSlice(nFrames) 3) There isn't a function to set a min buffer size.

With that being said, you can customize your code as you see fit by overriding more and more functions in the AUBase class. As far as I know there isn't a way to set a minimum buffer size. What you can do is store frames into a buffer until you've reached the number you want, and then send out frames. Just like a circular buffer on a delay effect.

Is there a reason you're wanting a certain number of frames?

Upvotes: 2

Related Questions