-
Notifications
You must be signed in to change notification settings - Fork 21
/
audiokeys.min.js
1 lines (1 loc) · 7.14 KB
/
audiokeys.min.js
1
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,t.AudioKeys=e()}}(function(){return function e(t,o,i){function s(n,a){if(!o[n]){if(!t[n]){var p="function"==typeof require&&require;if(!a&&p)return p(n,!0);if(r)return r(n,!0);var y=new Error("Cannot find module '"+n+"'");throw y.code="MODULE_NOT_FOUND",y}var _=o[n]={exports:{}};t[n][0].call(_.exports,function(e){var o=t[n][1][e];return s(o||e)},_,_.exports,e,t,o,i)}return o[n].exports}for(var r="function"==typeof require&&require,n=0;n<i.length;n++)s(i[n]);return s}({1:[function(e,t,o){t.exports={_addKey:function(e){var t=this;if(t._isNote(e.keyCode)&&!t._isPressed(e.keyCode)){var o=t._makeNote(e.keyCode);t._state.keys=(t._state.keys||[]).concat(o),t._update()}else t._isSpecialKey(e.keyCode)&&t._specialKey(e.keyCode)},_removeKey:function(e){var t=this;if(t._isPressed(e.keyCode)){for(var o,i=0;i<t._state.keys.length;i++)if(t._state.keys[i].keyCode===e.keyCode){o=t._state.keys[i];break}t._state.keys.splice(t._state.keys.indexOf(o),1),t._update()}},_isPressed:function(e){var t=this;if(!t._state.keys||!t._state.keys.length)return!1;for(var o=0;o<t._state.keys.length;o++)if(t._state.keys[o].keyCode===e)return!0;return!1},_makeNote:function(e){var t=this;return{keyCode:e,note:t._map(e),frequency:t._toFrequency(t._map(e)),velocity:t._state.velocity}},clear:function(){var e=this;e._state.buffer.forEach(function(t){t.velocity=0,e._trigger("up",t)}),e._state.keys=[],e._state.buffer=[]},_update:function(){var e=this,t=e._state.buffer;e._prioritize(),e._diff(t)},_diff:function(e){var t=this,o=e.map(function(e){return e.keyCode}),i=t._state.buffer.map(function(e){return e.keyCode}),s=[];o.forEach(function(e){-1===i.indexOf(e)&&s.push(e)});var r=[];i.forEach(function(e){-1===o.indexOf(e)&&r.push(e)}),r.forEach(function(e){for(var o=0;o<t._state.buffer.length;o++)if(t._state.buffer[o].keyCode===e){t._trigger("down",t._state.buffer[o]);break}}),s.forEach(function(o){for(var i=0;i<e.length;i++)if(e[i].keyCode===o){e[i].velocity=0,t._trigger("up",e[i]);break}})}}},{}],2:[function(e,t,o){t.exports={down:function(e){var t=this;t._listeners.down=(t._listeners.down||[]).concat(e)},up:function(e){var t=this;t._listeners.up=(t._listeners.up||[]).concat(e)},_trigger:function(e){var t=this;if(t._listeners[e]&&t._listeners[e].length){var o=Array.prototype.slice.call(arguments);o.splice(0,1),t._listeners[e].forEach(function(e){e.apply(t,o)})}},_bind:function(){var e=this;if("undefined"!=typeof window&&window.document){window.document.addEventListener("keydown",function(t){e._addKey(t)}),window.document.addEventListener("keyup",function(t){e._removeKey(t)});var t=!0;setInterval(function(){window.document.hasFocus()!==t&&((t=!t)||e.clear())},100)}}}},{}],3:[function(e,t,o){function i(e){var t=this;t._setState(e),t._listeners={},t._bind()}var s=e("./AudioKeys.state"),r=e("./AudioKeys.events"),n=e("./AudioKeys.mapping"),a=e("./AudioKeys.buffer"),p=e("./AudioKeys.priority"),y=e("./AudioKeys.special");i.prototype._setState=s._setState,i.prototype._extendState=s._extendState,i.prototype.set=s.set,i.prototype.get=s.get,i.prototype.down=r.down,i.prototype.up=r.up,i.prototype._trigger=r._trigger,i.prototype._bind=r._bind,i.prototype._map=n._map,i.prototype._offset=n._offset,i.prototype._isNote=n._isNote,i.prototype._toFrequency=n._toFrequency,i.prototype._keyMap=n._keyMap,i.prototype._addKey=a._addKey,i.prototype._removeKey=a._removeKey,i.prototype._isPressed=a._isPressed,i.prototype._makeNote=a._makeNote,i.prototype.clear=a.clear,i.prototype._update=a._update,i.prototype._diff=a._diff,i.prototype._prioritize=p._prioritize,i.prototype._last=p._last,i.prototype._first=p._first,i.prototype._highest=p._highest,i.prototype._lowest=p._lowest,i.prototype._isSpecialKey=y._isSpecialKey,i.prototype._specialKey=y._specialKey,i.prototype._specialKeyMap=y._specialKeyMap,t.exports=i},{"./AudioKeys.buffer":1,"./AudioKeys.events":2,"./AudioKeys.mapping":4,"./AudioKeys.priority":5,"./AudioKeys.special":6,"./AudioKeys.state":7}],4:[function(e,t,o){t.exports={_map:function(e){return this._keyMap[this._state.rows][e]+this._offset()},_offset:function(){return this._state.rootNote-this._keyMap[this._state.rows].root+12*this._state.octave},_isNote:function(e){return!!this._keyMap[this._state.rows][e]},_toFrequency:function(e){return 440*Math.pow(2,(e-69)/12)},_keyMap:{1:{root:60,65:60,87:61,83:62,69:63,68:64,70:65,84:66,71:67,89:68,72:69,85:70,74:71,75:72,79:73,76:74,80:75,186:76,222:77},2:{root:60,90:60,83:61,88:62,68:63,67:64,86:65,71:66,66:67,72:68,78:69,74:70,77:71,188:72,76:73,190:74,186:75,191:76,81:72,50:73,87:74,51:75,69:76,82:77,53:78,84:79,54:80,89:81,55:82,85:83,73:84,57:85,79:86,48:87,80:88,219:89,187:90,221:91}}}},{}],5:[function(e,t,o){t.exports={_prioritize:function(){var e=this;if(!e._state.keys.length)return void(e._state.buffer=[]);e._state.polyphony>=e._state.keys.length?e._state.keys=e._state.keys.map(function(e){return e.isActive=!0,e}):(e._state.keys=e._state.keys.map(function(e){return e.isActive=!1,e}),e["_"+e._state.priority]()),e._state.buffer=[],e._state.keys.forEach(function(t){t.isActive&&e._state.buffer.push(t)})},_last:function(){for(var e=this,t=e._state.keys.length-e._state.polyphony;t<e._state.keys.length;t++)e._state.keys[t].isActive=!0},_first:function(){for(var e=this,t=0;t<e._state.polyphony;t++)e._state.keys[t].isActive=!0},_highest:function(){var e=this,t=e._state.keys.map(function(e){return e.note});t.sort(function(e,t){return t===e?0:t<e?-1:1}),t.splice(e._state.polyphony,Number.MAX_VALUE),e._state.keys.forEach(function(e){-1!==t.indexOf(e.note)&&(e.isActive=!0)})},_lowest:function(){var e=this,t=e._state.keys.map(function(e){return e.note});t.sort(function(e,t){return e===t?0:e<t?-1:1}),t.splice(e._state.polyphony,Number.MAX_VALUE),e._state.keys.forEach(function(e){-1!==t.indexOf(e.note)&&(e.isActive=!0)})}}},{}],6:[function(e,t,o){t.exports={_isSpecialKey:function(e){return 1===this._state.rows&&this._specialKeyMap[e]},_specialKey:function(e){var t=this;"octave"===t._specialKeyMap[e].type&&t._state.octaveControls?t._state.octave+=t._specialKeyMap[e].value:"velocity"===t._specialKeyMap[e].type&&t._state.velocityControls&&(t._state.velocity=t._specialKeyMap[e].value)},_specialKeyMap:{90:{type:"octave",value:-1},88:{type:"octave",value:1},49:{type:"velocity",value:1},50:{type:"velocity",value:14},51:{type:"velocity",value:28},52:{type:"velocity",value:42},53:{type:"velocity",value:56},54:{type:"velocity",value:70},55:{type:"velocity",value:84},56:{type:"velocity",value:98},57:{type:"velocity",value:112},48:{type:"velocity",value:127}}}},{}],7:[function(e,t,o){t.exports={_setState:function(e){var t=this;e||(e={}),t._state={},t._extendState({polyphony:4,rows:1,priority:"last",rootNote:60,octaveControls:!0,octave:0,velocityControls:!0,velocity:127,keys:[],buffer:[]}),t._extendState(e)},_extendState:function(e){var t=this;for(var o in e)t._state[o]=e[o]},set:function(){var e=this;return 1===arguments.length?e._extendState(arguments[0]):e._state[arguments[0]]=arguments[1],this},get:function(e){return this._state[e]}}},{}]},{},[3])(3)});