2012-02-16 05:46:03 +01:00
|
|
|
/*
|
2012-11-21 10:58:19 +01:00
|
|
|
-----------------------------------------------------------
|
2013-01-19 08:18:41 +01:00
|
|
|
MIDI.loadPlugin : 0.1.2 : 01/18/2012
|
2012-11-21 10:58:19 +01:00
|
|
|
-----------------------------------------------------------
|
2013-01-12 03:05:42 +01:00
|
|
|
https://github.com/mudcube/MIDI.js
|
2012-11-21 10:58:19 +01:00
|
|
|
-----------------------------------------------------------
|
|
|
|
MIDI.loadPlugin({
|
|
|
|
instrument: "acoustic_grand_piano", // or 1 (default)
|
|
|
|
instruments: [ "acoustic_grand_piano", "acoustic_guitar_nylon" ], // or multiple instruments
|
|
|
|
callback: function() { }
|
|
|
|
});
|
2012-02-16 05:46:03 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
if (typeof (MIDI) === "undefined") var MIDI = {};
|
|
|
|
if (typeof (MIDI.Soundfont) === "undefined") MIDI.Soundfont = {};
|
|
|
|
|
|
|
|
(function() { "use strict";
|
|
|
|
|
2013-01-19 08:27:48 +01:00
|
|
|
// Turn on to get "onprogress" event. XHR will not work from file://
|
|
|
|
var USE_XHR = false;
|
2013-01-19 08:18:41 +01:00
|
|
|
|
2012-11-21 10:58:19 +01:00
|
|
|
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 ];
|
|
|
|
instruments.map(function(data) {
|
|
|
|
if (typeof(data) === "number") data = MIDI.GeneralMIDI.byId[data];
|
|
|
|
return data;
|
|
|
|
});
|
2013-01-12 03:05:42 +01:00
|
|
|
///
|
|
|
|
MIDI.soundfontUrl = conf.soundfontUrl || MIDI.soundfontUrl || "./soundfont/";
|
2012-11-21 10:58:19 +01:00
|
|
|
/// Detect the best type of audio to use.
|
2012-02-16 05:46:03 +01:00
|
|
|
MIDI.audioDetect(function(types) {
|
2012-11-21 10:58:19 +01:00
|
|
|
var type = "";
|
2012-02-16 05:46:03 +01:00
|
|
|
// use the most appropriate plugin if not specified
|
|
|
|
if (typeof(type) === 'undefined') {
|
|
|
|
if (plugins[window.location.hash]) {
|
2012-11-21 10:58:19 +01:00
|
|
|
type = window.location.hash.substr(1);
|
|
|
|
} else { //
|
2012-02-16 05:46:03 +01:00
|
|
|
type = "";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (type === "") {
|
|
|
|
if (window.webkitAudioContext) { // Chrome
|
2012-11-21 10:58:19 +01:00
|
|
|
type = "webaudio";
|
2012-04-14 06:59:48 +02:00
|
|
|
} else if (window.Audio) { // Firefox
|
2012-11-21 10:58:19 +01:00
|
|
|
type = "audiotag";
|
2012-04-14 06:59:48 +02:00
|
|
|
} else { // Internet Explorer
|
2012-11-21 10:58:19 +01:00
|
|
|
type = "flash";
|
2012-02-16 05:46:03 +01:00
|
|
|
}
|
|
|
|
}
|
2012-11-21 10:58:19 +01:00
|
|
|
if (!connect[type]) return;
|
2012-02-16 05:46:03 +01:00
|
|
|
// use audio/ogg when supported
|
|
|
|
var filetype = types["audio/ogg"] ? "ogg" : "mp3";
|
|
|
|
// load the specified plugin
|
2012-11-21 10:58:19 +01:00
|
|
|
connect[type](filetype, instruments, conf.callback);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
var connect = {};
|
|
|
|
|
|
|
|
connect.java = function(filetype, instruments, callback) {
|
|
|
|
// works well cross-browser, and fully featured, but has delay when Java machine starts.
|
2013-01-12 03:05:42 +01:00
|
|
|
if (MIDI.loader) MIDI.loader.message("Java API...");
|
2012-11-21 10:58:19 +01:00
|
|
|
MIDI.Java.connect(callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
connect.flash = function(filetype, instruments, callback) {
|
|
|
|
// fairly quick, but requires loading of individual MP3s (more http requests).
|
2013-01-12 03:05:42 +01:00
|
|
|
if (MIDI.loader) MIDI.loader.message("Flash API...");
|
2012-11-21 10:58:19 +01:00
|
|
|
DOMLoader.script.add({
|
|
|
|
src: "./inc/SoundManager2/script/soundmanager2.js",
|
|
|
|
verify: "SoundManager",
|
|
|
|
callback: function () {
|
|
|
|
MIDI.Flash.connect(callback);
|
2012-02-16 05:46:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2012-11-21 10:58:19 +01:00
|
|
|
connect.audiotag = function(filetype, instruments, callback) {
|
2013-01-12 03:05:42 +01:00
|
|
|
if (MIDI.loader) MIDI.loader.message("HTML5 Audio API...");
|
2012-11-21 10:58:19 +01:00
|
|
|
// works ok, kinda like a drunken tuna fish, across the board.
|
|
|
|
var queue = createQueue({
|
|
|
|
items: instruments,
|
|
|
|
getNext: function(instrumentId) {
|
2013-01-19 08:18:41 +01:00
|
|
|
if (USE_XHR) {
|
|
|
|
DOMLoader.sendRequest({
|
|
|
|
url: MIDI.soundfontUrl + instrumentId + "-" + filetype + ".js",
|
|
|
|
onprogress: getPercent,
|
|
|
|
onload: function (response) {
|
|
|
|
MIDI.Soundfont[instrumentId] = JSON.parse(response.responseText);
|
|
|
|
if (MIDI.loader) MIDI.loader.update(null, "Downloading", 100);
|
|
|
|
queue.getNext();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
DOMLoader.script.add({
|
|
|
|
src: MIDI.soundfontUrl + instrumentId + "-" + filetype + ".js",
|
|
|
|
verify: instrumentId,
|
|
|
|
callback: function() {
|
|
|
|
if (MIDI.loader) MIDI.loader.update(null, "Downloading...", 100);
|
|
|
|
queue.getNext();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-11-21 10:58:19 +01:00
|
|
|
},
|
|
|
|
onComplete: function() {
|
|
|
|
MIDI.AudioTag.connect(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
connect.webaudio = function(filetype, instruments, callback) {
|
2013-01-12 03:05:42 +01:00
|
|
|
if (MIDI.loader) MIDI.loader.message("Web Audio API...");
|
2012-11-21 10:58:19 +01:00
|
|
|
// works awesome! safari and chrome support
|
|
|
|
var queue = createQueue({
|
|
|
|
items: instruments,
|
|
|
|
getNext: function(instrumentId) {
|
2013-01-19 08:18:41 +01:00
|
|
|
if (USE_XHR) {
|
|
|
|
DOMLoader.sendRequest({
|
|
|
|
url: MIDI.soundfontUrl + instrumentId + "-" + filetype + ".js",
|
|
|
|
onprogress: getPercent,
|
|
|
|
onload: function(response) {
|
|
|
|
MIDI.Soundfont[instrumentId] = JSON.parse(response.responseText);
|
|
|
|
if (MIDI.loader) MIDI.loader.update(null, "Downloading...", 100);
|
|
|
|
queue.getNext();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
DOMLoader.script.add({
|
|
|
|
src: MIDI.soundfontUrl + instrumentId + "-" + filetype + ".js",
|
|
|
|
verify: "MIDI.Soundfont." + instrumentId,
|
|
|
|
callback: function() {
|
|
|
|
if (MIDI.loader) MIDI.loader.update(null, "Downloading...", 100);
|
|
|
|
queue.getNext();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-11-21 10:58:19 +01:00
|
|
|
},
|
|
|
|
onComplete: function() {
|
|
|
|
MIDI.WebAudioAPI.connect(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Helpers
|
|
|
|
|
|
|
|
var plugins = {
|
|
|
|
"#webaudio": true,
|
|
|
|
"#audiotag": true,
|
|
|
|
"#java": true,
|
|
|
|
"#flash": true
|
|
|
|
};
|
|
|
|
|
2013-01-12 03:05:42 +01:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2012-11-21 10:58:19 +01:00
|
|
|
var createQueue = function(conf) {
|
|
|
|
var self = {};
|
|
|
|
self.queue = [];
|
|
|
|
for (var key in conf.items) {
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2012-02-16 05:46:03 +01:00
|
|
|
})();
|