Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Interrupt-level callbacks



Dear list members,

I am writing an application based on the cross-platform PortAudio library (www.portaudio.com) to do some real-time audio processing. PortAudio handles the audio I/O through a callback (which on OS X is implemented with the AudioToolbox and CoreAudio). The PortAudio documentation states:

"Your callback function is often called by an interrupt, or low level process so you should not do any complex system activities like allocating memory, or reading or writing files, or printf(). Just crunch numbers and generate audio signals. What is safe or not safe will vary from platform to platform."

Some postings in the list archives suggest that it is unsafe to use Objective-C in any way at the interrupt level. Other places, e.g.,

http://www.stepwise.com/Articles/Technical/2001-03-20.01.html

suggest that it is safe and that the only problems are the usual ones caused by multiple threads. Does anybody know which is the case?

At the moment, a stripped version of the code looks like this:

1 @interface UPPitchDetector
2
3 - (void)setNextBuffer:(SAMPLE *)aBuffer
4 length:(unsigned long)aLong;
5
6 @end
7
8 static UPPitchDetector *instance;
9
10 static int detectorCallback(void *inputBuffer,
11 void *outputBuffer,
12 unsigned long framesPerBuffer,
13 PaTimestamp outTime,
14 void *userData)
15 {
16 [instance setNextBuffer:(SAMPLE *)inputBuffer
17 length:framesPerBuffer];
18
19 // Returning anything nonzero stops the stream.
20 return 0;
21 }
22
23 @implementation UPPitchDetector
24
25 + (UPPitchDetector *)sharedInstance
26 {
27 if (instance == nil) {
28 instance = [[UPPitchDetector alloc] init];
29 }
30 return instance;
31 }
32
33 - (id)init
34 {
35 // Initialize the UPPitchDetector.
36 }
37
38 - (void)setNextBuffer:(SAMPLE *)aBuffer
39 length:(unsigned long)aLong
40 {
41 // Process the audio data in aBuffer.
42
43 // Notify observers.
44 [[NSNotificationCenter defaultCenter]
45 postNotificationName:@"UPNewPitchNotification"
46 object:self];
47 }
48
49 @end

If it is not safe to use Objective-C, is it safe to access an object by way of an @defs() struct? There is at least one demo,

http://www.audiosynth.com/sinewavedemo.html

that does so and works. For that matter, the code posted above seems to work correctly without crashing. I'm just concerned that these instances might be unsafe but lucky.

If it is safe to use Objective-C, is it safe to use NSNotifications? If I understand correctly, the Foundation framework is thread-safe but the AppKit is not. Slightly more complicated, the purpose of the NSNotification posted in ll. 44-46--a Foundation object--is to notify an observing object of an NSOpenGLView subclass--so an object derived from AppKit--that it should update itself. Is *that* safe?

Finally, in any of these scenarios would it be necessary to qualify as volatile any declarations of instance or local method variables?

Thank you in advance for any help, and please let me know if I cut out any code that would help to clarify my questions.

Best,

Ashley Burgoyne
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.