/* Copyright 2013 Chris Wilson Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ // Initialize the MIDI library. (function (global) { 'use strict'; var midiIO, _requestMIDIAccess, MIDIAccess, _onReady, MIDIPort, MIDIInput, MIDIOutput, _midiProc; function Promise() { } Promise.prototype.then = function(accept, reject) { this.accept = accept; this.reject = reject; } Promise.prototype.succeed = function(access) { if (this.accept) this.accept(access); } Promise.prototype.fail = function(error) { if (this.reject) this.reject(error); } function _JazzInstance() { this.inputInUse = false; this.outputInUse = false; // load the Jazz plugin var o1 = document.createElement("object"); o1.id = "_Jazz" + Math.random() + "ie"; o1.classid = "CLSID:1ACE1618-1C7D-4561-AEE1-34842AA85E90"; this.activeX = o1; var o2 = document.createElement("object"); o2.id = "_Jazz" + Math.random(); o2.type="audio/x-jazz"; o1.appendChild(o2); this.objRef = o2; var e = document.createElement("p"); e.appendChild(document.createTextNode("This page requires the ")); var a = document.createElement("a"); a.appendChild(document.createTextNode("Jazz plugin")); a.href = "http://jazz-soft.net/"; e.appendChild(a); e.appendChild(document.createTextNode(".")); o2.appendChild(e); var insertionPoint = document.getElementById("MIDIPlugin"); if (!insertionPoint) { // Create hidden element var insertionPoint = document.createElement("div"); insertionPoint.id = "MIDIPlugin"; insertionPoint.style.position = "absolute"; insertionPoint.style.visibility = "hidden"; insertionPoint.style.left = "-9999px"; insertionPoint.style.top = "-9999px"; document.body.appendChild(insertionPoint); } insertionPoint.appendChild(o1); if (this.objRef.isJazz) this._Jazz = this.objRef; else if (this.activeX.isJazz) this._Jazz = this.activeX; else this._Jazz = null; if (this._Jazz) { this._Jazz._jazzTimeZero = this._Jazz.Time(); this._Jazz._perfTimeZero = window.performance.now(); } } _requestMIDIAccess = function _requestMIDIAccess() { var access = new MIDIAccess(); return access._promise; }; // API Methods MIDIAccess = function() { this._jazzInstances = new Array(); this._jazzInstances.push( new _JazzInstance() ); this._promise = new Promise; if (this._jazzInstances[0]._Jazz) { this._Jazz = this._jazzInstances[0]._Jazz; window.setTimeout( _onReady.bind(this), 3 ); } else { window.setTimeout( _onNotReady.bind(this), 3 ); } }; _onReady = function _onReady() { if (this._promise) this._promise.succeed(this); }; function _onNotReady() { if (this._promise) this._promise.fail( { code: 1 } ); }; MIDIAccess.prototype.inputs = function( ) { if (!this._Jazz) return null; var list=this._Jazz.MidiInList(); var inputs = new Array( list.length ); for ( var i=0; i1)) { var sendObj = new Object(); sendObj.jazz = this._jazzInstance; sendObj.data = data; window.setTimeout( _sendLater.bind(sendObj), delayBeforeSend ); } else { this._jazzInstance.MidiOutLong( data ); } return true; }; //init: create plugin if (!window.navigator.requestMIDIAccess) window.navigator.requestMIDIAccess = _requestMIDIAccess; }(window)); // Polyfill window.performance.now() if necessary. (function (exports) { var perf = {}, props; function findAlt() { var prefix = ['moz', 'webkit', 'o', 'ms'], i = prefix.length, //worst case, we use Date.now() props = { value: (function (start) { return function () { return Date.now() - start; }; }(Date.now())) }; //seach for vendor prefixed version for (; i >= 0; i--) { if ((prefix[i] + "Now") in exports.performance) { props.value = function (method) { return function () { exports.performance[method](); } }(prefix[i] + "Now"); return props; } } //otherwise, try to use connectionStart if ("timing" in exports.performance && "connectStart" in exports.performance.timing) { //this pretty much approximates performance.now() to the millisecond props.value = (function (start) { return function() { Date.now() - start; }; }(exports.performance.timing.connectStart)); } return props; } //if already defined, bail if (("performance" in exports) && ("now" in exports.performance)) return; if (!("performance" in exports)) Object.defineProperty(exports, "performance", { get: function () { return perf; }}); //otherwise, performance is there, but not "now()" props = findAlt(); Object.defineProperty(exports.performance, "now", props); }(window));