-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMIDI.Player2.js
140 lines (126 loc) · 4.38 KB
/
MIDI.Player2.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
var CONTINUE=false;
// Constants
var PLAY_BUFFER_DELAY = 300;
var PAGE_HIDDEN_BUFFER_RATIO = 20;
// MIDIPlayer constructor
function MIDIPlayer(options) {
var i;
options = options || {};
this.output = options.output || null; // midi output
this.volume = options.volume || 100; // volume in percents
this.startTime = -1; // ms since page load
this.pauseTime = -1; // ms elapsed before player paused
this.events = [];
this.notesOn = new Array(32); // notesOn[channel][note]
for(i = 31; 0 <= i; i--) {
this.notesOn[i] = [];
}
this.midiFile = null;
window.addEventListener('unload', this.stop.bind(this));
}
// Parsing all tracks and add their events in a single event queue
MIDIPlayer.prototype.load = function(midiFile) {
this.stop();
this.position = 0;
this.midiFile = midiFile;
this.events = this.midiFile.getMidiEvents();
};
MIDIPlayer.prototype.play = function(endCallback) {
this.endCallback = endCallback;
if(0 === this.position) {
this.startTime = performance.now();
this.timeout = setTimeout(this.processPlay.bind(this), 0);
//console.log('lastPlayTime ',this.lastPlayTime);
//console.log('CONTINUE ',CONTINUE);
//console.log('performance.now() ',performance.now());
//console.log('position :',this.position);
//console.log('this.position :',this.events[this.position].playTime;);
return 1;
}
return 0;
};
MIDIPlayer.prototype.processPlay = function() {
var elapsedTime = performance.now() - this.startTime;
var event;
var index;
var param2;
var bufferDelay = PLAY_BUFFER_DELAY * (
document.hidden || document.mozHidden || document.webkitHidden ||
document.msHidden || document.oHidden ? PAGE_HIDDEN_BUFFER_RATIO : 1
);
event = this.events[this.position];
while(this.events[this.position] && event.playTime - elapsedTime < bufferDelay) {
param2 = 0;
if(event.subtype === MIDIEvents.EVENT_MIDI_NOTE_ON) {
param2 = Math.floor(event.param1 * ((this.volume || 1) / 100));
this.notesOn[event.channel].push(event.param1);
}
else if(event.subtype === MIDIEvents.EVENT_MIDI_NOTE_OFF) {
index = this.notesOn[event.channel].indexOf(event.param1);
if(-1 !== index) {
this.notesOn[event.channel].splice(index, 1);
}
}
this.output.send(
-1 !== MIDIEvents.MIDI_1PARAM_EVENTS.indexOf(event.subtype) ?
[(event.subtype << 4) + event.channel, event.param1] :
[(event.subtype << 4) + event.channel, event.param1, (param2 || event.param2 || 0x00)],
Math.floor(event.playTime + this.startTime)
);
this.lastPlayTime = event.playTime + this.startTime;
this.position++;
///////////////
///////////////
document.getElementById("XX").value=parseInt(((this.position)/(this.events.length))*100);
//document.getElementById("YY").value=parseInt(((this.events.length)/(this.events.length))*100);
//console.log('position :',this.position);
//console.log('events.length :',this.events.length);
//////////////
//////////////
event = this.events[this.position];
}
if(this.position < this.events.length - 1) {
this.timeout = setTimeout(this.processPlay.bind(this), PLAY_BUFFER_DELAY - 250);
}
else {
setTimeout(this.endCallback, PLAY_BUFFER_DELAY + 100);
this.position = 0;
}
};
MIDIPlayer.prototype.pause = function() {
var i;
var j;
if(this.timeout) {
clearTimeout(this.timeout);
this.timeout = null;
this.pauseTime = performance.now();
for(i = this.notesOn.length - 1; 0 <= i; i--) {
for(j = this.notesOn[i].length - 1; 0 <= j; j--) {
this.output.send([(MIDIEvents.EVENT_MIDI_NOTE_OFF << 4) + i, this.notesOn[i][j],
0x00], this.lastPlayTime + 100);
}
}
return true;
}
return false;
};
MIDIPlayer.prototype.resume = function(endCallback) {
this.endCallback = endCallback;
if(this.events && this.events[this.position] && !this.timeout) {
this.startTime += performance.now() - this.pauseTime;
this.timeout = setTimeout(this.processPlay.bind(this), 0);
return this.events[this.position].playTime;
}
return 0;
};
MIDIPlayer.prototype.stop = function() {
var i;
if(this.pause()) {
this.position = 0;
for(i = 31; 0 <= i; i--) {
this.notesOn[i] = [];
}
return true;
}
return false;
};