Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to play a sound with an attack / sustain (loop) / decay with AVAudioPlayer

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!

like image 716
umop Avatar asked Oct 12 '22 02:10

umop


2 Answers

From your description, it sounds as if you're trying to write a software synthesizer. The only way that you could use AVAudioPlayer for something like this would be to compose the entire duration of a note as a single WAV file and then play the whole thing with AVAudioPlayer.

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 (AVAudioPlayer can 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. AudioQueue calls 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.

like image 96
MusiGenesis Avatar answered Oct 20 '22 18:10

MusiGenesis


creating your own envelope generator is very simple. the tough part will be updating your program to use lower level audio services in order to alter the signal directly.

to do this, you will need:

  • the audio file's samples
  • set up an AudioQueue (that's one approach, but i am going with it because it was mentioned in the OP, and it is relatively high level API for a user provided sample buffer)
  • provide a signal to the queue
  • determine if your program is best in realtime or pre-rendered

Realtime

  • Allows live variations
  • manage your loop points
  • manage your render position
  • be able to determine the amplitude to apply based on the sample position range you are reading

or

Prerendered

  • May require more memory
  • Requires less CPU
  • apply the envelope to your copy of the sample buffer
  • manage your render position

I also assume that you need only slow/simple transitions. If you want some crazy/fast LFO, without aliasing, you will have a lot more work to do. This approach should not produce audible aliasing unless your changes are too abrupt:

Writing a simple envelope generator (EG) is easy; check out Apple's SinSynth for a very basic EG if you need a push in that direction.

like image 21
justin Avatar answered Oct 20 '22 18:10

justin