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
Swiftsupport Folder Not Included in The IPA When Generating Build from Script
Why Does a Simple Swift Arithmetic Operation Compile So Slow
How to Draw an Image in an Nsopenglview with Swift
Why Does My Swift Bundle Get The Wrong Principal Class
Adding Drop Shadow to Table View Cell
How to Change an Inout Parameter from Within a Escaping Closure
Animate UIlabel Width with Fixed Center
Wrong Image Orientation After Displaytransform Call
Error Combining Nscalendarunit with or (Pipe) in Swift 2.0
Is There an Firebase Realtime Database Equivalent to The Increment Method Available in Firestore
Swift: Get The Compile Time Name of Variable (Referencing to a Class)
Swift Preserve UIswitch State on UIlongpress
How to Increment a Swift Int Enumeration
Force Refresh on Another Viewcontroller Component with Swift