re-enable Web MIDI API with fallback
This commit is contained in:
parent
381f4c7da2
commit
41f90e8bb1
|
@ -20,40 +20,41 @@ if (typeof (MIDI.Soundfont) === "undefined") MIDI.Soundfont = {};
|
|||
var USE_XHR = false;
|
||||
|
||||
MIDI.loadPlugin = function(conf) {
|
||||
if (typeof(conf) === "function") conf = { callback: 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;
|
||||
return data;
|
||||
});
|
||||
///
|
||||
MIDI.soundfontUrl = conf.soundfontUrl || MIDI.soundfontUrl || "./soundfont/";
|
||||
/// Detect the best type of audio to use.
|
||||
MIDI.audioDetect(function(types) {
|
||||
var type = "";
|
||||
var api = "";
|
||||
// use the most appropriate plugin if not specified
|
||||
if (plugins[window.location.hash]) {
|
||||
type = window.location.hash.substr(1);
|
||||
if (apis[conf.api]) {
|
||||
api = conf.api;
|
||||
} else if (apis[window.location.hash]) {
|
||||
api = window.location.hash.substr(1);
|
||||
} else if (navigator.requestMIDIAccess) {
|
||||
api = "webmidi";
|
||||
} else if (window.webkitAudioContext) { // Chrome
|
||||
api = "webaudio";
|
||||
} else if (window.Audio) { // Firefox
|
||||
api = "audiotag";
|
||||
} else { // Internet Explorer
|
||||
api = "flash";
|
||||
}
|
||||
///
|
||||
if (type === "") {
|
||||
if (false && navigator.requestMIDIAccess) {
|
||||
type = "webmidi";
|
||||
} else if (window.webkitAudioContext) { // Chrome
|
||||
type = "webaudio";
|
||||
} else if (window.Audio) { // Firefox
|
||||
type = "audiotag";
|
||||
} else { // Internet Explorer
|
||||
type = "flash";
|
||||
}
|
||||
}
|
||||
if (!connect[type]) return;
|
||||
if (!connect[api]) return;
|
||||
// use audio/ogg when supported
|
||||
var filetype = types["audio/ogg"] ? "ogg" : "mp3";
|
||||
// load the specified plugin
|
||||
connect[type](filetype, instruments, conf.callback);
|
||||
connect[api](filetype, instruments, conf);
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -61,24 +62,24 @@ MIDI.loadPlugin = function(conf) {
|
|||
|
||||
var connect = {};
|
||||
|
||||
connect.webmidi = function(filetype, instruments, callback) {
|
||||
connect.webmidi = function(filetype, instruments, conf) {
|
||||
if (MIDI.loader) MIDI.loader.message("Web MIDI API...");
|
||||
MIDI.WebMIDI.connect(callback);
|
||||
MIDI.WebMIDI.connect(conf);
|
||||
};
|
||||
|
||||
connect.flash = function(filetype, instruments, callback) {
|
||||
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: "./inc/SoundManager2/script/soundmanager2.js",
|
||||
verify: "SoundManager",
|
||||
callback: function () {
|
||||
MIDI.Flash.connect(callback);
|
||||
MIDI.Flash.connect(conf);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
connect.audiotag = function(filetype, instruments, callback) {
|
||||
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({
|
||||
|
@ -106,12 +107,12 @@ connect.audiotag = function(filetype, instruments, callback) {
|
|||
}
|
||||
},
|
||||
onComplete: function() {
|
||||
MIDI.AudioTag.connect(callback);
|
||||
MIDI.AudioTag.connect(conf);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
connect.webaudio = function(filetype, instruments, callback) {
|
||||
connect.webaudio = function(filetype, instruments, conf) {
|
||||
if (MIDI.loader) MIDI.loader.message("Web Audio API...");
|
||||
// works awesome! safari and chrome support
|
||||
var queue = createQueue({
|
||||
|
@ -139,14 +140,14 @@ connect.webaudio = function(filetype, instruments, callback) {
|
|||
}
|
||||
},
|
||||
onComplete: function() {
|
||||
MIDI.WebAudioAPI.connect(callback);
|
||||
MIDI.WebAudioAPI.connect(conf);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/// Helpers
|
||||
|
||||
var plugins = {
|
||||
var apis = {
|
||||
"#webmidi": true,
|
||||
"#webaudio": true,
|
||||
"#audiotag": true,
|
||||
|
|
|
@ -25,7 +25,7 @@ if (typeof (MIDI) === "undefined") var MIDI = {};
|
|||
(function() { "use strict";
|
||||
|
||||
var setPlugin = function(root) {
|
||||
MIDI.technology = root.technology;
|
||||
MIDI.api = root.api;
|
||||
MIDI.setVolume = root.setVolume;
|
||||
MIDI.programChange = root.programChange;
|
||||
MIDI.noteOn = root.noteOn;
|
||||
|
@ -50,7 +50,7 @@ var setPlugin = function(root) {
|
|||
var output = null;
|
||||
var channels = [];
|
||||
var root = MIDI.WebMIDI = {
|
||||
technology: "Web MIDI API"
|
||||
api: "webmidi"
|
||||
};
|
||||
root.setVolume = function (channel, volume) { // set channel volume
|
||||
output.send([0xB0 + channel, 0x07, volume]);
|
||||
|
@ -96,14 +96,21 @@ var setPlugin = function(root) {
|
|||
return plugin.getOutputs();
|
||||
};
|
||||
|
||||
root.connect = function (callback) {
|
||||
root.connect = function (conf) {
|
||||
setPlugin(root);
|
||||
navigator.requestMIDIAccess(function (access) {
|
||||
plugin = access;
|
||||
output = plugin.getOutput(0);
|
||||
if (callback) callback();
|
||||
}, function (err) {
|
||||
console.log("uh-oh! Something went wrong! Error code: " + err.code );
|
||||
if (conf.callback) conf.callback();
|
||||
}, function (err) { // well at least we tried!
|
||||
if (window.webkitAudioContext) { // Chrome
|
||||
conf.api = "webaudio";
|
||||
} else if (window.Audio) { // Firefox
|
||||
conf.api = "audiotag";
|
||||
} else { // Internet Explorer
|
||||
conf.api = "flash";
|
||||
}
|
||||
MIDI.loadPlugin(conf);
|
||||
});
|
||||
};
|
||||
})();
|
||||
|
@ -120,7 +127,7 @@ if (window.AudioContext || window.webkitAudioContext) (function () {
|
|||
|
||||
var AudioContext = window.AudioContext || window.webkitAudioContext;
|
||||
var root = MIDI.WebAudioAPI = {
|
||||
technology: "Web Audio API"
|
||||
api: "webaudio"
|
||||
};
|
||||
var ctx;
|
||||
var sources = {};
|
||||
|
@ -213,7 +220,7 @@ if (window.AudioContext || window.webkitAudioContext) (function () {
|
|||
return ret;
|
||||
};
|
||||
|
||||
root.connect = function (callback) {
|
||||
root.connect = function (conf) {
|
||||
setPlugin(root);
|
||||
//
|
||||
MIDI.Player.ctx = ctx = new AudioContext();
|
||||
|
@ -226,7 +233,7 @@ if (window.AudioContext || window.webkitAudioContext) (function () {
|
|||
var oncomplete = function(instrument) {
|
||||
delete pending[instrument];
|
||||
for (var key in pending) break;
|
||||
if (!key) callback();
|
||||
if (!key) conf.callback();
|
||||
};
|
||||
for (var instrument in MIDI.Soundfont) {
|
||||
pending[instrument] = true;
|
||||
|
@ -248,7 +255,7 @@ if (window.AudioContext || window.webkitAudioContext) (function () {
|
|||
if (window.Audio) (function () {
|
||||
|
||||
var root = MIDI.AudioTag = {
|
||||
technology: "Audio Tag"
|
||||
api: "audiotag"
|
||||
};
|
||||
var note2id = {};
|
||||
var volume = 1; // floating point
|
||||
|
@ -336,7 +343,7 @@ if (window.Audio) (function () {
|
|||
}
|
||||
};
|
||||
|
||||
root.connect = function (callback) {
|
||||
root.connect = function (conf) {
|
||||
var loading = {};
|
||||
for (var key in MIDI.keyToNote) {
|
||||
note2id[MIDI.keyToNote[key]] = key;
|
||||
|
@ -346,7 +353,7 @@ if (window.Audio) (function () {
|
|||
}
|
||||
setPlugin(root);
|
||||
///
|
||||
if (callback) callback();
|
||||
if (conf.callback) conf.callback();
|
||||
};
|
||||
})();
|
||||
|
||||
|
@ -361,7 +368,7 @@ if (window.Audio) (function () {
|
|||
(function () {
|
||||
|
||||
var root = MIDI.Flash = {
|
||||
technology: "Flash"
|
||||
api: "flash"
|
||||
};
|
||||
var noteReverse = {};
|
||||
var notes = {};
|
||||
|
@ -414,7 +421,7 @@ if (window.Audio) (function () {
|
|||
|
||||
};
|
||||
|
||||
root.connect = function (callback) {
|
||||
root.connect = function (conf) {
|
||||
soundManager.flashVersion = 9;
|
||||
soundManager.useHTML5Audio = true;
|
||||
soundManager.url = '../inc/SoundManager2/swf/';
|
||||
|
@ -452,7 +459,7 @@ if (window.Audio) (function () {
|
|||
var interval = window.setInterval(function () {
|
||||
if (loaded.length !== 88) return;
|
||||
window.clearInterval(interval);
|
||||
if (callback) callback();
|
||||
if (conf.callback) conf.callback();
|
||||
}, 25);
|
||||
};
|
||||
soundManager.onerror = function () {
|
||||
|
|
Loading…
Reference in a new issue