I have been writing some OpenAL apps from some tutorials and they are all command line projects. One project continually changes the location of a source in space by repeatedly calling the alSource3f
in a while loop. This is fine for an app that just runs. I can hear the source buffer's audio and hear it changing position in 3d space as the app runs. However, I want to dynamically change the source position myself using some UI slider's in a cocoa application. The problem is that now I cannot allow this while loop to operate on the main thread as it locks down the UI. I tried to throw the while loop and its contents on a background queue using the GCD api like this:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
do {
// get next theta
updateSourceLocation(player);
CheckALError ("Couldn't set source position");
// refill buffers if needed
refillALBuffers (&player);
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
} while (difftime(time(NULL), startTime) < RUN_TIME);
});
The function updateSourceLocation internally calls alSource3f
and by the next line at CheckALError I get OpenAL Error: Couldn't set source position (AL_INVALID_ENUM)
. Can OpenAL library functions only be called on the main thread? What can I do to be able to control UI on the main thread and run OpenAL on a background queue or thread?