Audiounit Callback and Synchronization: How to Ensure Thread Safety with Gcd

Is there any reason to not use sleep in a Grand Central Dispatch queue?

You can use sleep, but as you mentioned, do it off of the main thread, as you should never tie up the main thread.

But, if you need a small sleep, the amount of time may not be precise, and is unlikely to be, but the thread will be woken up at least after the amount of sleep, depending on what else may be using the cpu.

But, I see no problem with using sleep as it will at least give other threads/applications, a chance to run.

How to sync code callbacks to playback time of an audio track?

An AudioUnit has a callback property you can set which you pass in an AURenderCallbackStruct.
That callback will have a timestamp.

OSStatus YourAURenderCallack (
void *inRefCon,
AudioUnitRenderActionFlags *ioActionFlags,
const AudioTimeStamp *inTimeStamp,
UInt32 inBusNumber,
UInt32 inNumberFrames,
AudioBufferList *ioData
);

OSX: AudioUnit callback not being called enough

If you try to do any significant amount of processing inside an Audio Unit callback (or anything else that does Objective C messaging, synchronization, locks, or memory management, etc.), your callback function or block might take too long, and thus your app might miss some callbacks, and thus miss some audio data. You can check for this by removing all processing inside the callback, and just total the number of audio samples received to make sure the right amount is coming in per second.

If this is happening, then, to prevent your callback from blocking too long, you should rearrange your processing to do all or most of it in another thread, and just quickly copy the data out of the Audio Unit callback into an array, queue, or fifo to pass to that other processing task or thread. Since you know the rate of Audio Callbacks, you can determine the correct rate to poll for the needed processing.



Related Topics



Leave a reply



Submit