-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmusic.js
76 lines (60 loc) · 1.84 KB
/
music.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
notaAtual=0;
notas = [];
function tock(){
var delay = 0; // play one note every quarter second
var note = 50; // the MIDI note
var velocity = 127; // how hard the note hits
// noteOn
// console.log('noteOn', notas[notaAtual])
note = notas[notaAtual].noteNumber;
velocity = notas[notaAtual].velocity;
//
// play the note
MIDI.setVolume(0, 127);
MIDI.noteOn(0, note, velocity, delay);
delay = notas[notaAtual].deltaTime;
MIDI.noteOff(0, note, delay);
notaAtual++;
}
function dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, {type:mime});
}
function playSong(index){
var song = songs[index];
var file = dataURLtoFile(song.base64, 'song'+index + '.midi');
var reader = new FileReader();
reader.onload = function(e){
midiFile = MidiFile(e.target.result);
console.log('midiFile', midiFile);
pegaNotas(song);
}
reader.readAsBinaryString(file);
}
function pegaNotas(song){
var faixa = song.track;
// console.log('pegaNotas', midiFile.tracks[faixa].length)
for(i=0; i < midiFile.tracks[faixa].length; i++){
nota = midiFile.tracks[faixa][i];
// console.log('nota', nota)
if(nota.noteNumber && nota.subtype == 'noteOn'){
notas.push(nota);
}
}
}
window.onload = function () {
MIDI.loadPlugin({
soundfontUrl: "./soundfont/",
instrument: "acoustic_grand_piano",
onprogress: function(state, progress) {
console.log('onprogress', state, progress);
},
onsuccess: function() {
console.log('Midi.js loaded!')
}
});
}