Search code examples
iosswiftaudioavaudioplayeraudio-player

Best strategy for managing audio instances to allow cross-fades, etc


I'm working on an iOS application that works with different pieces of audio. Each piece of audio is tied to a separate button (similar to the kind of functionality you'd see in a soundboard app).

In a simple soundboard application, you've got an instance of a player object (AVAudioPlayer or an AVAudioEngine player node) that gets fired whenever a button is pressed.

If you push buttonOne, then sound1 plays.

If you push buttonOne again while sound1 is still playing, then the current instance is interrupted and "replaced" with a new instance of sound1 that starts over at the beginning.

If you push buttonOne, THEN push buttonTwo before sound1 is finished, the instance of sound1 is interrupted and replaced with sound2, again, played from the beginning.

Suppose you're trying to enable cross-fading between the two sounds. You can just create two player instances, load the first sound into player1 and the second into player2, and cross fade between them.

Building on that, suppose you're trying to allow different combinations of sounds to play at the same time. Either the idea that a large number of sounds (maybe the whole soundboard) can all play without interrupting each other. Or, perhaps, the ability to have multiple sounds playing at the same time, e.g. sound1 is a music bed, and sound2...soundXX are sound effects that should be able to play over the music bed without interrupting it.

QUESTIONS: what is the best design strategy for managing your player instances in this situation? Suppose you have a 5 x 5 grid of buttons in your soundboard. If, hypothetically, you should be able to play all 25 sounds at the same time, would that require you to init 25 player instances at setup? (this seems very anti-DRY and not particularly efficient). Or is there some way to dynamically manage the number of instances you need (maybe with a lazy variable?) so that additional instances are only generated as needed, e.g. when you have x number of sounds playing and you start another, an additional instance is generated to contain the newly added sound?


Solution

  • what is the best design strategy for managing instances of AVAudioPlayer in this situation

    None. You should be using AVAudioEngine. That's exactly what it is, a sound board / patch kit.

    Or is there some way to dynamically manage the number of instances you need

    If I have 25 ordered instances of a thing where one possibility is no instance at all, that sounds like an array of Optionals.