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