Isn't NSNotificationCenter based on Objective C? If so, then I'd say: No, that is not the way to go, at least not from within your CoreAudio callback.
As I mentioned, it's best to stick with Standard C. You can poll the global or instance variable from another thread, or you could use a non-blocking semaphore call from the C API in OSX. Look for functions semaphore_create(), semaphore_timedwait(), and semaphore_destory() for your main program. Inside the CoreAudio callback, you would only call semaphore_signal(). In fact, your global/instance variable should be of type semaphore_t for this particular solution.
There's nothing wrong with using ObjC if you don't mind jumping through the hoops necessary to make sure it won't block. But the simplest method is to use C API, provided that you only call non-blocking functions.
Brian Willoughby Sound Consulting
On Jan 18, 2011, at 15:03, Paul Fredlein wrote: If I use a global variable then I suppose I should use NSNotificationCenter - would that be the way to go?
On 18/01/2011, at 10:11 AM, Brian Willoughby wrote: On Jan 17, 2011, at 15:55, Paul Fredlein wrote: I'm recording to a single buffer as I need to only capture x seconds of audio. I've determined the buffer size by the number of seconds I need to record so when the buffer is full it stops recording. The maximum recording duration varies from 1 second to about 5 seconds maximum.
The file is called audioAppDelegate.m and in my callback I'm using 'self' as the 'inUserData'.
// set record callback AudioQueueNewInput(&asbdRecord, recordCallback, self, NULL, NULL, 0, &queueRecord);
In my callback, I need to stop recording when the single buffer is full.
[(audioAppDelegate*)inUserData performSelectorOnMainThread:@selector(stopRecording:) withObject:nil waitUntilDone:NO];
This works but there must be a better way. Any help appreciated, thanks.
Why not just create a global variable - or, better yet, pass in the address of an instance variable as your inUserData - and set this variable when your recording data has been completely captured. In your App delegate, you can set up a timer to periodically poll this BOOL, and then call the -stopRecording: method when the flag changes. You don't need to set the timer to repeat very fast - in fact, you don't even need a repeating timer. If you do end up looping on the flag, make sure you call sleep() or the equivalent so that you don't end up with a busy wait and spinning cursor.
It's generally best to create standard C mechanisms for communication between the CoreAudio callbacks and the non-time-critical parts of your application.
|