text: !function(e){var t={};function a(i){if(t[i])return t[i].exports;var s=t[i]={i:i,l:!1,exports:{}};return e[i].call(s.exports,s,s.exports,a),s.l=!0,s.exports}a.m=e,a.c=t,a.d=function(e,t,i){a.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:i})},a.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},a.t=function(e,t){if(1&t&&(e=a(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var i=Object.create(null);if(a.r(i),Object.defineProperty(i,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var s in e)a.d(i,s,function(t){return e[t]}.bind(null,s));return i},a.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return a.d(t,"a",t),t},a.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},a.p="",a(a.s=0)}([function(e,t,a){"use strict";function i(e,t){this.debug=!1,this.isStarted=!1,this.isPlayed=!1,this.isTonyu2=!0,this.settings={masterVolume:1,generateVolume:.15,tempo:120,basePitch:440,resolution:480,isWebMIDI:!1,WebMIDIPortOutputs:null,WebMIDIPortOutput:null,WebMIDIPort:-1,WebMIDIPortSysEx:!0,isReverb:!0,reverbVolume:1.5,isChorus:!0,chorusVolume:.5,isCC111:!0,loop:!1,isSkipBeginning:this.isTonyu2,isSkipEnding:!0,holdOnValue:64,maxPoly:-1,maxPercPoly:-1,isOfflineRendering:!1,isSameDrumSoundOverlap:!1},this.events=[],this.trigger={isNoteTrigger:!0,play:()=>{},stop:()=>{},noteOn:()=>{},noteOff:()=>{},songEnd:()=>{}},this.states={isPlaying:!1,startTime:0,stopTime:0,stopFuncs:[],webMIDIWaitState:null,webMIDIStopTime:0,playIndices:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],updateBufTime:100,updateBufMaxTime:350,updateIntervalTime:0,latencyLimitTime:0},this.hashedDataList=[],this.hashedMessageList=[],this.playData=null,this.channels=[],this.tempoTrack=[{timing:0,value:120},{timing:0,value:120}],this.cc111Time=-1,this.onSongEndListener=null;for(let e=0;e<17;e++)this.channels.push([0,0,1]);e&&this.init(e,t),"undefined"==typeof performance&&(window.performance={}),performance.now||(performance.now=()=>Date.now()),Number.MAX_SAFE_INTEGER||(Number.MAX_SAFE_INTEGER=9007199254740991)}a.r(t);class s{static resetSeed(){this.init=!0,this.x=123456789,this.y=362436069,this.z=521288629,this.w=8867512}static random(){this.init||this.resetSeed();let e=this.x^this.x<<11;this.x=this.y,this.y=this.z,this.z=this.w;let t=this.w=this.w^this.w>>>19^e^e>>>8;return t=Math.abs(t)/2147483648%2,t}}class n{static lerpWave(e,t){const a=e.getChannelData(0).length,i=t[0].length;if(a==i)for(let i=0;i<2;i++){const s=e.getChannelData(i),n=t[i];for(let e=0;e<a;e++)s[e]=n[e]}else{const s=i/a;for(let n=0;n<2;n++){const r=e.getChannelData(n),c=t[n];for(let e=0;e<a;e++){const t=e*s,a=Math.trunc(t),n=(a+1)%i,l=t-a,o=c[a]*(1-l)+c[n]*l;r[e]=o}}}}}function r(e,t){if(this.isStarted)return;this.isStarted=!0;let a=window.AudioContext||window.webkitAudioContext;this.context=e||new a,this.masterGainNode=this.context.createGain(),this.masterGainNode.gain.value=this.settings.masterVolume;const i=this.context.sampleRate,r=i>=48e3?48e3:i;if(t&&t.whitenoise)this.whitenoise=t.whitenoise;else{s.resetSeed();const e=1,t=i*e,a=r*e,c=[];for(let e=0;e<2;e++){c.push(new Float32Array(a));const t=c[e];for(let e=0;e<a;e++){let a=s.random();t[e]=2*a-1}}this.whitenoise=this.context.createBuffer(2,t,i),n.lerpWave(this.whitenoise,c)}if(t&&t.impulseResponse)this.impulseResponse=t.impulseResponse;else{s.resetSeed();const e=3.5,t=i*e,a=r*e,c=[];for(let e=0;e<2;e++){c.push(new Float32Array(a));const t=c[e];for(let e=0;e<a;e++){let i=(a-e)/a,n=e/r,c=(n<.03?0:i)*(n>=.03&&n<.031?2*i:i)*(n>=.04&&n<.042?1.5*i:i)*(n>=.05&&n<.054?1.25*i:i)*s.random()*.2*Math.pow(i-.03,4);t[e]=c}}this.impulseResponse=this.context.createBuffer(2,t,this.context.sampleRate),n.lerpWave(this.impulseResponse,c)}this.convolver=this.context.createConvolver(),this.convolver.buffer=this.impulseResponse,this.convolver.normalize=!0,this.convolverGainNode=this.context.createGain(),this.convolverGainNode.gain.value=this.settings.reverbVolume,this.convolver.connect(this.convolverGainNode),this.convolverGainNode.connect(this.masterGainNode),this.masterGainNode.connect(this.context.destination),this.chorusDelayNode=this.context.createDelay(),this.chorusGainNode=this.context.createGain(),this.chorusOscillator=this.context.createOscillator(),this.chorusLfoGainNode=this.context.createGain(),this.chorusDelayNode.delayTime.value=.025,this.chorusLfoGainNode.gain.value=.01,this.chorusOscillator.frequency.value=.05,this.chorusGainNode.gain.value=this.settings.chorusVolume,this.chorusOscillator.connect(this.chorusLfoGainNode),this.chorusLfoGainNode.connect(this.chorusDelayNode.delayTime),this.chorusDelayNode.connect(this.chorusGainNode),this.chorusGainNode.connect(this.masterGainNode),this.masterGainNode.connect(this.context.destination),this.chorusOscillator.start(0),this.isTonyu2&&(this.settings.isReverb=t?t.settings.isReverb:this.measurePerformanceReverb())}function c(e){if(this.debug)var t=performance.now();if(this.states.isPlaying&&this.stop(),this.playData=e,this.settings.resolution=e.header.resolution,this.settings.tempo=e.tempo||120,this.tempoTrack=e.tempoTrack,this.cc111Time=e.cc111Time,this.firstNoteOnTiming=e.firstNoteOnTiming,this.lastNoteOffTiming=e.lastNoteOffTiming,this.firstNoteOnTime=e.firstNoteOnTime,this.lastNoteOffTime=e.lastNoteOffTime,this.initStatus(),this.debug){const e=performance.now();console.log("setData time",e-t)}return this}function l(e,t){if((!this.settings.isWebMIDI||null==this.states.webMIDIWaitState)&&(this.stop(e),this.states={isPlaying:!1,startTime:0,stopTime:0,stopFuncs:[],webMIDIWaitState:null,webMIDIStopTime:this.states.webMIDIStopTime,playIndices:[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],updateBufTime:this.states.updateBufTime,updateBufMaxTime:this.states.updateBufMaxTime,updateIntervalTime:this.states.updateIntervalTime,latencyLimitTime:this.states.latencyLimitTime,noteOnAry:[],noteOffAry:[]},this.settings.isWebMIDI&&!t)){if(e)return;if(null==this.settings.WebMIDIPortOutput)return void this.startWebMIDI();if(this.settings.WebMIDIPortSysEx)this.settings.WebMIDIPortOutput.send([240,126,127,9,1,247]);else for(let e=0;e<16;e++)this.settings.WebMIDIPortOutput.send([192+e,0]),this.settings.WebMIDIPortOutput.send([224+e,0,64]),this.settings.WebMIDIPortOutput.send([176+e,100,0]),this.settings.WebMIDIPortOutput.send([176+e,101,0]),this.settings.WebMIDIPortOutput.send([176+e,6,2]),this.settings.WebMIDIPortOutput.send([176+e,100,1]),this.settings.WebMIDIPortOutput.send([176+e,96,0]),this.settings.WebMIDIPortOutput.send([176+e,97,64]),this.settings.WebMIDIPortOutput.send([176+e,7,100]),this.settings.WebMIDIPortOutput.send([176+e,10,64]),this.settings.WebMIDIPortOutput.send([176+e,11,127]),this.settings.WebMIDIPortOutput.send([176+e,98,0]),this.settings.WebMIDIPortOutput.send([176+e,99,0]),this.settings.WebMIDIPortOutput.send([176+e,122,0])}}class o extends Array{static delete(e,t){t==e.length-1?e.pop():0==t?e.shift():e.splice(t,1)}}class u{static getInt(e,t,a){let i=0;for(let s=t;s<a;s++)i=(i<<8)+e[s];return i}static variableLengthToInt(e,t,a){let i=t,s=0;for(;i<a-1&&e[i]>=128;)i<t+4&&(s=(s<<7)+(e[i]-128)),i++;return s=(s<<7)+e[i],i++,[s,i-t]}static chIndicesInsert(e,t,a,i,s){let n=t.indices;if(t.indicesLength>=4&&a<n[t.indicesFoot])for(;-1!=t.indicesCur;){if(a<n[t.indicesCur]){t.indicesCur==t.indicesHead?t.indicesHead=t.indicesLength:n[t.indicesPre+3]=t.indicesLength,n[t.indicesLength]=a,n[t.indicesLength+1]=s,n[t.indicesLength+2]=i,n[t.indicesLength+3]=t.indicesCur,t.indicesPre=t.indicesLength,t.indicesLength+=4;break}t.indicesPre=t.indicesCur,t.indicesCur=n[t.indicesCur+3]}else t.indicesLength>=4?n[t.indicesFoot+3]=t.indicesLength:t.indicesHead=0,t.indicesFoot=t.indicesLength,n[t.indicesLength]=a,n[t.indicesLength+1]=s,n[t.indicesLength+2]=i,n[t.indicesLength+3]=-1,t.indicesLength+=4}}class m{static init(e,t){this.updatePreTime=performance.now(),this.pPreTime=performance.now(),this.cPreTime=1e3*e.context.currentTime,this.pTimeSum=0,this.cTimeSum=0,this.cnt=0,this.initCurrentTime=t}static update(e){const t=e.context,a=e.settings,i=e.states;let s=performance.now(),n=this.updatePreTime,r=this.pPreTime,c=this.cPreTime,l=this.pTimeSum,o=this.cTimeSum,m=this.cnt,T=s-n;const p=s,h=1e3*t.currentTime;l+=p-r,o+=h-c,r=p,c=h;const g=l-o;if(i.latencyTime=g,g>=100?(i.latencyLimitTime+=g,o+=100):g<=-100?o=l:i.latencyLimitTime>0&&(i.latencyLimitTime-=.003*T,i.latencyLimitTime<0&&(i.latencyLimitTime=0)),i.updateIntervalTime=T,i.updateBufTime<T?i.updateBufTime=T:(i.updateBufMaxTime>350&&(i.updateBufMaxTime-=.002*i.updateBufMaxTime),i.updateBufTime<20&&(i.updateBufTime+=5e-4*i.updateBufTime),i.updateBufMaxTime>=10&&i.updateBufMaxTime<340&&(i.updateBufMaxTime+=.002*i.updateBufMaxTime)),i.updateBufTime>i.updateBufMaxTime){if(T>=900&&i.latencyLimitTime<=150)i.updateBufMaxTime+=T;else{const e=T-i.updateBufMaxTime;i.updateBufTime=i.updateBufMaxTime,i.updateBufMaxTime<10?(i.updateBufTime=i.updateBufMaxTime,i.updateBufMaxTime*=1.25):i.updateBufMaxTime+=e/2}i.updateBufMaxTime>1100&&(i.updateBufMaxTime=1100)}i.latencyLimitTime>150&&(o=l,i.latencyLimitTime-=5,i.latencyLimitTime>1e3&&(i.latencyLimitTime=1e3),i.updateBufMaxTime=1,i.updateBufTime=1,T=1);for(let s=0;s<16;s++){const n=e.playData.channels[s].notes;let r=i.playIndices[s];for(;r<n.length;r++){const s=n[r],c=0==m?this.initCurrentTime-i.startTime:t.currentTime-i.startTime;if(!(c>=s.stopTime)&&!(0==m&&c>s.startTime+.05||c+s.startTime<0)){if(c<s.startTime-i.updateBufTime/1e3)break;if(!a.isWebMIDI){if(i.stopFuncs.length>=350&&i.updateBufTime<1e3&&(i.updateBufTime=12,i.updateBufMaxTime=i.updateBufTime),-1!=a.maxPoly||-1!=a.maxPercPoly){let e=0,t=0;if(i.stopFuncs.forEach(a=>{a.note&&(9!=a.note.channel?s.start>=a.note.start&&s.start<a.note.stop&&e++:s.start==a.note.start&&t++)}),9!=s.channel&&e>=a.maxPoly||9==s.channel&&t>=a.maxPercPoly)continue}const t=9!=s.channel?e.createNote(s):e.createPercussionNote(s);if(!t)continue;e.pushFunc({note:s,stopFunc:t})}i.noteOnAry.push(s)}}i.playIndices[s]=r}if(this.checkNoteOn(e),this.checkNoteOff(e),a.isWebMIDI&&null!=a.WebMIDIPortOutput){const s=e.playData.messages,n=e.playData.smfData;let r=i.playIndices[16];for(;r<s.length;r++){const e=s[r],c=t.currentTime-i.startTime;if(c>e.time+1)continue;if(c<e.time-1)break;const l=e.smfPtrLen,o=e.smfPtr,m=e.time,T=n[o];if(255!=T)try{if(240==T||247==T){if(a.WebMIDIPortSysEx){const e=u.variableLengthToInt(n,o+1,o+1+4),s=o+1+e[1],r=s+e[0],c=new Uint8Array(1+e[0]);c[0]=T;const l=r-s;for(let e=0;e<l;e++)c[e+1]=n[s+e];a.WebMIDIPortOutput.send(c,1e3*(m-t.currentTime+window.performance.now()/1e3+i.startTime))}}else{const e=[];for(let t=0;t<l;t++)e.push(n[o+t]);a.WebMIDIPortOutput.send(e,1e3*(m-t.currentTime+window.performance.now()/1e3+i.startTime))}}catch(e){console.log(e,o,l,m,T)}}i.playIndices[16]=r}m++,this.updatePreTime=s,this.pPreTime=r,this.cPreTime=c,this.pTimeSum=l,this.cTimeSum=o,this.cnt=m}static checkNoteOn(e){const t=e.context,a=e.trigger,i=e.states,s=e.states.noteOnAry,n=e.states.noteOffAry;for(let r=0;r<s.length;r++){const c=s[r],l=t.currentTime-i.startTime;c.startTime-l<=0&&(o.delete(s,r),n.push(c),a.isNoteTrigger&&a.noteOn(c),e.fireEvent("noteOn",c),r--)}}static checkNoteOff(e){const t=e.context,a=e.trigger,i=e.states,s=e.states.noteOffAry;for(let n=0;n<s.length;n++){const r=s[n],c=t.currentTime-i.startTime;(9!=r.channel&&r.stopTime-c<=0||9==r.channel&&r.drumStopTime-c<=0)&&(o.delete(s,n),e.clearFunc("note",r),a.isNoteTrigger&&a.noteOff(r),e.fireEvent("noteOff",r),n--)}}}function T(e){const t=this.context,a=this.settings,i=this.trigger,s=this.states;if(s.isPlaying)return;if(a.isWebMIDI&&!e){if("completed"!=s.webMIDIWaitState){if("waiting"!=s.webMIDIWaitState){s.webMIDIWaitState="waiting";let e=1e3-1e3*(t.currentTime-s.webMIDIStopTime);0==s.webMIDIStopTime&&(e=1e3),setTimeout(()=>{s.webMIDIWaitState="completed",s.isPlaying=!1,this.play()},e)}return}s.webMIDIWaitState=null}const n=t.currentTime;if(this.isPlayed=!0,s.isPlaying=!0,s.startTime=s.startTime||s.stopTime?s.startTime+n-s.stopTime:n,s.stopFuncs=[],a.isSkipBeginning){const e=this.firstNoteOnTime;-s.startTime+n<e&&this.setStartTime(e+s.startTime-n)}let r;const c=()=>{this.clearFunc("rootTimeout",r),(a.isCC111&&-1!=this.cc111Time?this.lastNoteOffTime:this.getTime(Number.MAX_SAFE_INTEGER))-t.currentTime+s.startTime<=0?(i.songEnd(),this.onSongEnd(),this.fireEvent("songEnd")):(r=setTimeout(c,1),this.pushFunc({rootTimeout:r,stopFunc:()=>{clearTimeout(r)}}))},l=1e3*((a.isCC111&&-1!=this.cc111Time?this.lastNoteOffTime:this.getTime(Number.MAX_SAFE_INTEGER))-t.currentTime+s.startTime);r=setTimeout(c,l),this.pushFunc({rootTimeout:r,stopFunc:()=>{clearTimeout(r)}}),i.play(),this.fireEvent("play"),m.init(this,n);const o=setInterval(()=>{m.update(this)},1);this.pushFunc({rootTimeout:o,stopFunc:()=>{clearInterval(o)}})}function p(e){let t=this.states;if(0!=t.isPlaying){if(t.isPlaying=!1,t.stopTime=this.context.currentTime,t.stopFuncs.forEach(e=>{e.stopFunc()}),t.stopFuncs=[],t.playIndices.forEach((e,t,a)=>{a[t]=0}),t.noteOnAry=[],t.noteOffAry=[],this.settings.isWebMIDI){if(e)return;if(null==this.settings.WebMIDIPortOutput)return;t.webMIDIStopTime=this.context.currentTime,setTimeout(()=>{for(let e=0;e<16;e++)this.settings.WebMIDIPortOutput.send([176+e,120,0])},1e3)}this.trigger.stop(),this.fireEvent("stop")}}function h(e,t,a,i,s){const n=this.settings,r=this.context,c=this.states.startTime,l=i?0:e.channel||0,o=e.velocity*Number(i?1:null!=this.channels[l][2]?this.channels[l][2]:1)*n.generateVolume;let u=!0;if(o<=0)return{isGainValueZero:!0};const m=o*((e.expression?e.expression[0].value:100)/127),T=r.createGain();if(T.gain.value=m,a?e.expression&&e.expression.forEach(e=>{let t=o*(e.value/127);t>0&&(u=!1),T.gain.setValueAtTime(t,e.time+c)}):m>0&&(u=!1),u)return{isGainValueZero:!0};const p=e.startTime+c,h=e.stopTime+c,f=n.basePitch*Math.pow(Math.pow(2,1/12),(e.pitch||69)-69),A=t?r.createBufferSource():r.createOscillator(),d=r.createStereoPanner?r.createStereoPanner():r.createPanner?r.createPanner():{pan:{setValueAtTime:()=>{}}},V=r.createGain(),b=r.createGain();t?(A.loop=!0,A.buffer=this.whitenoise):(A.type=e.type||"sine",A.detune.value=0,A.frequency.value=f,e.pitchBend&&e.pitchBend.forEach(t=>{A.frequency.setValueAtTime(n.basePitch*Math.pow(Math.pow(2,1/12),e.pitch-69+t.value),t.time+c)}));const y=e.pan&&64!=e.pan[0].value?e.pan[0].value/127*2-1:0;if(function(e,t,a){if(e.createStereoPanner)a>1&&(a=1),t.pan.value=a;else if(e.createPanner){const e=g(a);t.panningModel="equalpower",t.setPosition(e.x,e.y,e.z)}}(r,d,y),r.createStereoPanner||r.createPanner){let t=!0;r.createStereoPanner?e.pan&&e.pan.forEach(e=>{if(t)return void(t=!1);let a=64==e.value?0:e.value/127*2-1;a>1&&(a=1),d.pan.setValueAtTime(a,e.time+c)}):r.createPanner&&(d.positionX?e.pan&&e.pan.forEach(e=>{if(firstPan)return void(firstPan=!1);const t=g(64==e.value?0:e.value/127*2-1);d.positionX.setValueAtTime(t.x,e.time+c),d.positionY.setValueAtTime(t.y,e.time+c),d.positionZ.setValueAtTime(t.z,e.time+c)}):e.pan&&e.pan.forEach(e=>{if(t)return void(t=!1);const a=setTimeout(()=>{this.clearFunc("pan",a);let t=64==e.value?0:e.value/127*2-1;t>1&&(t=1);let i=g(t);d.setPosition(i.x,i.y,i.z)},1e3*(e.time+c-r.currentTime));this.pushFunc({pan:a,stopFunc:()=>{clearTimeout(a)}})})),A.connect(d),d.connect(T)}else A.connect(T);let v,R;if(T.connect(V),V.connect(b),b.connect(this.masterGainNode),this.masterGainNode.connect(r.destination),!t&&e.modulation&&(e.modulation.length>=2||e.modulation[0].value>0)){v=r.createOscillator(),R=r.createGain();let t=!0;e.modulation&&e.modulation.forEach(e=>{if(t)return void(t=!1);let a=e.value/127;a>1&&(a=1),R.gain.setValueAtTime(10*f/440*a,e.time+c)});let a=e.modulation?e.modulation[0].value/127:0;a>1&&(a=1),R.gain.value=10*f/440*a,v.frequency.value=6,v.connect(R),R.connect(A.frequency)}if(this.settings.isReverb&&e.reverb&&(e.reverb.length>=2||e.reverb[0].value>0)){const t=this.convolver,a=r.createGain();let i=!0;e.reverb&&e.reverb.forEach(e=>{if(i)return void(i=!1);let t=e.value/127;t>1&&(t=1),a.gain.setValueAtTime(t,e.time+c)});let s=e.reverb?e.reverb[0].value/127:0;s>1&&(s=1),a.gain.value=s,V.connect(b),b.connect(a),a.connect(t)}if(this.settings.isChorus&&e.chorus&&(e.chorus.length>=2||e.chorus[0].value>0)){const t=this.chorusDelayNode,a=r.createGain();let i=!0;e.chorus&&e.chorus.forEach(e=>{if(i)return void(i=!1);let t=e.value/127;t>1&&(t=1),a.gain.setValueAtTime(t,e.time+c)});let s=e.chorus?e.chorus[0].value/127:0;s>1&&(s=1),a.gain.value=s,V.connect(b),b.connect(a),a.connect(t)}return v&&(v.start(p),this.stopAudioNode(v,h,R)),A.start(p),t||i||s||this.stopAudioNode(A,h,b),{start:p,stop:h,pitch:f,channel:l,velocity:o,oscillator:A,panNode:d,gainNode:V,stopGainNode:b,isGainValueZero:!1}}function g(e){e>1&&(e=1);const t={},a=90*e;return t.x=Math.sin(a*(Math.PI/180)),t.y=0,t.z=-Math.cos(a*(Math.PI/180)),t}function f(e){const t=this.createBaseNote(e,!1,!0,!1,!0);if(t.isGainValueZero)return null;const a=t.oscillator,i=t.gainNode,s=t.stopGainNode;let n,r=!1,c=!1;switch(1e3*this.channels[t.channel][0]||e.instrument){case 1e3:case 6:case 15:case 24:case 26:case 46:case 50:case 51:case 52:case 53:case 54:case 82:case 85:case 86:a.type="sine",i.gain.value*=1.5;break;case 2e3:case 4:case 12:case 13:case 16:case 19:case 20:case 32:case 34:case 45:case 48:case 49:case 55:case 56:case 57:case 61:case 62:case 63:case 71:case 72:case 73:case 74:case 75:case 76:case 77:case 78:case 79:case 80:case 84:a.type="square",i.gain.value*=.8;break;case 3e3:case 0:case 1:case 2:case 3:case 6:case 7:case 17:case 18:case 21:case 22:case 23:case 27:case 28:case 29:case 30:case 36:case 37:case 38:case 39:case 40:case 41:case 42:case 43:case 44:case 47:case 59:case 64:case 65:case 66:case 67:case 68:case 69:case 70:case 71:case 82:case 87:a.type="sawtooth";break;case 4e3:case 8:case 9:case 10:case 11:case 14:case 25:case 31:case 33:case 35:case 58:case 60:case 83:case 88:case 89:case 90:case 91:case 92:case 93:case 94:case 95:a.type="triangle",i.gain.value*=1.5;break;default:a.type="square"}switch(("sine"==a.type||"triangle"==a.type)&&!r&&t.stop-t.start>.01&&(c=!0),this.channels[t.channel][1]/10||e.instrument){case.2:case 12:case 13:case 45:case 55:r=!0,i.gain.value*=1.1,i.gain.setValueAtTime(i.gain.value,t.start),i.gain.linearRampToValueAtTime(0,t.start+.2),this.stopAudioNode(a,t.start+.2,s);break;case.3:case 0:case 1:case 2:case 3:case 6:case 9:case 11:case 14:case 15:case 32:case 36:case 37:case 46:case 47:{i.gain.value*=1.1;const n=(128-e.pitch)/128;i.gain.setValueAtTime(i.gain.value,t.start),i.gain.linearRampToValueAtTime(.85*i.gain.value,t.start+n*n/8),i.gain.linearRampToValueAtTime(.8*i.gain.value,t.start+n*n/4),i.gain.setTargetAtTime(0,t.start+n*n/4,5*n*n),this.stopAudioNode(a,t.stop,s,c);break}case.4:case 24:case 25:case 26:case 27:case 28:case 29:case 30:case 31:case 34:i.gain.value*=1.1,i.gain.setValueAtTime(i.gain.value,t.start),i.gain.linearRampToValueAtTime(0,t.start+1+4*t.velocity),this.stopAudioNode(a,t.stop,s,c);break;case.5:case 4:case 5:case 7:case 8:case 10:case 33:case 35:i.gain.value*=1,i.gain.setValueAtTime(i.gain.value,t.start),i.gain.linearRampToValueAtTime(.95*i.gain.value,t.start+.1),i.gain.setValueAtTime(.95*i.gain.value,t.start+.1),i.gain.linearRampToValueAtTime(0,t.start+2+10*t.velocity),this.stopAudioNode(a,t.stop,s,c);break;case 119:if(i.gain.value=0,this.stopAudioNode(a,t.stop,s,c),n=this.createBaseNote(e,!0,!0),n.isGainValueZero)break;n.oscillator.playbackRate.setValueAtTime((e.pitch+1)/128,t.start),n.gainNode.gain.setValueAtTime(0,t.start),n.gainNode.gain.linearRampToValueAtTime(1.3,t.start+2),this.stopAudioNode(n.oscillator,t.stop,n.stopGainNode);break;default:i.gain.value*=1.1,i.gain.setValueAtTime(i.gain.value,t.start),this.stopAudioNode(a,t.stop,s,c)}return()=>{this.stopAudioNode(a,0,s,!0),n&&n.oscillator&&this.stopAudioNode(n.oscillator,0,n.stopGainNode,!0)}}function A(e){const t=this.createBaseNote(e,!0,!1);if(t.isGainValueZero)return null;const a=t.oscillator,i=t.gainNode,s=t.stopGainNode;let n=t.start;const r=this.createBaseNote(e,!1,!1,!0),c=r.oscillator,l=r.gainNode,o=r.stopGainNode,u=e.nextSameNoteOnInterval;n<this.context.currentTime&&(n=this.context.currentTime);let m=0,T=0;switch(e.pitch){case 35:case 36:i.gain.value=.6,a.playbackRate.value=.02,m=.07,l.gain.value=1.1,c.frequency.setValueAtTime(120,n),c.frequency.linearRampToValueAtTime(50,n+.07),T=.07;break;case 38:case 40:a.playbackRate.value=.7,m=.05,l.gain.setValueAtTime(.8,n),l.gain.linearRampToValueAtTime(0,n+.05),c.frequency.setValueAtTime(300,n),c.frequency.linearRampToValueAtTime(200,n+.05),T=.05;break;case 41:case 43:case 45:case 47:case 48:case 50:a.playbackRate.value=.01,m=.1,c.type="square",l.gain.setValueAtTime(1,n),l.gain.linearRampToValueAtTime(.01,n+.1),c.frequency.setValueAtTime(150+20*(e.pitch-40),n),c.frequency.linearRampToValueAtTime(50+20*(e.pitch-40),n+.1),T=.1;break;case 42:case 44:a.playbackRate.value=1.5,m=.02,T=0;break;case 46:a.playbackRate.value=1.5,m=.3,i.gain.setValueAtTime(.9,n),i.gain.linearRampToValueAtTime(0,n+.3),T=0;break;case 49:case 51:case 52:case 53:case 55:case 57:a.playbackRate.value=1.2,m=.5,i.gain.setValueAtTime(1,n),i.gain.linearRampToValueAtTime(0,n+.5),T=0;break;case 51:a.playbackRate.value=1.1,m=.4,i.gain.setValueAtTime(.8,n),i.gain.linearRampToValueAtTime(0,n+.4),T=0;break;case 59:a.playbackRate.value=1.8,m=.3,i.gain.setValueAtTime(.5,n),i.gain.linearRampToValueAtTime(0,n+.3),T=0;break;case 60:case 61:a.playbackRate.value=.03,m=.03,l.gain.setValueAtTime(.8,n),l.gain.linearRampToValueAtTime(0,n+.1),c.frequency.setValueAtTime(400-40*(e.pitch-60),n),c.frequency.linearRampToValueAtTime(450-40*(e.pitch-60),n+.1),T=.1;break;case 62:a.playbackRate.value=.03,m=.03,l.gain.setValueAtTime(1,n),l.gain.linearRampToValueAtTime(0,n+.03),c.frequency.setValueAtTime(200,n),c.frequency.linearRampToValueAtTime(250,n+.03),T=.03;break;case 63:case 64:a.playbackRate.value=.03,m=.03,l.gain.setValueAtTime(1,n),l.gain.linearRampToValueAtTime(0,n+.1),c.frequency.setValueAtTime(200-30*(e.pitch-63),n),c.frequency.linearRampToValueAtTime(250-30*(e.pitch-63),n+.1),T=.1;break;case 56:case 75:a.playbackRate.value=.01,m=.1,l.gain.setValueAtTime(1,n),l.gain.linearRampToValueAtTime(0,n+.1),c.frequency.setValueAtTime(1e3+48*(e.pitch-56),n),T=.1;break;case 80:a.playbackRate.value=5,i.gain.setValueAtTime(.5,n),i.gain.linearRampToValueAtTime(0,n+.2),m=.05,c.type="triangle",l.gain.setValueAtTime(.7,n),l.gain.linearRampToValueAtTime(0,n+.2),c.frequency.setValueAtTime(6e3,n),T=.05;break;case 81:a.playbackRate.value=5,i.gain.setValueAtTime(.9,n),i.gain.linearRampToValueAtTime(0,n+.5),m=.5,c.type="triangle",l.gain.setValueAtTime(.8,n),l.gain.linearRampToValueAtTime(0,n+.3),c.frequency.setValueAtTime(6e3,n),T=.3;break;case 35:case 36:a.playbackRate.value=.25,i.gain.setValueAtTime(0,n),i.gain.linearRampToValueAtTime(.7,n+.004),i.gain.linearRampToValueAtTime(0,n+.008),m=.008,c.frequency.setValueAtTime(35==e.pitch?90:160,n),c.frequency.linearRampToValueAtTime(40,n+.08),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(3,n+.02),l.gain.linearRampToValueAtTime(0,n+.08),T=.08;break;case 37:a.playbackRate.value=.26,i.gain.setValueAtTime(1.5,n),i.gain.linearRampToValueAtTime(0,n+.041),m=.041,c.frequency.setValueAtTime(330,n),c.frequency.linearRampToValueAtTime(120,n+.02),l.gain.setValueAtTime(1,n),l.gain.linearRampToValueAtTime(0,n+.02),T=.02;break;case 38:case 40:{const t=38==e.pitch?.25:.2;a.playbackRate.value=.7,i.gain.setValueAtTime(1,n),i.gain.linearRampToValueAtTime(0,n+t),m=t,c.frequency.setValueAtTime(38==e.pitch?140:200,n),c.frequency.linearRampToValueAtTime(38==e.pitch?100:160,n+.1),l.gain.setValueAtTime(2,n),l.gain.linearRampToValueAtTime(0,n+.1),T=.1;break}case 39:a.playbackRate.value=.5,i.gain.setValueAtTime(1.3,n),i.gain.linearRampToValueAtTime(0,n+.01),i.gain.setValueAtTime(1.3,n+.0101),i.gain.linearRampToValueAtTime(0,n+.02),i.gain.setValueAtTime(1.3,n+.0201),i.gain.linearRampToValueAtTime(0,n+.09),m=.09,c.type="triangle",c.frequency.setValueAtTime(180,n),l.gain.setValueAtTime(.8,n),l.gain.linearRampToValueAtTime(0,n+.01),l.gain.setValueAtTime(.8,n+.0101),l.gain.linearRampToValueAtTime(0,n+.02),l.gain.setValueAtTime(.8,n+.0201),l.gain.linearRampToValueAtTime(0,n+.03),T=.11;break;case 41:case 43:case 45:case 47:case 48:case 50:{const t=e.pitch-41+(e.pitch>=48?1:0);a.playbackRate.value=.3+t/45,i.gain.setValueAtTime(1.5,n),i.gain.linearRampToValueAtTime(0,n+.02),m=.02,c.frequency.setValueAtTime(90+15*t,n),c.frequency.linearRampToValueAtTime(30+7.5*t,n+.5-t/35),l.gain.setValueAtTime(1.5,n),l.gain.linearRampToValueAtTime(0,n+.5-t/35),T=.5-t/35;break}case 42:case 44:a.playbackRate.value=1,42==e.pitch?i.gain.setValueAtTime(.8,n):(i.gain.setValueAtTime(0,n),i.gain.linearRampToValueAtTime(.8,n+.014)),i.gain.linearRampToValueAtTime(0,n+.08),m=.08,l.gain.value=0,T=0;break;case 46:a.playbackRate.setValueAtTime(.35,n),a.playbackRate.linearRampToValueAtTime(.6,n+.1),a.playbackRate.linearRampToValueAtTime(1,n+.3),i.gain.setValueAtTime(1.1,n),i.gain.setTargetAtTime(0,n,.3),m=1.5,l.gain.value=0,T=0;break;case 49:case 57:{const t=49==e.pitch?.3:.5,s=49==e.pitch?.4:.7;a.playbackRate.setValueAtTime(t,n),a.playbackRate.linearRampToValueAtTime(s,n+.15),a.playbackRate.linearRampToValueAtTime(.9,n+.4),i.gain.setValueAtTime(1.3,n),i.gain.setTargetAtTime(0,n,.35),m=2,l.gain.value=0,T=0;break}case 51:case 59:{a.playbackRate.value=1,i.gain.setValueAtTime(.9,n),i.gain.setTargetAtTime(0,n,.35),m=2,c.type="triangle";const t=51==e.pitch?372:400;c.frequency.setValueAtTime(t,n),l.gain.setValueAtTime(.4,n),l.gain.setTargetAtTime(0,n,.35),T=2;break}case 52:a.playbackRate.setValueAtTime(.17,n),a.playbackRate.linearRampToValueAtTime(.25,n+.1),a.playbackRate.linearRampToValueAtTime(.5,n+.6),i.gain.setValueAtTime(1.3,n),i.gain.setTargetAtTime(0,n,.35),m=2,c.type="triangle",c.frequency.setValueAtTime(382,n),l.gain.setValueAtTime(.2,n),l.gain.setTargetAtTime(0,n,.35),T=2;break;case 53:a.playbackRate.setValueAtTime(.6,n),i.gain.setValueAtTime(1,n),i.gain.setTargetAtTime(0,n,.3),m=2,c.type="triangle",c.frequency.setValueAtTime(377,n),l.gain.setValueAtTime(.5,n),l.gain.setTargetAtTime(0,n,.35),T=2;break;case 55:a.playbackRate.setValueAtTime(.5,n),a.playbackRate.linearRampToValueAtTime(.8,n+.1),a.playbackRate.linearRampToValueAtTime(1,n+.6),i.gain.setValueAtTime(1.5,n),i.gain.setTargetAtTime(0,n,.3),m=1.75,l.gain.value=0,T=0;break;case 54:case 56:{a.playbackRate.setValueAtTime(1,n);let t=54==e.pitch?1:.4;const s=54==e.pitch?.01:0;i.gain.setValueAtTime(1*t/2,n),i.gain.linearRampToValueAtTime(1*t,n+s),i.gain.setTargetAtTime(0,n+s,.05),m=.3,c.frequency.setValueAtTime(54==e.pitch?6e3:495,n),t=54==e.pitch?1:2,l.gain.setValueAtTime(1*t/2,n),l.gain.linearRampToValueAtTime(1*t,n+s),l.gain.setTargetAtTime(0,n+s,.05),T=.3;break}case 58:{a.playbackRate.setValueAtTime(.6,n),a.playbackRate.linearRampToValueAtTime(1,n+.8);const e=40;i.gain.setValueAtTime(1.5,n),l.gain.setValueAtTime(.5,n);for(let t=0;t<e;t++)i.gain.linearRampToValueAtTime(.1*(e-t)/e,n+t/e*.8),i.gain.linearRampToValueAtTime(1.5*(e-(t+1))/e,n+(t+.99)/e*.8),l.gain.linearRampToValueAtTime(.025*(e-t)/e,n+t/e*.8),l.gain.linearRampToValueAtTime(.25*(e-(t+1))/e,n+(t+.99)/e*.8);i.gain.linearRampToValueAtTime(0,n+.8),l.gain.linearRampToValueAtTime(0,n+.8),m=.8,c.type="triangle",c.frequency.setValueAtTime(1e3,n),T=.8;break}case 80:a.playbackRate.value=1,i.gain.setValueAtTime(.5,n),i.gain.setTargetAtTime(0,n,.015),m=.2,c.type="triangle",c.frequency.setValueAtTime(6e3,n),l.gain.setValueAtTime(2.5,n),l.gain.setTargetAtTime(0,n,.02),T=.3;break;case 81:a.playbackRate.value=5,i.gain.setValueAtTime(.5,n),i.gain.setTargetAtTime(0,n,.08),m=.75,c.type="triangle",c.frequency.setValueAtTime(6e3,n),l.gain.setValueAtTime(2.5,n),l.gain.setTargetAtTime(0,n,.18),T=1;break;case 60:case 61:case 62:case 63:case 64:{const t=e.pitch,s=60==t?700:61==t?282:62==t?385:63==t?295:210,r=60==t?.08:61==t?.1:62==t?.03:63==t?.12:.15;a.playbackRate.value=.03,i.gain.setValueAtTime(1.2,n),m=.03,c.frequency.setValueAtTime(.97*s,n),c.frequency.linearRampToValueAtTime(s,n+r),l.gain.setValueAtTime(1.8,n),l.gain.linearRampToValueAtTime(0,n+r),T=r;break}case 65:case 66:{const t=65==e.pitch?.22:.25;a.playbackRate.setValueAtTime(65==e.pitch?.25:.22,n),a.playbackRate.linearRampToValueAtTime(65==e.pitch?.2:.18,n+t),i.gain.setValueAtTime(1.3,n),i.gain.linearRampToValueAtTime(.2,n+t/3.5),i.gain.linearRampToValueAtTime(0,n+t),m=t,c.type="triangle",c.frequency.setValueAtTime(65==e.pitch?203.3:145.52,n),c.frequency.linearRampToValueAtTime(65==e.pitch?190:136,n+.1),l.gain.setValueAtTime(3.2,n),l.gain.setTargetAtTime(0,n,.08),T=1;break}case 67:case 68:a.playbackRate.value=1,i.gain.setValueAtTime(.5,n),i.gain.linearRampToValueAtTime(.1,n+.02),i.gain.linearRampToValueAtTime(0,n+.08),m=.08,c.type="triangle",c.frequency.setValueAtTime(67==e.pitch?1430:1055,n),l.gain.setValueAtTime(2,n),l.gain.setTargetAtTime(0,n,.06),T=.75;break;case 69:a.playbackRate.value=1,i.gain.setValueAtTime(.3,n),i.gain.linearRampToValueAtTime(.8,n+.03),i.gain.linearRampToValueAtTime(0,n+.08),m=.08,l.gain.value=0,T=0;break;case 70:a.playbackRate.value=1,i.gain.setValueAtTime(1.2,n),i.gain.linearRampToValueAtTime(0,n+.06),m=.06,l.gain.value=0,T=0;break;case 71:case 72:{i.gain.value=0,m=0;const t=71==e.pitch?.07:.4;c.type="triangle",c.frequency.setValueAtTime(71==e.pitch?2408:2105,n),l.gain.setValueAtTime(0,n);for(let e=0;e<74*t;e++)l.gain.linearRampToValueAtTime(2.5,n+(e+.2)/75),l.gain.linearRampToValueAtTime(.5,n+(e+.9)/75);l.gain.linearRampToValueAtTime(0,n+t),T=t;break}case 73:case 74:{const t=73==e.pitch?.05:.35;a.playbackRate.setValueAtTime((e.pitch,.2),n),a.playbackRate.linearRampToValueAtTime(73==e.pitch?.7:.5,n+t),i.gain.value=.2;for(let e=0;e<100*t;e++)i.gain.setValueAtTime(.4,n+e/100),i.gain.setValueAtTime(.9,n+(e+.7)/100);m=t,l.gain.value=0,T=0;break}case 75:i.gain.value=0,m=0,c.frequency.setValueAtTime(2181,n),l.gain.setValueAtTime(0,n),l.gain.setValueAtTime(2,n+.005),l.gain.linearRampToValueAtTime(1,n+.015),l.gain.linearRampToValueAtTime(1.5,n+.025),l.gain.linearRampToValueAtTime(0,n+.08),T=.1;break;case 76:case 77:a.playbackRate.value=.1,i.gain.setValueAtTime(1.2,n),i.gain.linearRampToValueAtTime(0,n+.015),m=.015,c.frequency.setValueAtTime(76==e.pitch?800:600,n),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(3,n+.005),l.gain.setTargetAtTime(0,n+.005,.02),T=.2;break;case 78:case 79:{i.gain.value=0,m=0;const t=.18,a=78==e.pitch?750:270;c.frequency.setValueAtTime(a,n),c.frequency.linearRampToValueAtTime(a,n+t/3),78==e.pitch&&c.frequency.linearRampToValueAtTime(.9*a,n+t),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(1.5,n+.005),l.gain.linearRampToValueAtTime(.5,n+.02),l.gain.linearRampToValueAtTime(3,n+.04),l.gain.linearRampToValueAtTime(2,n+t/4*3),l.gain.linearRampToValueAtTime(0,n+t),T=t;break}case 27:a.playbackRate.value=1,i.gain.setValueAtTime(1,n),i.gain.linearRampToValueAtTime(0,n+.002),m=.002,c.frequency.setValueAtTime(1500,n),c.frequency.linearRampToValueAtTime(280,n+.015),c.frequency.linearRampToValueAtTime(0,n+.07),l.gain.setValueAtTime(1.9,n),l.gain.linearRampToValueAtTime(0,n+.07),T=.07;break;case 28:a.playbackRate.value=1,i.gain.setValueAtTime(1.3,n),i.gain.linearRampToValueAtTime(0,n+.01),i.gain.setValueAtTime(1.1,n+.0101),i.gain.linearRampToValueAtTime(0,n+.02),i.gain.setValueAtTime(.9,n+.0201),i.gain.setTargetAtTime(0,n+.0201,.03),m=.2,l.gain.value=0,T=0;break;case 29:case 30:{const t=29==e.pitch?.05:.07,s=29==e.pitch?.06:.09,r=29==e.pitch?.07:.11,o=29==e.pitch?.1:.15,u=29==e.pitch?.25:.4,p=29==e.pitch?.1:.06,h=29==e.pitch?.3:.2,g=29==e.pitch?.18:.12;a.playbackRate.setValueAtTime(p,n),a.playbackRate.linearRampToValueAtTime(h,n+t),a.playbackRate.linearRampToValueAtTime(0,n+s),a.playbackRate.linearRampToValueAtTime(h,n+r),a.playbackRate.linearRampToValueAtTime(g,n+o),a.playbackRate.linearRampToValueAtTime(0,n+u),i.gain.setValueAtTime(0,n),i.gain.linearRampToValueAtTime(.4,n+t),i.gain.linearRampToValueAtTime(.1,n+r),i.gain.linearRampToValueAtTime(.3,n+o),i.gain.linearRampToValueAtTime(0,n+u),m=u;const f=29==e.pitch?500:400,A=29==e.pitch?1950:1200,d=29==e.pitch?430:250;c.frequency.setValueAtTime(f,n),c.frequency.linearRampToValueAtTime(A,n+t),c.frequency.linearRampToValueAtTime(0,n+s),c.frequency.linearRampToValueAtTime(A,n+r),c.frequency.linearRampToValueAtTime(d,n+o),c.frequency.linearRampToValueAtTime(0,n+u),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(.7,n+t),l.gain.linearRampToValueAtTime(.2,n+r),l.gain.linearRampToValueAtTime(.6,n+o),l.gain.linearRampToValueAtTime(0,n+u),T=u;break}case 31:a.playbackRate.setValueAtTime(.4,n),a.playbackRate.linearRampToValueAtTime(.5,n+.015),i.gain.setValueAtTime(1.2,n),i.gain.setTargetAtTime(0,n,.035),m=.3,c.frequency.setValueAtTime(3140,n),l.gain.setValueAtTime(1.2,n),l.gain.setTargetAtTime(0,n,.012),T=.3;break;case 32:i.gain.value=0,m=0,c.type="square",c.frequency.setValueAtTime(333,n),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(4,n+.0016),l.gain.linearRampToValueAtTime(0,n+.0032),T=.0032;break;case 33:case 34:a.playbackRate.setValueAtTime(.17,n),a.playbackRate.linearRampToValueAtTime(.22,n+.01),i.gain.setValueAtTime(1.5,n),i.gain.setTargetAtTime(0,n,.015),m=.3,34==e.pitch?(c.frequency.setValueAtTime(2040,n),l.gain.setValueAtTime(1,n),l.gain.setTargetAtTime(0,n,.12),T=1.1):(l.gain.value=0,T=0);break;case 82:a.playbackRate.value=1,i.gain.setValueAtTime(.5,n),i.gain.linearRampToValueAtTime(1,n+.02),i.gain.linearRampToValueAtTime(0,n+.07),m=.07,l.gain.value=0,T=0;break;case 83:a.playbackRate.value=1,i.gain.setValueAtTime(0,n),i.gain.linearRampToValueAtTime(1.2,n+.015),i.gain.setTargetAtTime(0,n+.015,.06),m=.5,c.type="triangle",c.frequency.setValueAtTime(2709,n),c.frequency.linearRampToValueAtTime(2657,n+.3),l.gain.setValueAtTime(0,n),l.gain.linearRampToValueAtTime(.7,n+.025),l.gain.setTargetAtTime(0,n+.025,.07),T=.5;break;case 84:{const e=!1;a.playbackRate.value=1;for(let t=0;t<28;t++)i.gain.setValueAtTime(.1,n+t/24*.45),i.gain.setTargetAtTime(0,n+t/24*.45,.01),c.frequency.setValueAtTime(1380*(1+(e?(24-t)/24:t/24)),n+t/24*.45),l.gain.setValueAtTime(1*(.2+t/24),n+t/24*.45),l.gain.setTargetAtTime(0,n+t/24*.45,27==t?.2:.01);m=.5,T=1.5;break}case 85:a.playbackRate.setValueAtTime(.35,n),i.gain.setValueAtTime(1.3,n),i.gain.setTargetAtTime(0,n,.01),m=.1,c.frequency.setValueAtTime(1730,n),l.gain.setValueAtTime(.5,n),l.gain.setTargetAtTime(0,n,.01),T=.1;break;case 86:case 87:a.playbackRate.setValueAtTime(.02,n),a.playbackRate.linearRampToValueAtTime(.015,n+.5),i.gain.setValueAtTime(0,n),i.gain.linearRampToValueAtTime(2,n+.005),i.gain.setTargetAtTime(0,n+.005,86==e.pitch?.03:.06),m=.5,c.frequency.setValueAtTime(88,n),c.frequency.linearRampToValueAtTime(86,n+.3),l.gain.setValueAtTime(2.5,n),l.gain.setTargetAtTime(0,n,86==e.pitch?.1:.3),T=86==e.pitch?.5:1.5;break;default:a.playbackRate.value=e.pitch/69*2,m=.05,T=0}return this.settings.isSameDrumSoundOverlap||-1==u||(m>u&&(m=u),T>u&&(T=u)),this.stopAudioNode(a,n+m,s),this.stopAudioNode(c,n+T,o),e.drumStopTime=e.startTime+(m>=T?m:T),()=>{this.stopAudioNode(a,0,s,!0),this.stopAudioNode(c,0,o,!0)}}function d(e,t,a,i){let s=t-.005,n=t;t<=this.context.currentTime&&(i?(s=this.context.currentTime,n=this.context.currentTime+.005):n=this.context.currentTime);try{i?(e.stop(n),a.gain.cancelScheduledValues(0),a.gain.setValueAtTime(1,s),a.gain.linearRampToValueAtTime(0,n)):e.stop(n)}catch(e){a.gain.cancelScheduledValues(0),i?(a.gain.setValueAtTime(1,s),a.gain.linearRampToValueAtTime(0,n)):a.gain.setValueAtTime(0,n)}}function V(e){(e.note||e.rootTimeout||e.pan||this.trigger.isNoteTrigger)&&this.states.stopFuncs.push(e)}function b(e,t){("note"==e||"rootTimeout"==e||"pan"==e||this.trigger.isNoteTrigger)&&this.states.stopFuncs.some((a,i,s)=>{if(a[e]==t)return o.delete(s,i),!0})}function y(e){let t=-1;if(this.tempoTrack&&this.tempoTrack.length>=1){if(e>=this.tempoTrack[this.tempoTrack.length-1].timing)return this.tempoTrack[this.tempoTrack.length-1].time;let a=0,i=this.tempoTrack.length-1;for(;;){t=Math.floor(a+(i-a)/2);const s=this.tempoTrack[t].timing;if(e<s)i=t-1;else{if(!(e>s))break;a=t+1}if(a>i){e<s&&t--;break}}}let a=0,i=0,s=120;if(t>=0){const e=this.tempoTrack[t];a=e.time,i=e.timing,s=e.value}return a+=60/s/this.settings.resolution*(e-i),a}function v(e){let t=-1;if(this.tempoTrack&&this.tempoTrack.length>=1){if(e>=this.tempoTrack[this.tempoTrack.length-1].time)return this.tempoTrack[this.tempoTrack.length-1].timing;let a=0,i=this.tempoTrack.length-1;for(;;){t=Math.floor(a+(i-a)/2);const s=this.tempoTrack[t].time;if(e<s)i=t-1;else{if(!(e>s))break;a=t+1}if(a>i){e<s&&t--;break}}}let a=0,i=0,s=120;if(t>=0){const e=this.tempoTrack[t];a=e.time,i=e.timing,s=e.value}return i+=(e-a)/(60/s/this.settings.resolution),i}function R(e){const t=e.smf;let a=4;const i={};i.size=u.getInt(t,4,8),i.format=t[9],i.trackcount=u.getInt(t,10,12),i.timemanage=t[12],i.resolution=u.getInt(t,12,14),a+=4+i.size;const s=[],n=this.settings.isWebMIDI?17:16;for(let e=0;e<n;e++){const e={};s.push(e),e.indices=[],e.indicesLength=0,e.indicesHead=-1,e.indicesFoot=0,e.indicesCur=0,e.indicesPre=0,e.notes=[]}return e.p=a,e.header=i,e.channels=s,e}function k(e){const t=e.smf;let a=e.p;const i=e.header,s=e.channels,n=[],r=[];let c=0;for(let e=0;e<i.trackcount;e++){if(77!=t[a]||84!=t[a+1]||114!=t[a+2]||107!=t[a+3])return"Irregular SMF.";a+=4;const e=a+4+u.getInt(t,a,a+4);a+=4;let l,o=0,m=120,T=0,p=0,h=1;for(;a<e;){if(null!=h){let e=u.variableLengthToInt(t,a,a+5);l=e[0],o+=l,a+=e[1]}let e=a;switch(t[a]>>4){case 8:case 9:case 10:case 11:case 14:{h=t[a];const e=s[15&h];u.chIndicesInsert(this,e,o,a,3),a+=3;break}case 12:case 13:{h=t[a];const e=s[15&h];u.chIndicesInsert(this,e,o,a,2),a+=2;break}case 15:switch(t[a]){case 240:case 247:{const e=u.variableLengthToInt(t,a+1,a+1+4);if(e[0]>=7&&127==t[a+2]&&127==t[a+3]&&4==t[a+4]&&1==t[a+5])for(let t=0;t<16;t++){const i=s[t];u.chIndicesInsert(this,i,o,a,e[0])}a+=1+e[1]+e[0];break}case 241:a+=2;break;case 242:a+=3;break;case 243:a+=2;break;case 246:case 248:case 250:case 251:case 252:case 254:a+=1;break;case 255:{switch(t[a+1]){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:case 32:break;case 47:o+=(this.settings.isSkipEnding?0:i.resolution)-l;break;case 81:for(let e=0;e<16;e++){const t=s[e];u.chIndicesInsert(this,t,o,a,6)}p+=60/m/i.resolution*(o-T),T=o,m=6e7/(65536*t[a+3]+256*t[a+4]+t[a+5]),n.push({timing:o,time:p,value:m});break;case 84:break;case 88:r.push({timing:o,value:[t[a+3],Math.pow(2,t[a+4])]})}const e=u.variableLengthToInt(t,a+2,a+2+4);a+=2+e[1]+e[0];break}}break;default:if(null==h)return"Irregular SMF. ("+a+" byte addr)";a--,t[a]=h,h=null}this.settings.isWebMIDI&&null!=h&&u.chIndicesInsert(this,s[16],o,e,a-e)}!this.settings.isSkipEnding&&c<o&&(c=o);for(let e=0;e<s.length;e++)s[e].indicesCur=s[e].indicesHead,s[e].indicesPre=s[e].indicesHead}return e.p=a,e.tempoTrack=n,e.beatTrack=r,e.songLength=c,e}function I(e){const t=e.smf,a=e.header,i=e.channels,s=e.tempoTrack;let n,r,c,l=e.songLength,u=-1,m=-1,T=Number.MAX_SAFE_INTEGER,p=Number.MAX_SAFE_INTEGER,h=0,g=0;for(let e=0;e<16;e++){const s=i[e];let l=2,f=0,A=64,d=127,V=100,b=0,y=0,v=this.isTonyu2?0:10,R=0,k=127,I=127,M=127,N=127,P=0,O=127;n=120,r=0,c=0;const D=[];let S=s.indicesHead;const x=s.indices,w=new Array(128);for(;-1!=S;){const i=x[S],q=x[S+2],E=x[S+3],W=60/n/a.resolution*(i-r)+c,L=t[q]>>4;switch(L){case 8:case 9:if(9==L&&0!=t[q+2]){const a={start:i,stop:null,startTime:W,stopTime:null,pitch:t[q+1],pitchBend:[{timing:i,time:W,value:f}],pan:[{timing:i,time:W,value:A}],expression:[{timing:i,time:W,value:d*(O/127)}],velocity:t[q+2]/127*(V/127),modulation:[{timing:i,time:W,value:b}],holdBeforeStop:null,reverb:[{timing:i,time:W,value:v}],chorus:[{timing:i,time:W,value:R}],instrument:P,channel:e,nextSameNoteOnInterval:-1,drumStopTime:2},n=w[t[q+1]];n&&(n.nextSameNoteOnInterval=W-n.startTime),w[t[q+1]]=a,D.some((e,a)=>{const n=s.notes[e];n.pitch==t[q+1]&&null==n.stop&&(n.stop=i,n.stopTime=W,o.delete(D,a))}),D.push(s.notes.length),s.notes.push(a),i<T&&(T=i,p=W)}else D.some((e,a)=>{const n=s.notes[e];if(n.pitch==t[q+1]&&null==n.stop)return y>=this.settings.holdOnValue?null==n.holdBeforeStop&&(n.holdBeforeStop=[{timing:i,time:W,value:y}]):(n.stop=i,n.stopTime=W,o.delete(D,a)),i>h&&(h=i,g=W),!0});break;case 10:break;case 11:switch(t[q+1]){case 1:b=t[q+2],D.forEach(e=>{s.notes[e].modulation.push({timing:i,time:W,value:b})});break;case 6:0==M&&0==N&&(l=t[q+2],l>24&&(l=24));break;case 7:V=t[q+2];break;case 10:A=t[q+2],D.forEach(e=>{s.notes[e].pan.push({timing:i,time:W,value:A})});break;case 11:d=t[q+2],D.forEach(e=>{s.notes[e].expression.push({timing:i,time:W,value:d*(O/127)})});break;case 64:if(y=t[q+2],y<this.settings.holdOnValue)for(let e=D.length-1;e>=0;e--){const t=D[e],a=s.notes[t];null==a.stop&&null!=a.holdBeforeStop&&(a.stop=i,a.stopTime=W,o.delete(D,e))}break;case 91:v=t[q+2],D.forEach(e=>{s.notes[e].reverb.push({timing:i,time:W,value:v})});break;case 93:R=t[q+2],D.forEach(e=>{s.notes[e].chorus.push({timing:i,time:W,value:R})});break;case 98:k=t[q+2];break;case 99:I=t[q+2];break;case 100:M=t[q+2];break;case 101:N=t[q+2];break;case 111:-1==u&&(u=i,m=W)}break;case 12:P=t[q+1];break;case 13:break;case 14:f=(128*t[q+2]+t[q+1]-8192)/8192*l,D.forEach(e=>{s.notes[e].pitchBend.push({timing:i,time:W,value:f})});break;case 15:switch(t[q]){case 240:case 247:if(127==t[q+1]&&127==t[q+2]&&4==t[q+3]&&1==t[q+4]){let e=t[q+6];e>127&&(e=127),O=e,D.forEach(e=>{s.notes[e].expression.push({timing:i,time:W,value:d*(O/127)})})}break;case 255:switch(t[q+1]){case 81:c+=60/n/a.resolution*(i-r),r=i,n=6e7/(65536*t[q+3]+256*t[q+4]+t[q+5])}}break;default:return"Error parseSMF. "}S=E}s.nowNoteOnIdxAry=D,this.debug||delete s.indices}for(let e=0;e<16;e++){const t=i[e],a=t.nowNoteOnIdxAry;for(let e=a.length-1;e>=0;e--){const i=t.notes[a[e]];if(null==i.stop){i.stop=h,i.stopTime=g,["pitchBend","pan","expression","modulation","reverb","chorus"].forEach(e=>{const t=i[e];for(let e=t.length-1;e>=1;e--){t[e].timing>h&&o.delete(t,e)}}),o.delete(a,e)}}delete t.nowNoteOnIdxAry}this.settings.isSkipEnding&&(l=h),s.push({timing:l,time:60/n/a.resolution*(l-r)+c,value:120});const f=[];if(this.settings.isWebMIDI){const e=i[16];let s=120,n=0,r=0,c=e.indicesHead;const l=e.indices;for(;-1!=c;){const e=l[c],i=l[c+1],o=l[c+2],u=l[c+3],m=60/s/a.resolution*(e-n)+r;switch(t[o]){case 255:switch(t[o+1]){case 81:r+=60/s/a.resolution*(e-n),n=e,s=6e7/(65536*t[o+3]+256*t[o+4]+t[o+5])}}f.push({time:m,tick:e,smfPtr:o,smfPtrLen:i}),c=u}}return e.songLength=l,e.cc111Tick=u,e.cc111Time=m,e.firstNoteOnTiming=T,e.firstNoteOnTime=p,e.lastNoteOffTiming=h,e.lastNoteOffTime=g,this.settings.isWebMIDI&&(e.messages=f,e.smfData=new Uint8Array(t)),e}function M(e){if(this.debug){console.log(e);var t=performance.now()}const a=new Uint8Array(e);if(77!=a[0]||84!=a[1]||104!=a[2]||100!=a[3])return"Not Sandard MIDI File.";const i={};if(i.smf=a,R.call(this,i),this.debug)var s=performance.now();if(k.call(this,i),this.debug)var n=performance.now();I.call(this,i);const r={};if(r.header=i.header,r.tempoTrack=i.tempoTrack,r.beatTrack=i.beatTrack,r.channels=i.channels,r.songLength=i.songLength,r.cc111Tick=i.cc111Tick,r.cc111Time=i.cc111Time,r.firstNoteOnTiming=i.firstNoteOnTiming,r.firstNoteOnTime=i.firstNoteOnTime,r.lastNoteOffTiming=i.lastNoteOffTiming,r.lastNoteOffTime=i.lastNoteOffTime,this.settings.isWebMIDI&&(r.messages=i.messages,r.smfData=new Uint8Array(a)),this.debug){const e=performance.now();console.log("parseSMF time",e-t),console.log("parseSMF(0/2) time",s-t),console.log("parseSMF(1/2) time",n-s),console.log("parseSMF(2/2) time",e-n),console.log(r)}return r}function N(){let e;if(!navigator.requestMIDIAccess)return;let t=this.settings.WebMIDIPortSysEx;const a=a=>{let i;return e=a.outputs,this.settings.WebMIDIPortOutputs=e,-1==this.settings.WebMIDIPort?this.settings.WebMIDIPortOutputs.forEach(e=>{i||(i=e)}):i=this.settings.WebMIDIPortOutputs.get(settings.WebMIDIPort),this.settings.WebMIDIPortOutput=i,this.settings.WebMIDIPortSysEx=t,i&&(i.open(),this.initStatus()),e},i=e=>{console.log(e),t&&(t=!1,navigator.requestMIDIAccess({sysex:t}).then(a).catch(i))};navigator.requestMIDIAccess({sysex:t}).then(a).catch(i),window.addEventListener("unload",e=>{for(let e=0;e<16;e++){this.settings.WebMIDIPortOutput.send([176+e,120,0]);for(let t=0;t<128;t++)this.settings.WebMIDIPortOutput.send([128+e,t,0])}})}window.PicoAudio=class{constructor(e,t){i.call(this,e,t)}init(e,t){return r.call(this,e,t)}parseSMF(e){return M.call(this,e)}setData(e){return c.call(this,e)}play(e){return T.call(this,e)}stop(e){return p.call(this,e)}initStatus(e,t){return l.call(this,e,t)}getTime(e){return y.call(this,e)}getTiming(e){return v.call(this,e)}createBaseNote(e,t,a,i,s){return h.call(this,e,t,a,i,s)}createNote(e){return f.call(this,e)}createPercussionNote(e){return A.call(this,e)}stopAudioNode(e,t,a,i){return d.call(this,e,t,a,i)}pushFunc(e){return V.call(this,e)}clearFunc(e,t){return b.call(this,e,t)}startWebMIDI(){return N.call(this)}measurePerformanceReverb(){return class{static measureReverb(){let e=performance.now(),t=0;for(;t<5e5&&!(performance.now()-e>=500);t++);return this.debug&&console.log("measureReverb",t,performance.now()-e),!(t<5e5)}}.measureReverb.call(this)}addEventListener(e,t){this.events.push({type:e,func:t})}fireEvent(e,t){this.events.forEach(a=>{if(a.type==e)try{a.func(t)}catch(e){console.log(e)}})}getChannels(){return this.channels}setChannels(e){e.forEach((e,t)=>{this.channels[t]=e})}initChannels(){for(let e=0;e<16;e++)this.channels[e]=[0,0,1]}getMasterVolume(){return this.settings.masterVolume}setMasterVolume(e){this.settings.masterVolume=e,this.isStarted&&(this.masterGainNode.gain.value=this.settings.masterVolume)}isLoop(){return this.settings.loop}setLoop(e){this.settings.loop=e}isWebMIDI(){return this.settings.isWebMIDI}setWebMIDI(e){this.settings.isWebMIDI=e}isCC111(){return this.settings.isCC111}setCC111(e){this.settings.isCC111=e}setStartTime(e){this.states.startTime-=e}setOnSongEndListener(e){this.onSongEndListener=e}onSongEnd(){if(this.onSongEndListener){if(this.onSongEndListener())return}this.settings.loop&&(this.initStatus(!0),this.settings.isCC111&&-1!=this.cc111Time&&this.setStartTime(this.cc111Time),this.play(!0))}isReverb(){return this.settings.isReverb}setReverb(e){this.settings.isReverb=e}getReverbVolume(){return this.settings.reverbVolume}setReverbVolume(e){this.settings.reverbVolume=e}isChorus(){return this.settings.isChorus}setChorus(e){this.settings.isChorus=e}getChorusVolume(){return this.settings.chorusVolume}setChorusVolume(e){this.settings.chorusVolume=e}}}]);