update loadPlugin to support multiple instruments in WebAudioAPI and AudioTag

This commit is contained in:
Michael Deal 2012-11-21 01:58:19 -08:00
parent d53fe90663
commit 30c224b067

View file

@ -1,9 +1,14 @@
/* /*
------------------------------------- -----------------------------------------------------------
MIDI.loadPlugin(callback, type); MIDI.loadPlugin : 0.1 : 11/20/2012
------------------------------------- -----------------------------------------------------------
https://github.com/mudx/MIDI.js https://github.com/mudx/MIDI.js
------------------------------------- -----------------------------------------------------------
MIDI.loadPlugin({
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) === "undefined") var MIDI = {};
@ -11,84 +16,138 @@ if (typeof (MIDI.Soundfont) === "undefined") MIDI.Soundfont = {};
(function() { "use strict"; (function() { "use strict";
var plugins = { "#webaudio": true, "#html5": true, "#java": true, "#flash": true }; MIDI.loadPlugin = function(conf) {
if (typeof(conf) === "function") conf = { callback: conf };
MIDI.loadPlugin = function(callback, instrument) { /// Get the instrument name.
var type = ""; var instruments = conf.instruments || conf.instrument || "acoustic_grand_piano";
var instrument = instrument || ""; if (typeof(instruments) !== "object") instruments = [ instruments ];
instruments.map(function(data) {
if (typeof(data) === "number") data = MIDI.GeneralMIDI.byId[data];
MIDI.Soundfont[data] = true;
return data;
});
/// Detect the best type of audio to use.
MIDI.audioDetect(function(types) { MIDI.audioDetect(function(types) {
var type = "";
// use the most appropriate plugin if not specified // use the most appropriate plugin if not specified
if (typeof(type) === 'undefined') { if (typeof(type) === 'undefined') {
if (plugins[window.location.hash]) { if (plugins[window.location.hash]) {
type = window.location.hash; type = window.location.hash.substr(1);
} else { } else { //
type = ""; type = "";
} }
} }
if (type === "") { if (type === "") {
if (window.webkitAudioContext) { // Chrome if (window.webkitAudioContext) { // Chrome
type = "#webaudio"; type = "webaudio";
} else if (window.Audio) { // Firefox } else if (window.Audio) { // Firefox
type = "#html5"; type = "audiotag";
} else { // Internet Explorer } else { // Internet Explorer
type = "#flash"; type = "flash";
} }
} }
if (typeof(loader) === "undefined") var loader; if (!connect[type]) return;
// use audio/ogg when supported // use audio/ogg when supported
var filetype = types["audio/ogg"] ? "ogg" : "mp3"; var filetype = types["audio/ogg"] ? "ogg" : "mp3";
// load the specified plugin // load the specified plugin
switch (type) { connect[type](filetype, instruments, conf.callback);
case "#java": });
// works well cross-browser, and highly featured, but required Java machine to startup };
if (loader) loader.message("Soundfont (500KB)<br>Java Interface...");
MIDI.Java.connect(callback); ///
break;
case "#flash": var connect = {};
// fairly quick, but requires loading of individual MP3s
if (loader) loader.message("Soundfont (2MB)<br>Flash Interface..."); connect.java = function(filetype, instruments, callback) {
DOMLoader.script.add({ // works well cross-browser, and fully featured, but has delay when Java machine starts.
src: "./inc/SoundManager2/script/soundmanager2.js", if (typeof(loader) === "undefined") var loader;
verify: "SoundManager", if (loader) loader.message("Soundfont (500KB)<br>Java Interface...");
callback: function () { MIDI.Java.connect(callback);
MIDI.Flash.connect(callback); };
}
}); connect.flash = function(filetype, instruments, callback) {
break; // fairly quick, but requires loading of individual MP3s (more http requests).
case "#html5": if (typeof(loader) === "undefined") var loader;
// works well in Firefox if (loader) loader.message("Soundfont (2MB)<br>Flash Interface...");
DOMLoader.sendRequest({ DOMLoader.script.add({
url: "./soundfont/soundfont-" + filetype + instrument + ".js", src: "./inc/SoundManager2/script/soundmanager2.js",
onload: function (response) { verify: "SoundManager",
MIDI.Soundfont = JSON.parse(response.responseText); callback: function () {
if (loader) loader.message("Downloading: 100%<br>Processing..."); MIDI.Flash.connect(callback);
MIDI.HTML5.connect(callback);
},
onprogress: function (evt) {
var percent = evt.loaded / 1719931 * 100 >> 0;
if (loader) loader.message("Downloading: " + (percent + "%"));
}
});
break;
case "#webaudio":
// works well in Chrome
DOMLoader.sendRequest({
url: "./soundfont/soundfont-" + filetype + instrument + ".js",
onload: function(response) {
MIDI.Soundfont = JSON.parse(response.responseText);
if (loader) loader.message("Downloading: 100%<br>Processing...");
MIDI.WebAudioAPI.connect(callback);
},
onprogress: function (evt) {
var percent = evt.loaded / 1719931 * 100 >> 0;
if (loader) loader.message("Downloading: " + (percent + "%"));
}
});
break;
default:
break;
} }
}); });
}; };
connect.audiotag = function(filetype, instruments, callback) {
// works ok, kinda like a drunken tuna fish, across the board.
if (typeof(loader) === "undefined") var loader;
var queue = createQueue({
items: instruments,
getNext: function(instrumentId) {
DOMLoader.sendRequest({
url: "./soundfont/" + instrumentId + "-" + filetype + ".js",
onload: function (response) {
MIDI.Soundfont[instrumentId] = JSON.parse(response.responseText);
if (loader) loader.message("Downloading", 100);
queue.getNext();
},
onprogress: function (evt) {
var percent = Math.round(evt.loaded / evt.total * 100);
if (loader) loader.message("Downloading", percent);
}
});
},
onComplete: function() {
MIDI.AudioTag.connect(callback);
}
});
};
connect.webaudio = function(filetype, instruments, callback) {
// works awesome! safari and chrome support
var queue = createQueue({
items: instruments,
getNext: function(instrumentId) {
DOMLoader.sendRequest({
url: "./soundfont/" + instrumentId + "-" + filetype + ".js",
onload: function(response) {
MIDI.Soundfont[instrumentId] = JSON.parse(response.responseText);
if (loader) loader.message("Downloading", 100);
queue.getNext();
},
onprogress: function (evt) {
var percent = Math.round(evt.loaded / evt.total * 100);
if (loader) loader.message("Downloading", percent);
}
});
},
onComplete: function() {
MIDI.WebAudioAPI.connect(callback);
}
});
};
/// Helpers
var plugins = {
"#webaudio": true,
"#audiotag": true,
"#java": true,
"#flash": true
};
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;
};
})(); })();