forked from zz85/wild-web-midi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
554 lines (458 loc) · 17.1 KB
/
index.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
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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
<!doctype html>
<html lang="en-us">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Wild Web Midi - Quality streaming Midi to Wave converter with JS </title>
<style>
body {
font-family: arial;
margin: 20px;
padding: none;
}
.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: 30px;
}
#controls {
display: inline-block;
float: right;
vertical-align: top;
margin-top: 30px;
margin-right: 20px;
}
button {
font-size: 14px;
padding: 10px;
margin: 10px;
cursor: pointer;
}
</style>
</head>
<body>
<h2>Wild Web Midi</h2>
<div>
WildMidi is a a wavetable synthesizer that is rather plays and converts midi at pretty good quality. "WildWebMidi" is a port of that runs in your browser in javascript. Open a midi or pick a midi sample below.
</div>
<div class="spinner" id='spinner'></div>
<div class="emscripten" id="status">Downloading...</div>
<div class="emscripten">
<progress value="0" max="100" id="progress" hidden=1></progress>
</div>
<br/>
<style>
#playerbar {
position: absolute;;
height: 2px;
width: 500px;
background: #ccc;
cursor: pointer;
transition: height 0.4s ease-in;
display: inline-block;
}
#playerbar:hover {
height: 4px;
transition: height 0.05s ease-out;
}
#playerprogress {
margin: 0;
background: lime;
width: 0;
height: 100%;
}
#playingtime {
position: absolute;
font-size: 12px;
}
#totaltime {
position: absolute;
left: 500px;
font-size: 12px;
}
</style>
<span>
<span id="playerbar">
<div id="playerprogress"></div>
</span>
<span id="playingtime">0:00</span>
<span id="totaltime">0:00</span>
</span>
<br/>
<div>
<input id="stop" type="button" onclick="stop()" value="Stop"></input>
<label><input type="checkbox" id="waveconversion" /> Run WAV Converter (instead of web audio playback)</label>
</div>
<div>
<button onclick="openFile()" id="drop_zone">Open Midi! (or Drag Files here)</button>
<i>or</i> <select id="playlist">
<option value="">Select a song</option>
</select>
</div>
<br/>
<!--
⬤ #8226
<i>Samples (<a href="https://archive.org/details/GravisUltrasoundMidiFiles">source</a>)</i>
● <a href="#" onclick="return convertMidi('TOCATTA.MID');">Tocatta (2:14)</a>
● <a href="#" onclick="return convertMidi('ENTERTNR.MID');">Entertainer (1:32)</a>
● <a href="#" onclick="return convertMidi('STRIVING.MID');">Striving (4:17)</a>
<br/>
<i>Super Mario Bros (<a href="http://www.gamemusicthemes.com/sheetmusic/nintendo/supermariobros/overworldtheme/">source</a>)</i>
● <a href="#" onclick="return convertMidi('Super_Mario_Bros_-_Overworld_Theme_by_BlueSCD.mid');">Overworld Theme (1:30)</a>
<br/>
<i>Final Fantasy 9 (<a href="http://www.midishrine.com/index.php?id=87">source</a>)</i>
● <a href="#" onclick="return convertMidi('prelude.mid');">Prelude (4:29)</a>
● <a href="#" onclick="return convertMidi('melodies_of_life.mid');">Melodies of life (7:32)</a>
</div>
-->
<div id="completed"></div>
<br/>
<div>Got any suggestions or found any bug? Check out <a href="https://github.com/zz85/wild-web-midi">project on github</a> or follow <a href="https://twitter.com/blurspline">@blurspline</a> on twitter for updates
<br/><br/>
<i>Powered by <a href="https://www.mindwerks.net/projects/wildmidi/">wildmidi</a>, <a href="https://kripken.github.io/emscripten-site">emscripten</a>, and <a href="http://freepats.zenvoid.org/freepats/">Gravis Ultrasound patches</a>.</i>
<i>
p.s. Only loading a piano instrument bank for now to reduce page loading time (2MB!). will add side loading for more instrument patches later
</i>
</div>
<script src="web_audio_player.js"></script>
<script type='text/javascript'>
var statusElement = document.getElementById('status');
var progressElement = document.getElementById('progress');
var spinnerElement = document.getElementById('spinner');
var completedElement = document.getElementById('completed');
var waveConversion = document.getElementById('waveconversion');
var playerbar = document.getElementById('playerbar');
var playerprogress = document.getElementById('playerprogress');
var playingtime = document.getElementById('playingtime');
var totaltime = document.getElementById('totaltime');
var playlist = document.getElementById('playlist');
var SONGS = {
'Debussy - Clair de lune': 'deb_clai.mid',
'Final Fantasy 3 - Chocobo': 'ff3chocobo.mid',
'Chopin - Etude Op.10 No.4': 'etude_10_4_(c)finley.mid',
'Chopin - Fantasy Impromptu': 'chpn_op66.mid',
'Lizst - Etude No. 3 "La Campanella"': 'liz_et3.mid',
'Tocatta': 'TOCATTA.MID',
'Entertainer': 'ENTERTNR.MID',
'Striving': 'STRIVING.MID',
'Super Mario Bros Overworld Theme': 'Super_Mario_Bros_-_Overworld_Theme_by_BlueSCD.mid',
'Final Fantasy 9 - Prelude': 'prelude.mid',
'Final Fantasy 9 - Melodies of life': 'melodies_of_life.mid'
};
for (var s in SONGS) {
var option = document.createElement('option');
option.innerHTML = s;
option.value = SONGS[s];
playlist.appendChild(option);
}
playlist.onchange = function() {
if (this.value) convertMidi(this.value);
}
var input;
var webAudioMode;
var midiName = ''
var convertionJob = null;
function processAudio(buffer_loc, size) {
buffer = circularBuffer.prepare();
var left_buffer_f32 = buffer.getChannelData(0);
var right_buffer_f32 = buffer.getChannelData(1);
// Copy emscripten memory (OpenAL stereo16 format) to JS
for (var i = 0; i < size; i++) {
left_buffer_f32[i] = Module.HEAP16[(buffer_loc >> 1) + 2 * i + 0] / 32768;
right_buffer_f32[i] = Module.HEAP16[(buffer_loc >> 1) + 2 * i + 1] / 32768;
}
}
var ULONG_MAX = 4294967295;
var
currentSamples = 0,
totalSamples = 0,
seekSamples = ULONG_MAX,
pauseAudioAfterDrainingBuffer = false,
signalStop = 0,
callbackOnStop = null
;
playerbar.addEventListener('mousedown', function(e) {
var percent = e.offsetX / 500;
seekSamples = percent * totalSamples | 0;
if (webAudioMode && !convertionJob) {
convertMidi(midiName)
}
});
function stop() {
circularBuffer.reset();
signalStop = 1;
}
function updateProgress(current, total) {
currentSamples = current;
totalSamples = total;
playerprogress.style.width = (current / total * 100) + '%';
playingtime.innerHTML = samplesToTime(current);
totaltime.innerHTML = samplesToTime(total);
// console.log('t', current, total);
}
function samplesToTime(at) {
var in_s = Math.ceil(at / SAMPLE_RATE);
var s = in_s % 60;
var min = in_s / 60 | 0;
return min + ':' + (s === 0 ? '00' : s < 10 ? '0' + s : s);
}
function convertMidi(name) {
midiName = name;
convert();
return false;
}
// File Open code adapted from "mrdoobapproves" project
function openFile() {
openAs(document.body);
}
function onFileOpen(file, data) {
midiName = file.name;
console.log('open ', midiName);
FS.writeFile('/freepats/' + midiName, data, { encoding: 'binary' });
convert();
}
// function handleFileSelect(evt) {
// evt.stopPropagation();
// evt.preventDefault();
// var files = evt.dataTransfer.files; // FileList object.
// // files is a FileList of File objects. List some properties.
// var output = [];
// for (var i = 0, f; f = files[i]; i++) {
// output.push('<li><strong>', escape(f.name), '</strong> (', f.type || 'n/a', ') - ',
// f.size, ' bytes, last modified: ',
// f.lastModifiedDate ? f.lastModifiedDate.toLocaleDateString() : 'n/a',
// '</li>');
// }
// document.getElementById('list').innerHTML = '<ul>' + output.join('') + '</ul>';
// }
function handleFileSelect(evt) {
console.log('handleFileSelect', evt)
evt.stopPropagation();
evt.preventDefault();
var files = evt.dataTransfer ? evt.dataTransfer.files : evt.target.files;
// var files = evt.target.files; // FileList object
var f = files[ 0 ]; // TODO: handle multiple midi files in future
if (!f) return;
if (!f.type.match(/audio\/mid/)) {
console.log('Warning: ' + f.type + ' does not seem to be a midi file');
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = function(e) {
var arrayBuffer = e.target.result;
var byteArray = new Uint8Array(arrayBuffer);
onFileOpen(f, byteArray);
};
reader.readAsArrayBuffer(f);
if (input) input.value = '';
}
function openAs(target) {
if (!input) {
input = document.createElement('input');
input.style.display = 'none';
input.type = 'file';
input.addEventListener('change', handleFileSelect);
target = target || document.body;
target.appendChild(input);
}
var e = document.createEvent('MouseEvents');
e.initMouseEvent(
'click', true, false, window, 0, 0, 0, 0, 0,
false, false, false, false, 0, null
);
input.dispatchEvent(e);
}
function convert() {
webAudioMode = !waveConversion.checked;
if (webAudioMode && convertionJob) {
stop();
callbackOnStop = convert;
return;
}
if (convertionJob) {
console.log('current midi is running...');
Module.setStatus('Current job still running...');
return;
}
spinnerElement.style.display = 'inline-block';
Module.setStatus(webAudioMode ?
'Playing':
'Converting...');
// add small delay so UI can update.
setTimeout(runConversion, 200);
}
function runConversion() {
if(!audioIsInitted){initAudio();}
convertionJob = {
sourceMidi: 'freepats/' + midiName,
targetWav: midiName.replace(/\.midi?$/i, '.wav'),
targetPath: this.sourceMidi + '.wav',
conversion_start: Date.now()
};
var sleep = -1; // use -1 for a blocking loop which is the fastest actually.
if (webAudioMode) {
convertionJob.targetPath = '';
sleep = 10;
circularBuffer.reset();
setTimeout(startAudio, 100);
}
var method = 'async';
switch (method) {
case 'synchronous':
wildwebmidi(sourceMidi, targetPath);
break;
case 'worker':
worker.postMessage({
type: 'convert',
content: [sourceMidi, targetPath]
});
break;
case 'async':
Module.ccall('wildwebmidi',
null,
['string', 'string', 'number'],
[convertionJob.sourceMidi, convertionJob.targetPath, sleep],
{ async: true }
);
break;
}
}
function completeConversion(status) {
pauseAudioAfterDrainingBuffer = true;
console.log('complete conversion', status)
var conversion_time = Date.now() - convertionJob.conversion_start;
// console.timeEnd('conversion');
Module.setStatus('');
if (convertionJob.targetPath) {
var wave = FS.readFile(convertionJob.targetPath);
FS.unlink(convertionJob.targetPath); // clean memeory!!
var blob = new Blob( [ wave ], { type: 'audio/wave' } );
var objectURL = URL.createObjectURL( blob );
var audio = document.createElement('audio');
audio.src = objectURL;
audio.controls = true;
audio.autoplay = true;
completedElement.appendChild(audio);
completedElement.appendChild(document.createTextNode(' Took ' + (conversion_time / 1000 | 0) + 's. '));
var link = document.createElement('a');
link.innerHTML = 'Download ' + convertionJob.targetWav;
link.href = objectURL;
link.download = convertionJob.targetWav;
link.target = '_blank';
completedElement.appendChild(link);
completedElement.appendChild(document.createElement('br'));
}
convertionJob = null;
}
var Module = {
// arguments: '-c /freepats/freepats.cfg -o /freepats/hoho.wav /freepats/alb_se5.mid'.split(' '),
noInitialRun: true,
// noExitRuntime: true,
preRun: [],
postRun: [],
print: (function() {
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(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);
}
},
setStatus: function(text) {
if (!Module.setStatus.last) Module.setStatus.last = { time: Date.now(), text: '' };
if (text === Module.setStatus.text) return;
var m = text.match(/([^(]+)\((\d+(\.\d+)?)\/(\d+)\)/);
var now = Date.now();
if (m && now - Date.now() < 30) return; // if this is a progress update, skip it if too soon
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';
}
statusElement.innerHTML = text;
},
totalDependencies: 0,
monitorRunDependencies: function(left) {
this.totalDependencies = Math.max(this.totalDependencies, left);
Module.setStatus(left ? 'Preparing... (' + (this.totalDependencies-left) + '/' + this.totalDependencies + ')' : 'Just downloading midi patches now.');
}
};
Module.setStatus('Downloading...');
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);
};
};
</script>
<script>
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy'; // Explicitly show this is a copy.
}
// Setup the dnd listeners.
var dropZone = document.getElementById('drop_zone');
dropZone.addEventListener('dragover', handleDragOver, false);
dropZone.addEventListener('drop', handleFileSelect, false);
</script>
<script async type="text/javascript" src="wildwebmidi.js"></script>
</body>
</html>