-
Notifications
You must be signed in to change notification settings - Fork 0
/
DSP-processor.js
356 lines (300 loc) · 16.9 KB
/
DSP-processor.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
/*
Code generated with Faust version 2.40.0
Compilation options: -lang wasm-ib -cn DSP -es 1 -mcd 16 -single -ftz 2
*/
function getJSONDSP() {
return '{"name": "DSP","filename": "DSP.dsp","version": "2.40.0","compile_options": "-lang wasm-ib -cn DSP -es 1 -mcd 16 -single -ftz 2","library_list": ["/usr/local/Cellar/faust/2.40.0/share/faust/stdfaust.lib","/usr/local/Cellar/faust/2.40.0/share/faust/oscillators.lib","/usr/local/Cellar/faust/2.40.0/share/faust/platform.lib","/usr/local/Cellar/faust/2.40.0/share/faust/maths.lib","/usr/local/Cellar/faust/2.40.0/share/faust/basics.lib"],"include_pathnames": ["/usr/local/Cellar/faust/2.40.0/share/faust","/usr/local/share/faust","/usr/share/faust",".","/Users/Flo/Projects/faust-wasm-three"],"size": 262192,"inputs": 0,"outputs": 2,"meta": [ { "basics.lib/name": "Faust Basic Element Library" },{ "basics.lib/version": "0.5" },{ "compile_options": "-lang wasm-ib -cn DSP -es 1 -mcd 16 -single -ftz 2" },{ "filename": "DSP.dsp" },{ "maths.lib/author": "GRAME" },{ "maths.lib/copyright": "GRAME" },{ "maths.lib/license": "LGPL with exception" },{ "maths.lib/name": "Faust Math Library" },{ "maths.lib/version": "2.5" },{ "name": "DSP" },{ "oscillators.lib/name": "Faust Oscillator Library" },{ "oscillators.lib/version": "0.3" },{ "platform.lib/name": "Generic Platform Library" },{ "platform.lib/version": "0.2" }],"ui": [ {"type": "vgroup","label": "DSP","items": [ {"type": "hslider","label": "mouseX","address": "/DSP/mouseX","index": 262152,"init": 0,"min": 0,"max": 1,"step": 0.0001},{"type": "hslider","label": "mouseY","address": "/DSP/mouseY","index": 262164,"init": 0,"min": 0,"max": 1,"step": 0.0001}]}]}';
}
function getBase64CodeDSP() { return "AGFzbQEAAAAB0ICAgAAPYAJ/fwBgBH9/f38AYAF/AX9gAX8Bf2ACf38BfWABfwF/YAJ/fwBgAX8AYAJ/fwBgAn9/AGABfwBgAn9/AX9gAn9/AX9gA39/fQBgAX0BfQKNgICAAAEDZW52BV9zaW5mAA4Dj4CAgAAOAAECAwQFBgcICQoLDA0FjICAgAABAYiAgIAA8IeAgAAHuoGAgAAMB2NvbXB1dGUAAgxnZXROdW1JbnB1dHMAAw1nZXROdW1PdXRwdXRzAAQNZ2V0UGFyYW1WYWx1ZQAFDWdldFNhbXBsZVJhdGUABgRpbml0AAcNaW5zdGFuY2VDbGVhcgAIEWluc3RhbmNlQ29uc3RhbnRzAAkMaW5zdGFuY2VJbml0A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cmVzcyI6ICIvRFNQL21vdXNlWSIsImluZGV4IjogMjYyMTY0LCJpbml0IjogMCwibWluIjogMCwibWF4IjogMSwic3RlcCI6IDAuMDAwMX1dfV19"; }
/*
faust2wasm: GRAME 2017-2019
*/
'use strict';
// Monophonic Faust DSP
class DSPProcessor extends AudioWorkletProcessor {
// JSON parsing functions
static parse_ui(ui, obj, callback) {
for (var i = 0; i < ui.length; i++) {
DSPProcessor.parse_group(ui[i], obj, callback);
}
}
static parse_group(group, obj, callback) {
if (group.items) {
DSPProcessor.parse_items(group.items, obj, callback);
}
}
static parse_items(items, obj, callback) {
for (var i = 0; i < items.length; i++) {
callback(items[i], obj, callback);
}
}
static parse_item1(item, obj, callback) {
if (item.type === "vgroup"
|| item.type === "hgroup"
|| item.type === "tgroup") {
DSPProcessor.parse_items(item.items, obj, callback);
} else if (item.type === "hbargraph"
|| item.type === "vbargraph") {
// Nothing
} else if (item.type === "vslider"
|| item.type === "hslider"
|| item.type === "button"
|| item.type === "checkbox"
|| item.type === "nentry") {
obj.push({
name: item.address,
defaultValue: item.init,
minValue: item.min,
maxValue: item.max
});
}
}
static parse_item2(item, obj, callback) {
if (item.type === "vgroup"
|| item.type === "hgroup"
|| item.type === "tgroup") {
DSPProcessor.parse_items(item.items, obj, callback);
} else if (item.type === "hbargraph"
|| item.type === "vbargraph") {
// Keep bargraph adresses
obj.outputs_items.push(item.address);
obj.pathTable[item.address] = parseInt(item.index);
} else if (item.type === "soundfile") {
// Keep soundfile adresses
obj.soundfile_items.push(item.address);
obj.pathTable[item.address] = parseInt(item.index);
} else if (item.type === "vslider"
|| item.type === "hslider"
|| item.type === "button"
|| item.type === "checkbox"
|| item.type === "nentry") {
// Keep inputs adresses
obj.inputs_items.push(item.address);
obj.pathTable[item.address] = parseInt(item.index);
}
}
static get parameterDescriptors() {
// Analyse JSON to generate AudioParam parameters
var params = [];
DSPProcessor.parse_ui(JSON.parse(getJSONDSP()).ui, params, DSPProcessor.parse_item1);
return params;
}
constructor(options) {
super(options);
this.running = true;
const importObject = {
env: {
memoryBase: 0,
tableBase: 0,
// Integer version
_abs: Math.abs,
// Float version
_acosf: Math.acos,
_asinf: Math.asin,
_atanf: Math.atan,
_atan2f: Math.atan2,
_ceilf: Math.ceil,
_cosf: Math.cos,
_expf: Math.exp,
_floorf: Math.floor,
_fmodf: function (x, y) { return x % y; },
_logf: Math.log,
_log10f: Math.log10,
_max_f: Math.max,
_min_f: Math.min,
_remainderf: function (x, y) { return x - Math.round(x / y) * y; },
_powf: Math.pow,
_roundf: Math.fround,
_sinf: Math.sin,
_sqrtf: Math.sqrt,
_tanf: Math.tan,
_acoshf: Math.acosh,
_asinhf: Math.asinh,
_atanhf: Math.atanh,
_coshf: Math.cosh,
_sinhf: Math.sinh,
_tanhf: Math.tanh,
_isnanf: Number.isNaN,
_isinff: function (x) { return !isFinite(x); },
_copysignf: function (x, y) { return Math.sign(x) === Math.sign(y) ? x : -x; },
// Double version
_acos: Math.acos,
_asin: Math.asin,
_atan: Math.atan,
_atan2: Math.atan2,
_ceil: Math.ceil,
_cos: Math.cos,
_exp: Math.exp,
_floor: Math.floor,
_fmod: function (x, y) { return x % y; },
_log: Math.log,
_log10: Math.log10,
_max_: Math.max,
_min_: Math.min,
_remainder: function (x, y) { return x - Math.round(x / y) * y; },
_pow: Math.pow,
_round: Math.fround,
_sin: Math.sin,
_sqrt: Math.sqrt,
_tan: Math.tan,
_acosh: Math.acosh,
_asinh: Math.asinh,
_atanh: Math.atanh,
_cosh: Math.cosh,
_sinh: Math.sinh,
_tanh: Math.tanh,
_isnan: Number.isNaN,
_isinf: function (x) { return !isFinite(x); },
_copysign: function (x, y) { return Math.sign(x) === Math.sign(y) ? x : -x; },
table: new WebAssembly.Table({ initial: 0, element: 'anyfunc' })
}
};
this.DSP_instance = new WebAssembly.Instance(options.processorOptions.wasm_module, importObject);
this.json_object = JSON.parse(options.processorOptions.json);
this.output_handler = function (path, value) { this.port.postMessage({ path: path, value: value }); };
this.ins = null;
this.outs = null;
this.dspInChannnels = [];
this.dspOutChannnels = [];
this.numIn = parseInt(this.json_object.inputs);
this.numOut = parseInt(this.json_object.outputs);
// Memory allocator
this.ptr_size = 4;
this.sample_size = 4;
this.integer_size = 4;
this.factory = this.DSP_instance.exports;
this.HEAP = this.DSP_instance.exports.memory.buffer;
this.HEAP32 = new Int32Array(this.HEAP);
this.HEAPF32 = new Float32Array(this.HEAP);
// Warning: keeps a ref on HEAP in Chrome and prevent proper GC
//console.log(this.HEAP);
//console.log(this.HEAP32);
//console.log(this.HEAPF32);
// bargraph
this.outputs_timer = 5;
this.outputs_items = [];
// input items
this.inputs_items = [];
// soundfile items
this.soundfile_items = [];
// Start of HEAP index
// DSP is placed first with index 0. Audio buffer start at the end of DSP.
this.audio_heap_ptr = parseInt(this.json_object.size);
// Setup pointers offset
this.audio_heap_ptr_inputs = this.audio_heap_ptr;
this.audio_heap_ptr_outputs = this.audio_heap_ptr_inputs + (this.numIn * this.ptr_size);
// Setup buffer offset
this.audio_heap_inputs = this.audio_heap_ptr_outputs + (this.numOut * this.ptr_size);
this.audio_heap_outputs = this.audio_heap_inputs + (this.numIn * NUM_FRAMES * this.sample_size);
// Start of DSP memory : DSP is placed first with index 0
this.dsp = 0;
this.pathTable = [];
// Send output values to the AudioNode
this.update_outputs = function () {
if (this.outputs_items.length > 0 && this.output_handler && this.outputs_timer-- === 0) {
this.outputs_timer = 5;
for (var i = 0; i < this.outputs_items.length; i++) {
this.output_handler(this.outputs_items[i], this.HEAPF32[this.pathTable[this.outputs_items[i]] >> 2]);
}
}
}
this.initAux = function () {
var i;
if (this.numIn > 0) {
this.ins = this.audio_heap_ptr_inputs;
for (i = 0; i < this.numIn; i++) {
this.HEAP32[(this.ins >> 2) + i] = this.audio_heap_inputs + ((NUM_FRAMES * this.sample_size) * i);
}
// Prepare Ins buffer tables
var dspInChans = this.HEAP32.subarray(this.ins >> 2, (this.ins + this.numIn * this.ptr_size) >> 2);
for (i = 0; i < this.numIn; i++) {
this.dspInChannnels[i] = this.HEAPF32.subarray(dspInChans[i] >> 2, (dspInChans[i] + NUM_FRAMES * this.sample_size) >> 2);
}
}
if (this.numOut > 0) {
this.outs = this.audio_heap_ptr_outputs;
for (i = 0; i < this.numOut; i++) {
this.HEAP32[(this.outs >> 2) + i] = this.audio_heap_outputs + ((NUM_FRAMES * this.sample_size) * i);
}
// Prepare Out buffer tables
var dspOutChans = this.HEAP32.subarray(this.outs >> 2, (this.outs + this.numOut * this.ptr_size) >> 2);
for (i = 0; i < this.numOut; i++) {
this.dspOutChannnels[i] = this.HEAPF32.subarray(dspOutChans[i] >> 2, (dspOutChans[i] + NUM_FRAMES * this.sample_size) >> 2);
}
}
// Parse UI
DSPProcessor.parse_ui(this.json_object.ui, this, DSPProcessor.parse_item2);
// Init DSP
this.factory.init(this.dsp, sampleRate); // 'sampleRate' is defined in AudioWorkletGlobalScope
}
this.setParamValue = function (path, val) {
this.HEAPF32[this.pathTable[path] >> 2] = val;
}
this.getParamValue = function (path) {
return this.HEAPF32[this.pathTable[path] >> 2];
}
// Init resulting DSP
this.initAux();
console.log(this);
}
handleMessage(event) {
var msg = event.data;
switch (msg.type) {
case "destroy": this.running = false; break;
}
}
process(inputs, outputs, parameters) {
var input = inputs[0];
var output = outputs[0];
// Check inputs
if (this.numIn > 0 && (!input || !input[0] || input[0].length === 0)) {
//console.log("Process input error");
return true;
}
// Check outputs
if (this.numOut > 0 && (!output || !output[0] || output[0].length === 0)) {
//console.log("Process output error");
return true;
}
// Copy inputs
if (input !== undefined) {
for (var chan = 0; chan < Math.min(this.numIn, input.length); ++chan) {
var dspInput = this.dspInChannnels[chan];
dspInput.set(input[chan]);
}
}
/*
TODO: sample accurate control change is not yet handled
When no automation occurs, params[i][1] has a length of 1,
otherwise params[i][1] has a length of NUM_FRAMES with possible control change each sample
*/
// Update controls
for (const path in parameters) {
const paramArray = parameters[path];
this.setParamValue(path, paramArray[0]);
}
// Compute
try {
this.factory.compute(this.dsp, NUM_FRAMES, this.ins, this.outs);
} catch (e) {
console.log("ERROR in compute (" + e + ")");
}
// Update bargraph
this.update_outputs();
// Copy outputs
if (output !== undefined) {
for (var chan = 0; chan < Math.min(this.numOut, output.length); ++chan) {
var dspOutput = this.dspOutChannnels[chan];
output[chan].set(dspOutput);
}
}
return this.running;
}
}
// Globals
const NUM_FRAMES = 128;
try {
registerProcessor('DSP', DSPProcessor);
} catch (error) {
console.warn(error);
}