-
Notifications
You must be signed in to change notification settings - Fork 0
/
adsr.jsfx-inc
201 lines (159 loc) · 4.06 KB
/
adsr.jsfx-inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
desc:ADSR envelope
// Copyright (C) 2013-2017 Theo Niessink <[email protected]>
// This work is free. You can redistribute it and/or modify it under the
// terms of the Do What The Fuck You Want To Public License, Version 2,
// as published by Sam Hocevar. See http://www.wtfpl.net/ for more details.
/* Example
desc:Mono synth with ADSR
slider1:3<0,5000,1>Attack (ms)
slider2:1000<1,15000,1>Decay (ms)
slider3:-12.0<-120.0,24.0,1.0>Sustain (dB)
slider4:500<0,5000,1>Release (ms)
import Tale/adsr.jsfx-inc
import Tale/midi_queue.jsfx-inc
import Tale/poly_blep.jsfx-inc
@slider
adsr.adsr_seta(slider1 * 0.001);
adsr.adsr_setd(slider2 * 0.001);
adsr.adsr_sets(exp(log(10)/20 * slider3));
adsr.adsr_setr(slider4 * 0.001);
@sample
while(midi.midiq_recv()) (
midi.msg1 &= 0xF0;
// Note On
midi.msg1 == 0x90 && midi.msg3 ? (
osc.poly_setf(note = midi.msg2, 440);
// Attack
adsr.adsr_a(0.5 * midi.msg3 / 128);
) :
// Note Off
midi.msg1 == 0x80 || midi.msg1 == 0x90 ? (
// Release
midi.msg2 == note ? adsr.adsr_r();
);
);
// Process ADSR envelope and apply it to oscillator.
adsr.adsr_process() ? spl0 = spl1 = adsr.env * osc.poly_saw();
Setting Functions
* adsr_seta(time) -- Attack
* adsr_setd(time) -- Decay
* adsr_sets(gain) -- Sustain
* adsr_setr(time) -- Release
Example: adsr.adsr_seta(0.003);
Sets the ADSR envelope time (specified in seconds) or the gain
[0.0..1.0] factor, and returns the coefficient or the gain factor.
Processing Functions
* adsr_a(scale) -- Attack
Example: adsr.adsr_a(1.0);
Starts processing the ADSR envelope, scaling it to [0.0..scale], and
returns the state.
* adsr_d() -- Decay
* adsr_s() -- Sustain
* adsr_r() -- Release
Example: adsr.adsr_r();
Continues processing the ADSR envelope, and returns the state.
* adsr_process()
Example: state = adsr.adsr_process();
Processes the ADSR envelope, and returns its state.
Note: You should call this function from the @sample section.
* adsr_reset()
Example: adsr.adsr_reset();
Resets the ADSR envelope, and returns the state.
Instance Variables
* state
Example: released = !adsr.state;
The current state (1=attack, 2=decay, 4=sustain, 8=release, 0=off).
* env
Example: spl0 *= adsr.env;
The scaled ADSR envelope output.
* scale
Example: adsr2.adsr_a(adsr1.scale);
The current scale factor.
*/
@init
function _adsr_set(time)
(
1 / (0.2 * time * srate + 1);
);
function adsr_seta(time)
instance(a)
(
a = _adsr_set(time);
);
function adsr_setd(time)
instance(d)
(
d = _adsr_set(time);
);
function adsr_sets(gain)
instance(s)
(
s = gain;
);
function adsr_setr(time)
instance(r)
(
r = _adsr_set(time);
);
function adsr_reset()
instance(state, env)
(
state = env = 0;
);
function adsr_r()
instance(state, r)
(
r < 1 ? (
state = 8;
) : (
this.adsr_reset();
);
);
function adsr_s()
instance(state, s, env, scale)
(
env = scale * s;
state = 4;
);
function adsr_d()
instance(state, d)
(
d < 1 ? (
state = 2;
) : (
this.adsr_s();
);
);
function adsr_a(scale)
instance(state, a, env)
(
this.scale = scale;
a < 1 ? (
state = 1;
) : (
env = scale;
this.adsr_d();
);
);
function adsr_process()
instance(state, a, d, s, r, env, scale)
(
state ? (
// Decay
state == 2 ? (
env += d * (scale * s - env);
abs(env - scale * s) <= scale * 0.000001 ? this.adsr_s();
) :
// Release
state == 8 ? (
env += r * (0 - env);
abs(env) <= scale * 0.000001 ? this.adsr_reset();
) :
// Attack
state == 1 ? (
env += a * (scale - env);
abs(env - scale) <= scale * exp(-5) ? this.adsr_d();
);
);
state;
);