forked from VCVRack/Rack
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Rack.html
334 lines (287 loc) · 14.6 KB
/
Rack.html
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
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>miRack</title>
<style>
html {
height:100%;
}
body {
font-family: sans-serif;
margin: 0;
height:100%;
overflow:hidden;
}
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
div.emscripten { text-align: center; }
div.emscripten_border { border: 1px solid black; }
/* the canvas *must not* have any border or padding, or mouse coords will be wrong */
canvas.emscripten { border: 0px none; background-color: black; }
#emscripten_logo {
display: inline-block;
margin: 0;
}
.spinner {
height: 30px;
width: 30px;
margin: 0;
margin-top: 20px;
margin-left: 20px;
display: inline-block;
vertical-align: top;
-webkit-animation: rotation .8s linear infinite;
-moz-animation: rotation .8s linear infinite;
-o-animation: rotation .8s linear infinite;
animation: rotation 0.8s linear infinite;
border-left: 5px solid rgb(235, 235, 235);
border-right: 5px solid rgb(235, 235, 235);
border-bottom: 5px solid rgb(235, 235, 235);
border-top: 5px solid rgb(120, 120, 120);
border-radius: 100%;
background-color: rgb(189, 215, 46);
}
@-webkit-keyframes rotation {
from {-webkit-transform: rotate(0deg);}
to {-webkit-transform: rotate(360deg);}
}
@-moz-keyframes rotation {
from {-moz-transform: rotate(0deg);}
to {-moz-transform: rotate(360deg);}
}
@-o-keyframes rotation {
from {-o-transform: rotate(0deg);}
to {-o-transform: rotate(360deg);}
}
@keyframes rotation {
from {transform: rotate(0deg);}
to {transform: rotate(360deg);}
}
#status {
display: inline-block;
vertical-align: top;
margin-top: 30px;
margin-left: 20px;
font-weight: bold;
color: rgb(120, 120, 120);
}
#progress {
height: 20px;
width: 300px;
}
</style>
</head>
<body>
<div>
<canvas class="emscripten" id="canvas" oncontextmenu="event.preventDefault()" style="display:none;"></canvas>
<button id="startaudio" onclick="startAudio2.apply(null, startAudioParams);this.remove();" style="display:none;position:absolute;top:100px;left:50%;padding:20px;background:lightgreen;border:1px solid black;">Start Audio</button>
</div>
<div style="position: absolute;top:0;left:0">
<div class="spinner" id='spinner'></div>
<div class="emscripten" id="status">Downloading...</div>
</div>
<div class="emscripten">
<progress value="0" max="100" id="progress" hidden=1></progress>
</div>
<input id="upload" type="file" style="display:none;">
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var spinnerElement = document.getElementById('spinner');
var Module = {
preRun: [ function() {
Browser.resizeListeners.push(function(w, h) {
Module.canvas.style.width = w/window.devicePixelRatio + 'px';
Module.canvas.style.height = h/window.devicePixelRatio + 'px';
});
}],
postRun: [],
print: (function() {
return function(text) {
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (0) { // XXX disabled for safety typeof dump == 'function') {
dump(text + '\n'); // fast, straight to the real console
} else {
console.error(text);
}
},
canvas: (function() {
var canvas = document.getElementById('canvas');
// As a default initial behavior, pop up an alert when webgl context is lost. To make your
// application robust, you may want to override this behavior before shipping!
// See http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.15.2
canvas.addEventListener("webglcontextlost", function(e) { alert('WebGL context lost. You will need to reload the page.'); e.preventDefault(); }, false);
return canvas;
})(),
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.last.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Module.setStatus.last.time < 30) return; // if this is a progress update, skip it if too soon
Module.setStatus.last.time = now;
Module.setStatus.last.text = text;
if (m) {
text = m[1];
progressElement.value = parseInt(m[2])*100;
progressElement.max = parseInt(m[4])*100;
progressElement.hidden = false;
spinnerElement.hidden = false;
} else {
progressElement.value = null;
progressElement.max = null;
progressElement.hidden = true;
if (!text) spinnerElement.style.display = 'none';
Module.canvas.style.display = '';
}
statusElement.innerHTML = text;
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'All downloads complete.');
}
};
Module.setStatus('Downloading...');
var w = document.body.clientWidth, h = document.body.clientHeight;
document.getElementById('canvas').width = w * window.devicePixelRatio;
document.getElementById('canvas').height = h * window.devicePixelRatio;
/* function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer.files; // FileList object.
}
document.getElementById('canvas').addEventListener('dragover', handleDragOver, false);
document.getElementById('canvas').addEventListener('drop', handleFileSelect, false);*/
// In Safari, there are Atomics but they don't work without SharedArrayBuffer which is disabled
if (window.SharedArrayBuffer === undefined)
delete window.Atomics;
var useAudioWorklet;
var startAudioParams;
function startAudio(audio, addr, blockSize) {
if (useAudioWorklet === undefined)
useAudioWorklet = (window.AudioWorkletNode !== undefined && window.SharedArrayBuffer !== undefined && window.confirm("Use Audio Worklet?"));
startAudioParams = arguments;
document.getElementById('startaudio').style.display = null;
}
function startAudio2(audio, addr, blockSize) {
if (useAudioWorklet) {
if (Module.context) {
if (Module.node)
Module.node.port.postMessage({
cmd: 'start',
audio: audio,
addr: addr,
});
return;
}
Module.context = new AudioContext({ sampleRate:44100, latencyHint:'balanced' });
Module.context.audioWorklet.addModule('Rack2.js').then(()=>{
Module.node = new AudioWorkletNode(Module.context, 'port-processor', { outputChannelCount:[2] });
Module.node.channelCountMode = 'explicit';
Module.node.port.postMessage({
cmd: 'init',
buffer: buffer,
PthreadWorkerInit: PthreadWorkerInit,
stackSize: 2097152,
stackBase: Module._malloc(2097152),
tempDoublePtr: Module.getMemory(8),
TOTAL_MEMORY: Module.TOTAL_MEMORY,
STATICTOP: Module.STATICTOP,
DYNAMIC_BASE: Module.DYNAMIC_BASE,
DYNAMICTOP_PTR: Module.DYNAMICTOP_PTR,
});
Module.node.port.postMessage({
cmd: 'start',
audio: audio,
addr: addr,
});
let oscillator = Module._osc = new OscillatorNode(Module.context);
Module.osc = oscillator;
oscillator.connect(Module.node);
Module.node.connect(Module.context.destination);
oscillator.start();
});
} else {
if (Module.audioNode)
return;
if (!Module.context) {
Module.context = new(window.AudioContext || window.webkitAudioContext)({ sampleRate:44100, latencyHint:'balanced' });
Module.sourceNode = Module.context.createOscillator();
Module.sourceNode.start(0);
}
Module.audioNode = Module.context.createScriptProcessor(blockSize, 0, 2);
Module.audioNode.blockSize = blockSize;
Module.audioNode.onaudioprocess = function(ev) {
Module._processAudioJS(blockSize);
var channel0 = ev.outputBuffer.getChannelData(0);
var channel1 = ev.outputBuffer.getChannelData(1);
var pData = addr;
pData >>= 2;
for (var i = 0; i < blockSize; ++i) {
channel0[i] = HEAPF32[pData++];
channel1[i] = HEAPF32[pData++];
}
};
Module.sourceNode.connect(Module.audioNode);
Module.audioNode.connect(Module.context.destination);
}
}
function stopAudio() {
if (useAudioWorklet) {
if (!Module.node)
return;
Module.node.port.postMessage({
cmd: 'stop',
});
} else {
if (!Module.audioNode)
return;
Module.sourceNode.disconnect();
Module.audioNode.disconnect();
Module.audioNode.onaudioprocess = null;
Module.audioNode = null;
}
}
function getAudioSampleRate() {
if (!Module.context)
return 44100;
return Module.context.sampleRate;
}
function getAudioBlockSize() {
if (!Module.context)
return useAudioWorklet ? 128 : startAudioParams[2];
return useAudioWorklet ? 128 : Module.audioNode.blockSize;
}
//alert('Download size is about 40MB, please be patient. Some modules may not work and/or crash the app, you\'ve been warned.\n\nIMPORTANT\n\n1. MIDI input requires Chrome browser.\n\n2. For best results, use the latest version of Chrome, then navigate to chrome://flags and enable SharedArrayBuffer support.\n\nSee github.com/mi-rack/Rack for information about miRack, plugins, and licenses.');
alert('Download size is about 40MB, please be patient. Some modules may not work and/or crash the app, you\'ve been warned.\n\nMIDI input requires Chrome browser.\n\nSee github.com/mi-rack/Rack for information about miRack, plugins, and licenses.');
</script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js"></script>
<script type="text/javascript" src="Rack.js"></script>
<script>
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
Module.setStatus('Exception thrown, see JavaScript console');
spinnerElement.style.display = 'none';
Module.setStatus = function(text) {
if (text) Module.printErr('[post-exception status] ' + text);
};
};
window.onresize = function() {
var w = document.body.clientWidth, h = document.body.clientHeight;
Module.setCanvasSize(w * window.devicePixelRatio, h * window.devicePixelRatio);
Module.canvas.style.width = w + 'px';
Module.canvas.style.height = h + 'px';
};
</script>
</body>
</html>