UNPKG

@logue/smfplayer

Version:

smfplayer.js is JavaScript based Standard Midi Player for WebMidiLink based synthesizer.

37 lines (36 loc) 15.9 kB
/** @license smfplayer.js 2013 - imaya / GREE Inc. [ https://github.com/gree/smfplayer.js ] The MIT License */(function() {var t,u=this;function v(b,d){b=b.split(".");var f=u;b[0]in f||!f.execScript||f.execScript("var "+b[0]);for(var c;b.length&&(c=b.shift());)b.length||void 0===d?f[c]?f=f[c]:f=f[c]={}:f[c]=d}function w(b,d){function f(){}f.prototype=d.prototype;b.qa=d.prototype;b.prototype=new f;b.prototype.constructor=b;b.la=function(b,a,f){for(var c=Array(arguments.length-2),g=2;g<arguments.length;g++)c[g-2]=arguments[g];return d.prototype[a].apply(b,c)}};function y(b,d){d=d||{};this.f=b;this.a=d.index||0;this.length=d.length||b.length-this.a;this.offset=this.a;this.padding=void 0!==d.padding?d.padding:!0;this.m=void 0!==d.m?d.m:!1}function z(b,d){b=b.b[d];return void 0===b?null:b};function A(b,d,f){this.h=b;this.T=d;this.time=f}function B(b,d,f,c,a,h){A.call(this,b,d,f);this.b=c;this.I=a;this.a=h}w(B,A);function C(b,d,f,c){A.call(this,b,d,f);this.data=c}w(C,A);function D(b,d,f,c){A.call(this,b,d,f);this.data=c}w(D,A);function F(b){var d=d||{};d.padding=!1;d.m=!0;this.b=b;this.f=0;this.a=new y(b,d);this.l=[];this.u=[]} function G(b){function d(){var b=0;do{var d=c[a++];b=b<<7|d&127}while(0!==(d&128));return b}var f=z(b.a,b.f++),c=b.b,a=f.offset,h=-1,n=-1,g=0;if(!f||"MTrk"!==f.type)throw Error("invalid header signature");f=f.offset+f.size;for(var x=[],p=[];a<f;){var e=d();g+=e;var l=a;var q=c[a++];var k=q>>4&15;var r=q&15;8>k?(k=h,r=n,q=h<<4|n,a--,l--):(h=k,n=r);var E=[,,,,,,,,"NoteOff","NoteOn","NoteAftertouch","ControlChange","ProgramChange","ChannelAftertouch","PitchBend"];switch(k){case 8:case 9:case 10:case 11:case 13:case 14:var m= new B(E[k],e,g,r,c[a++],c[a++]);break;case 12:m=new B(E[k],e,g,r,c[a++]);break;case 15:switch(r){case 0:m=d();if(247!==c[a+m-1])throw Error("invalid SysEx event");m=new C("SystemExclusive",e,g,c.subarray(a,(a+=m)-1));break;case 7:m=d();m=new C("SystemExclusive(F7)",e,g,c.subarray(a,a+=m));break;case 15:k=c[a++];m=d();switch(k){case 0:m=new D("SequenceNumber",e,g,[c[a++],c[a++]]);break;case 1:m=new D("TextEvent",e,g,[String.fromCharCode.apply(null,c.subarray(a,a+=m))]);break;case 2:m=new D("CopyrightNotice", e,g,[String.fromCharCode.apply(null,c.subarray(a,a+=m))]);break;case 3:m=new D("SequenceTrackName",e,g,[String.fromCharCode.apply(null,c.subarray(a,a+=m))]);break;case 4:m=new D("InstrumentName",e,g,[String.fromCharCode.apply(null,c.subarray(a,a+=m))]);break;case 5:m=new D("Lyrics",e,g,[String.fromCharCode.apply(null,c.subarray(a,a+=m))]);break;case 6:m=new D("Marker",e,g,[String.fromCharCode.apply(null,c.subarray(a,a+=m))]);break;case 7:m=new D("CuePoint",e,g,[String.fromCharCode.apply(null,c.subarray(a, a+=m))]);break;case 32:m=new D("MidiChannelPrefix",e,g,[c[a++]]);break;case 47:m=new D("EndOfTrack",e,g,[]);break;case 81:m=new D("SetTempo",e,g,[c[a++]<<16|c[a++]<<8|c[a++]]);break;case 84:m=new D("SmpteOffset",e,g,[c[a++],c[a++],c[a++],c[a++],c[a++]]);break;case 88:m=new D("TimeSignature",e,g,[c[a++],c[a++],c[a++],c[a++]]);break;case 89:m=new D("KeySignature",e,g,[c[a++],c[a++]]);break;case 127:m=new D("SequencerSpecific",e,g,[c.subarray(a,a+=m)]);break;default:m=new D("Unknown",e,g,[k,c.subarray(a, a+=m)])}break;default:u.console.log("unknown message:",q.toString(16))}break;default:throw Error("invalid status");}e=a-l;l=c.subarray(l,l+e);l[0]=q;m instanceof B&&"NoteOn"===m.h&&0===m.a&&(m.h=E[8],l=new Uint8Array([128|m.b,m.I,m.a]));p.push(l);x.push(m)}b.l.push(x);b.u.push(p)};function H(b){var d=d||{};this.b=b;this.a=d.index||0} function I(b){function d(){var b=c[a++]<<8|c[a++];b=a+b;var d=[];if(1!==c[a++])throw Error("invalid EditInstrument const value:"+c[a-1]);for(;a<b;){var e={};e.part=c[a++]>>4&3;e.modulator={ML:c[a]>>5,VIV:c[a]>>4&1,EG:c[a]>>3&1,SUS:c[a]>>2&1,RR:(c[a++]&3)<<2|c[a]>>6,DR:c[a]>>4&15,AR:(c[a++]&3)<<2|c[a]>>6,SL:c[a]>>4&15,TL:(c[a++]&3)<<4|c[a]>>4,WF:c[a]>>3&1,FB:c[a++]&7};e.carrier={ML:c[a]>>5,VIV:c[a]>>4&1,EG:c[a]>>3&1,SUS:c[a]>>2&1,RR:(c[a++]&3)<<2|c[a]>>6,DR:c[a]>>4&15,AR:(c[a++]&3)<<2|c[a]>>6,SL:c[a]>> 4&15,TL:(c[a++]&3)<<4|c[a]>>4,WF:c[a]>>3&1,FB:c[a++]&7};e.octaveSelect=c[a++]&3;d.push(e)}return d}function f(){var b=c[a++]<<8|c[a++];b=a+b;if(17!==c[a++])throw Error("invalid DeviceSpecific const value:"+c[a-1]);return{data:c.subarray(a,a+=b-a)}}var c=b.b,a=b.a,h=b.l=[],n,g;var x=0;for(g=b.g.v;x<g;++x){var p=String.fromCharCode(c[a++],c[a++],c[a++],c[a++]);if("trac"!==p)throw Error("invalid track signature:"+p);p=c[a++]<<24|c[a++]<<16|c[a++]<<8|c[a++];p=a+p;for(n=h[x]=[];a<p;){var e={key:null,length:null, aa:null,c:null,type:null,value:{},ja:null,ka:null};e.T=c[a++];var l=c[a++];if(255!==l)e.type="note",e.c="Note",e.ka=l>>6,e.key=l&63,e.length=c[a++],1===b.f.pa&&(l=c[a++],e.ja=l>>2,e.aa=l&3);else switch(e.type="meta",l=c[a++],l>>4){case 11:switch(l&15){case 0:e.c="MasterVolume";e.value=c[a++];break;case 10:e.c="DrumScale";e.value={channel:c[a]>>3&7,drum:c[a++]&1};break;default:throw Error("unknown message type:"+l.toString(16));}break;case 12:e.c="SetTempo";e.value={timeBase:7===(l&7)?NaN:Math.pow(2, l&7)*(0===(l&8)?6:15),tempo:c[a++]};break;case 13:switch(l&15){case 0:e.c="Point";e.value=c[a++];break;case 13:e.c="Loop";e.value={id:c[a]>>6,count:c[a]>>2&15,point:c[a++]&3};break;case 14:e.c="Nop";e.value=c[a++];break;case 15:e.c="EndOfTrack";e.value=c[a++];break;default:throw Error("unkwnon message type:"+l.toString(16));}break;case 14:switch(l&15){case 0:e.c="InstrumentLowPart";e.value={part:c[a]>>6,instrument:c[a++]&63};break;case 1:e.c="InstrumentHighPart";e.value={part:c[a]>>6,instrument:c[a++]& 1};break;case 2:e.c="Volume";e.value={part:c[a]>>6,volume:c[a++]&63};break;case 3:e.c="Valance";e.value={part:c[a]>>6,valance:c[a++]&63};break;case 4:e.c="PitchBend";e.value={part:c[a]>>6,value:c[a++]&63};break;case 5:e.c="ChannelAssign";e.value={part:c[a]>>6,channel:c[a++]&63};break;case 6:e.c="VolumeChange";e.value={part:c[a]>>6,volume:(c[a++]&63)<<26>>26};break;case 7:e.c="PitchBendRange";e.value={part:c[a]>>6,value:c[a++]&63};break;case 9:e.c="MasterCoarseTuning";e.value={part:c[a]>>6,value:c[a++]& 63};break;case 10:e.c="Modulation";e.value={part:c[a]>>6,depth:c[a++]&63};break;default:throw Error("unkwnon message type:"+l.toString(16));}break;case 15:switch(l&15){case 0:e.c="EditInstrument";e.value=d();break;case 1:e.c="Vibrato";l=e;a++;a++;if(1!==c[a++])throw Error("invalid Vibrato const value:"+c[a-1]);var q={part:c[a++]>>5&3,"switch":c[a++]>>6};l.value=q;break;case 15:e.c="DeviceSpecific";e.value=f();break;default:throw Error("unkwnon message type:"+l.toString(16));}break;default:throw Error("unkwnon message type:"+ l.toString(16));}n.push(e)}a=p}b.a=a} function J(b){var d={M:48,ra:[],u:[]},f=d.l;d=d.u;var c=b.l,a,h,n,g,x,p=[];for(h=0;16>h;++h)d[h]=[],p[h]=0;h=0;for(n=c.length;h<n;++h){var e=c[h];var l=[];var q=a=g=0;for(x=e.length;g<x;++g){var k=e[g];q+=k.deltaTime;k.id=a;k.time=q;switch(k.subType){case "Nop":break;case "Note":l[a++]=k;l[a]={id:a,type:"internal",subType:"NoteOff",time:q+k.length,key:k.key,voice:k.voice,velocity:k.velocity,octaveShift:k.octaveShift};a++;break;case "InstrumentHighPart":var r=k;k=e[++g];if("InstrumentLowPart"!==k.subType)throw Error("broken instrument"); l[a]={id:a,type:"internal",subType:"ProgramChange",time:q,part:k.value.part,instrument:r.value.instrument<<6|k.value.instrument};a++;break;default:l[a++]=k}}l.sort(function(a,b){return a.time>b.time?1:a.time<b.time?-1:a.id>b.id?1:a.id<b.id?-1:0});f[h]=[];q=g=0;for(x=l.length;g<x;++g){k=l[g];q=k.time;switch(k.subType){case "Note":r=K(k.key+45,k.octaveShift);e=4*h+k.voice;9===e&&(r-=10);d[e].push(L(q-p[e]).concat(144|e,r,2*k.velocity));break;case "NoteOff":r=K(k.key+45,k.octaveShift);e=4*h+k.voice; 9===e&&(r-=10);d[e].push(L(q-p[e]).concat(128|e,r,2*k.velocity));break;case "ProgramChange":e=4*h+k.part;d[e].push(L(q-p[e]).concat(192|e,k.instrument));break;case "SetTempo":r=288E7/(k.value.tempo*k.value.timeBase);e=0;d[e].push(L(q-p[e]).concat(255,81,3,r>>16&255,r>>8&255,r&255));break;case "Loop":r=k.value.count;r="LOOP_"+(0===k.value.point?"START":"END")+"=ID:"+k.value.id+",COUNT:"+(0===r?-1:r);e=0;d[e].push(L(q-p[e]).concat([255,6,r.length],r.split("").map(function(a){return a.charCodeAt(0)}))); break;case "MasterVolume":r=k.value;e=0;d[e].push(L(q-p[e]).concat(240,7,127,127,4,1,r,r,247));break;case "Modulation":e=4*h+k.value.part;d[e].push(L(q-p[e]).concat(176|e,1,2*k.value.depth));break;case "Volume":e=4*h+k.value.part;d[e].push(L(q-p[e]).concat(176|e,7,2*k.value.volume));break;case "Valance":e=4*h+k.value.part;d[e].push(L(q-p[e]).concat(176|e,10,2*(k.value.valance-32)+64));break;case "PitchBend":e=4*h+k.value.part;d[e].push(L(q-p[e]).concat(224|e,2*k.value.value,2*k.value.value));break; case "PitchBendRange":e=4*h+k.value.part;d[e].push(L(q-p[e]).concat(176|e,100,0),[0,176|e,101,0],[0,176|e,6,2*k.value.value]);break;case "MasterCoarseTuning":e=4*h+k.value.part;d[e].push(L(q-p[e]).concat(176|e,100,0),[0,176|e,101,2],[0,176|e,6,2*k.value.value]);break;default:continue}p[e]=k.time}}return M(b,d)}function K(b,d){var f=[0,12,-24,-12];if(void 0!==f[d])return b+f[d];throw Error("invalid OctaveShift value:"+d);} function M(b,d){var f,c=[77,84,104,100,0,0,0,6,0,1,0,16,0,48],a;if(void 0!==b.f.copy){b=b.f.copy;var h=b.length;var n=Array(h);for(f=0;f<h;++f)n[f]=b.charCodeAt(f);f=n;b=f.length;f=[0,255,2].concat(L(b),f);d[0].unshift(f)}h=0;for(b=d.length;h<b;++h){var g=d[h];f=[];n=0;for(a=g.length;n<a;++n)Array.prototype.push.apply(f,g[n]);a=f.length;n=[77,84,114,107,a>>24&255,a>>16&255,a>>8&255,a&255];c=c.concat(n,f)}return new Uint8Array(c)} function L(b){for(var d=[];128<=b;)d.unshift(b&127|(0===d.length?0:128)),b>>>=7;d.unshift(b|(0===d.length?0:128));return d};function N(){this.j=5E5;this.s=!1;this.b=0;this.J=this.N=this.H=this.G=!1;this.A=1;this.O=16383;this.window=u.window}t=N.prototype;t.ca=function(b){this.G=b};t.da=function(b){this.H=b};t.fa=function(b){this.N=b};t.ea=function(b){this.J=b};t.C=function(){var b;this.o=!0;this.i=Date.now();if(this.a)for(b=0;16>b;++b)this.a.postMessage("midi,b"+b.toString(16)+",78,0")};t.Z=function(){return this.a}; function O(b){b.C();P(b);b.o=!0;b.f=null;b.i=-1;b.P=null;b.w=null;b.D=null;b.length=0;b.b=0;b.time=0;b.B=0;b.window.clearTimeout(b.g);b.s?Q(b):b.a.addEventListener("message",function(d){"link,ready"===d.data&&Q(b)},!1)}function P(b){b.j=5E5;b.b=0;Q(b)} t.S=function(){var b=this;if(!this.a)throw Error("WebMidiLink not found");this.s?(this.length=this.f.length,this.f instanceof Array&&this.b>=this.f.length&&(this.b=0),R(this)):this.a.addEventListener("message",function(d){"link,ready"===d.data&&(b.s=!0,R(b))},!1)}; function Q(b){var d;for(d=0;16>d;++d)b.a.postMessage("midi,b"+d.toString(16)+",07,64"),b.a.postMessage("midi,b"+d.toString(16)+",0a,40"),b.a.postMessage("midi,e"+d.toString(16)+",00,40"),b.a.postMessage("midi,b"+d.toString(16)+",64,00"),b.a.postMessage("midi,b"+d.toString(16)+",65,00"),b.a.postMessage("midi,b"+d.toString(16)+",06,02"),b.a.postMessage("midi,b"+d.toString(16)+",26,00");b.K()} t.ha=function(b){var d=this;this.a=b;this.a.addEventListener("message",function(b){"link,ready"===b.data&&(d.s=!0,d.L(d.O))},!1)};t.L=function(b){this.O=b;this.a&&this.a.postMessage("midi,f0,7f,7f,04,01,"+[("0"+(b&127).toString(16)).substr(-2),("0"+(b>>7&127).toString(16)).substr(-2),"7f"].join())};t.ga=function(b){this.A=b}; function R(b){function d(){var b=a[n].time,p=a.length,e=Date.now();if(f.o)f.i=Date.now()-f.i;else{do{var l=a[n].event;"SetTempo"===l.h&&(f.j=l.data[0]);"ControlChange"===l.h&&111===l.I&&(g[0]={pos:n});if("Marker"===l.h&&("A"===l.data[0]&&(g[0]={pos:n}),"B"===l.data[0]&&f.H&&g[0]&&"number"===typeof g[0].pos)){n=g[0].pos;f.g=this.window.setTimeout(d,0);f.b=n;return}if("Marker"===l.h&&(l=l.data[0].match(/^LOOP_(START|END)=ID:(\d+),COUNT:(-?\d+)$/)))if("START"===l[1])g[l[2]|0]=g[l[2]]||{pos:n,count:l[3]| 0};else if("END"===l[1]&&f.N){var q=g[l[2]|0];if(0!==q.count){0<q.count&&q.count--;n=q.pos;f.g=this.window.setTimeout(d,0);f.b=n;return}g[l[2]|0]=null}h.postMessage(a[n++].webMidiLink)}while(n<p&&a[n].time===b);n<p?(e=Date.now()-e,f.g=this.window.setTimeout(d,f.j/(1E3*c)*(a[n].time-b-e)*(1/f.A))):(this.window.postMessage("endoftrack","*"),f.G&&g[0]&&"number"===typeof g[0].pos?(n=g[0].pos,f.g=this.window.setTimeout(d,0)):f.J&&(P(f),R(f)));f.b=n;f.time=b}}var f=b,c=b.P.M,a=b.f,h=b.a,n=b.b||0,g=[];b.o? (b.g=b.window.setTimeout(d,b.i),b.o=!1,b.i=-1):b.g=b.window.setTimeout(d,b.j/1E3*c*b.f[0].time)} t.F=function(b){b=new F(b);O(this);var d=b.a;var f=d.length+d.offset;for(d.b=[];d.a<f;){var c=d;var a=c.f;var h=c.a;c.b.push({type:String.fromCharCode(a[h++],a[h++],a[h++],a[h++]),size:a=c.m?(a[h++]<<24|a[h++]<<16|a[h++]<<8|a[h++])>>>0:(a[h++]|a[h++]<<8|a[h++]<<16|a[h++]<<24)>>>0,offset:h});h+=a;c.padding&&1===(h-c.offset&1)&&h++;c.a=h}d=z(b.a,b.f++);f=b.b;c=d.offset;if(!d||"MThd"!==d.type)throw Error("invalid header signature");b.U=f[c++]<<8|f[c++];b.v=f[c++]<<8|f[c++];b.M=f[c++]<<8|f[c++];d=0;for(f= b.v;d<f;++d)G(b);S(this,b)}; t.$=function(b){b=new H(b);O(this);var d=b.b,f=b.a,c=b.g={},a=String.fromCharCode(d[f++],d[f++],d[f++],d[f++]);if("melo"!==a)throw Error("invalid MFi signature:"+a);c.oa=(d[f++]<<24|d[f++]<<16|d[f++]<<8|d[f++])>>>0;c.ia=(d[f++]<<16|d[f++])+f;c.ma=d[f++];c.na=d[f++];c.v=d[f++];b.a=f;d=b.b;f=b.a;c=b.f={copy:null,date:null,exst:null,note:null,prot:null,sorc:null,titl:null,trac:null,vers:null};for(var h;f<b.g.ia;)switch(a=String.fromCharCode(d[f++],d[f++],d[f++],d[f++]),h=d[f++]<<8|d[f++],a){case "titl":case "copy":case "vers":case "date":case "prot":c[a]= String.fromCharCode.apply(null,d.subarray(f,f+=h));break;case "sorc":c[a]=d[f++];break;case "note":c[a]=d[f++]<<8|d[f++];break;case "exst":break;default:c[a]=d.subarray(f,f+=h)}b.a=f;I(b);this.F(J(b))}; function S(b,d){var f=b.f=[],c=b.D=[],a,h;var n=d.l;var g=Array(n.length);var x=d.u;var p=0;for(a=n.length;p<a;++p)g[p]=0;p=0;for(a=n.length;p<a;++p){g=n[p];var e=0;for(h=g.length;e<h;++e)0===d.U&&"SequenceTrackName"===g[e].h&&(b.w=g[e].data[0]),"CopyrightNotice"===g[e].h&&c.push(g[e].data[0]),f.push({track:p,eventId:e,time:g[e].time,event:g[e],webMidiLink:"midi,"+Array.prototype.map.call(x[p][e],function(a){return a.toString(16)}).join(",")})}f.sort(function(a,b){return a.time>b.time?1:a.time<b.time? -1:a.track>b.track?1:a.track<b.track?-1:a.eventId>b.eventId?1:a.eventId<b.eventId?-1:0});b.B=f.slice(-1)[0].time;b.P=d}t.Y=function(){return this.w};t.V=function(){return this.D};t.X=function(){return this.b};t.W=function(){return this.length};t.K=function(){this.a&&this.a.postMessage("midi,F0,7E,7F,09,01,F7")};t.ba=function(){this.a&&this.a.postMessage("midi,b0,78,0")}; t.R=function(b){b*=this.j/6E6;var d=b%3600;return Math.floor(b/3600)+":"+("00"+Math.floor(d/60)).slice(-2)+":"+("00"+Math.ceil(d%60)).slice(-2)};v("SMF.Worker",N);v("SMF.Worker.prototype.play",N.prototype.S);v("SMF.Worker.prototype.stop",N.prototype.C);v("SMF.Worker.prototype.loadMidiFile",N.prototype.F);v("SMF.Worker.prototype.loadMldFile",N.prototype.$);v("SMF.Worker.prototype.setLoop",N.prototype.ea);v("SMF.Worker.prototype.setCC111Loop",N.prototype.ca);v("SMF.Worker.prototype.setFalcomLoop",N.prototype.da);v("SMF.Worker.prototype.setMFiLoop",N.prototype.fa);v("SMF.Worker.prototype.setWebMidiLink",N.prototype.ha); v("SMF.Worker.prototype.getWebMidiLink",N.prototype.Z);v("SMF.Worker.prototype.setTempoRate",N.prototype.ga);v("SMF.Worker.prototype.setMasterVolume",N.prototype.L);v("SMF.Worker.prototype.getCopyright",N.prototype.V);v("SMF.Worker.prototype.getSequenceName",N.prototype.Y);v("SMF.Worker.prototype.getLength",N.prototype.W);v("SMF.Worker.prototype.getPosition",N.prototype.X);v("SMF.Worker.prototype.sendGmReset",N.prototype.K);v("SMF.Worker.prototype.sendAllSoundOff",N.prototype.ba); v("SMF.Worker.prototype.time",N.prototype.time);v("SMF.Worker.prototype.timeTotal",N.prototype.B);v("SMF.Worker.prototype.getTime",N.prototype.R);}).call(this); //# sourceMappingURL=smf.worker.min.js.map