-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadsr-processor.worklet.ts
242 lines (201 loc) · 7.63 KB
/
adsr-processor.worklet.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
235
236
237
238
239
240
241
242
import StoppableAudioWorkletProcessor from "./StoppableAudioWorkletProcessor";
import { Mode, Parameters, Stage } from "./adsr-processor.types";
import { exponential, linear, logarithmic } from "utils/scale";
const GATE_OFF = 0;
const GATE_ON = 1;
const EPSILON = 0.01;
class ADSRProcessor extends StoppableAudioWorkletProcessor {
lastGainGetter: (time: number) => number;
lastGainGetterAtStageChange: (time: number) => number;
lastStage: Stage;
lastTriggerType: number;
lastTriggerTime: number[];
sustainOn: boolean;
mode: Mode;
constructor(options?: AudioWorkletNodeOptions) {
super(options);
this.lastGainGetter = () => 0;
this.lastGainGetterAtStageChange = () => 0;
this.lastStage = Stage.Attack;
this.lastTriggerTime = [-Number.MAX_SAFE_INTEGER, -Number.MAX_SAFE_INTEGER];
this.lastTriggerType = GATE_OFF;
this.sustainOn = options?.processorOptions.sustainOn ?? true;
this.mode = options?.processorOptions.mode ?? Mode.Linear;
}
static get parameterDescriptors() {
return [
{
name: Parameters.AttackTime,
defaultValue: 0,
minValue: 0,
maxValue: Number.MAX_SAFE_INTEGER,
automationRate: "k-rate" as AutomationRate,
},
{
name: Parameters.DecayTime,
defaultValue: 0,
minValue: 0,
maxValue: Number.MAX_SAFE_INTEGER,
automationRate: "k-rate" as AutomationRate,
},
{
name: Parameters.ReleaseTime,
defaultValue: 0,
minValue: 0,
maxValue: Number.MAX_SAFE_INTEGER,
automationRate: "k-rate" as AutomationRate,
},
{
name: Parameters.SustainLevel,
defaultValue: 0,
minValue: 0,
maxValue: 1,
automationRate: "k-rate" as AutomationRate,
},
];
}
process(inputs: Float32Array[][], outputs: Float32Array[][], parameters: Record<string, Float32Array>) {
const input = inputs?.[0]?.[0];
if (input == null) {
return this.running;
}
const isGateOn = this.isGateOn(input);
if (this.lastTriggerType === GATE_OFF && isGateOn) {
this.lastTriggerType = GATE_ON;
this.lastTriggerTime[GATE_ON] = currentTime;
} else if (this.lastTriggerType === GATE_ON && !isGateOn) {
this.lastTriggerType = GATE_OFF;
// Do not re-trigger release stage
if (this.lastStage !== Stage.Release) {
this.lastTriggerTime[GATE_OFF] = currentTime;
}
}
const stage = this.getStage(input, parameters);
if (stage !== this.lastStage) {
this.lastGainGetterAtStageChange = this.lastGainGetter;
this.lastStage = stage;
}
this.lastGainGetter = this.getGainGetter(stage, parameters);
const output = outputs[0][0];
const sampleFrames = output.length;
for (let i = 0; i < sampleFrames; i++) {
output[i] = this.lastGainGetter(currentTime + i / sampleRate);
}
return this.running;
}
getGainGetter(stage: Stage, parameters: Record<Parameters, Float32Array>): (time: number) => number {
// TODO change this to a-rate parameters to support their automation
const attackTime = parameters[Parameters.AttackTime][0];
const decayTime = parameters[Parameters.DecayTime][0];
const releaseTime = parameters[Parameters.ReleaseTime][0];
const sustainLevel = parameters[Parameters.SustainLevel][0];
const timeToValue = this.getTimeToValue();
const valueToTime = this.getValueToTime();
// attack always starts at 0
if (stage === Stage.Attack) {
if (attackTime <= 0) {
return () => 1;
}
const startTime = this.lastTriggerTime[GATE_ON];
const endTime = startTime + attackTime;
const clampTime = (time: number) => clamp((time - startTime) / (endTime - startTime), 0, 1);
// 0 -> 1 (peak)
return (time: number) => timeToValue(clampTime(time));
}
if (stage === Stage.Decay) {
if (decayTime <= 0) {
return () => sustainLevel;
}
const startTime = this.lastTriggerTime[GATE_ON] + attackTime;
const endTime = startTime + decayTime;
const clampTime = (time: number) => clamp((time - startTime) / (endTime - startTime), 0, 1);
// 1 (peak) -> sustainLevel
return (time: number) => 1 - (1 - sustainLevel) * timeToValue(clampTime(time));
}
if (stage === Stage.Sustain) {
return () => sustainLevel;
}
if (stage === Stage.Release) {
if (releaseTime <= 0) {
return () => 0;
}
if (this.sustainOn) {
const triggered = this.lastTriggerTime[GATE_OFF] < this.lastTriggerTime[GATE_ON] + attackTime + decayTime;
const timeFactor = (time: number) => (triggered ? valueToTime(this.lastGainGetterAtStageChange(time)) : 1);
const startTime = this.lastTriggerTime[GATE_OFF];
const endTime = (time: number) => startTime + releaseTime * timeFactor(time);
const clampTime = (time: number) => clamp((time - startTime) / (endTime(time) - startTime), 0, 1);
const startLevel = (time: number) => (triggered ? this.lastGainGetterAtStageChange(time) : sustainLevel);
// sustainLevel / lastGain -> 0
return (time: number) => (1 - timeToValue(clampTime(time))) * startLevel(time);
} else {
const triggered = this.lastTriggerTime[GATE_OFF] > this.lastTriggerTime[GATE_ON];
const timeFactor = (time: number) => (triggered ? valueToTime(this.lastGainGetterAtStageChange(time)) : 1);
const startTime = triggered
? this.lastTriggerTime[GATE_OFF]
: this.lastTriggerTime[GATE_ON] + attackTime + decayTime;
const endTime = (time: number) => startTime + releaseTime * timeFactor(time);
const clampTime = (time: number) => clamp((time - startTime) / (endTime(time) - startTime), 0, 1);
const startLevel = (time: number) => (triggered ? this.lastGainGetterAtStageChange(time) : sustainLevel);
// sustainLevel / lastGain -> 0
return (time: number) => (1 - timeToValue(clampTime(time))) * startLevel(time);
}
}
return () => 0;
}
getStage(input: Float32Array, parameters: Record<string, Float32Array>): Stage {
const isGateOn = this.isGateOn(input);
const attackTime = parameters[Parameters.AttackTime][0];
const decayTime = parameters[Parameters.DecayTime][0];
if (this.lastTriggerTime[GATE_OFF] > this.lastTriggerTime[GATE_ON]) {
return Stage.Release;
}
if (currentTime <= this.lastTriggerTime[GATE_ON] + attackTime) {
return Stage.Attack;
}
if (currentTime <= this.lastTriggerTime[GATE_ON] + attackTime + decayTime) {
return Stage.Decay;
}
if (this.sustainOn && isGateOn) {
return Stage.Sustain;
}
return Stage.Release;
}
getTimeToValue() {
switch (this.mode) {
case Mode.Exponential:
return exponential;
case Mode.Linear:
return linear;
case Mode.Logarithmic:
return logarithmic;
}
}
getValueToTime() {
switch (this.mode) {
case Mode.Exponential:
return logarithmic;
case Mode.Linear:
return linear;
case Mode.Logarithmic:
return exponential;
}
}
isGateOn(input: Float32Array): boolean {
const length = input.length;
// Naive lo-fi signal detection that triggers if all of the sample frames are high
for (let i = 0; i < length; i++) {
if (input[i] < GATE_ON - EPSILON) {
return false;
}
}
return true;
}
}
registerProcessor("adsr-processor", ADSRProcessor);
function clamp(t: number, min: number, max: number) {
return Math.min(Math.max(t, min), max);
}
// Fixes TypeScript error TS1208:
// File cannot be compiled under '--isolatedModules' because it is considered a global script file.
export {};