DEPARTMENT OF COMPUTING

Example ADSR Envelope

Let’s look at an example ADSR envelope, and the amplitudes that should be generated.

For this example, we’ll use 100 samples per second over a 1.0 second duration, with maximum amplitude of 1.0 and a sustain amplitude of 0.5.

The attack duration will be 0.1 seconds, the decay duration will be 0.2 seconds, the release duration will be 0.3 seconds.

We can then compute the sustain duration will be 1.0 - (0.1 + 0.2 + 0.3) = 0.4 seconds.

Computing starting and ending indexes for each phase

The audio track that stores the amplitudes should be 100 samples long (1.0 seconds * 100 samples per second = 100 samples).

Attack phase

The attack phase will start at index 0 in the audio track and go up to (but not including) position 100(samples_per_second) * 0.1(attack duration) = 10. That’s the range [0, 10), in mathematical notation.

Decay Phase

The decay phase will start at the index where the attack phase ended, 10. It will go up to (but not including) position 100(samples_per_second) * (0.2+0.1)(decay duration + attack duration) = 30. That’s the range [10, 30), in mathematical notation.

Release Phase

The release phase will start at the index 0.3 seconds before the end of the audio track. This is 100(samples_per_second) * (1.0-0.3)(full duration - release duration) = 70. It will go up to (but not including) position 100(samples_per_second) * (1.0)(full duration) = 100. That’s the range [70, 100), in mathematical notation.

Sustain Phase

Finally, the sustain phase will start where the decay phase ended, 30. It will go up to (but not including) the position where the release phase starts, 70. That’s the range [30, 70), in mathematical notation.

Computing the amplitudes

Each phase will be a linear ramp function. We need to know the starting and ending amplitudes to compute the values correct.

Attack phase

Attack phase starts at 0 and ends at the maximum amplitude.

Decay Phase

Decay phase starts at maximum amplitude and ends at the sustain amplitude.

Release Phase

Release phase starts at sustain amplitude and ends at 0.

Sustain Phase

Sustain phase has all elements at the sustain amplitude.

Running the program

At a shell prompt, in the root of the repository, we can run the envelope_test program with inputs that match this example and see the expected output.

$ ./program-envelope-test/envelope_test
Samples/Second: 100
Seconds: 1.0
Envelope style: ADSR
Maximum amplitude: 1.0
Attack seconds: 0.1
Decay seconds: 0.2
Release seconds: 0.3
Sustain amplitude: 0.5

sample_number,amplitude
0,0
1,0.1
2,0.2
3,0.3
4,0.4
5,0.5
6,0.6
7,0.7
8,0.8
9,0.9
10,1 
11,0.975
12,0.95
13,0.925
14,0.9
15,0.875
16,0.85
17,0.825
18,0.8
19,0.775
20,0.75
21,0.725
22,0.7
23,0.675
24,0.65
25,0.625
26,0.6
27,0.575
28,0.55
29,0.525
30,0.5
31,0.5
32,0.5
33,0.5
34,0.5
35,0.5
36,0.5
37,0.5
38,0.5
39,0.5
40,0.5
41,0.5
42,0.5
43,0.5
44,0.5
45,0.5
46,0.5
47,0.5
48,0.5
49,0.5
50,0.5
51,0.5
52,0.5
53,0.5
54,0.5
55,0.5
56,0.5
57,0.5
58,0.5
59,0.5
60,0.5
61,0.5
62,0.5
63,0.5
64,0.5
65,0.5
66,0.5
67,0.5
68,0.5
69,0.5
70,0.5
71,0.483333
72,0.466667
73,0.45
74,0.433333
75,0.416667
76,0.4
77,0.383333
78,0.366667
79,0.35
80,0.333333
81,0.316667
82,0.3
83,0.283333
84,0.266667
85,0.25
86,0.233333
87,0.216667
88,0.2
89,0.183333
90,0.166667
91,0.15
92,0.133333
93,0.116667
94,0.1
95,0.0833333
96,0.0666667
97,0.05
98,0.0333333
99,0.0166667

Last Updated 02/18/2025