-
Notifications
You must be signed in to change notification settings - Fork 27
/
main.js
424 lines (394 loc) · 22.8 KB
/
main.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
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
var parseMidi = require('midi-file').parseMidi;
var writeMidi = require('midi-file').writeMidi;
var inst_to_track = {"piano":0,"violin":1,"cello":2,"bass":3,"guitar":4,"flute":5,"clarinet":6,"trumpet":7,"harp":8,"drum":9};
var inst_to_instgroup = {"piano":"piano","violin":"strings","cello":"strings","bass":"bass","guitar":"guitar","flute":"winds","clarinet":"winds","trumpet":"winds","harp":"harp","drums":"drums"};
window.copyup = function(elemid) {
document.getElementById("inbox").value = document.getElementById(elemid).value.trim();
window.encodingToMidiFile(document.getElementById("inbox").value, "download_inbox");
}
window.importMidi = function() {
alert("For best results, assign different tracks to different instruments out of Piano, Violin, Cello, Bass, Guitar, Flute, Clarinet, Trumpet, Harp, and Drums.\n" +
"The base MIDI linked to the right is already set up with these tracks.");
javascript:document.getElementById('file-input').click();
}
window.importMidiOpenAI = function() {
alert("For best results, use a tempo of 62.5, 125, or 250.");
if (!window.isLoading) {
javascript:document.getElementById('file-input-openai').click();
}
}
window.importMidiFile = function(file) {
var reader = new FileReader();
reader.readAsArrayBuffer(file);
window.file = file;
reader.onload = readerEvent => {
var content = new Uint8Array(readerEvent.target.result);
var midiData = parseMidi(content);
var mergedTrack = [];
var originalOrder = 0;
for (var i=0; i<midiData.tracks.length; i++) {
var startTime = 0;
for (var j=0; j<midiData.tracks[i].length; j++) {
var event = midiData.tracks[i][j];
startTime += event.deltaTime;
event.startTime = Math.round(startTime * 48 / midiData.header.ticksPerBeat);
event.originalOrder = originalOrder;
mergedTrack.push(event);
originalOrder++;
}
}
mergedTrack.sort(function(a, b){
if(a.startTime < b.startTime) { return -1; }
if(a.startTime > b.startTime) { return 1; }
if(a.originalOrder < b.originalOrder) { return -1; }
if(a.originalOrder > b.originalOrder) { return 1; }
return 0;
})
document.getElementById("piano").checked = false;
document.getElementById("strings").checked = false;
document.getElementById("winds").checked = false;
document.getElementById("drums").checked = false;
document.getElementById("harp").checked = false;
document.getElementById("guitar").checked = false;
document.getElementById("bass").checked = false;
var encoded = "";
var currentInsts = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
for (var i=0; i<mergedTrack.length; i++) {
var event = mergedTrack[i];
var deltaTime = (i == 0) ? event.startTime : (event.startTime - mergedTrack[i-1].startTime);
var timeLeftToWait = deltaTime;
while (timeLeftToWait > 0) {
var waitTime = timeLeftToWait > 128 ? 128 : timeLeftToWait;
var token = 3968 + waitTime - 1;
encoded += token + " ";
timeLeftToWait -= waitTime;
}
if (event.type == "programChange") {
currentInsts[event.channel] = event.programNumber;
}
var currentInst = currentInsts[event.channel];
var inst;
var baseNoteOn;
var baseNoteOff;
var checkboxID;
if ([40,41,44,45,48,49,50,51].indexOf(currentInst) > -1) {
inst = "violin";
baseNoteOn = 14*128;
baseNoteOff = 15*128;
checkboxID = "strings";
} else if ([42,43].indexOf(currentInst) > -1) {
inst = "cello";
baseNoteOn = 16*128;
baseNoteOff = 17*128;
checkboxID = "strings";
} else if ([32,33,34,35,36,37,38,39].indexOf(currentInst) > -1) {
inst = "bass";
baseNoteOn = 18*128;
baseNoteOff = 19*128;
checkboxID = "bass";
} else if ([24,25,26,27,28,29,30,31].indexOf(currentInst) > -1) {
inst = "guitar";
baseNoteOn = 20*128;
baseNoteOff = 21*128;
checkboxID = "guitar";
} else if ([72,73,74,75,76,77,78,79].indexOf(currentInst) > -1) {
inst = "flute";
baseNoteOn = 22*128;
baseNoteOff = 23*128;
checkboxID = "winds";
} else if ([64,65,66,67,68,69,70,71].indexOf(currentInst) > -1) {
inst = "clarinet";
baseNoteOn = 24*128;
baseNoteOff = 25*128;
checkboxID = "winds";
} else if ([56,57,58,59,60,61,62,63].indexOf(currentInst) > -1) {
inst = "trumpet";
baseNoteOn = 26*128;
baseNoteOff = 27*128;
checkboxID = "winds";
} else if ([46].indexOf(currentInst) > -1) {
inst = "harp";
baseNoteOn = 28*128;
baseNoteOff = 29*128;
checkboxID = "harp";
} else {
inst = "piano";
var baseNoteGroup = Math.round(event.velocity/8) - 2;
if (baseNoteGroup < 1) { baseNoteGroup = 1; }
if (baseNoteGroup > 13) { baseNoteGroup = 13; }
baseNoteOn = baseNoteGroup*128;
baseNoteOff = 0*128;
checkboxID = "piano";
}
if (event.channel == 9) {
inst = "drum";
baseNoteOn = 3840;
baseNoteOff = null;
checkboxID = "drums";
}
if (event.type == "noteOff" || (event.type == "noteOn" && event.velocity==0)) {
if (baseNoteOff !== null) {
token = baseNoteOff + event.noteNumber;
encoded += token + " ";
}
} else if (event.type == "noteOn" && event.velocity > 0) {
token = baseNoteOn + event.noteNumber;
encoded += token + " ";
document.getElementById(checkboxID).checked = true;
}
}
document.getElementById("inbox").value = encoded.trim();
window.encodingToMidiFile(document.getElementById("inbox").value, "download_inbox");
}
}
window.importMidiFileOpenAI = function(file) {
var reader = new FileReader();
reader.readAsDataURL(file);
window.file = file;
startLoading();
reader.onload = readerEvent => {
var b64content = "b'" + readerEvent.target.result.split(',')[1] + "'";
console.log(b64content);
fetch("https://musenet.openai.com/midi", {
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": JSON.stringify({
"midi": b64content,
"audioFormat": document.getElementById("format").value
})
}).then(res => res.json()).then(function (response) {
document.getElementById("inbox").value = response.completions[0].encoding;
window.encodingToMidiFile(document.getElementById("inbox").value, "download_inbox");
document.getElementById("piano").checked = false;
document.getElementById("strings").checked = false;
document.getElementById("winds").checked = false;
document.getElementById("drums").checked = false;
document.getElementById("harp").checked = false;
document.getElementById("guitar").checked = false;
document.getElementById("bass").checked = false;
for (var i=0; i<response.completions[0].tracks.length; i++) {
document.getElementById(inst_to_instgroup[response.completions[0].tracks[i].instrument]).checked = true;
}
stopLoading();
}).catch(error => {
stopLoading();
alert(error);
});
}
}
var ding = new Audio('chord.wav');
ding.volume = 0.1;
window.isLoading = false;
function startLoading() {
document.getElementById("button").disabled = true;
window.isLoading = true;
document.getElementById("loader-inner").style.animation = "progress 60s linear both";
}
function stopLoading() {
ding.play();
document.getElementById("button").disabled = false;
window.isLoading = false;
document.getElementById("loader-inner").style.animation = "none";
}
window.extend = function() {
startLoading();
var genreList = ["chopin","mozart","rachmaninoff","ladygaga","country","disney","jazz","bach","beethoven","journey","thebeatles","video","broadway","franksinatra","bluegrass","tchaikovsky","liszt","everything","ragtime","andrehazes","cocciante","thecranberries","ligabue","metallica","traffic","philcollins","nineinchnails","thepretenders","sugarray","grandfunkrailroad","ron","ellington","fleetwoodmac","thebeachboys","kool & the gang","foreigner","tlc","scottjames","benfoldsfive","smashmouth","oasis","allsaints","donnasummer","weezer","bjork","mariahcarey","berte","cheaptrick","caroleking","thecars","gganderson","robertpalmer","zucchero","alicecooper","vanhalen","brucehornsby","coolio","jimmybuffett","lobo","badcompany","eminem","creedenceclearwaterrevival","deeppurple","shearinggeorge","robbiewilliams","dalla","ub40","lindaronstadt","sinatra","inxs","jonimitchell","michaeljackson","last","devo","shaniatwain","korn","brooksgarth","sweet","thewho","roxette","bowiedavid","beegees","renefroger","mina","estefangloria","mccartney","theventures","carboni","simplyred","santana","jewel","meatloaf","giorgia","nofx","rickymartin","thecure","thetemptations","tozzi","beck","eiffel65","jenniferlopez","reelbigfish","patsycline","richardcliff","styx","acdc","brucespringsteen","michaelgeorge","blondie","pinkfloyd","oldfieldmike","redhotchilipeppers","therollingstones","morandi","heart","robertaflack","pantera","alabama","jethrotull","hanson","mosch","ludwigvanbeethoven","dvorak","chrisrea","guns n' roses","duranduran","ericclapton","bettemidler","bwitched","gordonlightfoot","thegrassroots","chicago","whitezombie","michaelbolton","paulsimon","marillion","thepointersisters","theanimals","cher","haydn","aerosmith","supertramp","littleriverband","america","tonyorlando","tompetty","thecorrs","aliceinchains","kiss","prince","toto","vanmorrison","wagner","cashjohnny","annielennox","enya","thedoobiebrothers","thetragicallyhip","rush","laurapausini","stevemillerband","simonandgarfunkel","fiorellamannoia","olivianewton-john","carlysimon","elvispresley","vangelis","bobdylan","bbking","vengaboys","paoli","thehollies","alainsouchon","pooh","raf","fiorello","lionelrichie","jimihendrix","theeverlybrothers","limpbizkit","donhenley","georgeharrison","threedognight","johnmellencamp","carpenters","raycharles","basie","billyocean","scorpions","royorbison","whitneyhouston","ironmaiden","jovanotti","alanjackson","barrymanilow","hueylewis","kennyloggins","chopinfrederic","talkingheads","themonkees","rem","jeanmicheljarre","michelezarrillo","eurythmics","thedoors","guesswho","miller","thefourseasons","matiabazar","tompettyandtheheartbreakers","chickcorea","scottjoplin","amedeominghi","bryanadams","paulaabdul","rossivasco","billyjoel","daniele","claudedebussy","gilbert & sullivan","chakakhan","nirvana","garbage","andreabocelli","johnnyrivers","emerson, lake & palmer","theallmanbrothersband","zappa","boston","mango","barbrastreisand","willsmith","ozzyosbourne","janetjackson","antonellovenditti","u2","humperdinckengelbert","jamiroquai","zero","chuckberry","spicegirls","ledzeppelin","masini","thekinks","eagles","billyidol","alanismorissette","joecocker","jimcroce","bobmarley","blacksabbath","stonetemplepilots","silverchair","paulmccartney","blur","nek","greenday","thepolice","depechemode","rageagainstthemachine","madonna","rogerskenny","brooks & dunn","883","thedrifters","amygrant","herman","toriamos","eltonjohn","britneyspears","lennykravitz","celentano","ringostarr","neildiamond","aqua","oscarpeterson","joejackson","moby","collinsphil","leosayer","takethat","electriclightorchestra","pearljam","marcanthony","borodin","petshopboys","stevienicks","hollybuddy","turnertina","annaoxa","zztop","sting","themoodyblues","ruggeri","creed","claudebolling","renzoarbore","erasure","elviscostello","airsupply","tinaturner","leali","petergabriel","nodoubt","bread","huey lewis & the news","brandy","level42","radiohead","georgebenson","wonderstevie","thesmashingpumpkins","cyndilauper","rodstewart","bush","ramazzotti","bobseger","theshadows","gershwin","cream","biagioantonacci","steviewonder","nomadi","direstraits","davidbowie","amostori","thealanparsonsproject","johnlennon","crosbystillsnashandyoung","battiato","kansas","clementi","richielionel","yes","brassensgeorges","steelydan","jacksonmichael","buddyholly","earthwindandfire","natkingcole","therascals","bonjovi","alanparsons","backstreetboys","glencampbell","howardcarpendale","thesupremes","villagepeople","blink-182","jacksonbrowne","sade","lynyrdskynyrd","foofighters","2unlimited","battisti","hall & oates","stansfieldlisa","genesis","boyzone","theoffspring","tomjones","davematthewsband","johnelton","neilyoung","dionnewarwick","aceofbase","marilynmanson","taylorjames","rkelly","grandi","sublime","edvardgrieg","tool","bachjohannsebastian","patbenatar","celinedion","queen","soundgarden","abba","drdre","defleppard","dominofats","realmccoy","natalieimbruglia","hole","spinners","arethafranklin","reospeedwagon","indian","movie","scottish","irish","african","taylorswift","shakira","blues","latin","katyperry","world","kpop","africandrum","michaelbuble","rihanna","gospel","beyonce","chinese","arabic","adele","kellyclarkson","theeagles","handel","rachmaninov","schumann","christmas","dance","punk","natl_anthem","brahms","rap","ravel","burgmueller","other","schubert","granados","albeniz","mendelssohn","debussy","grieg","moszkowski","godowsky","folk","mussorgsky","kids","balakirev","hymns","verdi","hummel","deleted","delibes","saint-saens","puccini","satie","offenbach","widor","songs","stravinsky","vivaldi","gurlitt","alkan","weber","strauss","traditional","rossini","mahler","soler","sousa","telemann","busoni","scarlatti","stamitz","classical","jstrauss2","gabrieli","nielsen","purcell","donizetti","kuhlau","gounod","gibbons","weiss","faure","holst","spohr","monteverdi","reger","bizet","elgar","czerny","sullivan","shostakovich","franck","rubinstein","albrechtsberger","paganini","diabelli","gottschalk","wieniawski","lully","morley","sibelius","scriabin","heller","thalberg","dowland","carulli","pachelbel","sor","marcello","ketterer","rimsky-korsakov","ascher","bruckner","janequin","anonymous","kreutzer","sanz","joplin","susato","giuliani","lassus","palestrina","smetana","berlioz","couperin","gomolka","daquin","herz","campion","walthew","pergolesi","reicha","polak","holborne","hassler","corelli","cato","azzaiolo","anerio","gastoldi","goudimel","dussek","prez","cimarosa","byrd","praetorius","rameau","khachaturian","machaut","gade","perosi","gorzanis","smith","haberbier","carr","marais","glazunov","guerrero","cabanilles","losy","roman","hasse","sammartini","blow","zipoli","duvernoy","aguado","cherubini","victoria","field","andersen","poulenc","d'aragona","lemire","krakowa","maier","rimini","encina","banchieri","best","galilei","warhorse","gypsy","soundtrack","encore","roblaidlow","nationalanthems","benjyshelton","ongcmu","crosbystillsnashyoung","smashingpumpkins","aaaaaaaaaaa","alanismorrisette","animenz","onedirection","nintendo","disneythemes","gunsnroses","rollingstones","juliancasablancas","abdelmoinealfa","berckmansdeoliveira","moviethemes","beachboys","davemathews","videogamethemes","moabberckmansdeoliveira","unknown","cameronleesimpson","johannsebastianbach","thecarpenters","elo","nightwish","blink182","emersonlakeandpalmer","tvthemes"];
var genre = document.getElementById("genre").value;
if (genre == "<random>") {
genre = genreList[Math.floor(Math.random()*genreList.length)];
}
fetch("https://musenet.openai.com/sample", {
"method": "POST",
"headers": {
"Content-Type": "application/json"
},
"body": JSON.stringify({
"genre": genre,
"instrument":{
"piano": document.getElementById("piano").checked,
"strings": document.getElementById("strings").checked,
"winds": document.getElementById("winds").checked,
"drums": document.getElementById("drums").checked,
"harp": document.getElementById("harp").checked,
"guitar": document.getElementById("guitar").checked,
"bass": document.getElementById("bass").checked
},
"encoding":document.getElementById("inbox").value,
"temperature":parseFloat(document.getElementById("temperature").value),
"truncation":parseFloat(document.getElementById("truncation").value),
"generationLength":parseFloat(document.getElementById("generationLength").value),
"audioFormat": document.getElementById("format").value
})
}).then(res => res.json()).then(function (response) {
var seekTo = window.oldDuration - 5;
if (isNaN(seekTo) || !isFinite(seekTo) || seekTo < 0) {
seekTo = 0;
}
//need to convert from dataURI to blob to avoid firefox issue
var format = "audio/mp3";
var audioKey = "audioFile";
if (response.completions[0].oggFile) {
format = "audio/ogg";
audioKey = "oggFile";
}
document.getElementById('sound1').src=URL.createObjectURL(new Blob([convertDataURIToBinary("data:"+format+";base64,"+response.completions[0][audioKey].substring(2,response.completions[0][audioKey].length-1))], {type : format}));
document.getElementById('sound2').src=URL.createObjectURL(new Blob([convertDataURIToBinary("data:"+format+";base64,"+response.completions[1][audioKey].substring(2,response.completions[1][audioKey].length-1))], {type : format}));
document.getElementById('sound3').src=URL.createObjectURL(new Blob([convertDataURIToBinary("data:"+format+";base64,"+response.completions[2][audioKey].substring(2,response.completions[2][audioKey].length-1))], {type : format}));
document.getElementById('sound4').src=URL.createObjectURL(new Blob([convertDataURIToBinary("data:"+format+";base64,"+response.completions[3][audioKey].substring(2,response.completions[3][audioKey].length-1))], {type : format}));
document.getElementById("outbox1").value = response.completions[0].encoding;
document.getElementById("outbox2").value = response.completions[1].encoding;
document.getElementById("outbox3").value = response.completions[2].encoding;
document.getElementById("outbox4").value = response.completions[3].encoding;
window.encodingToMidiFile(response.completions[0].encoding, "download_outbox1");
window.encodingToMidiFile(response.completions[1].encoding, "download_outbox2");
window.encodingToMidiFile(response.completions[2].encoding, "download_outbox3");
window.encodingToMidiFile(response.completions[3].encoding, "download_outbox4");
document.getElementById('sound1').currentTime = seekTo;
document.getElementById('sound2').currentTime = seekTo;
document.getElementById('sound3').currentTime = seekTo;
document.getElementById('sound4').currentTime = seekTo;
stopLoading();
}).catch(error => {
stopLoading();
alert(error);
});
}
function convertDataURIToBinary(dataURI) {
var BASE64_MARKER = ';base64,';
var base64Index = dataURI.indexOf(BASE64_MARKER) + BASE64_MARKER.length;
var base64 = dataURI.substring(base64Index);
var raw = window.atob(base64);
var rawLength = raw.length;
var array = new Uint8Array(new ArrayBuffer(rawLength));
for(i = 0; i < rawLength; i++) {
array[i] = raw.charCodeAt(i);
}
return array;
}
window.encodingToMidiFile = function(encoding, outlink) {
var midiData = {
header: {
"format": 1,
"numTracks": 10,
"ticksPerBeat": 48
},
tracks: [[{"deltaTime":0,"channel":0,"type":"programChange","programNumber":0}],
[{"deltaTime":0,"channel":1,"type":"programChange","programNumber":40}],
[{"deltaTime":0,"channel":2,"type":"programChange","programNumber":42}],
[{"deltaTime":0,"channel":3,"type":"programChange","programNumber":32}],
[{"deltaTime":0,"channel":4,"type":"programChange","programNumber":24}],
[{"deltaTime":0,"channel":5,"type":"programChange","programNumber":73}],
[{"deltaTime":0,"channel":6,"type":"programChange","programNumber":71}],
[{"deltaTime":0,"channel":7,"type":"programChange","programNumber":56}],
[{"deltaTime":0,"channel":8,"type":"programChange","programNumber":46}],
[{"deltaTime":0,"channel":9,"type":"programChange","programNumber":0}]]
};
var tokens = [];
if (encoding.trim()) {
tokens = encoding.trim().split(" ");
}
var deltaTimes = [0,0,0,0,0,0,0,0,0,0];
var totalDelay = 0;
var usedDrumNotes = new Set();
for (var i=0; i<tokens.length; i++) {
var token = tokens[i];
var parsedToken = parseToken(token);
if (parsedToken.type == "note") {
var trackIndex = inst_to_track[parsedToken.instrument];
midiData.tracks[trackIndex].push({
"deltaTime": deltaTimes[trackIndex],
"channel": trackIndex,
"type": parsedToken.volume > 0 ? "noteOn" : "noteOff",
"noteNumber": parsedToken.pitch,
"velocity": parsedToken.volume
});
if (parsedToken.instrument == "drum") {
usedDrumNotes.add(parsedToken.pitch);
}
deltaTimes[trackIndex] = 0;
} else if (parsedToken.type == "wait") {
for (var j=0; j<10; j++) {
deltaTimes[j] += parsedToken.delay;
}
totalDelay += parsedToken.delay;
for (let pitch of usedDrumNotes) {
midiData.tracks[9].push({
"deltaTime": deltaTimes[9],
"channel": 9,
"type": "noteOff",
"noteNumber": pitch,
"velocity": 0
});
deltaTimes[9] = 0;
}
usedDrumNotes = new Set();
}
}
for (let pitch of usedDrumNotes) {
midiData.tracks[9].push({
"deltaTime": deltaTimes[9],
"channel": 9,
"type": "noteOff",
"noteNumber": pitch,
"velocity": 0
});
deltaTimes[9] = 0;
}
for (var i=0; i<midiData.tracks.length; i++) {
midiData.tracks[i].push({
"deltaTime": deltaTimes[i],
"meta": true,
"type": "endOfTrack"
});
deltaTimes[i] = 0;
}
var filteredMidiTracks = midiData.tracks.filter(track => track.length > 2);
if (filteredMidiTracks.length > 0) {
midiData.tracks = filteredMidiTracks;
}
midiData.tracks[0].unshift({
"deltaTime": 0,
"meta": true,
"type": "setTempo",
"microsecondsPerBeat": 480000
});
console.log(midiData);
var midiBlob = new Blob([new Uint8Array(writeMidi(midiData))], {type: "audio/midi"});
var objectUrl = URL.createObjectURL(midiBlob);
document.getElementById(outlink).href = objectUrl;
document.getElementById(outlink).target = "_blank";
document.getElementById(outlink).download = new Date().toISOString().replace("T","-").replaceAll(":","-").replace(/\..+/,"")+".mid";
if (outlink == "download_inbox") {
window.oldDuration = totalDelay / 100;
console.log(window.oldDuration);
}
}
window.encodingToMidiFile(document.getElementById("inbox").value, "download_inbox");
window.encodingToMidiFile(document.getElementById("outbox1").value, "download_outbox1");
window.encodingToMidiFile(document.getElementById("outbox2").value, "download_outbox2");
window.encodingToMidiFile(document.getElementById("outbox3").value, "download_outbox3");
window.encodingToMidiFile(document.getElementById("outbox4").value, "download_outbox4");
function parseToken(token) {
token = +token;
if (token >= 0 && token < 3840) {
var pitch = token % 128;
var inst_vol_index = token >> 7;
var instrument = ["piano","piano","piano","piano","piano","piano","piano","piano","piano","piano","piano","piano","piano","piano",
"violin","violin","cello","cello","bass","bass","guitar","guitar",
"flute","flute","clarinet","clarinet","trumpet","trumpet","harp","harp"][inst_vol_index];
var volume = [0,24,32,40,48,56,64,72,80,88,96,104,112,120,80,0,80,0,80,0,80,0,80,0,80,0,80,0,80,0][inst_vol_index];
return {"type":"note","pitch":pitch,"instrument":instrument,"volume":volume};
} else if (token >= 3840 && token < 3968) {
var pitch = token % 128;
return {"type":"note","pitch":pitch,"instrument":"drum","volume":80};
} else if (token >= 3968 && token < 4096) {
var delay = (token % 128) + 1;
return {"type":"wait","delay":delay}
} else if (token == 4096) {
return {"type":"start"}
} else {
return {"type":"invalid"}
}
}