I’m attempting to write an app that can arrange timed playback of some (mostly tiny) audio files. The catch: it needs to be timed with accuracy worthy of music. The sounds won’t be played frequently (it’s not, for example, a sampler or a drum kit), but they will need to be absolutely precisely placed. There are two parts to this question:
-
How should I organize the timing? I’ve heard
NSTimeris inaccurate, or at least unreliable? Should I use MIDI somehow? Or a tight loop and some time-getting function with high resolution? -
Once I’ve got the timing set up, and I know when to play my sounds… how should I play them? Is
NSSoundfast and reliable enough to maintain the accuracy needed?
NSTimer is accurate enough for most purposes as long as you don’t dilly-dally on whatever thread the timer is scheduled on long enough for the timer to be late. You need to return to the run loop in time for its next fire date. Fortunately, NSSound plays asynchronously, so this shouldn’t be a problem.
What usually causes problems with NSTimers is when people set the interval really low (one question I saw had it at 1 centisecond). If what you do takes longer than that interval (and taking longer than 1 centisecond is really easy), then you will return to the run loop after the timer was supposed to fire, which will make the timer late, which will screw up your timing.
You just need to make your timer method implementation as fast as possible, and if you can’t make it fast enough, make an NSOperation subclass to do the job and have your timer method just instantiate the operation, set it up, and add it to an operation queue. (The operation will run on another thread, so it won’t tie up the thread whose run loop you scheduled your timer on.) If the timer may be really frequent, then this is one case where micro-optimization (guided by Instruments and Shark, of course) may be warranted.