Tone.js/examples/scripts/Keyboard.js

6 lines
66 KiB
JavaScript
Raw Normal View History

2016-12-22 20:18:33 +00:00
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.Keyboard=t():e.Keyboard=t()}(this,function(){return function(e){function t(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return e[r].call(i.exports,i,i.exports,t),i.loaded=!0,i.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{default:e}}function i(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function s(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}var a=function(){function e(e,t){for(var n=0;n<t.length;n++){var r=t[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(e,r.key,r)}}return function(t,n,r){return n&&e(t.prototype,n),r&&e(t,r),t}}(),u=n(5),c=r(u),h=n(1),l=r(h),p=n(2),f=n(3),d=function(e){function t(e){var n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:36,r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:4;i(this,t);var s=o(this,(t.__proto__||Object.getPrototypeOf(t)).call(this));return s._lowest=n,s._octaves=r,s._audioKeys=new c.default({polyphony:88,rows:1,octaveControls:!1}),s._audioKeys.down(function(e){s.keyDown(e.note),s.emit("keyDown",e.note)}),s._audioKeys.up(function(e){s.keyUp(e.note),s.emit("keyUp",e.note)}),s._keyboardInterface=new p.KeyboardElement(e,48,2),s._keyboardInterface.on("keyDown",function(e){s.keyDown(e),s.emit("keyDown",e)}),s._keyboardInterface.on("keyUp",function(e){s.keyUp(e),s.emit("keyUp",e)}),window.addEventListener("resize",s._resize.bind(s)),s._resize(),s._midi=new f.Midi,s._midi.on("keyDown",function(e){s.keyDown(e),s.emit("keyDown",e)}),s._midi.on("keyUp",function(e){s.keyUp(e),s.emit("keyUp",e)}),s}return s(t,e),a(t,[{key:"keyDown",value:function(e){this._keyboardInterface.keyDown(e)}},{key:"keyUp",value:function(e){this._keyboardInterface.keyUp(e)}},{key:"_resize",value:function(){this._keyboardInterface.resize(this._lowest,this._octaves)}},{key:"octaves",get:function(){return this._octaves},set:function(e){this._octaves=e,this._resize()}},{key:"rootNote",get:function(){return this._rootNote},set:function(e){this._lowest=e,this._resize()}}]),t}(l.default.EventEmitter);e.exports=d},function(e,t){function n(){this._events=this._events||{},this._maxListeners=this._maxListeners||void 0}function r(e){return"function"==typeof e}function i(e){return"number"==typeof e}function o(e){return"object"==typeof e&&null!==e}function s(e){return void 0===e}e.exports=n,n.EventEmitter=n,n.prototype._events=void 0,n.prototype._maxListeners=void 0,n.defaultMaxListeners=10,n.prototype.setMaxListeners=function(e){if(!i(e)||e<0||isNaN(e))throw TypeError("n must be a positive number");return this._maxListeners=e,this},n.prototype.emit=function(e){var t,n,i,a,u,c;if(this._events||(this._events={}),"error"===e&&(!this._events.error||o(this._events.error)&&!this._events.error.length)){if(t=arguments[1],t instanceof Error)throw t;var h=new Error('Uncaught, unspecified "error" event. ('+t+")");throw h.context=t,h}if(n=this._events[e],s(n))return!1;if(r(n))switch(arguments.length){case 1:n.call(this);break;case 2:n.call(this,arguments[1]);break;case 3:n.call(this,arguments[1],arguments[2]);break;default:a=Array.prototype.slice.call(arguments,1),n.apply(this,a)}else if(o(n))for(a=Array.prototype.slice.call(arguments,1),c=n.slice(),i=c.length,u=0;u<i;u++)c[u].apply(this,a);return!0},n.prototype.addListener=function(e,t){var i;if(!r(t))throw TypeError("listener must be a function");return this._events||(this._events={}),this._events.new
* PEP v0.4.2 | https://github.com/jquery/PEP
* Copyright jQuery Foundation and other contributors | http://jquery.org/license
*/
!function(t,n){e.exports=n()}(this,function(){"use strict";function e(e,t){t=t||Object.create(null);var n=document.createEvent("Event");n.initEvent(e,t.bubbles||!1,t.cancelable||!1);for(var r,i=2;i<p.length;i++)r=p[i],n[r]=t[r]||f[i];n.buttons=t.buttons||0;var o=0;return o=t.pressure&&n.buttons?t.pressure:n.buttons?.5:0,n.x=n.clientX,n.y=n.clientY,n.pointerId=t.pointerId||0,n.width=t.width||0,n.height=t.height||0,n.pressure=o,n.tiltX=t.tiltX||0,n.tiltY=t.tiltY||0,n.pointerType=t.pointerType||"",n.hwTimestamp=t.hwTimestamp||0,n.isPrimary=t.isPrimary||!1,n}function t(){this.array=[],this.size=0}function n(e,t,n,r){this.addCallback=e.bind(r),this.removeCallback=t.bind(r),this.changedCallback=n.bind(r),T&&(this.observer=new T(this.mutationWatcher.bind(this)))}function r(e){return"body /shadow-deep/ "+i(e)}function i(e){return'[touch-action="'+e+'"]'}function o(e){return"{ -ms-touch-action: "+e+"; touch-action: "+e+"; }"}function s(){if(O){k.forEach(function(e){String(e)===e?(P+=i(e)+o(e)+"\n",N&&(P+=r(e)+o(e)+"\n")):(P+=e.selectors.map(i)+o(e.rule)+"\n",N&&(P+=e.selectors.map(r)+o(e.rule)+"\n"))});var e=document.createElement("style");e.textContent=P,document.head.appendChild(e)}}function a(){if(!window.PointerEvent){if(window.PointerEvent=e,window.navigator.msPointerEnabled){var t=window.navigator.msMaxTouchPoints;Object.defineProperty(window.navigator,"maxTouchPoints",{value:t,enumerable:!0}),b.registerSource("ms",J)}else b.registerSource("mouse",G),void 0!==window.ontouchstart&&b.registerSource("touch",z);b.register(document)}}function u(e){if(!b.pointermap.has(e)){var t=new Error("InvalidPointerId");throw t.name="InvalidPointerId",t}}function c(e){if(!e.ownerDocument.contains(e)){var t=new Error("InvalidStateError");throw t.name="InvalidStateError",t}}function h(e){var t=b.pointermap.get(e);return 0!==t.buttons}function l(){window.Element&&!Element.prototype.setPointerCapture&&Object.defineProperties(Element.prototype,{setPointerCapture:{value:q},releasePointerCapture:{value:W}})}var p=["bubbles","cancelable","view","detail","screenX","screenY","clientX","clientY","ctrlKey","altKey","shiftKey","metaKey","button","relatedTarget","pageX","pageY"],f=[!1,!1,null,null,0,0,0,0,!1,!1,!1,!1,0,null,0,0],d=window.Map&&window.Map.prototype.forEach,v=d?Map:t;t.prototype={set:function(e,t){return void 0===t?this.delete(e):(this.has(e)||this.size++,void(this.array[e]=t))},has:function(e){return void 0!==this.array[e]},delete:function(e){this.has(e)&&(delete this.array[e],this.size--)},get:function(e){return this.array[e]},clear:function(){this.array.length=0,this.size=0},forEach:function(e,t){return this.array.forEach(function(n,r){e.call(t,n,r,this)},this)}};var y=["bubbles","cancelable","view","detail","screenX","screenY","clientX","clientY","ctrlKey","altKey","shiftKey","metaKey","button","relatedTarget","buttons","pointerId","width","height","pressure","tiltX","tiltY","pointerType","hwTimestamp","isPrimary","type","target","currentTarget","which","pageX","pageY","timeStamp"],m=[!1,!1,null,null,0,0,0,0,!1,!1,!1,!1,0,null,0,0,0,0,0,0,0,"",0,!1,"",null,null,0,0,0,0],_={pointerover:1,pointerout:1,pointerenter:1,pointerleave:1},E="undefined"!=typeof SVGElementInstance,b={pointermap:new v,eventMap:Object.create(null),captureInfo:Object.create(null),eventSources:Object.create(null),eventSourceList:[],registerSource:function(e,t){var n=t,r=n.events;r&&(r.forEach(function(e){n[e]&&(this.eventMap[e]=n[e].bind(n))},this),this.eventSources[e]=n,this.eventSourceList.push(n))},register:function(e){for(var t,n=this.eventSourceList.length,r=0;r<n&&(t=this.eventSourceList[r]);r++)t.register.call(t,e)},unregister:function(e){for(var t,n=this.eventSourceList.length,r=0;r<n&&(t=this.eventSourceList[r]);r++)t.unregister.call(t,e)},contains:function(e,t){try{return e.contains(t)}catch(e){return!1}},down:function(e){e.bubbles=!0,this.fireEvent("pointerdown",e)},move:function(e){e.bubbles=!0,this.fireEvent("pointermove",e)},up:function(e){e.bubbles=!0,this.fireEvent("pointerup",e)},enter:function(e){e.bubbles=!1,this.fireEvent("pointerenter",e)},leave:f
var r=this;if(void 0!==n&&"function"!=typeof n)throw new TypeError("The 'listener' parameter must be a function.");if(void 0===t&&(t="all"),t.constructor!==Array&&(t=[t]),u.MIDI_SYSTEM_MESSAGES[e])if(void 0===n)this._userHandlers.system[e]=[];else for(var i=0;i<this._userHandlers.system[e].length;i++)this._userHandlers.system[e][i]===n&&this._userHandlers.system[e].splice(i,1);else if(u.MIDI_CHANNEL_MESSAGES[e]){if(t.indexOf("all")>-1){t=[];for(var o=1;16>=o;o++)t.push(o)}if(!this._userHandlers.channel[e])return this;t.forEach(function(t){var i=r._userHandlers.channel[e][t];if(i)if(void 0===n)r._userHandlers.channel[e][t]=[];else for(var o=0;o<i.length;o++)i[o]===n&&i.splice(o,1)})}else{if(void 0!==e)throw new TypeError("The specified event type is not supported.");this._initializeUserHandlers()}return this},s.prototype._initializeUserHandlers=function(){for(var e in u.MIDI_CHANNEL_MESSAGES)u.MIDI_CHANNEL_MESSAGES.hasOwnProperty(e)&&(this._userHandlers.channel[e]={});for(var t in u.MIDI_SYSTEM_MESSAGES)u.MIDI_SYSTEM_MESSAGES.hasOwnProperty(t)&&(this._userHandlers.system[t]=[])},s.prototype._onMidiMessage=function(e){e.data[0]<240?this._parseChannelEvent(e):e.data[0]<=255&&this._parseSystemEvent(e)},s.prototype._parseChannelEvent=function(e){var t,n,r=e.data[0]>>4,i=(15&e.data[0])+1;e.data.length>1&&(t=e.data[1],n=e.data.length>2?e.data[2]:void 0);var o={target:this,data:e.data,timestamp:e.timeStamp,channel:i};r===u.MIDI_CHANNEL_MESSAGES.noteoff||r===u.MIDI_CHANNEL_MESSAGES.noteon&&0===n?(o.type="noteoff",o.note={number:t,name:u._notes[t%12],octave:Math.floor(t/12-1)-3},o.velocity=n/127,o.rawVelocity=n):r===u.MIDI_CHANNEL_MESSAGES.noteon?(o.type="noteon",o.note={number:t,name:u._notes[t%12],octave:Math.floor(t/12-1)-3},o.velocity=n/127,o.rawVelocity=n):r===u.MIDI_CHANNEL_MESSAGES.keyaftertouch?(o.type="keyaftertouch",o.note={number:t,name:u._notes[t%12],octave:Math.floor(t/12-1)-3},o.value=n/127):r===u.MIDI_CHANNEL_MESSAGES.controlchange&&t>=0&&119>=t?(o.type="controlchange",o.controller={number:t,name:this.getCcNameByNumber(t)},o.value=n):r===u.MIDI_CHANNEL_MESSAGES.channelmode&&t>=120&&127>=t?(o.type="channelmode",o.controller={number:t,name:this.getChannelModeByNumber(t)},o.value=n):r===u.MIDI_CHANNEL_MESSAGES.programchange?(o.type="programchange",o.value=t):r===u.MIDI_CHANNEL_MESSAGES.channelaftertouch?(o.type="channelaftertouch",o.value=t/127):r===u.MIDI_CHANNEL_MESSAGES.pitchbend?(o.type="pitchbend",o.value=((n<<7)+t-8192)/8192):o.type="unknownchannelmessage",this._userHandlers.channel[o.type]&&this._userHandlers.channel[o.type][i]&&this._userHandlers.channel[o.type][i].forEach(function(e){e(o)})},s.prototype.getCcNameByNumber=function(e){if(e=parseInt(e),!(e>=0&&119>=e))throw new RangeError("The control change number must be between 0 and 119.");for(var t in u.MIDI_CONTROL_CHANGE_MESSAGES)if(e===u.MIDI_CONTROL_CHANGE_MESSAGES[t])return t},s.prototype.getChannelModeByNumber=function(e){if(e=parseInt(e),!(e>=120&&status<=127))throw new RangeError("The control change number must be between 120 and 127.");for(var t in u.MIDI_CHANNEL_MODE_MESSAGES)if(e===u.MIDI_CHANNEL_MODE_MESSAGES[t])return t},s.prototype._parseSystemEvent=function(e){var t=e.data[0],n={target:this,data:e.data,timestamp:e.timeStamp};t===u.MIDI_SYSTEM_MESSAGES.sysex?n.type="sysex":t===u.MIDI_SYSTEM_MESSAGES.timecode?n.type="timecode":t===u.MIDI_SYSTEM_MESSAGES.songposition?n.type="songposition":t===u.MIDI_SYSTEM_MESSAGES.songselect?(n.type="songselect",n.song=e.data[1]):t===u.MIDI_SYSTEM_MESSAGES.tuningrequest?n.type="tuningrequest":t===u.MIDI_SYSTEM_MESSAGES.clock?n.type="clock":t===u.MIDI_SYSTEM_MESSAGES.start?n.type="start":t===u.MIDI_SYSTEM_MESSAGES.continue?n.type="continue":t===u.MIDI_SYSTEM_MESSAGES.stop?n.type="stop":t===u.MIDI_SYSTEM_MESSAGES.activesensing?n.type="activesensing":t===u.MIDI_SYSTEM_MESSAGES.reset?n.type="reset":n.type="unknownsystemmessage",this._userHandlers.system[n.type]&&this._userHandlers.system[n.type].forEach(function(e){e(n)})},a.prototype.send=function(e,t,n){if(!(e>=128&&255>=e))throw new RangeError("The status byte m