📈 Understanding ADSR Envelopes | myBetabox

📈 Understanding ADSR Envelopes

Keyboard Guide

Understanding ADSR Envelopes

The Code Explained

So far, we have learned to play different notes and allow for rests in between. What if we want to make our notes last for a longer amount of time? Luckily, we can easily do this and so much more by using ADSR Envelopes! An envelope is simply the name we give to the ADSR set of opts to avoid confusion. ADSR stands for

  • (A) Attack: how long it takes for a note to reach its peak volume from 0, akin to hitting a piano key.
  • (D) Decay: how long it takes for a note to go from its peak volume to its sustain value.
  • (S) Sustain: maintains the note, akin to a piano foot pedal.
  • (R) Release: how long it takes to go from its sustained value to the end of the note, akin to releasing a piano key.

and refers to the different stages of a note. The below graphic explains it best.

The only part of the envelope that is turned on by default is the release opt and it is always set to 1. You can set these numbers yourself by doing something like the following:

play :F4, attack: 1, decay: 0.5, sustain: 2, release: 3
Pro Tip: Adding an amp (like amp: 3) at the end can help you hear these notes better.

You don’t have to set all of the numbers if you don’t want to. Simply declaring one of them will also work! You may want to only use two of them and you can do that!

🌟release: 0 would have the note cut off immediately with no fading out and has a value of 0 instead of 1. This is a good technique for staccatos, arpeggios, and scales.

🌟 Coding attack: 0, sustain: 1 would make the note go from 0 straight to its sustained note. The same would happen it you omit attack and decay and start your code with a sustain instead.

The important thing to know is that your note will then last the duration of each of the ADSR numbers added together. This is called note length.

In the above example, the note length is 6.5 beats (1+0.5+2+3 = 6.5).

If you want a second note to play once the previous note has completed its full sound without overlap, insert a sleep command with the value being the note’s note length between the two notes. In our example above, you would type something like this:

play :F4, attack: 1, decay: 0.5, sustain: 2, release: 3
sleep 6.5
play :B3

B3 will wait to start until F4 has finished playing.

If you do want an overlap between notes, make the sleep value less than the previous note length, like this:

play :F4, attack: 1, decay: 0.5, sustain: 2, release: 3
sleep 4
play :B3

In this case, B3 will start playing somewhere near the middle of F4’s tone, creating an overlapping sound.

🌟 Keep this in mind! If you code attack: 1, decay: 0.5, sustain: 1 and do not include a release, the release would automatically be set to 1 and the note length would be 3.5 beats (1+0.5+1+1 = 3.5).