-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathym2612.min.js
1 lines (1 loc) · 27.4 KB
/
ym2612.min.js
1
function YM2612(){if(!this instanceof YM2612)return new YM2612;this.version=257;this.start=0;this.count=0;this.chip=null}(function(e){"use strict";function c(){function e(){this.ar=0;this.d1r=0;this.d2r=0;this.rr=0;this.ksr=0;this.mul=1;this.init=function(){this.ar=0;this.d1r=0;this.d2r=0;this.rr=0;this.ksr=0;this.mul=1};this.toString=function(){return["MUL="+this.mul,"KS="+this.ksr,"AR="+this.ar,"D1R="+this.d1r,"D2R="+this.d2r,"RR="+this.rr].join(",")}}function t(){this.ar=0;this.d1r=0;this.d2r=0;this.rr=0;this.init=function(){this.ar=0;this.d1r=0;this.d2r=0;this.rr=0}}this.DT=-1;this.KSR=0;this.rate=new e;this.phase=0;this.Incr=0;this.state=0;this.tl=0;this.volume=0;this.sl=0;this.vol_out=0;this.eg={sh:new t,sel:new t,init:function(){this.sh.init();this.sel.init()}};this.ssg=0;this.ssgn=0;this.key=0;this.AMmask=0;this.reset=function(){this.Incr=-1;this.key=0;this.phase=0;this.ssgn=0;this.state=i.OFF;this.volume=r.MAX_ATT_INDEX;this.vol_out=r.MAX_ATT_INDEX;this.out[0]=0,this.out[1]=0};this.debug={dt1mul:0,dt1:0,mul:0,tl:0,ksar:0,ks:0,ar:0,amd1r:0,am:0,d1r:0,d2r:0,slrr:0,sl:0,rr:0};this.debug.toString=function(){return["TL:"+this.tl,"DT1:"+this.dt1,"MUL:"+this.mul,"KS:"+this.ks,"AR:"+this.ar,"AM:"+this.am,"D1R:"+this.d1r,"D2R:"+this.d2r,"SL:"+this.sl,"RR:"+this.rr].join(",")};this.out=[0,0];this.toString=function(){return"OP{"+this.debug.toString()+"}"}}function h(){this.SLOT=[new c,new c,new c,new c];this.ALGO=0;this.FB=0;this.op1_out=[0,0];this.connect=["x","x","x","x"];this.mem={connect:"mem",value:0};this.pms=0;this.ams=0;this.fc=0;this.kcode=0;this.block_fnum=0;this.fn_h=0;this.outputs={m1:0,m2:0,c1:0,c2:0,mem:0,x:0,out:0};this.canCSM=0;this.canDAC=0;this.muted=0;this.pan=[0,0];this.reset=function(){this.mem.value=0,this.op1_out[0]=0,this.op1_out[1]=0;var e=this.SLOT.length;while(--e>-1)this.SLOT[e].reset()};this.toString=function(){return"CH{"+["ALGO:"+this.ALGO,"FB:"+this.FB,"PMS:"+this.pms,"AMS:"+this.ams,"SLOTS["+this.SLOT.join(",")+"]"].join(",")+"}"}}function p(e,t){this.address=0;this.status=0;this.mode=0;this.timer_base=1;this.TA=0;this.TAL=0;this.TAC=0;this.TB=0;this.TBL=0;this.TBC=0;this.dt_tab=[new Array(32),new Array(32),new Array(32),new Array(32),new Array(32),new Array(32),new Array(32),new Array(32)];this.clock=e||7670448;this.rate=t||44100}function d(){this.fc=[0,0,0];this.fn_h=0;this.kcode=[0,0,0];this.block_fnum=[0,0,0];this.key_csm=0}function v(e,t){function n(){this.cnt=0;this.timer=0;this.timer_add=0;this.timer_overflow=0;this.init=function(){this.cnt=0;this.timer=0;this.timer_add=0;this.timer_overflow=0}}this.ST=new p(e,t);this.SL3=new d;this.eg=new n;this.lfo=new n;this.lfo.AM=0;this.lfo.PM=0;this.fn={table:new Array(4096),max:0}}function m(e,t){this.CH=[new h,new h,new h,new h,new h,new h];this.CH[2].canCSM=1;this.CH[5].canDAC=1;this.dacen=0;this.dacout=0;this.OPN=new v(e,t);this.toString=function(){return"YM[\n"+this.CH.join(",\n")+"\n]"}}function g(e){if((e.OPN.ST.mode&1)>0){if(t.mode)e.OPN.ST.TAC-=e.OPN.ST.timer_base;else--e.OPN.ST.TAC;if(e.OPN.ST.TAC<=0){if((e.OPN.ST.mode&4)>0)e.OPN.ST.status|=1;if(t.mode&&e.OPN.ST.TAL)e.OPN.ST.TAC+=e.OPN.ST.TAL;else e.OPN.ST.TAC=e.OPN.ST.TAL;if((e.OPN.ST.mode&192)===128)e.CH[2].keyControlCSM()}}}function y(e,n){if((e.OPN.ST.mode&2)>0){if(t.mode)e.OPN.ST.TBC-=e.OPN.ST.timer_base*n;else e.OPN.ST.TBC-=n;if(e.OPN.ST.TBC<=0){if((e.OPN.ST.mode&8)>0)e.OPN.ST.status|=2;if(e.OPN.ST.TBL)e.OPN.ST.TBC+=e.OPN.ST.TBL;else e.OPN.ST.TBC=e.OPN.ST.TBL}}}function b(e,t){if(((e.OPN.ST.mode^t)&192)>0){e.CH[2].SLOT[l[0]].Incr=-1;if((t&192)!==128&&e.OPN.SL3.key_csm){e.CH[2].keyOffCSM(l[0]);e.CH[2].keyOffCSM(l[1]);e.CH[2].keyOffCSM(l[2]);e.CH[2].keyOffCSM(l[3]);e.OPN.SL3.key_csm=0}}if(t&1&&!(e.OPN.ST.mode&1))e.OPN.ST.TAC=e.OPN.ST.TAL;if(t&2&&!(e.OPN.ST.mode&2))e.OPN.ST.TBC=e.OPN.ST.TBL;e.OPN.ST.status&=~t>>4;e.OPN.ST.mode=t}function w(e){var n;if(t.mode)n=function(e){while(e.lfo.timer>=e.lfo.timer_overflow){e.lfo.timer-=e.lfo.timer_overflow;e.lfo.cnt=e.lfo.cnt+1&127;if(e.lfo.cnt<64)e.lfo.AM=(e.lfo.cnt^63)<<1;else e.lfo.AM=(e.lfo.cnt&63)<<1;e.lfo.PM=e.lfo.cnt>>2}};else n=function(e){if(e.lfo.timer>e.lfo.timer_overflow){e.lfo.timer=0;e.lfo.cnt=e.lfo.cnt+1&127;if(e.lfo.cnt<64)e.lfo.AM=(e.lfo.cnt^63)<<1;else e.lfo.AM=(e.lfo.cnt&63)<<1;e.lfo.PM=e.lfo.cnt>>2}};if(e.OPN.lfo.timer_overflow){if(t.mode)e.OPN.lfo.timer+=e.OPN.lfo.timer_add;else++e.OPN.lfo.timer;n(e.OPN)}}function E(e,t){var n=e.CH.length;while(--n>-1)e.CH[n].advance_eg(t)}function S(e){var t=e.CH.length;while(--t>-1)e.CH[t].update_ssg_eg()}function T(e,t){if(t>e.CH.length)t=e.CH.length;while(--t>-1)e.CH[t].reset()}function N(e){if(t.debug)console.log("init_tables",e.CH.length);var i,s,a;var l;var c,h;var p,d;var v=Math.PI,m=Math.log,g=Math.pow,y=Math.sin;var b=r.STEP/32,w=1<<16,E=u.RES_LEN<<1;for(a=0;a<u.RES_LEN;++a){h=w/g(2,(a+1)*b);l=(h|0)>>4;if(l&1)l=(l>>1)+1;else l=l>>1;l<<=2;d=a<<1;u.tab[d+0]=l;u.tab[d+1]=-l;for(s=1;s<13;++s){p=d+0+s*E|0;u.tab[p]=u.tab[d]>>s;u.tab[p+1]=-u.tab[p]}}p=v/o.LEN,d=8/m(2),b=2*4/r.STEP;for(s=0;s<o.LEN;++s){h=y(((s<<1)+1)*p);if(h>0)c=m(1/h)*d;else c=m(-1/h)*d;l=c*b|0;if(l&1)l=(l>>1)+1;else l=l>>1;n.sin[s]=(l<<1)+(h>=0?0:1)}for(s=0;s<8;++s){for(l=0;l<128;++l){for(a=0;a<8;++a){d=0;for(c=0;c<7;++c){if((l&1<<c)>0){d+=f.pm_output[(c<<3)+s][a]}}i=(l<<8)+(s<<5);f.pm_table[i+a+0]=d;f.pm_table[i+(a^7)+8]=d;f.pm_table[i+a+16]=-d;f.pm_table[i+(a^7)+24]=-d}}}}var t={hq_fm:0,dac_bits:8,maxcalc:0,debug:0,debugLocal:0,debugArr:[],mode:0,strict:0};var n={FREQ_SH:16,EG_SH:16,LFO_SH:24,TIMER_SH:16};n.FREQ_MASK=(1<<n.FREQ_SH)-1;var r={BITS:10,MIN_ATT_INDEX:0};r.LEN=1<<r.BITS;r.STEP=128/r.LEN;r.MAX_ATT_INDEX=r.LEN-1;var i={ATT:4,DEC:3,SUS:2,REL:1,OFF:0};var s={BITS:17};s.LEN=1<<s.BITS;s.MASK=s.LEN-1;var o={BITS:10};o.LEN=1<<o.BITS;o.MASK=o.LEN-1;var u={BITS:14};u.RES_LEN=256;u.TAB_LEN=13*2*u.RES_LEN;u.tab=new Array(u.TAB_LEN);r.QUIET=u.TAB_LEN>>3;n.sin=new Array(o.LEN);n.sl=function(){var e=function(e){return e*4/r.STEP|0};return[e(0),e(1),e(2),e(3),e(4),e(5),e(6),e(7),e(8),e(9),e(10),e(11),e(12),e(13),e(14),e(31)]}();i.RATE_STEPS=8;i.inc=[0,1,0,1,0,1,0,1,0,1,0,1,1,1,0,1,0,1,1,1,0,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,2,1,2,1,2,1,2,1,2,1,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,4,2,2,2,4,2,4,2,4,2,4,2,4,2,4,4,4,2,4,4,4,4,4,4,4,4,4,4,4,4,4,4,8,4,4,4,8,4,8,4,8,4,8,4,8,4,8,8,8,4,8,8,8,8,8,8,8,8,8,8,8,16,16,16,16,16,16,16,16,0,0,0,0,0,0,0,0];i.rate_select=function(){var e=function(e){return e*i.RATE_STEPS|0};return[e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(18),e(0),e(0),e(0),e(0),e(2),e(2),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(0),e(1),e(2),e(3),e(4),e(5),e(6),e(7),e(8),e(9),e(10),e(11),e(12),e(13),e(14),e(15),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16),e(16)]}();i.rate_shift=function(){var e=function(e){return e|0};return[e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(11),e(10),e(10),e(10),e(10),e(9),e(9),e(9),e(9),e(8),e(8),e(8),e(8),e(7),e(7),e(7),e(7),e(6),e(6),e(6),e(6),e(5),e(5),e(5),e(5),e(4),e(4),e(4),e(4),e(3),e(3),e(3),e(3),e(2),e(2),e(2),e(2),e(1),e(1),e(1),e(1),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0),e(0)]}();s.tab=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,7,8,8,8,8,1,1,1,1,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,7,8,8,9,10,11,12,13,14,16,16,16,16,2,2,2,2,2,3,3,3,4,4,4,5,5,6,6,7,8,8,9,10,11,12,13,14,16,17,19,20,22,22,22,22];var a={fktable:[0,0,0,0,0,0,0,1,2,3,3,3,3,3,3,3]};var f={samples_per_step:[108,77,71,67,62,44,8,5],ams_depth_shift:[8,3,1,0],pm_output:[[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,1,1,1,1],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,1,1,1,1],[0,0,1,1,2,2,2,3],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,1],[0,0,0,0,1,1,1,1],[0,0,1,1,2,2,2,3],[0,0,2,3,4,4,5,6],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,0,0],[0,0,0,0,0,0,1,1],[0,0,0,0,1,1,1,1],[0,0,0,1,1,1,1,2],[0,0,1,1,2,2,2,3],[0,0,2,3,4,4,5,6],[0,0,4,6,8,8,10,12],[0,0,0,0,0,0,0,0],[0,0,0,0,1,1,1,1],[0,0,0,1,1,1,2,2],[0,0,1,1,2,2,3,3],[0,0,1,2,2,2,3,4],[0,0,2,3,4,4,5,6],[0,0,4,6,8,8,10,12],[0,0,8,12,16,16,20,24],[0,0,0,0,0,0,0,0],[0,0,0,0,2,2,2,2],[0,0,0,2,2,2,4,4],[0,0,2,2,4,4,6,6],[0,0,2,4,4,4,6,8],[0,0,4,6,8,8,10,12],[0,0,8,12,16,16,20,24],[0,0,16,24,32,32,40,48],[0,0,0,0,0,0,0,0],[0,0,0,0,4,4,4,4],[0,0,0,4,4,4,8,8],[0,0,4,4,8,8,12,12],[0,0,4,8,8,8,12,16],[0,0,8,12,16,16,20,24],[0,0,16,24,32,32,40,48],[0,0,32,48,64,64,80,96]],pm_table:new Array(128*8*32)};a.CHAN=function(e){return e&3};a.SLOT=function(e){return e>>2&3};var l=[0,2,1,3];n.bitmask=0;c.prototype.keyOn=function(e,t){if(!this.key&&!e.OPN.SL3.key_csm){this.phase=0;this.ssgn=0;if(this.rate.ar+this.rate.ksr<94)this.state=this.volume<=r.MIN_ATT_INDEX?this.sl===r.MIN_ATT_INDEX?i.SUS:i.DEC:i.ATT;else{this.volume=r.MIN_ATT_INDEX;this.state=this.sl===r.MIN_ATT_INDEX?i.SUS:i.DEC}if((this.ssg&8)>0&&(this.ssgn^this.ssg&4)>0)this.vol_out=this.tl+(512-this.volume&r.MAX_ATT_INDEX);else this.vol_out=this.tl+(this.volume|0)}if(!t)this.key=1};h.prototype.keyOn=function(e,t){this.SLOT[t].keyOn(e,0)};c.prototype.keyOff=function(e,t){if(t&&!this.key||!t&&this.key&&!e.OPN.SL3.key_csm){if(this.state>i.REL){this.state=i.REL;if((this.ssg&8)>0){if((this.ssgn^this.ssg&4)>0)this.volume=512-this.volume|0;if(this.volume>=512){this.volume=r.MAX_ATT_INDEX;this.state=i.OFF}this.vol_out=this.tl+(this.volume|0)}}}if(!t)this.key=0};h.prototype.keyOff=function(e,t){this.SLOT[t].keyOff(e,0)};h.prototype.keyOnCSM=function(e,t){this.SLOT[t].keyOn(e,1)};h.prototype.keyOffCSM=function(e,t){this.SLOT[t].keyOff(e,1)};h.prototype.keyControlCSM=function(e){this.keyOnCSM(l[0]);this.keyOnCSM(l[1]);this.keyOnCSM(l[2]);this.keyOnCSM(l[3]);e.OPN.SL3.key_csm=1};h.prototype.setupConnection=function(){var e="out";var n={m1:0,m2:2,c1:1,c2:3},r=this.mem.connect;switch(this.ALGO){case 0:this.connect[n.m1]="c1";this.connect[n.c1]="mem";this.connect[n.m2]="c2";this.mem.connect="m2";break;case 1:this.connect[n.m1]="mem";this.connect[n.c1]="mem";this.connect[n.m2]="c2";this.mem.connect="m2";break;case 2:this.connect[n.m1]="c2";this.connect[n.c1]="mem";this.connect[n.m2]="c2";this.mem.connect="m2";break;case 3:this.connect[n.m1]="c1";this.connect[n.c1]="mem";this.connect[n.m2]="c2";this.mem.connect="c2";break;case 4:this.connect[n.m1]="c1";this.connect[n.c1]=e;this.connect[n.m2]="c2";this.mem.connect="mem";break;case 5:this.connect[n.m1]="x";this.connect[n.c1]=e;this.connect[n.m2]=e;this.mem.connect="m2";break;case 6:this.connect[n.m1]="c1";this.connect[n.c1]=e;this.connect[n.m2]=e;this.mem.connect="mem";break;case 7:this.connect[n.m1]=e;this.connect[n.c1]=e;this.connect[n.m2]=e;this.mem.connect="mem";break;default:if(t.strict)throw new Error("CH::setup_connection - unsupported algorithm ("+this.ALGO+")");else break}this.connect[3]=e};c.prototype.set_det_mul=function(e,t){this.rate.mul=(t&15)>0?(t&15)<<1:1;this.DT=t>>4&7;this.debug.dt1mul=t&255;this.debug.dt1=this.DT;this.debug.mul=t&15};h.prototype.set_det_mul=function(e,t,n){this.SLOT[t].set_det_mul(e,n);this.SLOT[l[0]].Incr=-1};c.prototype.set_tl=function(e){this.debug.tl=e&127;this.tl=this.debug.tl<<r.BITS-7;if((this.ssg&8)>0&&(this.ssgn^this.ssg&4)>0&&this.state>i.REL)this.vol_out=this.tl+((512-this.volume|0)&r.MAX_ATT_INDEX);else this.vol_out=this.tl+(this.volume|0)};h.prototype.set_tl=function(e,t){this.SLOT[e].set_tl(t)};c.prototype.set_ar_ksr=function(e){this.debug.ksar=e&255;this.debug.ks=e>>6;this.debug.ar=e&31;var t=this.KSR|0;this.rate.ar=this.debug.ar>0?32+(this.debug.ar<<1):0;this.KSR=3-this.debug.ks;if(this.rate.ar+this.rate.ksr<94){var n=this.rate.ar+this.rate.ksr|0;this.eg.sh.ar=i.rate_shift[n];this.eg.sel.ar=i.rate_select[n]}else{this.eg.sh.ar=0;this.eg.sel.ar=18*i.RATE_STEPS}return this.KSR!==t};h.prototype.set_ar_ksr=function(e,t){if(this.SLOT[e].set_ar_ksr(t))this.SLOT[l[0]].Incr=-1};c.prototype.set_dr=function(e){this.debug.amd1r=e&255;this.debug.am=e&128;this.debug.d1r=e&31;this.rate.d1r=this.debug.d1r>0?32+(this.debug.d1r<<1):0;var t=this.rate.d1r+this.rate.ksr|0;this.eg.sh.d1r=i.rate_shift[t];this.eg.sel.d1r=i.rate_select[t]};h.prototype.set_dr=function(e,t){this.SLOT[e].set_dr(t)};c.prototype.set_sr=function(e){this.debug.d2r=e&31;this.rate.d2r=this.debug.d2r>0?32+(this.debug.d2r<<1):0;var t=this.rate.d2r+this.rate.ksr|0;this.eg.sh.d2r=i.rate_shift[t];this.eg.sel.d2r=i.rate_select[t]};h.prototype.set_sr=function(e,t){this.SLOT[e].set_sr(t)};c.prototype.set_sl_rr=function(e){this.debug.slrr=e&255;this.debug.sl=e>>4&15;this.debug.rr=e&15;this.sl=n.sl[this.debug.sl];if(this.state===i.DEC&&this.volume>=(this.sl|0))this.state=i.SUS;this.rate.rr=34+(this.debug.rr<<2);var t=this.rate.rr+this.rate.ksr|0;this.eg.sh.rr=i.rate_shift[t];this.eg.sel.rr=i.rate_select[t]};h.prototype.set_sl_rr=function(e,t){this.SLOT[e].set_sl_rr(t)};c.prototype.advance_eg=function(e){switch(this.state){case i.ATT:if(!(e&(1<<this.eg.sh.ar)-1)){this.volume+=~this.volume*i.inc[this.eg.sel.ar+(e>>this.eg.sh.ar&7)]>>4;if(this.volume<=r.MIN_ATT_INDEX){this.volume=r.MIN_ATT_INDEX;this.state=this.sl===r.MIN_ATT_INDEX?i.SUS:i.DEC}if((this.ssg&8)>0&&(this.ssgn^this.ssg&4)>0)this.vol_out=this.tl+((512-this.volume|0)&r.MAX_ATT_INDEX);else this.vol_out=this.tl+(this.volume|0)}break;case i.DEC:if(!(e&(1<<this.eg.sh.d1r)-1)){if((this.ssg&8)>0){if(this.volume<512){this.volume+=i.inc[this.eg.sel.d1r+(e>>this.eg.sh.d1r&7)]<<2;if((this.ssgn^this.ssg&4)>0)this.vol_out=this.tl+((512-this.volume|0)&r.MAX_ATT_INDEX);else this.vol_out=this.tl+(this.volume|0)}}else{this.volume+=i.inc[this.eg.sel.d1r+(e>>this.eg.sh.d1r&7)];this.vol_out=this.tl+(this.volume|0)}if(this.volume>=(this.sl|0))this.state=i.SUS}break;case i.SUS:if(!(e&(1<<this.eg.sh.d2r)-1)){if((this.ssg&8)>0){if(this.volume<512){this.volume+=i.inc[this.eg.sel.d2r+(e>>this.eg.sh.d2r&7)]<<2;if((this.ssgn^this.ssg&4)>0)this.vol_out=this.tl+((512-this.volume|0)&r.MAX_ATT_INDEX);else this.vol_out=this.tl+(this.volume|0)}}else{this.volume+=i.inc[this.eg.sel.d2r+(e>>this.eg.sh.d2r&7)];if(this.volume>=r.MAX_ATT_INDEX)this.volume=r.MAX_ATT_INDEX;this.vol_out=this.tl+(this.volume|0)}}break;case i.REL:if(!(e&(1<<this.eg.sh.rr)-1)){if((this.ssg&8)>0){if(this.volume<512){this.volume+=i.inc[this.eg.sel.rr+(e>>this.eg.sh.rr&7)]<<2;if(this.volume>=512){this.volume=r.MAX_ATT_INDEX;this.state=i.OFF}}}else{this.volume+=i.inc[this.eg.sel.rr+(e>>this.eg.sh.rr&7)];if(this.volume>=r.MAX_ATT_INDEX){this.volume=r.MAX_ATT_INDEX;this.state=i.OFF}}this.vol_out=this.tl+(this.volume|0)}break;default:if(t.strict)throw new Error("FM_SLOT::advance_eg - unsupported state ("+this.state+")");else break}};h.prototype.advance_eg=function(e){var t=this.SLOT.length;while(--t>-1)this.SLOT[t].advance_eg(e)};c.prototype.update_ssg_eg=function(){if((this.ssg&8)>0&&this.volume>=512&&this.state>i.REL){if((this.ssg&1)>0){if((this.ssg&2)>0)this.ssgn=4;if(this.state!==i.ATT&&!(this.ssgn^this.ssg&4))this.volume=r.MAX_ATT_INDEX}else{if((this.ssg&2)>0)this.ssgn^=4;else this.phase=0;if(this.state!==i.ATT){if(this.rate.ar+this.rate.ksr<94)this.state=this.volume<=r.MIN_ATT_INDEX?this.sl===r.MIN_ATT_INDEX?i.SUS:i.DEC:i.ATT;else{this.volume=r.MIN_ATT_INDEX;this.state=this.sl===r.MIN_ATT_INDEX?i.SUS:i.DEC}}}if((this.ssgn^this.ssg&4)>0)this.vol_out=this.tl+((512-this.volume|0)&r.MAX_ATT_INDEX);else this.vol_out=this.tl+(this.volume|0)}};h.prototype.update_ssg_eg=function(){var e=this.SLOT.length;while(--e>-1)this.SLOT[e].update_ssg_eg()};c.prototype.update_phase_lfo=function(e,n,r){var i=f.pm_table[((r&2032)>>4<<8)+n+e.OPN.lfo.PM];if(i){if(this.DT<0){console.log("FM_SLOT::update_phase_lfo - invalid DT",this.DT);if(t.strict)throw new Error("FM_SLOT::update_phase_lfo - invalid DT="+this.DT);else return}var o,u,l;r=i+(r<<1);o=(r&28672)>>12;r=r&4095;u=o<<2|a.fktable[r>>8];if(t.mode){l=(e.OPN.fn.table[r]>>7-o)+e.OPN.ST.dt_tab[this.DT][u];if(l<0)l+=e.OPN.fn.max}else l=(r<<5>>7-o)+e.OPN.ST.dt_tab[this.DT][u]&s.MASK;this.phase+=l*this.rate.mul>>1}else this.phase+=this.Incr};c.prototype.update_phase_lfo_precalc=function(e,n,r){if(n!==-1){var i;if(t.mode){i=n+e.OPN.ST.dt_tab[this.DT][r];if(i<0)i+=e.OPN.fn.max}else i=n+e.OPN.ST.dt_tab[this.DT][r]&s.MASK;this.phase+=i*this.rate.mul>>1}else this.phase+=this.Incr};h.prototype.update_phase_lfo=function(e){var n=this.pms,r=this.block_fnum;var i,s,o;var u=f.pm_table[((r&2032)>>4<<8)+n+e.OPN.lfo.PM];if(u){r=u+(r<<1);i=(r&28672)>>12;r=r&4095;s=i<<2|a.fktable[r>>8];if(t.mode){o=e.OPN.fn.table[r]>>7-i}else o=r<<5>>7-i}else{o=-1}var c=this.SLOT.length;while(--c>-1)this.SLOT[l[c]].update_phase_lfo_precalc(e,o,s)};c.prototype.refresh_fc_eg=function(e,n,r){if(this.DT<0){console.log("FM_SLOT::refresh_fc_eg - invalid DT",this.DT);if(t.strict)throw new Error("FM_SLOT::refresh_fc_eg - invalid DT="+this.DT);else return}if(t.debug>1)console.log("OPN.ST.dt_tab["+this.DT+"]["+r+"]",e.OPN.ST.dt_tab[this.DT][r]);n+=e.OPN.ST.dt_tab[this.DT][r];if(t.mode){if(n<0)n+=e.OPN.fn.max}else n&=s.MASK;this.Incr=n*this.rate.mul>>1;r=r>>this.KSR;if(this.rate.ksr!==r){this.rate.ksr=r;var o=this.rate.ar+r|0;if(o<94){this.eg.sh.ar=i.rate_shift[o];this.eg.sel.ar=i.rate_select[o]}else{this.eg.sh.ar=0;this.eg.sel.ar=18*i.RATE_STEPS}o=this.rate.d1r+r|0;this.eg.sh.d1r=i.rate_shift[o];this.eg.sel.d1r=i.rate_select[o];o=this.rate.d2r+r|0;this.eg.sh.d2r=i.rate_shift[o];this.eg.sel.d2r=i.rate_select[o];o=this.rate.rr+r|0;this.eg.sh.rr=i.rate_shift[o];this.eg.sel.rr=i.rate_select[o]}};h.prototype.refresh_fc_eg=function(e){if(this.SLOT[l[0]].Incr===-1){var n=this.fc|0,r=this.kcode|0;if(t.debug>1)console.log("FM_CH::refresh_fc_eg",n,r);var i=this.SLOT.length;while(--i>-1)this.SLOT[l[i]].refresh_fc_eg(e,n,r)}};c.prototype.calcVol=function(e){return this.vol_out+(e&this.AMmask)|0};var x;if(t.mode)x=function(e,t,r,i){var s=(t<<3)+n.sin[(e&~n.FREQ_MASK)+(i?r:r<<15)>>n.FREQ_SH&o.MASK];if(s>=u.TAB_LEN)return 0;return u.tab[s]};else x=function(e,t,r,i){var s=(t<<3)+n.sin[(i?e+r>>o.BITS:(e>>o.BITS)+(r>>1))&o.MASK];if(s>=u.TAB_LEN)return 0;return u.tab[s]};c.prototype.calculate=function(e,t,n){var i=this.vol_out+(t&this.AMmask)|0,s=0;if(n){var o=this.out[0]+this.out[1]|0;this.out[0]=this.out[1]|0;s=this.out[0]|0;if(i<r.QUIET){if(!e)o=0;this.out[1]=x(this.phase,i,o<<e,1)}else this.out[1]=0}else if(i<r.QUIET){this.out[0]=x(this.phase,i,e,0);s=this.out[0]|0}else s=this.out[0]=0;return s};h.prototype.calculate=function(e){var n="",r=t.debug>1&&t.maxcalc>0;var i=e.OPN.lfo.AM>>this.ams;var s,o;var u,a=["x","c1","m2","c2"];this.outputs.m2=0,this.outputs.c1=0,this.outputs.c2=0,this.outputs.mem=0;this.outputs.x=0;this.outputs[this.mem.connect]=this.mem.value;u=0;o=this.SLOT[l[u]].calculate(this.FB,i,1);if(o!==0){if(this.connect[u]==="x")this.outputs.x=o,this.outputs.mem=o,this.outputs.c1=o,this.outputs.c2=o;else this.outputs[this.connect[u]]+=o}u=2;o=this.SLOT[l[u]].calculate(this.outputs[a[u]],i,0);if(o!==0){if(this.connect[u]==="x"){}else this.outputs[this.connect[u]]+=o}u=1;o=this.SLOT[l[u]].calculate(this.outputs[a[u]],i,0);if(o!==0){if(this.connect[u]==="x"){}else this.outputs[this.connect[u]]+=o}u=3;o=this.SLOT[l[u]].calculate(this.outputs[a[u]],i,0);if(o!==0){if(this.connect[u]==="x"){}else this.outputs[this.connect[u]]+=o}this.mem.value=this.outputs.mem|0;if(this.pms){if((e.OPN.ST.mode&192)>0&&this.canCSM){this.SLOT[l[0]].update_phase_lfo(e,this.pms,e.OPN.SL3.block_fnum[1]);this.SLOT[l[1]].update_phase_lfo(e,this.pms,e.OPN.SL3.block_fnum[2]);this.SLOT[l[2]].update_phase_lfo(e,this.pms,e.OPN.SL3.block_fnum[0]);this.SLOT[l[3]].update_phase_lfo(e,this.pms,this.block_fnum)}else this.update_phase_lfo(e)}else{this.SLOT[l[0]].phase+=this.SLOT[l[0]].Incr;this.SLOT[l[1]].phase+=this.SLOT[l[1]].Incr;this.SLOT[l[2]].phase+=this.SLOT[l[2]].Incr;this.SLOT[l[3]].phase+=this.SLOT[l[3]].Incr}if(r)n+="; m2="+this.outputs.m2+";c1="+this.outputs.c1+";c2="+this.outputs.c2+";out="+this.outputs.out,console.log("FM_CH::calc",this.ALGO,n),--t.maxcalc};a.WriteMode=function(e,r,i){i=i&255;switch(r){case 33:break;case 34:if(i&8){if(t.mode)e.OPN.lfo.timer_overflow=f.samples_per_step[i&7]<<n.LFO_SH;else e.OPN.lfo.timer_overflow=f.samples_per_step[i&7]}else{e.OPN.lfo.timer_overflow=0;e.OPN.lfo.timer=0;e.OPN.lfo.cnt=0;e.OPN.lfo.AM=126;e.OPN.lfo.PM=0}break;case 36:e.OPN.ST.TA=e.OPN.ST.TA&3|(i|0)<<2;e.OPN.ST.TAL=1024-e.OPN.ST.TA;if(t.mode)e.OPN.ST.TAL<<=n.TIMER_SH;break;case 37:e.OPN.ST.TA=e.OPN.ST.TA&1020|i&3;e.OPN.ST.TAL=1024-e.OPN.ST.TA;if(t.mode)e.OPN.ST.TAL<<=n.TIMER_SH;break;case 38:e.OPN.ST.TB=i;if(t.mode)e.OPN.ST.TBL=256-i<<n.TIMER_SH+4;else e.OPN.ST.TBL=256-i<<4;break;case 39:b(e,i);break;case 40:var s=i&3;if(s===3)break;if(i&4)s+=3;(function(t){if(i&16)t.keyOn(e,l[0]);else t.keyOff(e,l[0]);if(i&32)t.keyOn(e,l[1]);else t.keyOff(e,l[1]);if(i&64)t.keyOn(e,l[2]);else t.keyOff(e,l[2]);if(i&128)t.keyOn(e,l[3]);else t.keyOff(e,l[3])})(e.CH[s]);break}};a.WriteReg=function(e,s,o){o=o&255;var u=a.CHAN(s),c=a.SLOT(s);if(u>=3){if(t.strict)throw new Error("OPN_Write - unsupported channel "+u+" or slot "+c+" from {$"+s.toString(16)+",$"+o.toString(16)+"}");else return}if(s>=256)u+=3;var h=l[c];switch(s&240){case 48:e.CH[u].set_det_mul(e,c,o);break;case 64:e.CH[u].set_tl(c,o);break;case 80:e.CH[u].set_ar_ksr(c,o);break;case 96:e.CH[u].set_dr(c,o);e.CH[u].SLOT[c].AMmask=o&128?~0:0;break;case 112:e.CH[u].set_sr(c,o);break;case 128:e.CH[u].set_sl_rr(c,o);break;case 144:(function(e){e.ssg=o&15;if(e.state>i.REL){if((e.ssg&8)>0&&(e.ssgn^e.ssg&4)>0)e.vol_out=e.tl+((512-e.volume|0)&r.MAX_ATT_INDEX);else e.vol_out=e.tl+(e.volume|0)}})(e.CH[u].SLOT[c]);break;case 160:var p,d;switch(c){case 0:p=((e.CH[u].fn_h&7)<<8)+o;d=e.CH[u].fn_h>>3&255;e.CH[u].kcode=d<<2|a.fktable[p>>7];if(t.mode)e.CH[u].fc=e.OPN.fn.table[p<<1]>>7-d;else e.CH[u].fc=p<<6>>7-d;e.CH[u].block_fnum=d<<11|p;e.CH[u].SLOT[l[0]].Incr=-1;if(t.debug>2)console.log("block_fnum=x",e.CH[u].block_fnum.toString(16)," kcode=",e.CH[u].kcode.toString(16)," fc=",e.CH[u].fc.toString(16));break;case 1:e.CH[u].fn_h=o&63|0;break;case 2:if(s<256){p=((e.OPN.SL3.fn_h&7)<<8)+o;d=e.OPN.SL3.fn_h>>3;e.OPN.SL3.kcode[u]=d<<2|a.fktable[p>>7];if(t.mode)e.OPN.SL3.fc[u]=e.OPN.fn.table[p<<1]>>7-d;else e.OPN.SL3.fc[u]=p<<6>>7-d;e.OPN.SL3.block_fnum[u]=d<<11|p;e.CH[2].SLOT[l[0]].Incr=-1}break;case 3:if(s<256)e.OPN.SL3.fn_h=o&63;break}break;case 176:switch(c){case 0:var v=o>>3&7;e.CH[u].ALGO=o&7;if(t.mode)e.CH[u].FB=v?v+6:0;else e.CH[u].FB=v;e.CH[u].setupConnection();break;case 1:e.CH[u].pms=(o&7)<<5;e.CH[u].ams=f.ams_depth_shift[o>>4&3];e.CH[u].pan[0]=o&128?n.bitmask:0;e.CH[u].pan[1]=o&64?n.bitmask:0;break}break}};a.SetPrescaler=function(e,r){e.ratio=r||144;e.OPN.ST.scale=e.OPN.ST.clock/e.OPN.ST.rate/e.ratio;if(t.debug)console.log("init_timetables",e.OPN.ST.clock,e.OPN.ST.rate,e.ratio,e.OPN.ST.scale);var i,o,u;var a=e.OPN.ST.scale*(1<<n.FREQ_SH-10);for(i=0;i<4;++i){for(o=0;o<32;++o){u=s.tab[(i<<5)+o];if(t.mode)u*=a;e.OPN.ST.dt_tab[i][o]=u|0;e.OPN.ST.dt_tab[i+4][o]=-e.OPN.ST.dt_tab[i][o]}}if(t.debug>2)console.log("init_timetables dt_tab",ym.OPN.ST.dt_tab);u=32*a,o=4096;while(--o>-1){e.OPN.fn.table[o]=o*u|0}e.OPN.fn.max=131072*a|0;e.OPN.eg.timer_add=e.OPN.ST.scale*(1<<n.EG_SH)|0;e.OPN.eg.timer_overflow=3*(1<<n.EG_SH);e.OPN.lfo.timer_add=e.OPN.ST.scale*(1<<n.LFO_SH)|0;e.OPN.ST.timer_base=e.OPN.ST.scale*(1<<n.TIMER_SH)|0};e.prototype.init=function(e,n){if(t.debug)console.log("OPN::init("+e+","+n+")");if(!this.chip)this.chip=new m(e,n);else this.chip.ST.clock=e||7670448,this.chip.ST.rate=n||44100;this.ratio=144;this.interval=t.mode;this.start=0;this.count=0;N(this.chip)};e.prototype.reset=function(){if(t.debug)console.log("OPN::reset");(function(e){var r;a.SetPrescaler(e,144);e.OPN.eg.timer=0;e.OPN.eg.cnt=0;e.OPN.lfo.timer_overflow=0;e.OPN.lfo.timer=0;e.OPN.lfo.cnt=0;e.OPN.lfo.AM=126;e.OPN.lfo.PM=0;e.OPN.ST.TAC=0;e.OPN.ST.TBC=0;e.OPN.SL3.key_csm=0;e.dacen=0;e.dacout=0;b(e,48);e.OPN.ST.TB=0;e.OPN.ST.TA=0;if(t.mode){e.OPN.ST.TBL=256<<n.TIMER_SH+4;e.OPN.ST.TAL=1024<<n.TIMER_SH}else{e.OPN.ST.TBL=256<<4;e.OPN.ST.TAL=1024}T(e,6);r=182;while(r>=180){if((r&3)!==3)a.WriteReg(e,r,192),a.WriteReg(e,r|256,192);--r}r=178;while(r>=30){if((r&3)!==3)a.WriteReg(e,r,0),a.WriteReg(e,r|256,0);--r}})(this.chip);this.start=0;this.count=0};e.prototype.write=function(e,n){if(t.debug>1)console.log("OPN::write",e.toString(16),n.toString(16));n&=255;this.chip.OPN.ST.address=e&511;var r=this.chip.OPN.ST.address;switch(r&496){case 32:switch(r){case 42:this.chip.dacout=(n-128|0)<<6;break;case 43:this.chip.dacen=!!(n&128);break;default:a.WriteMode(this.chip,r,n);break}break;default:a.WriteReg(this.chip,r,n);break}};e.prototype.read=function(e){return this.chip.OPN.ST.status&255};e.prototype.update=function(e){var n=e*this.ratio;if(t.debug)console.log("==== YM::update","samples="+e,"cycles="+n);var r=[[],[]],i,s,o;var u=!!(this.chip.OPN.ST.mode&192),a;var f=-1;while(++f<this.chip.CH.length){if(!u)this.chip.CH[f].refresh_fc_eg(this.chip);else if(this.chip.CH[f].canCSM){if(this.chip.CH[f].SLOT[l[0]].Incr===-1){this.chip.CH[f].SLOT[l[0]].refresh_fc_eg(this.chip,this.chip.OPN.SL3.fc[1],this.chip.OPN.SL3.kcode[1]);this.chip.CH[f].SLOT[l[1]].refresh_fc_eg(this.chip,this.chip.OPN.SL3.fc[2],this.chip.OPN.SL3.kcode[2]);this.chip.CH[f].SLOT[l[2]].refresh_fc_eg(this.chip,this.chip.OPN.SL3.fc[0],this.chip.OPN.SL3.kcode[0]);this.chip.CH[f].SLOT[l[3]].refresh_fc_eg(this.chip,this.chip.CH[f].fc,this.chip.CH[f].kcode)}}}t.debugArr.length=0;var c,h,p=this.chip.CH.length;f=-1;while(++f<e){s=0,o=0;a=!!(this.chip.OPN.SL3.key_csm&2);h=false;i=-1;while(++i<p){this.chip.CH[i].outputs.out=0;this.chip.CH[i].update_ssg_eg();if(this.chip.dacen&&this.chip.CH[i].canDAC&&!h)this.chip.CH[i].outputs.out+=this.chip.dacout,h=true;else this.chip.CH[i].calculate(this.chip);if(this.chip.CH[i].outputs.out>8192)this.chip.CH[i].outputs.out=8192;else if(this.chip.CH[i].outputs.out<-8192)this.chip.CH[i].outputs.out=-8192;if(!this.chip.CH[i].muted)s+=this.chip.CH[i].outputs.out&this.chip.CH[i].pan[0]|0,o+=this.chip.CH[i].outputs.out&this.chip.CH[i].pan[1]|0;if(a&&this.chip.CH[i].canCSM){this.chip.CH[i].keyOffCSM(this.chip,l[0]);this.chip.CH[i].keyOffCSM(this.chip,l[1]);this.chip.CH[i].keyOffCSM(this.chip,l[2]);this.chip.CH[i].keyOffCSM(this.chip,l[3])}}w(this.chip);if(t.mode){this.chip.OPN.eg.timer+=this.chip.OPN.eg.timer_add;while(this.chip.OPN.eg.timer>=this.chip.OPN.eg.timer_overflow){this.chip.OPN.eg.timer-=this.chip.OPN.eg.timer_overflow;++this.chip.OPN.eg.cnt;E(this.chip,this.chip.OPN.eg.cnt)}}else{++this.chip.OPN.eg.timer;if(this.chip.OPN.eg.timer>=3){this.chip.OPN.eg.timer=0;++this.chip.OPN.eg.cnt;E(this.chip,this.chip.OPN.eg.cnt)}}r[0][f]=s;r[1][f]=o;this.chip.OPN.SL3.key_csm<<=1;g(this.chip);if(a){this.chip.OPN.SL3.key_csm=0}}y(this.chip,e);if(t.debugArr.length>0)console.log(t.debugArr.join(", "));return r};e.prototype.config=function(e){n.bitmask=~((1<<u.BITS-e)-1);var t=this.chip.CH.length;while(--t>-1){if(this.chip.CH[t].pan[0])this.chip.CH[t].pan[0]=n.bitmask;if(this.chip.CH[t].pan[1])this.chip.CH[t].pan[1]=n.bitmask}};e.prototype.toggle=function(e,t){if(e<6)this.chip.CH[e].muted=!t};e.prototype.toString=function(){return this.chip.toString()};e.prototype.load=function(e){};e.prototype.save=function(e){}})(YM2612);