-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworklets.js
396 lines (361 loc) · 11.4 KB
/
worklets.js
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
class panProcessor extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: "panPosition",
defaultValue: .5,
minValue: 0,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "modPosition",
defaultValue: 0,
minValue: -1,
maxValue: 1,
automationRate: "a-rate"
}
];
}
constructor() {
super();
this.pan;
this.mod;
this.input;
this.outputL;
this.outputR;
this.panHasChanged;
this.modHasChanged;
this.panVal;
this.modVal;
this.i;
}
process(inputs, outputs, parameters) {
//get parameters
this.pan = parameters.panPosition;
this.mod = parameters.modPosition;
//get inputs/outputs
this.input = inputs[0][0]; //mono input
this.outputL = outputs[0][0]; //left out
this.outputR = outputs[0][1]; //right out
//flags to discern if params are k or a rate this quantum
this.panHasChanged = !(this.pan.length === 1);
this.modHasChanged = !(this.mod.length === 1);
//init param vars
this.panVal = this.pan[0];
this.modVal = this.mod[0];
//loop through mono input
for (this.i = 0; this.i < this.input.length; this.i++) {
if (this.panHasChanged) {
this.panVal = this.pan[this.i];
}
if (this.modHasChanged) {
this.modVal = this.mod[this.i];
}
this.outputL[this.i] = this.input[this.i] * (1 - this.panVal);
this.outputR[this.i] = this.input[this.i] * this.panVal;
}
return true;
}
}
class bitCrushNode extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: "sampleRate",
defaultValue: 1,
minValue: .01,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "bitDepth",
defaultValue: 1,
minValue: .025,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "baseFreq",
defaultValue: 2093.04,
minValue: 0,
maxValue: 72000,
automationRate: "a-rate"
}
];
}
constructor() {
super();
this.sampleRateHasChanged;
this.sampleRateArr;
this.sampleRate;
this.bitRateHasChanged;
this.bitRateArr;
this.bitRate;
this.baseFreqHasChanged;
this.baseFreqArr;
this.baseFreq = 2093.12;
this.i;
this.input;
this.output;
this.phasor = 0;
this.lastSample;
this.normSampleRate;
this.bitMax = Math.pow(2, 16) - 1;
this.bitBase;
this.crushSample;
this.normBitRate;
this.baseDiv;
}
process(inputs, outputs, parameters) {
this.sampleRateArr = parameters.sampleRate;
this.sampleRate = this.sampleRateArr[0]; //init s rate to k-rate
this.bitRateArr = parameters.bitDepth;
this.bitRate = this.bitRateArr[0]; //init b rate to k-rate
this.baseFreqArr = parameters.baseFreq;
this.baseFreq = this.baseFreqArr[0]; //init base freq to k-rate
this.input = inputs[0][0]; //input 0, channel 0
this.output = outputs[0][0]; //output 0, channel 0
//loop thru samples
for (this.i = 0; this.i < this.input.length; this.i++) {
//update sample rate param val if a-rate
if (!this.sampleRateArr.length === 1) {
this.sampleRate = this.sampleRateArr[this.i];
}
//update bit rate param val if a-rate
if (!this.bitRateArr.length === 1) {
this.bitRate = this.bitRateArr[this.i];
}
//update base freq param val if a-rate
if (!this.baseFreqArr.length === 1) {
this.baseFreq = this.baseFreqArr[this.i];
}
this.baseDiv = (this.baseFreq)/72000;
this.normSampleRate = this.sampleRate*this.baseDiv;
this.normBitRate = this.bitRate;
//boundary case - no srr in off position
if (this.sampleRate == 1) {
this.normSampleRate = 1;
}
//calculate new depth base
this.bitBase = this.bitMax * this.normBitRate;
//increment phasor & update sample if needed
this.phasor += this.normSampleRate;
if (this.phasor >= 1) {
this.phasor -= 1;
this.lastSample = this.input[this.i];
}
if (this.bitRate == 1) {
this.crushSample = this.lastSample; //bypass crush
} else {
this.crushSample = Math.floor(((this.lastSample + 1)/2) * this.bitBase);
this.crushSample = ((2*(this.crushSample/this.bitBase)) - 1); //normalize
}
//output most recently updated sample
this.output[this.i] = this.crushSample;
}
return true;
}
}
class envelopeNode extends AudioWorkletProcessor {
static get parameterDescriptors() {
return [
{
name: "attack",
defaultValue: .02,
minValue: .005,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "decay",
defaultValue: .5,
minValue: 0.005,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "sustain",
defaultValue: 0.005,
minValue: 0,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "release",
defaultValue: .5,
minValue: 0.005,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "aCurve",
defaultValue: .5,
minValue: 0,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "peakVal",
defaultValue: .5,
minValue: 0,
maxValue: 1,
automationRate: "a-rate"
},
{
name: "state",
defaultValue: 0,
minValue: 0,
maxValue: 1,
automationRate: "a-rate"
}
];
}
constructor() {
super();
this.acc = 0; //envelope accumulator state
this.accBuff = 0; //accumulator buffer (for release stage)
this.max = (Math.pow(2, 16) - 1); //accumulator max
this.inc = 0; //sample increment for current stage
this.prevState = 0; //previous envelope state
this.stage = 0; //init stage to not running
this.stateBuffer;
this.attackBuffer;
this.decayBuffer;
this.sustainBuffer;
this.releaseBuffer;
this.aCurveBuffer;
this.peakValBuffer;
this.stateHasChanged;
this.attackHasChanged;
this.decayHasChanged;
this.sustainHasChanged;
this.releaseHasChanged;
this.aCurveHasChanged;
this.peakValHasChanged;
this.state;
this.attack;
this.decay;
this.sustain;
this.release;
this.aCurve;
this.peakVal;
this.attackRate;
this.decayRate;
this.sustainThresh;
this.releaseRate;
this.output;
this.i;
}
process(inputs, outputs, parameters) {
//initialize output channe;
this.output = outputs[0][0]; //output 0, channel 0
//parameter value arrays for current quantum
this.stateBuffer = parameters.state;
this.attackBuffer = parameters.attack;
this.decayBuffer = parameters.decay;
this.sustainBuffer = parameters.sustain;
this.releaseBuffer = parameters.release;
this.aCurveBuffer = parameters.aCurve;
this.peakValBuffer = parameters.peakVal;
//flags to discern parameter states for current quantum
//true is a-rate, false is k-rate
this.stateHasChanged = !(this.stateBuffer.length === 1);
this.attackHasChanged = !(this.attackBuffer.length === 1);
this.decayHasChanged = !(this.decayBuffer.length === 1);
this.sustainHasChanged = !(this.sustainBuffer.length === 1);
this.releaseHasChanged = !(this.releaseBuffer.length === 1);
this.aCurveHasChanged = !(this.aCurveBuffer.length === 1);
this.peakValHasChanged = !(this.peakValBuffer.length === 1);
//assign parameter values for current sample based on flag states
//to prevent access of undefined elements
for (this.i = 0; this.i < this.output.length; this.i++) {
if (this.stateHasChanged) {
this.state = this.stateBuffer[this.i];
} else {
this.state = this.stateBuffer[0];
}
if (this.attackHasChanged) {
this.attack = this.attackBuffer[this.i];
} else {
this.attack = this.attackBuffer[0];
}
if (this.decayHasChanged) {
this.decay = this.decayBuffer[this.i];
} else {
this.decay = this.decayBuffer[0];
}
if (this.sustainHasChanged) {
this.sustain = this.sustainBuffer[this.i];
} else {
this.sustain = this.sustainBuffer[0];
}
if (this.releaseHasChanged) {
this.release = this.releaseBuffer[this.i];
} else {
this.release = this.releaseBuffer[0];
}
if (this.aCurveHasChanged) {
this.aCurve = this.aCurveBuffer[this.i];
} else {
this.aCurve = this.aCurveBuffer[0];
}
if (this.peakValHasChanged) {
this.peakVal = this.peakValBuffer[this.i];
} else {
this.peakVal = this.peakValBuffer[0];
}
//note on/off when state changes state :]
if (this.prevState == 0 && this.state == 1) { //note on
this.stage = 1; //initiate attack stage
this.prevState = this.state; //save state for current sample
} else if (this.prevState == 1 && this.state == 0) { //note off
this.stage = 4; //initiate release stage
this.prevState = this.state; //save state for current sample
}
//ADSR envelope implementation
if (this.stage == 1) { //attack stage
this.attackRate = this.attack*(2*sampleRate); //atk time as fraction of sample rate
this.inc = this.max/this.attackRate; //phase increment value
this.accBuff = this.acc; //save accumulator state before increment (for release)
this.acc += this.inc; //increment accumulator
if (this.acc < this.max) {
this.output[this.i] = this.aCurve*(this.acc/this.max) + (1 - this.aCurve)*Math.pow((this.acc/this.max), 3);
} else {
this.output[this.i] = 1.0;
this.stage = 2;
}
} else if (this.stage == 2) { //decay stage
this.decayRate = this.decay*(2*sampleRate); //decay time as fraction of sample rate
this.sustainThresh = this.sustain*this.max; //sustain threshold
this.inc = (this.max - this.sustainThresh)/this.decayRate;
this.accBuff = this.acc; //save accumulator state before decrement
this.acc -= this.inc; //decrement accumulator
if (this.acc > this.sustainThresh) {
this.output[this.i] = this.aCurve*(this.acc/this.max) + (1 - this.aCurve)*Math.pow((this.acc/this.max), 3);
} else {
this.output[this.i] = this.aCurve*this.sustain + (1 - this.aCurve)*Math.pow((this.sustain), 3);
this.stage = 3;
}
} else if (this.stage == 3) {
this.output[this.i] = this.aCurve*this.sustain + (1 - this.aCurve)*Math.pow((this.sustain), 3); //hold until release
this.acc = this.max*this.sustain; //update accumulator & buffer (for release)
this.accBuff = this.acc;
} else if (this.stage == 4) {
this.releaseRate = this.release*(2*sampleRate); //release time as fraction of sample rate
this.inc = this.accBuff/this.releaseRate;
this.acc -= this.inc;
if (this.acc > 0) {
this.output[this.i] = this.aCurve*(this.acc/this.max) + (1 - this.aCurve)*Math.pow((this.acc/this.max), 3);
} else {
this.output[this.i] = 0;
this.stage = 0; //end of envelope
}
}
}
return true;
}
}
registerProcessor("panProcessor", panProcessor);
registerProcessor("bitCrushNode", bitCrushNode);
registerProcessor("envelopeNode", envelopeNode);