views:

1057

answers:

5

I'm using OpenAL on iPhone to play multiple audio samples simultaneously.

Can I get OpenAL to notify me when a single sample is done playing?

I'd like to avoid hardcoding the sample length and setting a timer.

+1  A: 

This OpenAL guide suggests a possible solution:

The 'stream' function also tells us if the stream is finished playing.

...and provides sample source code to illustrate the usage.

Nathan de Vries
RJFalconer
+1  A: 

Wait, are you talking about having finished one sample (e.g., 1/44100 second for 44.1 KHz audio)? Or are you talking about knowing that a source has played through its buffer and has no more audio to play?

For the latter, I've had good results polling a source for the AL_BUFFERS_PROCESSED property when I stream buffers to a source; it might work for the single-buffer case to look for a non-zero value of this property.

invalidname
Once you have queued buffers to a source, AL_BUFFERS_RPOCESSED will never return 0. Even if you stop or pause the source, or it completes its buffer. The value only decreases if you rewind the source or unqueue buffers. A non-zero value only shows that at least one buffer has finished playing at some point.
RJFalconer
+1  A: 

If you have the OpenAL source abstracted into a class, I guess you can simply call performSelector:afterDelay: when you start the sound:

- (void) play
{
    [delegate performSelector:@selector(soundHasFinishedPlaying)
        afterDelay:self.length];
    …
}

(If you stop the sound manually in the meantime, the callback can be cancelled, see the NSObject Class Reference.) Or you can poll the AL_SOURCE_STATE:

- (void) checkStatus
{
    ALint state;
    alGetSourcei(source, AL_SOURCE_STATE, &state);
    if (state == AL_PLAYING)
        return;
    [timer invalidate];
    [delegate soundHasFinishedPlaying];
}

I don’t know how to have OpenAL call you back. What exactly do you want the callback for? Some things can be solved better without a callback.

zoul
+1  A: 
Pestilence
A: 

Hey Nathan, how does the stream function exactly tell us when the stream is finished?? i read the source and it seems pretty straight forward. but doesnt the stream function need to be called multiple times a second to fill the buffer?? My question is "Is there a way to get notified when the buffer is done and needs a refill without polling?"

Thanks

Ata
Sources need refilling, not buffers. The function works by calling their "playing()" function which polls AL_SOURCE_STATE for AL_PLAYING. The frequency at which stream needs to be called depends on how much sound you're loading at once, ie, how big your buffer is.
RJFalconer