-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmusic.js
176 lines (127 loc) · 4.31 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
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
var startDelay = 2000;
var notes = []; //store all of the notes
var start; //全局开始播放时间
var musicPlaying = false; //flag标志全局是否正在播放音乐
var player; //全局midi播放器
var clock = new THREE.Clock();
// window.onload = function() {
// //load the music_box soundfont and change the instrument to music_box.
// MIDI.loadPlugin({
// soundfontUrl: "/soundfont/",
// instruments: "music_box",
// callback: function() {
// MIDI.programChange(0, MIDI.GeneralMIDI.byName["music_box"].number);
// }
// });
// }
// window.onload = function() {
// MIDI.loadPlugin(function() {
// console.log("Sound being generated with " + MIDI.lang + ".");
// if (window.location.hash === '#' || window.location.hash === '') {
// //switchTo('tracks/157-Rachmaninov - Flight of the Bumblebee');
// }
// }, "soundfont/acoustic_grand_piano-mp3.js");
// }
window.onload = function() {
MIDI.loadPlugin(function() {
console.log("Sound being generated with " + MIDI.lang + ".");
switchTo('tracks/001-A Chinese Song - Molihua');
if (window.location.hash === '#' || window.location.hash === '') {
}
}, "soundfont/music_box-mp3.js");
}
function switchTo(file) {
var songName = file.substring(11);
$('#current-song').text('Currently Playing: ' + songName);
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", file, false);
xmlhttp.send(null);
var fileContent = xmlhttp.responseText;
player = MIDI.Player;
player.timeWarp = 1;
player.stop();
musicPlaying = false;
notes = []; //音符数组
timeInSong = -startDelay;
lastUpdatedTime = null;
player.loadFile(fileContent, function() {
midiData = player.data;
console.log('testingPlayerData');
currentTime = 0;
//扫描所有midi文件音符
//MIDI文件每个因为分为interval持续时间以及 event。event包含其类型subtype,以及参数,noteNumber猜想应该是其音高
//最终生成的notes数组 包含,第x秒,为第几个音符或为何种音频。
for (var i = 0; i < midiData.length; i++) {
midiDatum = midiData[i];
midiEvent = midiDatum[0].event;
interval = midiDatum[1];
currentTime += interval;
if (midiEvent.subtype === 'noteOn') {
notes.push({ note: midiEvent.noteNumber, time: currentTime })
}
}
addNotePins();
player.addListener(function(data) {
updateComb();
});
start = new Date();
//在startDelay之后开始播放midi
setTimeout(function() {
player.start();
}, startDelay);
musicPlaying = true;
//$("#exportlink").show();
$("#pause").show();
$("#play").hide();
});
}
function startPlay(midiFile) {
var fileContent = midiFile;
player = MIDI.Player;
player.timeWarp = 1;
player.stop();
musicPlaying = false;
notes = []; //音符数组
timeInSong = -startDelay;
lastUpdatedTime = null;
player.loadFile(fileContent, function() {
midiData = player.data;
console.log('testingPlayerData');
currentTime = 0;
//扫描所有midi文件音符
//MIDI文件每个因为分为interval持续时间以及 event。event包含其类型subtype,以及参数,noteNumber猜想应该是其音高
//最终生成的notes数组 包含,第x秒,为第几个音符或为何种音频。
for (var i = 0; i < midiData.length; i++) {
midiDatum = midiData[i];
midiEvent = midiDatum[0].event;
interval = midiDatum[1];
currentTime += interval;
if (midiEvent.subtype === 'noteOn') {
notes.push({ note: midiEvent.noteNumber, time: currentTime })
}
}
addNotePins();
player.addListener(function(data) {
updateComb();
});
start = new Date();
//在startDelay之后开始播放midi
setTimeout(function() {
player.start();
}, startDelay);
musicPlaying = true;
$("#exportlink").show();
$("#pause").show();
});
}
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// files is a FileList of File objects. List some properties.
var reader = new FileReader();
reader.onload = function(){
var newtrack = reader.result;
startPlay(newtrack);
};
reader.readAsDataURL(files[0]);
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);