web midi code cleanup, match format of other plugins

master
Michael Deal 2013-01-25 00:11:41 -08:00
parent 7806c980c3
commit 80a033f09b
2 changed files with 64 additions and 53 deletions

View File

@ -84,7 +84,7 @@ MIDI.Player.setAnimation(function(data) {
* <a href="./js/MusicTheory.Synesthesia.js">MusicTheory.Synesthesia.js</a>: Note-to-color mappings (from Isaac Newton onwards).
<h3>Many thanks to the authors of these libraries;</h3>
* <a href="https://dvcs.w3.org/hg/audio/raw-file/tip/midi/specification.html">Web MIDI API</a>: W3C proposal by Jussi Kalliokoski & Chris Wilson
* <a href="https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html">Web Audio API</a>: W3C proposal by Google
* <a href="https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html">Web Audio API</a>: W3C proposal by Chris Rogers
* <a href="http://dev.w3.org/html5/spec/Overview.html">&lt;audio&gt;</a>: HTML5 specs
* Flash package: <a href="http://www.schillmania.com/projects/soundmanager2/">SoundManager2</a> by <a href="http://schillmania.com">Scott Schiller</a>
* <a href="https://github.com/gasman/jasmid">jasmid</a>: Reads MIDI file byte-code, and translats into a Javascript array.

View File

@ -33,8 +33,63 @@ if (typeof (MIDI) === "undefined") var MIDI = {};
var output = null;
var channels = [];
var root = MIDI.WebMIDI = {};
root.setVolume = function (channel, volume) { // set channel volume
output.send([0xB0 + channel, 0x07, volume]);
};
root.programChange = function (channel, program) { // change channel instrument
output.send([0xC0 + channel, program]);
};
root.noteOn = function (channel, note, velocity, delay) {
output.send([0x90 + channel, note, velocity], delay * 1000);
};
root.noteOff = function (channel, note, delay) {
output.send([0x80 + channel, note], delay * 1000);
};
root.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);
}
};
root.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);
}
};
root.stopAllNotes = function () {
for (var channel = 0; channel < 16; channel ++) {
output.send([0xB0 + channel, 0x7B, 0]);
}
};
root.getInput = function () {
return plugin.getInputs();
};
root.getOutputs = function () {
return plugin.getOutputs();
};
root.connect = function (callback) {
MIDI.technology = "Web MIDI API";
MIDI.setVolume = root.setVolume;
MIDI.programChange = root.programChange;
MIDI.noteOn = root.noteOn;
MIDI.noteOff = root.noteOff;
MIDI.chordOn = root.chordOn;
MIDI.chordOff = root.chordOff;
MIDI.stopAllNotes = root.stopAllNotes;
MIDI.getInput = root.getInput;
MIDI.getOutputs = root.getOutputs;
navigator.requestMIDIAccess(function (access) {
plugin = access;
output = plugin.getOutput(0);
@ -43,50 +98,6 @@ if (typeof (MIDI) === "undefined") var MIDI = {};
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();
};
})();
/*
@ -164,14 +175,6 @@ if (window.AudioContext || window.webkitAudioContext) (function () {
return source;
};
root.chordOn = function (channel, chord, velocity, delay) {
var ret = {}, note;
for (var n = 0, length = chord.length; n < length; n++) {
ret[note = chord[n]] = root.noteOn(channel, note, velocity, delay);
}
return ret;
};
// FIX: needs some way to fade out smoothly..
// POSSIBLE FIX: fade out smoothly using gain and ramping to value
root.noteOff = function (channel, note, delay) {
@ -184,6 +187,14 @@ if (window.AudioContext || window.webkitAudioContext) (function () {
return source;
};
root.chordOn = function (channel, chord, velocity, delay) {
var ret = {}, note;
for (var n = 0, length = chord.length; n < length; n++) {
ret[note = chord[n]] = root.noteOn(channel, note, velocity, delay);
}
return ret;
};
root.chordOff = function (channel, chord, delay) {
var ret = {}, note;
for (var n = 0, length = chord.length; n < length; n++) {