I am having a problem finding resources on playing an attack (start of sound) / sustain (looping sound) / decay (ending of sound) sequence with no transition breaks. Are there any good libraries for handling this, or should I roll my own with AVAudioPlayer? Is AudioQueue a better place to look? I used to use SoundEngine.cpp, but that’s been long gone for a while. Is CAF still the best format to use for it?
Thanks!
From your description, it sounds as if you’re trying to write a software synthesizer. The only way that you could use
AVAudioPlayerfor something like this would be to compose the entire duration of a note as a single WAV file and then play the whole thing withAVAudioPlayer.To create a note sound of arbitrary duration, one that begins playing in response to a user action (like tapping a button) and then continues playing until a second user action (like tapping a “stop” button or lifting the finger off the first button) begins the process of ramping the looped region’s volume down to zero (the “release” part), you will need to use
AudioQueue(AVAudioPlayercan be used to play audio constructed entirely in memory, but the entire playback has to be constructed before play begins, meaning that you cannot change what is being played in response to user actions [other than to stop playback]).Here’s another question/answer that shows simply how to use
AudioQueue.AudioQueuecalls a callback method whenever it needs to load up more data to play – you would have to implement all the code that loops and envelope-wraps the original WAV file data.