From 076f233d555e01b5dd757c858b7c40dac7580815 Mon Sep 17 00:00:00 2001 From: Michael Deal Date: Thu, 24 Jan 2013 23:50:08 -0800 Subject: [PATCH] Web MIDI API is now working with Jazz Soft plugin and the Chris Wilson shim --- inc/WebMIDIAPI.js | 1 + js/MIDI/Plugin.js | 145 +++++++++++++++++++++++------------------- js/MIDI/loadPlugin.js | 4 +- 3 files changed, 83 insertions(+), 67 deletions(-) diff --git a/inc/WebMIDIAPI.js b/inc/WebMIDIAPI.js index 25e70a7..a320b0f 100644 --- a/inc/WebMIDIAPI.js +++ b/inc/WebMIDIAPI.js @@ -22,6 +22,7 @@ // load the Jazz plugin var o1 = document.createElement("object"); + o1.style.cssText = "top: -500px; position: absolute;"; o1.id = "_Jazz" + Math.random() + "ie"; o1.classid = "CLSID:1ACE1618-1C7D-4561-AEE1-34842AA85E90"; diff --git a/js/MIDI/Plugin.js b/js/MIDI/Plugin.js index 811b001..8e92059 100644 --- a/js/MIDI/Plugin.js +++ b/js/MIDI/Plugin.js @@ -1,26 +1,94 @@ /* -------------------------------------------- - MIDI.Plugin : 0.3 : 11/20/2012 + MIDI.Plugin : 0.3.2 : 2013/01/24 -------------------------------------------- https://github.com/mudcube/MIDI.js -------------------------------------------- - MIDI.WebAudioAPI - MIDI.Flash - MIDI.HTML5 - MIDI.GeneralMIDI - MIDI.channels - MIDI.keyToNote - MIDI.noteToKey + APIs: + MIDI.WebMIDIAPI + MIDI.WebAudioAPI + MIDI.Flash + MIDI.HTML5 -------------------------------------------- - setMute? - getInstruments? - ------------------------------------- + Helpers: + MIDI.GeneralMIDI + MIDI.channels + MIDI.keyToNote + MIDI.noteToKey */ if (typeof (MIDI) === "undefined") var MIDI = {}; (function() { "use strict"; +/* + -------------------------------------------- + Web MIDI API - Native Soundbank + -------------------------------------------- + https://dvcs.w3.org/hg/audio/raw-file/tip/midi/specification.html +*/ + +(function () { + var plugin = null; + var output = null; + var channels = []; + var root = MIDI.WebMIDI = {}; + root.connect = function (callback) { + MIDI.technology = "Web MIDI API"; + 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 ); + }); + }; + + MIDI.programChange = function (channel, program) { // change channel instrument + output.send([0xC0 + channel, program]); + }; + + MIDI.setVolume = function (channel, volume) { // set channel volume + output.send([0xB0 + channel, 0x07, volume]); + }; + + MIDI.noteOn = function (channel, note, velocity, delay) { + output.send([0x90 + channel, note, velocity], delay * 1000); + }; + + MIDI.noteOff = function (channel, note, delay) { + output.send([0x80 + channel, note], delay * 1000); + }; + + MIDI.chordOn = function (channel, chord, velocity, delay) { + for (var n = 0; n < chord.length; n ++) { + var note = chord[n]; + output.send([0x90 + channel, note, velocity], delay * 1000); + } + }; + + MIDI.chordOff = function (channel, chord, delay) { + for (var n = 0; n < chord.length; n ++) { + var note = chord[n]; + output.send([0x80, channel, note, velocity], delay * 1000); + } + }; + + MIDI.stopAllNotes = function () { + for (var channel = 0; channel < 16; channel ++) { + output.send([0xB0 + channel, 0x7B, 0]); + } + }; + + MIDI.getInput = function () { + return plugin.getInputs(); + }; + + MIDI.getOutputs = function () { + return plugin.getOutputs(); + }; +})(); + /* -------------------------------------------- Web Audio API - OGG or MPEG Soundbank @@ -152,7 +220,6 @@ if (window.AudioContext || window.webkitAudioContext) (function () { } } }; - })(); /* @@ -370,60 +437,6 @@ if (window.Audio) (function () { }; })(); -/* - -------------------------------------------- - WebMIDI - Native Soundbank - -------------------------------------------- -*/ - -(function () { - var root = MIDI.WebMIDI = {}; - root.connect = function (callback) { - // deferred loading of - MIDI.technology = "Web MIDI API"; - if (callback) callback(); - }; - - MIDI.programChange = function (channel, program) { - plugin.send([0xC0 + channel, program]); - }; - - MIDI.setVolume = function (n) { - - }; - - MIDI.noteOn = function (channel, note, velocity, delay) { - plugin.send([0x90 + channel, note, velocity], delay * 1000); - }; - - MIDI.noteOff = function (channel, note, delay) { - plugin.send([0x80 + channel, note], delay * 1000); - }; - - MIDI.chordOn = function (channel, chord, velocity, delay) { - for (var key in chord) { - var note = chord[key]; - plugin.send([0x90 + channel, note, velocity], delay * 1000); - } - }; - - MIDI.chordOff = function (channel, chord, delay) { - for (var key in chord) { - var note = chord[key]; - plugin.send(0x80, channel, note, velocity, delay * 1000); - } - }; - - MIDI.stopAllNotes = function () { - - }; - - MIDI.getInstruments = function() { - return []; - }; - -})(); - /* helper functions */ diff --git a/js/MIDI/loadPlugin.js b/js/MIDI/loadPlugin.js index 93eecec..1a8b12c 100644 --- a/js/MIDI/loadPlugin.js +++ b/js/MIDI/loadPlugin.js @@ -42,7 +42,9 @@ MIDI.loadPlugin = function(conf) { } } if (type === "") { - if (window.webkitAudioContext) { // Chrome + if (navigator.requestMIDIAccess) { + type = "webmidi"; + } else if (window.webkitAudioContext) { // Chrome type = "webaudio"; } else if (window.Audio) { // Firefox type = "audiotag";