forked from mudcube/MIDI.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLoadPlugin.js
181 lines (165 loc) · 5.07 KB
/
LoadPlugin.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
/*
-----------------------------------------------------------
MIDI.loadPlugin : 0.1.2 : 01/22/2014
-----------------------------------------------------------
https://github.com/mudcube/MIDI.js
-----------------------------------------------------------
MIDI.loadPlugin({
targetFormat: "mp3", // optionally can force to use MP3 (for instance on mobile networks)
instrument: "acoustic_grand_piano", // or 1 (default)
instruments: [ "acoustic_grand_piano", "acoustic_guitar_nylon" ], // or multiple instruments
callback: function() { }
});
*/
if (typeof (MIDI) === "undefined") var MIDI = {};
if (typeof (MIDI.Soundfont) === "undefined") MIDI.Soundfont = {};
(function() { "use strict";
var USE_JAZZMIDI = false; // Turn on to support JazzMIDI Plugin
MIDI.loadPlugin = function(conf) {
if (typeof(conf) === "function") conf = {
callback: conf
};
/// Get the instrument name.
var instruments = conf.instruments || conf.instrument || "acoustic_grand_piano";
if (typeof(instruments) !== "object") instruments = [ instruments ];
///
for (var n = 0; n < instruments.length; n ++) {
var instrument = instruments[n];
if (typeof(instrument) === "number") {
instruments[n] = MIDI.GeneralMIDI.byId[instrument];
}
};
///
MIDI.soundfontUrl = conf.soundfontUrl || MIDI.soundfontUrl || "./soundfont/";
/// Detect the best type of audio to use.
MIDI.audioDetect(function(types) {
var api = "";
// use the most appropriate plugin if not specified
if (apis[conf.api]) {
api = conf.api;
} else if (apis[window.location.hash.substr(1)]) {
api = window.location.hash.substr(1);
} else if (USE_JAZZMIDI && navigator.requestMIDIAccess) {
api = "webmidi";
} else if (window.webkitAudioContext || window.AudioContext) { // Chrome
api = "webaudio";
} else if (window.Audio) { // Firefox
api = "audiotag";
} else { // Internet Explorer
api = "flash";
}
///
if (!connect[api]) return;
// use audio/ogg when supported
if (conf.targetFormat) {
var filetype = conf.targetFormat;
} else { // use best quality
var filetype = types["audio/ogg"] ? "ogg" : "mp3";
}
// load the specified plugin
MIDI.lang = api;
MIDI.supports = types;
connect[api](filetype, instruments, conf);
});
};
///
var connect = {};
connect.webmidi = function(filetype, instruments, conf) {
if (MIDI.loader) MIDI.loader.message("Web MIDI API...");
MIDI.WebMIDI.connect(conf);
};
connect.flash = function(filetype, instruments, conf) {
// fairly quick, but requires loading of individual MP3s (more http requests).
if (MIDI.loader) MIDI.loader.message("Flash API...");
DOMLoader.script.add({
src: conf.soundManagerUrl || "./inc/SoundManager2/script/soundmanager2.js",
verify: "SoundManager",
callback: function () {
MIDI.Flash.connect(instruments, conf);
}
});
};
connect.audiotag = function(filetype, instruments, conf) {
if (MIDI.loader) MIDI.loader.message("HTML5 Audio API...");
// works ok, kinda like a drunken tuna fish, across the board.
var queue = createQueue({
items: instruments,
getNext: function(instrumentId) {
DOMLoader.sendRequest({
url: MIDI.soundfontUrl + instrumentId + "-" + filetype + ".js",
onprogress: getPercent,
onload: function (response) {
addSoundfont(response.responseText);
if (MIDI.loader) MIDI.loader.update(null, "Downloading", 100);
queue.getNext();
}
});
},
onComplete: function() {
MIDI.AudioTag.connect(conf);
}
});
};
connect.webaudio = function(filetype, instruments, conf) {
if (MIDI.loader) MIDI.loader.message("Web Audio API...");
// works awesome! safari, chrome and firefox support.
var queue = createQueue({
items: instruments,
getNext: function(instrumentId) {
DOMLoader.sendRequest({
url: MIDI.soundfontUrl + instrumentId + "-" + filetype + ".js",
onprogress: getPercent,
onload: function(response) {
addSoundfont(response.responseText);
if (MIDI.loader) MIDI.loader.update(null, "Downloading...", 100);
queue.getNext();
}
});
},
onComplete: function() {
MIDI.WebAudio.connect(conf);
}
});
};
/// Helpers
var apis = {
"webmidi": true,
"webaudio": true,
"audiotag": true,
"flash": true
};
var addSoundfont = function(text) {
var script = document.createElement("script");
script.language = "javascript";
script.type = "text/javascript";
script.text = text;
document.body.appendChild(script);
};
var getPercent = function(event) {
if (!this.totalSize) {
if (this.getResponseHeader("Content-Length-Raw")) {
this.totalSize = parseInt(this.getResponseHeader("Content-Length-Raw"));
} else {
this.totalSize = event.total;
}
}
///
var percent = this.totalSize ? Math.round(event.loaded / this.totalSize * 100) : "";
if (MIDI.loader) MIDI.loader.update(null, "Downloading...", percent);
};
var createQueue = function(conf) {
var self = {};
self.queue = [];
for (var key in conf.items) {
if (conf.items.hasOwnProperty(key)) {
self.queue.push(conf.items[key]);
}
}
self.getNext = function() {
if (!self.queue.length) return conf.onComplete();
conf.getNext(self.queue.shift());
};
setTimeout(self.getNext, 1);
return self;
};
})();