-
Notifications
You must be signed in to change notification settings - Fork 19
/
mml-emitter.min.js
1 lines (1 loc) · 13.9 KB
/
mml-emitter.min.js
1
!function t(e,n,r){function o(c,u){if(!n[c]){if(!e[c]){var a="function"==typeof require&&require;if(!u&&a)return a(c,!0);if(i)return i(c,!0);var s=new Error("Cannot find module '"+c+"'");throw s.code="MODULE_NOT_FOUND",s}var f=n[c]={exports:{}};e[c][0].call(f.exports,function(t){var n=e[c][1][t];return o(n?n:t)},f,f.exports,t,e,n,r)}return n[c].exports}for(var i="function"==typeof require&&require,c=0;c<r.length;c++)o(r[c]);return o}({1:[function(t,e){e.exports=t("./src/")},{"./src/":7}],2:[function(t,e){"use strict";function n(t){t=Object.create(t||{});var e={defaultTempo:120,minTempo:30,maxTempo:240,defaultOctave:5,minOctave:0,maxOctave:9,defaultLength:4,minLength:1,maxLength:64,defaultQuantize:6,minQuantize:0,maxQuantize:8,defaultVolume:12,minVolume:0,maxVolume:16,octaveShiftDirection:1,A4Frequency:440,A4Index:69};return Object.keys(e).forEach(function(n){"number"!=typeof t[n]&&(t[n]=e[n])}),t}e.exports.build=n},{}],3:[function(t,e){"use strict";function n(){this._callbacks={}}n.prototype.hasListeners=function(t){return this._callbacks.hasOwnProperty(t)},n.prototype.listeners=function(t){return this.hasListeners(t)?this._callbacks[t].slice():[]},n.prototype.on=function(t,e){return this.hasListeners(t)||(this._callbacks[t]=[]),this._callbacks[t].push(e),this},n.prototype.addListener=n.prototype.on,n.prototype.once=function(t,e){function n(r){this.off(t,n),e.call(this,r)}return n.listener=e,this.on(t,n),this},n.prototype.off=function(t,e){return"undefined"==typeof e?"undefined"==typeof t?this._callbacks={}:this.hasListeners(t)&&delete this._callbacks[t]:this.hasListeners(t)&&(this._callbacks[t]=this._callbacks[t].filter(function(t){return!(t===e||t.listener===e)})),this},n.prototype.removeListener=n.prototype.off,n.prototype.removeAllListeners=n.prototype.off,n.prototype.emit=function(t,e){this.listeners(t).forEach(function(t){t.call(this,e)},this)},e.exports=n},{}],4:[function(t,e){"use strict";function n(t){return"$"===t.charAt(0)}function r(t){return!this.hasOwnProperty(t)}function o(t){var e=t.substr(1);Object.defineProperty(this,t,{get:function(){return this._shared&&this._shared[e]||null},set:function(t){this._shared&&(this._shared[e]=t)}})}function i(t,e){var i;try{i=new Function("return "+e.expr+";")}catch(c){throw new Error("Error parsing expression: "+e.expr)}return e.variables.filter(n).filter(r,t).forEach(o,t),{valueOf:function(t){var e=i.call(t);return"number"==typeof e?e:null}}}e.exports.compile=i},{}],5:[function(t,e){"use strict";function n(t){return t[t.length-1]}function r(t){return-1!==i.indexOf(t)}function o(t){function e(){return t.scan(/[_$a-zA-Z][_$\w]*/)}function o(){return t.scan(/('|").*?\1/)}function i(){return t.scan(/\d+\.?\d*(e[-+]?\d+)?|0x[\da-f]+/i)}function u(){return t.next()+e()}function a(){function s(){var o=e();if(r(o))throw new SyntaxError("Statements should not be used in directives: "+o);if("}"===n(l)){t.scan(/\s*/),t.expect(":");var i=a();return[].push.apply(p,i.variables),o+":"+i.expr}return/^([A-Z]\w*|\$|_|true|false|null)$/.test(o)||(p.push(o),o="this."+o),o}function f(){if(!t.hasNext())return!1;var e=t.peek();switch(e){case"{":case"(":case"[":l.push(c[e]);break;case"]":case")":case"}":if(0===l.length)return!1;l.pop()!==e&&t.throwUnexpectedToken()}return!0}for(var p=[],l=[],h="";f();){var d=t.peek();h+="'"===d||'"'===d?o():d>="0"&&"9">=d?i():"."===d?u():/[_$a-zA-Z]/.test(d)?s():t.next()}return{expr:h,variables:p}}return a()}var i=["break","case","catch","continue","debugger","default","delete","do","else","finally","for","function","if","in","instanceof","new","return","switch","this","throw","try","typeof","var","void","while","with","undefined","abstract","boolean","byte","char","class","const","double","enum","export","extends","final","float","goto","implements","import","int","interface","long","native","package","private","protected","public","short","static","super","synchronized","throws","transient","volatile","arguments","let","yield"],c={"{":"}","(":")","[":"]"};e.exports.parse=o},{}],6:[function(t,e){"use strict";e.exports=function(t,e){t.prototype=Object.create(e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}})}},{}],7:[function(t,e){(function(n){"use strict";var r=t("./mml-emitter");r.version="0.2.8","undefined"!=typeof n.window&&(n.window.MMLEmitter=r),e.exports=r}).call(this,"undefined"!=typeof global?global:"undefined"!=typeof self?self:"undefined"!=typeof window?window:{})},{"./mml-emitter":9}],8:[function(t,e){"use strict";function n(t){return t[t.length-1]}function r(t,e,n){return Math.max(e,Math.min(t,n))}function o(t,e){return t+e}function i(t,e,n){return null!==e&&(e=e.valueOf(t)),null===e?n:e}function c(t,e){var n=t._config,c=null,u=0;return null===e[0]&&(e=t._lenList.concat(e.slice(1))),e.map(function(e){null===e?e=c:0===e?e=u=2*u:c=u=e;var o=i(t,e,4);return o=r(o,n.minLength,n.maxLength),60/t._tempo*(4/o)}).reduce(o,0)}function u(t,e){if(e&&"object"==typeof e){if(e.type===f.Expression)return s.compile(t,e);if(Array.isArray(e))return e.map(function(e){return u(t,e)});Object.keys(e).forEach(function(n){e[n]=u(t,e[n])})}return e}function a(t,e){return[].concat({type:f.Begin},e,{type:f.End}).map(function(e,n){return e=u(t,e),a[e.type](e,n)})}var s=t("./expr-compiler"),f=t("./syntax");a[f.Begin]=function(){return function(t,e){return t._tempo=t._config.defaultTempo,t._octave=t._config.defaultOctave,t._quantize=t._config.defaultQuantize,t._volume=t._config.defaultVolume,t._length=t._config.defaultLength,t._lenList=[t._length],t._loopStack=[],t._infLoopPos=null,t._infLoopWhen=e,t._noteIndex=0,e}},a[f.End]=function(){return function(t,e){return null!==t._infLoopPos?t._infLoopWhen!==e&&(t._pos=t._infLoopPos):t._recv({type:"end",playbackTime:e,when:e},{bubble:!0}),e}},a[f.Note]=function(t){return function(e,n){var r=e._config,o=c(e,t.length),i=o*(e._quantize/r.maxQuantize),u=e._noteIndex,a=t.note.length>1;return t.note.length&&(e._noteIndex+=1),t.note.forEach(function(t,c){function s(t,r){e._recv({type:"sched",playbackTime:n+i+(r||0),callback:t},{"private":!0})}var f,p;f=t.noteNum+t.acci,f+=12*e._octave,f+=r.A4Index-57,p=r.A4Frequency,p*=Math.pow(2,1*(f-r.A4Index)/12),e._recv({type:"note",index:u,playbackTime:n,nextPlaybackTime:n+o,when:n,nextWhen:n+o,midi:f,frequency:p,noteNum:t.noteNum,accidental:t.acci,duration:i,isChord:a,chordIndex:c,tempo:e._tempo,volume:e._volume,octave:e._octave,length:e._length,quantize:e._quantize,noteOff:s})}),n+o}},a[f.Octave]=function(t){return function(e,n){var o=e._config,c=i(e,t.value,o.defaultOctave);return e._octave=r(c,o.minOctave,o.maxOctave),n}},a[f.OctaveShift]=function(t){return function(e,n){var o=e._config,c=e._octave;return c+=t.direction*o.octaveShiftDirection*i(e,t.value,1),e._octave=r(c,o.minOctave,o.maxOctave),n}},a[f.Length]=function(t){return function(e,n){var o=e._config;return e._lenList=t.length.map(function(t){var n=i(e,t,o.defaultLength);return r(n,o.minLength,o.maxLength)},this),e._length=e._lenList[0],n}},a[f.Quantize]=function(t){return function(e,n){var o=e._config,c=i(e,t.value,o.defaultQuantize);return e._quantize=r(c,o.minQuantize,o.maxQuantize),n}},a[f.Tempo]=function(t){return function(e,n){var o=e._config,c=i(e,t.value,o.defaultTempo);return e._tempo=r(c,o.minTempo,o.maxTempo),n}},a[f.Volume]=function(t){return function(e,n){var o=e._config,c=i(e,t.value,o.defaultVolume);return e._volume=r(c,o.minVolume,o.maxVolume),n}},a[f.InfLoop]=function(t,e){return function(t,n){return t._infLoopPos=e,t._infLoopWhen=n,n}},a[f.LoopBegin]=function(t,e){return function(n,o){return n._loopStack.push([r(i(n,t.value,2),1,999),e,null]),o}},a[f.LoopExit]=function(){return function(t,e){var r=n(t._loopStack);return r[0]<=1&&null!==r[2]&&(t._pos=r[2]),e}},a[f.LoopEnd]=function(t,e){return function(t,r){var o=n(t._loopStack);return null===o[2]&&(o[2]=e),o[0]-=1,o[0]>0?t._pos=o[1]:t._loopStack.pop(),r}},a[f.Command]=function(t){return function(e,n){return i(e,t.value,0),n}},e.exports.compile=a},{"./expr-compiler":4,"./syntax":13}],9:[function(t,e){"use strict";function n(t,e,n){a.call(this),n=u.build(n),this.audioContext=t,this.tracks=i.parse(e).map(function(t){return new c(this,t,n)},this),this._ended=0,this._node=null,this._currentTime=0,this._currentTimeIncr=0}var r=512,o=t("./extend"),i=t("./mml-parser"),c=t("./mml-track"),u=t("./config"),a=t("./emitter");o(n,a),n.prototype.start=function(){this.stop();var t=this.audioContext.currentTime,e=r/this.audioContext.sampleRate;return this.tracks.forEach(function(n){n._init(t,e)},this),this._node=this.audioContext.createScriptProcessor(r,1,1),this._node.onaudioprocess=this._process.bind(this),this._node.connect(this.audioContext.destination),this},n.prototype.stop=function(){return this._node&&this._node.disconnect(),this._node=null,this},n.prototype._recv=function(t){t&&"end"===t.type&&(this._ended+=1,this.tracks.length<=this._ended&&this.emit("end",t))},n.prototype._process=function(t){var e=t.playbackTime||this.audioContext.currentTime;this.tracks.forEach(function(t){t._process(e)})},e.exports=n},{"./config":2,"./emitter":3,"./extend":6,"./mml-parser":10,"./mml-track":11}],10:[function(t,e){"use strict";function n(t,e){return Array.isArray(e)?Array.prototype.push.apply(t,e):e&&t.push(e),t}function r(t){function e(e,n){for(;;){if(t.forward(),!t.hasNext()||t.match(e))break;n()}}function r(t){return{noteNum:o(t),acci:u()}}function o(e){return{c:0,d:2,e:4,f:5,g:7,a:9,b:11}[t.next()]+e}function u(){return t.match("+")?1*t.scan(/\++/).length:t.match("-")?-1*t.scan(/\-+/).length:0}function a(){for(var e=(t.scan(/\.+/)||"").length,n=new Array(e),r=0;e>r;r++)n[r]=0;return n}function s(){return n([f(/\d+/)].concat(a()),p())}function f(e){if(t.match("("))return E();var n=t.scan(e);return null!==n?+n:null}function p(){return t.forward(),t.match("^")?(t.next(),s(null)):null}function l(){return{type:c.Note,note:[r(0)],length:s()}}function h(){t.expect("[");var n=[],o=0;return e("]",function(){switch(t.peek()){case"c":case"d":case"e":case"f":case"g":case"a":case"b":n.push(r(o));break;case"<":t.next(),o+=12;break;case">":t.next(),o-=12;break;default:t.throwUnexpectedToken()}}),t.expect("]"),{type:c.Note,note:n,length:s()}}function d(){return t.expect("r"),{type:c.Note,note:[],length:s()}}function m(){return t.expect("o"),{type:c.Octave,value:f(/\d+/)}}function v(e){return t.expect(/<|>/),{type:c.OctaveShift,direction:0|e,value:f(/\d+/)}}function _(){return t.expect("l"),{type:c.Length,length:s()}}function x(){return t.expect("q"),{type:c.Quantize,value:f(/\d+/)}}function y(){return t.expect("t"),{type:c.Tempo,value:f(/\d+(\.\d+)?/)}}function g(){return t.expect("v"),{type:c.Volume,value:f(/\d+/)}}function b(){return t.expect("$"),{type:c.InfLoop}}function k(){t.expect("/"),t.expect(":");var r=[{type:c.LoopBegin}];return e(/[|:]/,function(){n(r,O())}),n(r,L()),t.expect(":"),t.expect("/"),r.push({type:c.LoopEnd}),r[0].value=f(/\d+/)||2,r}function L(){var r=[];return t.match("|")&&(t.next(),r.push({type:c.LoopExit}),e(":",function(){n(r,O())})),r}function w(){return t.expect("@"),{type:c.Command,value:f(/\d+/)}}function E(){var e;return t.expect("("),e=i.parse(t),t.expect(")"),e.variables.forEach(function(t){if("_"===t.charAt(0))throw new SyntaxError("A variable in directives should not be started with '_': "+t)}),{type:c.Expression,expr:e.expr,variables:e.variables}}function O(){switch(t.peek()){case"c":case"d":case"e":case"f":case"g":case"a":case"b":return l();case"[":return h();case"r":return d();case"o":return m();case"<":return v(1);case">":return v(-1);case"l":return _();case"q":return x();case"t":return y();case"v":return g();case"$":return b();case"/":return k();case"@":return w()}t.throwUnexpectedToken()}function T(){var r=[];return e("",function(){var o=[];e(";",function(){n(o,O())}),r.push(o),t.match(";")&&t.next()}),r}return T()}var o=t("./scanner"),i=t("./expr-parser"),c=t("./syntax");e.exports.parse=function(t){return r(new o(t))}},{"./expr-parser":5,"./scanner":12,"./syntax":13}],11:[function(t,e){"use strict";function n(t,e){return t[o]-e[o]}function r(t,e,n){u.call(this),this._pos=0,this._parent=t,this._shared=t,this._config=n,this._nodes=a.compile(this,e),this._sched=[],this._currentTimeIncr=0}var o=0,i=1,c=t("./extend"),u=t("./emitter"),a=t("./mml-compiler");c(r,u),r.prototype._init=function(t,e){this._currentTimeIncr=e;var n=function(t){for(var e=t+this._currentTimeIncr+.015,r=this._nodes;this._pos<r.length&&e>t;)t=r[this._pos](this,t),this._pos+=1;this._pos<r.length&&this.sched(t,n)}.bind(this);n(t)},r.prototype._process=function(t){for(var e=t+this._currentTimeIncr+.015,n=this._sched;n.length&&n[0][o]<e;){var r=n.shift();r[i](r[o])}},r.prototype._recv=function(t,e){e=e||{},"sched"===t.type&&this.sched(t.playbackTime,t.callback),e.private||this.emit(t.type,t),e.bubble&&this._parent&&this._parent._recv(t)},r.prototype.sched=function(t,e){return this._sched.push([t,e]),this._sched.sort(n),this},e.exports=r},{"./emitter":3,"./extend":6,"./mml-compiler":8}],12:[function(t,e){"use strict";function n(t){function e(){return p>l}function n(){return t.charAt(l)}function r(){return t.charAt(l++)}function o(e){return e.test?e.test(t.charAt(l)):t.charAt(l)===e}function i(t){o(t)||f(),l+=1}function c(e){var n=e.exec(t.substr(l));return n&&0===n.index?(n=n[0],l+=n.length):n=null,n}function u(){for(;e();){var n=t.charCodeAt(l),r=t.charCodeAt(l+1);if(32===n||9===n)l+=1;else if(10===n)l+=1,h+=1,d=l;else if(47===n&&47===r)a();else{if(47!==n||42!==r)break;s()}}}function a(){for(l+=2;e();)if(10===t.charCodeAt(l++)){h+=1,d=l;break}}function s(){var n=1;for(l+=2;e();){var r=t.charCodeAt(l++),o=t.charCodeAt(l);if(10===r)h+=1,d=l;else if(47===r&&42===o)l+=1,++n;else if(42===r&&47===o&&(l+=1,0===--n))return void(l+=1)}f()}function f(){var t=n(),e="Unexpected token"+(t?": '"+t+"'":" ILLEGAL"),r=new SyntaxError(e);throw r.index=l,r.lineNumber=h,r.column=l-d+(t?1:0),r}t=String(t);var p=t.length,l=0,h=p?1:0,d=0;return{hasNext:e,peek:n,next:r,match:o,expect:i,scan:c,forward:u,throwUnexpectedToken:f}}e.exports=n},{}],13:[function(t,e){"use strict";e.exports={Expression:-1,Begin:0,Note:1,Octave:2,OctaveShift:3,Length:4,Quantize:5,Tempo:6,Volume:7,InfLoop:8,LoopBegin:9,LoopExit:10,LoopEnd:11,Command:12,End:99}},{}]},{},[1]);