Search code examples
iphoneopenalfinch

How to play the same Sound multiple times with overlap, using OpenAL or Finch?


Finch uses OpenAL. However, when I have an instance of Sound, and say -play, the sound plays. When I call -play multiple times one after another in a fast paced way, every -play makes the current sound playback of that sound stop and restart it.

That's not what I want. Would I have to create multiple sources or buffers to get that working? Or would I just instantiate multiple Sounds with the same file?


Solution

  • There’s a RevolverSound class exactly for this use case. It’s very simple, it allocates a number of Sound instances beforehand and then plays them in rotation:

    - (void) play
    {
        [[sounds objectAtIndex:current] play];
        current = (current + 1) % [sounds count];
    }
    

    This means that there is a hard limit of the sounds that can play simultaneously and the memory usage goes up with that limit. I didn’t find that to be a big problem in practice, because when there are five or more sounds playing at once, there’s already such a sonic chaos that you generally won’t notice that the first one did not play to the very end before starting again.