/
WebBridge.hpp
476 lines (410 loc) · 16.9 KB
/
WebBridge.hpp
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
/*
* Web Audio + MIDI Bridge for DPF
* Copyright (C) 2021-2022 Filipe Coelho <falktx@falktx.com>
*
* Permission to use, copy, modify, and/or distribute this software for any purpose with
* or without fee is hereby granted, provided that the above copyright notice and this
* permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
* TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
* NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef WEB_BRIDGE_HPP_INCLUDED
#define WEB_BRIDGE_HPP_INCLUDED
#include "NativeBridge.hpp"
#include <emscripten/emscripten.h>
struct WebBridge : NativeBridge {
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
bool captureAvailable = false;
#endif
#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
bool playbackAvailable = false;
#endif
bool active = false;
double timestamp = 0;
WebBridge()
{
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
captureAvailable = EM_ASM_INT({
if (typeof(navigator.mediaDevices) !== 'undefined' && typeof(navigator.mediaDevices.getUserMedia) !== 'undefined')
return 1;
if (typeof(navigator.webkitGetUserMedia) !== 'undefined')
return 1;
return false;
}) != 0;
#endif
#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
playbackAvailable = EM_ASM_INT({
if (typeof(AudioContext) !== 'undefined')
return 1;
if (typeof(webkitAudioContext) !== 'undefined')
return 1;
return 0;
}) != 0;
#endif
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
midiAvailable = EM_ASM_INT({
return typeof(navigator.requestMIDIAccess) === 'function' ? 1 : 0;
}) != 0;
#endif
}
bool open(const char*) override
{
// early bail out if required features are not supported
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
if (!captureAvailable)
{
#if DISTRHO_PLUGIN_NUM_OUTPUTS == 0
d_stderr2("Audio capture is not supported");
return false;
#else
if (!playbackAvailable)
{
d_stderr2("Audio capture and playback are not supported");
return false;
}
d_stderr2("Audio capture is not supported, but can still use playback");
#endif
}
#endif
#if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
if (!playbackAvailable)
{
d_stderr2("Audio playback is not supported");
return false;
}
#endif
const bool initialized = EM_ASM_INT({
if (typeof(Module['WebAudioBridge']) === 'undefined') {
Module['WebAudioBridge'] = {};
}
var WAB = Module['WebAudioBridge'];
if (!WAB.audioContext) {
if (typeof(AudioContext) !== 'undefined') {
WAB.audioContext = new AudioContext();
} else if (typeof(webkitAudioContext) !== 'undefined') {
WAB.audioContext = new webkitAudioContext();
}
}
return WAB.audioContext === undefined ? 0 : 1;
}) != 0;
if (!initialized)
{
d_stderr2("Failed to initialize web audio");
return false;
}
bufferSize = EM_ASM_INT({
var WAB = Module['WebAudioBridge'];
return WAB['minimizeBufferSize'] ? 256 : 2048;
});
sampleRate = EM_ASM_INT_V({
var WAB = Module['WebAudioBridge'];
return WAB.audioContext.sampleRate;
});
allocBuffers(true, true);
EM_ASM({
var numInputs = $0;
var numOutputs = $1;
var bufferSize = $2;
var WAB = Module['WebAudioBridge'];
var realBufferSize = WAB['minimizeBufferSize'] ? 2048 : bufferSize;
var divider = realBufferSize / bufferSize;
// main processor
WAB.processor = WAB.audioContext['createScriptProcessor'](realBufferSize, numInputs, numOutputs);
WAB.processor['onaudioprocess'] = function (e) {
// var timestamp = performance.now();
for (var k = 0; k < divider; ++k) {
for (var i = 0; i < numInputs; ++i) {
var buffer = e['inputBuffer']['getChannelData'](i);
for (var j = 0; j < bufferSize; ++j) {
// setValue($3 + ((bufferSize * i) + j) * 4, buffer[j], 'float');
HEAPF32[$3 + (((bufferSize * i) + j) << 2) >> 2] = buffer[bufferSize * k + j];
}
}
dynCall('vi', $4, [$5]);
for (var i = 0; i < numOutputs; ++i) {
var buffer = e['outputBuffer']['getChannelData'](i);
var offset = bufferSize * (numInputs + i);
for (var j = 0; j < bufferSize; ++j) {
buffer[bufferSize * k + j] = HEAPF32[$3 + ((offset + j) << 2) >> 2];
}
}
}
};
// connect to output
WAB.processor['connect'](WAB.audioContext['destination']);
// resume/start playback on first click
document.addEventListener('click', function(e) {
var WAB = Module['WebAudioBridge'];
if (WAB.audioContext.state === 'suspended')
WAB.audioContext.resume();
});
}, DISTRHO_PLUGIN_NUM_INPUTS_2, DISTRHO_PLUGIN_NUM_OUTPUTS_2, bufferSize, audioBufferStorage, WebAudioCallback, this);
return true;
}
bool close() override
{
freeBuffers();
return true;
}
bool activate() override
{
active = true;
return true;
}
bool deactivate() override
{
active = false;
return true;
}
bool supportsAudioInput() const override
{
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
return captureAvailable;
#else
return false;
#endif
}
bool isAudioInputEnabled() const override
{
#if DISTRHO_PLUGIN_NUM_INPUTS > 0
return EM_ASM_INT({ return Module['WebAudioBridge'].captureStreamNode ? 1 : 0 }) != 0;
#else
return false;
#endif
}
bool requestAudioInput() override
{
DISTRHO_SAFE_ASSERT_RETURN(DISTRHO_PLUGIN_NUM_INPUTS > 0, false);
EM_ASM({
var numInputs = $0;
var WAB = Module['WebAudioBridge'];
var constraints = {};
// we need to use this weird awkward way for objects, otherwise build fails
constraints['audio'] = true;
constraints['video'] = false;
constraints['autoGainControl'] = {};
constraints['autoGainControl']['exact'] = false;
constraints['echoCancellation'] = {};
constraints['echoCancellation']['exact'] = false;
constraints['noiseSuppression'] = {};
constraints['noiseSuppression']['exact'] = false;
constraints['channelCount'] = {};
constraints['channelCount']['min'] = 0;
constraints['channelCount']['ideal'] = numInputs;
constraints['latency'] = {};
constraints['latency']['min'] = 0;
constraints['latency']['ideal'] = 0;
constraints['sampleSize'] = {};
constraints['sampleSize']['min'] = 8;
constraints['sampleSize']['max'] = 32;
constraints['sampleSize']['ideal'] = 16;
// old property for chrome
constraints['googAutoGainControl'] = false;
var success = function(stream) {
WAB.captureStreamNode = WAB.audioContext['createMediaStreamSource'](stream);
WAB.captureStreamNode.connect(WAB.processor);
};
var fail = function() {
};
if (navigator.mediaDevices !== undefined && navigator.mediaDevices.getUserMedia !== undefined) {
navigator.mediaDevices.getUserMedia(constraints).then(success).catch(fail);
} else if (navigator.webkitGetUserMedia !== undefined) {
navigator.webkitGetUserMedia(constraints, success, fail);
}
}, DISTRHO_PLUGIN_NUM_INPUTS_2);
return true;
}
bool supportsBufferSizeChanges() const override
{
return true;
}
bool requestBufferSizeChange(const uint32_t newBufferSize) override
{
// try to create new processor first
bool success = EM_ASM_INT({
var numInputs = $0;
var numOutputs = $1;
var newBufferSize = $2;
var WAB = Module['WebAudioBridge'];
try {
WAB.newProcessor = WAB.audioContext['createScriptProcessor'](newBufferSize, numInputs, numOutputs);
} catch (e) {
return 0;
}
// got new processor, disconnect old one
WAB.processor['disconnect'](WAB.audioContext['destination']);
if (WAB.captureStreamNode)
WAB.captureStreamNode.disconnect(WAB.processor);
return 1;
}, DISTRHO_PLUGIN_NUM_INPUTS_2, DISTRHO_PLUGIN_NUM_OUTPUTS_2, newBufferSize) != 0;
if (!success)
return false;
bufferSize = newBufferSize;
freeBuffers();
allocBuffers(true, true);
if (bufferSizeCallback != nullptr)
bufferSizeCallback(newBufferSize, jackBufferSizeArg);
EM_ASM({
var numInputsR = $0;
var numInputs = $1;
var numOutputs = $2;
var bufferSize = $3;
var WAB = Module['WebAudioBridge'];
// store the new processor
delete WAB.processor;
WAB.processor = WAB.newProcessor;
delete WAB.newProcessor;
// setup new processor the same way as old one
WAB.processor['onaudioprocess'] = function (e) {
// var timestamp = performance.now();
for (var i = 0; i < numInputs; ++i) {
var buffer = e['inputBuffer']['getChannelData'](i);
for (var j = 0; j < bufferSize; ++j) {
// setValue($3 + ((bufferSize * i) + j) * 4, buffer[j], 'float');
HEAPF32[$4 + (((bufferSize * i) + j) << 2) >> 2] = buffer[j];
}
}
dynCall('vi', $5, [$6]);
for (var i = 0; i < numOutputs; ++i) {
var buffer = e['outputBuffer']['getChannelData'](i);
var offset = bufferSize * (numInputsR + i);
for (var j = 0; j < bufferSize; ++j) {
buffer[j] = HEAPF32[$3 + ((offset + j) << 2) >> 2];
}
}
};
// connect to output
WAB.processor['connect'](WAB.audioContext['destination']);
// and input, if available
if (WAB.captureStreamNode)
WAB.captureStreamNode.connect(WAB.processor);
}, DISTRHO_PLUGIN_NUM_INPUTS, DISTRHO_PLUGIN_NUM_INPUTS_2, DISTRHO_PLUGIN_NUM_OUTPUTS_2, bufferSize, audioBufferStorage, WebAudioCallback, this);
return true;
}
bool isMIDIEnabled() const override
{
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
return EM_ASM_INT({ return Module['WebAudioBridge'].midi ? 1 : 0 }) != 0;
#else
return false;
#endif
}
bool requestMIDI() override
{
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
if (midiAvailable)
{
EM_ASM({
var useInput = !!$0;
var useOutput = !!$1;
var maxSize = $2;
var WAB = Module['WebAudioBridge'];
var offset = Module._malloc(maxSize);
var inputCallback = function(event) {
if (event.data.length > maxSize)
return;
var buffer = new Uint8Array(Module.HEAPU8.buffer, offset, maxSize);
buffer.set(event.data);
dynCall('viiif', $3, [$4, buffer.byteOffset, event.data.length, event.timeStamp]);
};
var stateCallback = function(event) {
if (event.port.state === 'connected' && event.port.connection === 'open') {
if (useInput && event.port.type === 'input') {
if (event.port.name.indexOf('Midi Through') < 0)
event.port.onmidimessage = inputCallback;
} else if (useOutput && event.port.type === 'output') {
event.port.open();
}
}
};
var success = function(midi) {
WAB.midi = midi;
midi.onstatechange = stateCallback;
if (useInput) {
midi.inputs.forEach(function(port) {
if (port.name.indexOf('Midi Through') < 0)
port.onmidimessage = inputCallback;
});
}
if (useOutput) {
midi.outputs.forEach(function(port) {
port.open();
});
}
};
var fail = function(why) {
console.log("midi access failed:", why);
};
navigator.requestMIDIAccess().then(success, fail);
}, DISTRHO_PLUGIN_WANT_MIDI_INPUT, DISTRHO_PLUGIN_WANT_MIDI_OUTPUT, kMaxMIDIInputMessageSize, WebMIDICallback, this);
return true;
}
else
#endif
{
d_stderr2("MIDI is not supported");
return false;
}
}
static void WebAudioCallback(void* const userData /* , const double timestamp */)
{
WebBridge* const self = static_cast<WebBridge*>(userData);
// self->timestamp = timestamp;
const uint numFrames = self->bufferSize;
if (self->jackProcessCallback != nullptr && self->active)
{
self->jackProcessCallback(numFrames, self->jackProcessArg);
#if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
if (self->midiAvailable && self->midiOutBuffer.isDataAvailableForReading())
{
static_assert(kMaxMIDIInputMessageSize + 1u == 4, "change code if bumping this value");
uint32_t offset = 0;
uint8_t bytes[4] = {};
double timestamp = EM_ASM_DOUBLE({ return performance.now(); });
while (self->midiOutBuffer.isDataAvailableForReading() &&
self->midiOutBuffer.readCustomData(bytes, ARRAY_SIZE(bytes)))
{
offset = self->midiOutBuffer.readUInt();
EM_ASM({
var WAB = Module['WebAudioBridge'];
if (WAB.midi) {
var timestamp = $5 + $0;
var size = $1;
WAB.midi.outputs.forEach(function(port) {
if (port.state !== 'disconnected') {
port.send(size == 3 ? [ $2, $3, $4 ] :
size == 2 ? [ $2, $3 ] :
[ $2 ], timestamp);
}
});
}
}, offset, bytes[0], bytes[1], bytes[2], bytes[3], timestamp);
}
self->midiOutBuffer.clearData();
}
#endif
}
else
{
for (uint i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS_2; ++i)
std::memset(self->audioBuffers[DISTRHO_PLUGIN_NUM_INPUTS + i], 0, sizeof(float)*numFrames);
}
}
#if DISTRHO_PLUGIN_WANT_MIDI_INPUT
static void WebMIDICallback(void* const userData, uint8_t* const data, const int len, const double timestamp)
{
DISTRHO_SAFE_ASSERT_RETURN(len > 0 && len <= (int)kMaxMIDIInputMessageSize,);
WebBridge* const self = static_cast<WebBridge*>(userData);
// TODO timestamp handling
self->midiInBufferPending.writeByte(static_cast<uint8_t>(len));
self->midiInBufferPending.writeCustomData(data, kMaxMIDIInputMessageSize);
self->midiInBufferPending.commitWrite();
}
#endif
};
#endif // WEB_BRIDGE_HPP_INCLUDED