/
adsr.ts
234 lines (216 loc) · 5.15 KB
/
adsr.ts
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import type { ICopy, IReset } from "@thi.ng/api";
import { clamp01 } from "@thi.ng/math/interval";
import { add } from "./add.js";
import { AGen } from "./agen.js";
import type { IGen } from "./api.js";
import { curve } from "./curve.js";
const enum EnvPhase {
ATTACK,
DECAY,
SUSTAIN,
RELEASE,
IDLE,
}
export interface ADSROpts {
/**
* Attack time (in samples). Default: 0
*/
a: number;
/**
* Decay time (in samples). Default: 0
*/
d: number;
/**
* Sustain level/gain (in [0..1] range). Default: 1
*/
s: number;
/**
* Release time (in samples). Default: 0
*/
r: number;
/**
* Attack curvature. Recommended range [0.0001 .. 10000]
* (curved -> linear). Default: 0.1
*/
acurve: number;
/**
* Decay & release curvature. Recommended range [0.0001 .. 10000]
* (curved -> linear). Default: 0.1
*/
dcurve: number;
/**
* Sustain phase duration (in samples). Default: Infinity. If a
* finite value, then release phase is triggered automatically, else
* needs to be triggered manually via {@link ADSR.release}.
*/
slen: number;
/**
* Overall envelope gain / multiplier. Default: 1
*/
gain: number;
}
/**
* Time based ADSR envelope gen with customizable exponential attack,
* decay and release curves.
*
* @remarks
* The attack, decay and release options are to be given in samples
* (`num = time_in_seconds * sample_rate`). Unless the sustain length
* (`slen` opt) is finite (default: ∞), the release phase of the
* envelope MUST be triggered manually by calling {@link ADSR.release}.
* If only attack & decay phases are required, initialize the sustain
* level to zero and configure `dcurve` to adjust falloff shape.
*
* The envelope can be re-used & restarted by calling
* {@link ADSR.reset}. This will move the internal state back to the
* beginning of the attack phase and start producing a new envelope with
* current settings. Note: Any changes done to the envelope parameters
* are only guaranteed to be fully applied after reset.
*
* The `acurve` and `dcurve` options can be used to control the
* exponential curvature of the attack, decay and release phases.
* Recommended range [0.0001 - 100] (curved -> linear).
*
* @param opts -
*/
export const adsr = (opts?: Partial<ADSROpts>) => new ADSR(opts);
export class ADSR extends AGen<number> implements ICopy<ADSR>, IReset {
protected _phase!: EnvPhase;
protected _curve!: IGen<number>;
protected _atime!: number;
protected _dtime!: number;
protected _rtime!: number;
protected _acurve!: number;
protected _dcurve!: number;
protected _sustain!: number;
protected _speriod!: number;
protected _gain!: number;
constructor(opts?: Partial<ADSROpts>) {
super(0);
opts = {
a: 0,
d: 0,
s: 1,
r: 0,
acurve: 0.1,
dcurve: 0.001,
slen: Infinity,
gain: 1,
...opts,
};
this.setAttack(opts.a!);
this.setDecay(opts.d!);
this.setRelease(opts.r!);
this.setSustain(opts.s!, opts.slen);
this.setCurveA(opts.acurve!);
this.setCurveD(opts.dcurve!);
this.setGain(opts.gain!);
this.reset();
}
copy() {
return new ADSR({
a: this._atime,
d: this._dtime,
s: this._sustain,
r: this._rtime,
acurve: this._acurve,
dcurve: this._dcurve,
gain: this._gain,
slen: this._speriod,
});
}
reset() {
this._phase = EnvPhase.ATTACK;
this._curve = curve(0, 1, this._atime + 1, this._acurve, true);
this._val = 0;
return this;
}
release() {
if (this._phase < EnvPhase.RELEASE) {
this._phase = EnvPhase.RELEASE;
this._curve = curve(
this._sustain,
0,
this._rtime + 1,
this._dcurve,
true
);
}
}
isSustained() {
return this._phase === EnvPhase.SUSTAIN;
}
isDone() {
return this._phase === EnvPhase.IDLE;
}
next() {
let v: number;
switch (this._phase) {
case EnvPhase.IDLE:
return 0;
case EnvPhase.ATTACK:
v = this._curve.next();
if (v >= 1) {
v = 1;
this._phase = EnvPhase.DECAY;
this._curve = curve(
1,
this._sustain,
this._dtime + 1,
this._dcurve,
true
);
}
break;
case EnvPhase.DECAY:
v = this._curve.next();
if (v <= this._sustain) {
v = this._sustain;
this._phase = EnvPhase.SUSTAIN;
this._curve = add(1, 1);
}
break;
case EnvPhase.SUSTAIN:
if (this._curve.next() >= this._speriod) {
this.release();
}
return this._val;
case EnvPhase.RELEASE:
v = this._curve.next();
if (v < 0) {
v = 0;
this._phase = EnvPhase.IDLE;
}
}
return (this._val = v * this._gain);
}
setAttack(steps: number) {
this._atime = Math.max(steps, 0);
}
setDecay(steps: number) {
this._dtime = Math.max(steps, 0);
}
setRelease(steps: number) {
this._rtime = Math.max(steps, 0);
}
/**
* Sets sustain level & duration. If the latter is omitted, the
* current value will be retained.
*
* @param level -
* @param duration -
*/
setSustain(level: number, duration?: number) {
this._sustain = clamp01(level);
duration !== undefined && (this._speriod = duration);
}
setCurveA(ratio: number) {
this._acurve = Math.max(ratio, 1e-9);
}
setCurveD(ratio: number) {
this._dcurve = Math.max(ratio, 1e-9);
}
setGain(gain: number) {
this._gain = gain;
}
}