spessasynth_lib 3.26.13 → 3.26.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spessasynth_lib",
3
- "version": "3.26.13",
3
+ "version": "3.26.14",
4
4
  "description": "MIDI and SoundFont2/DLS library for the browsers with no compromises",
5
5
  "browser": "index.js",
6
6
  "type": "module",
@@ -55,16 +55,19 @@ export class SoundfontManager
55
55
  */
56
56
  async addNewSoundFont(soundfontBuffer, id, bankOffset = 0)
57
57
  {
58
+ this._sendToWorklet(WorkletSoundfontManagerMessageType.addNewSoundFont, [soundfontBuffer, id, bankOffset]);
59
+ await new Promise(r => this.synth._resolveWhenReady = r);
58
60
  if (this.soundfontList.find(s => s.id === id) !== undefined)
59
61
  {
60
- throw new Error("Cannot overwrite the existing soundfont. Use soundfontManager.delete(id) instead.");
62
+ this.soundfontList.find(s => s.id === id).bankOffset = bankOffset;
63
+ }
64
+ else
65
+ {
66
+ this.soundfontList.push({
67
+ id: id,
68
+ bankOffset: bankOffset
69
+ });
61
70
  }
62
- this._sendToWorklet(WorkletSoundfontManagerMessageType.addNewSoundFont, [soundfontBuffer, id, bankOffset]);
63
- await new Promise(r => this.synth._resolveWhenReady = r);
64
- this.soundfontList.push({
65
- id: id,
66
- bankOffset: bankOffset
67
- });
68
71
  }
69
72
 
70
73
  // noinspection JSUnusedGlobalSymbols
@@ -7,7 +7,7 @@ var Ts=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(A,t)=>(
7
7
  Transform type: ${A.transformType}
8
8
 
9
9
 
10
- `}sumTransform(A){return new e(this.sourceEnum,this.secondarySourceEnum,this.modulatorDestination,this.transformAmount+A.transformAmount,this.transformType)}},As=960,es=FA.concave;function ZA(e,A,t,n,s){return e<<10|A<<9|t<<8|n<<7|s}var di=[new X(ZA(es,0,1,0,_.noteOnVelocity),0,a.initialAttenuation,As,0),new X(129,0,a.vibLfoToPitch,50,0),new X(ZA(es,0,1,1,y.mainVolume),0,a.initialAttenuation,As,0),new X(13,0,a.vibLfoToPitch,50,0),new X(526,16,a.fineTune,12700,0),new X(650,0,a.pan,500,0),new X(ZA(es,0,1,1,y.expressionController),0,a.initialAttenuation,As,0),new X(219,0,a.reverbEffectsSend,200,0),new X(221,0,a.chorusEffectsSend,200,0)],fi=[new X(ZA(FA.linear,0,0,0,_.polyPressure),0,a.vibLfoToPitch,50,0),new X(ZA(FA.linear,0,0,1,y.tremoloDepth),0,a.modLfoToVolume,24,0),new X(ZA(FA.convex,1,0,1,y.attackTime),0,a.attackVolEnv,6e3,0),new X(ZA(FA.linear,1,0,1,y.releaseTime),0,a.releaseVolEnv,3600,0),new X(ZA(FA.linear,1,0,1,y.brightness),0,a.initialFilterFc,6e3,0),new X(ZA(FA.linear,1,0,1,y.filterResonance),0,a.initialFilterQ,250,0)],$s=di.concat(fi);var lA=128,rn=147,He=new Int16Array(rn).fill(0),MA=(e,A)=>He[e]=A<<7;MA(y.mainVolume,100);MA(y.balance,64);MA(y.expressionController,127);MA(y.pan,64);MA(y.portamentoOnOff,127);MA(y.filterResonance,64);MA(y.releaseTime,64);MA(y.attackTime,64);MA(y.brightness,64);MA(y.decayTime,64);MA(y.vibratoRate,64);MA(y.vibratoDepth,64);MA(y.vibratoDelay,64);MA(y.generalPurposeController6,64);MA(y.generalPurposeController8,64);MA(y.RPNLsb,127);MA(y.RPNMsb,127);MA(y.NRPNLsb,127);MA(y.NRPNMsb,127);var an=1;He[y.portamentoControl]=an;MA(lA+_.pitchWheel,64);MA(lA+_.pitchWheelRange,2);var EA={channelTuning:0,channelTransposeFine:1,modulationMultiplier:2,masterTuning:3,channelTuningSemitones:4,channelKeyShift:5},ts=Object.keys(EA).length,ns=new Float32Array(ts);ns[EA.modulationMultiplier]=1;var vA={Idle:0,RPCoarse:1,RPFine:2,NRPCoarse:3,NRPFine:4,DataCoarse:5,DataFine:6},ss={velocityOverride:128};function Ao(e,A,t){let n=e,s=A<<7|t;return e===127&&A===127&&t===127?{midiNote:-1,centTuning:null}:{midiNote:n,centTuning:s*.0061}}function eo(e,A=0){let t=e[0];if(this.deviceID!==-1&&e[1]!==127&&this.deviceID!==e[1])return;function n(o,r,g,i){p(`%cChannel %c${o}%c ${g}. %c${r} ${i}%c, with %c${HA(e)}`,I.info,I.recognized,I.info,I.value,I.info,I.value)}function s(){J(`%cUnrecognized Roland %cGS %cSysEx: %c${HA(e)}`,I.warn,I.recognized,I.warn,I.unrecognized)}switch(t){default:J(`%cUnrecognized SysEx: %c${HA(e)}`,I.warn,I.unrecognized);break;case 126:case 127:switch(e[2]){case 4:let o;switch(e[3]){case 1:let g=e[5]<<7|e[4];this.setMIDIVolume(g/16384),p(`%cMaster Volume. Volume: %c${g}`,I.info,I.value);break;case 2:let C=((e[5]<<7|e[4])-8192)/8192;this.setMasterParameter(JA.masterPan,C),p(`%cMaster Pan. Pan: %c${C}`,I.info,I.value);break;case 3:let B=(e[5]<<7|e[6])-8192;o=Math.floor(B/81.92),this.setMasterTuning(o),p(`%cMaster Fine Tuning. Cents: %c${o}`,I.info,I.value);break;case 4:o=(e[5]-64)*100,this.setMasterTuning(o),p(`%cMaster Coarse Tuning. Cents: %c${o}`,I.info,I.value);break;default:J(`%cUnrecognized MIDI Device Control Real-time message: %c${HA(e)}`,I.warn,I.unrecognized)}break;case 9:e[3]===1?(p("%cGM1 system on",I.info),this.setSystem("gm")):e[3]===3?(p("%cGM2 system on",I.info),this.setSystem("gm2")):(p("%cGM system off, defaulting to GS",I.info),this.setSystem("gs"));break;case 8:let r=4;switch(e[3]){case 1:let g=e[r++];e.currentIndex=r;let i=AA(e,16);if(r+=16,e.length<384){J(`The Bulk Tuning Dump is too short! (${e.length} bytes, at least 384 are expected)`);return}for(let c=0;c<128;c++)this.tunings[g][c]=Ao(e[r++],e[r++],e[r++]);p(`%cBulk Tuning Dump %c${i}%c Program: %c${g}`,I.info,I.value,I.info,I.recognized);break;case 2:case 7:e[3]===7&&r++;let C=e[r++],B=e[r++];for(let c=0;c<B;c++)this.tunings[C][e[r++]]=Ao(e[r++],e[r++],e[r++]);p(`%cSingle Note Tuning. Program: %c${C}%c Keys affected: %c${B}`,I.info,I.recognized,I.info,I.recognized);break;case 9:case 8:let h=new Int8Array(12);if(e[3]===8)for(let c=0;c<12;c++)h[c]=e[7+c]-64;else for(let c=0;c<24;c+=2){let l=(e[7+c]<<7|e[8+c])-8192;h[c/2]=Math.floor(l/81.92)}(e[4]&1)===1&&this.midiAudioChannels[14+A].setOctaveTuning(h),(e[4]>>1&1)===1&&this.midiAudioChannels[15+A].setOctaveTuning(h);for(let c=0;c<7;c++)(e[5]>>c&1)===1&&this.midiAudioChannels[7+c+A].setOctaveTuning(h);for(let c=0;c<7;c++)(e[6]>>c&1)===1&&this.midiAudioChannels[c+A].setOctaveTuning(h);p(`%cMIDI Octave Scale ${e[3]===8?"(1 byte)":"(2 bytes)"} tuning via Tuning: %c${h.join(" ")}`,I.info,I.value);break;default:J(`%cUnrecognized MIDI Tuning standard message: %c${HA(e)}`,I.warn,I.unrecognized);break}break;default:J(`%cUnrecognized MIDI Realtime/non realtime message: %c${HA(e)}`,I.warn,I.unrecognized)}break;case 65:if(e[2]===66&&e[3]===18){let o=e[7];if(e[4]===64||e[4]===0&&e[6]===127){if((e[5]&16)>0){let r=[9,0,1,2,3,4,5,6,7,8,10,11,12,13,14,15][e[5]&15]+A,g=this.midiAudioChannels[r];switch(e[6]){default:s();break;case 21:let i=o>0&&e[5]>>4;g.setDrums(i),p(`%cChannel %c${r}%c ${i?"is now a drum channel":"now isn't a drum channel"}%c via: %c${HA(e)}`,I.info,I.value,I.recognized,I.info,I.value);return;case 22:let C=o-64;g.setCustomController(EA.channelKeyShift,C),n(r,C,"key shift","keys");return;case 28:let B=o;B===0?(g.randomPan=!0,p(`%cRandom pan is set to %cON%c for %c${r}`,I.info,I.recognized,I.info,I.value)):(g.randomPan=!1,g.controllerChange(y.pan,B));break;case 33:g.controllerChange(y.chorusDepth,o);break;case 34:g.controllerChange(y.reverbDepth,o);break;case 64:case 65:case 66:case 67:case 68:case 69:case 70:case 71:case 72:case 73:case 74:case 75:let h=e.length-9,c=new Int8Array(12);for(let m=0;m<h;m++)c[m]=e[m+7]-64;g.setOctaveTuning(c);let l=o-64;n(r,c.join(" "),"octave scale tuning","cents"),g.setTuning(l);break}return}else if((e[5]&32)>0){let r=[9,0,1,2,3,4,5,6,7,8,10,11,12,13,14,15][e[5]&15]+A,g=this.midiAudioChannels[r],i=o-64,C=i/64,B=o/128,h=(c,l,m=!1)=>{switch(e[6]&15){case 0:g.sysExModulators.setModulator(c,a.fineTune,i*100,m),n(r,i,`${l} pitch control`,"semitones");break;case 1:g.sysExModulators.setModulator(c,a.initialFilterFc,C*9600,m),n(r,C*9600,`${l} pitch control`,"cents");break;case 2:g.sysExModulators.setModulator(c,a.initialAttenuation,C*960,m),n(r,C*960,`${l} amplitude`,"cB");break;case 4:g.sysExModulators.setModulator(c,a.vibLfoToPitch,B*600),n(r,B*600,`${l} LFO1 pitch depth`,"cents");break;case 5:g.sysExModulators.setModulator(c,a.vibLfoToFilterFc,B*2400),n(r,B*2400,`${l} LFO1 filter depth`,"cents");break;case 6:g.sysExModulators.setModulator(c,a.vibLfoToVolume,B*960),n(r,B*960,`${l} LFO1 amplitude depth`,"cB");break;case 8:g.sysExModulators.setModulator(c,a.modLfoToPitch,B*600),n(r,B*600,`${l} LFO2 pitch depth`,"cents");break;case 9:g.sysExModulators.setModulator(c,a.modLfoToFilterFc,B*2400),n(r,B*2400,`${l} LFO2 filter depth`,"cents");break;case 10:g.sysExModulators.setModulator(c,a.modLfoToVolume,B*960),n(r,B*960,`${l} LFO2 amplitude depth`,"cB");break}};switch(e[6]&240){default:s();break;case 0:h(y.modulationWheel,"mod wheel");break;case 16:h(lA+_.pitchWheel,"pitch bend",!0);break;case 32:h(lA+_.channelPressure,"channel pressure");break;case 48:h(lA+_.polyPressure,"poly pressure");break}return}else if(e[5]===0){switch(e[6]){default:s();break;case 127:o===0?(p("%cGS Reset received!",I.info),this.resetAllControllers(!1),this.setSystem("gs")):o===127&&(p("%cGS system off, switching to GM2",I.info),this.resetAllControllers(!1),this.setSystem("gm2"));break;case 6:p(`%cRoland GS Master Pan set to: %c${o}%c with: %c${HA(e)}`,I.info,I.value,I.info,I.value),this.setMasterParameter(JA.masterPan,(o-64)/64);break;case 4:p(`%cRoland GS Master Volume set to: %c${o}%c with: %c${HA(e)}`,I.info,I.value,I.info,I.value),this.setMIDIVolume(o/127);break;case 5:let r=o-64;p(`%cRoland GS Master Key-Shift set to: %c${r}%c with: %c${HA(e)}`,I.info,I.value,I.info,I.value),this.setMasterTuning(r*100);break}return}else if(e[5]===1)switch(e[6]){default:s();break;case 0:e.currentIndex=7;let r=AA(e,16);p(`%cGS Patch name: %c${r}`,I.info,I.value);break;case 51:p(`%cGS Reverb level: %c${o}`,I.info,I.value),this.reverbSend=o/64;break;case 48:case 49:case 50:case 52:case 53:case 55:p(`%cUnsupported GS Reverb Parameter: %c${e[6].toString(16)}`,I.warn,I.unrecognized);break;case 58:p(`%cGS Chorus level: %c${o}`,I.info,I.value),this.chorusSend=o/64;break;case 56:case 57:case 59:case 60:case 61:case 62:case 63:case 64:p(`%cUnsupported GS Chorus Parameter: %c${e[6].toString(16)}`,I.warn,I.unrecognized);break}}else s();return}else if(e[2]===69&&e[3]===18){if(e[4]===16&&e[6]===0)if(e[5]===0){let o=new Uint8Array(e.slice(7,e.length-2));this.callEvent("synthdisplay",{displayData:o,displayType:Ut.SoundCanvasText})}else if(e[5]===1){let o=new Uint8Array(e.slice(7,e.length-3));this.callEvent("synthdisplay",{displayData:o,displayType:Ut.SoundCanvasDotDisplay}),p(`%cRoland SC Display Dot Matrix via: %c${HA(e)}`,I.info,I.value)}else s()}else if(e[2]===22&&e[3]===18&&e[4]===16){this.setMIDIVolume(e[7]/100),p(`%cRoland Master Volume control set to: %c${e[7]}%c via: %c${HA(e)}`,I.info,I.value,I.info,I.value);return}else{J(`%cUnrecognized Roland SysEx: %c${HA(e)}`,I.warn,I.unrecognized);return}break;case 67:if(e[2]===76)if(e[3]===0&&e[4]===0)switch(e[5]){case 4:let o=e[6];this.setMIDIVolume(o/127),p(`%cXG master volume. Volume: %c${o}`,I.info,I.recognized);break;case 6:let r=e[6]-64;this.transposeAllChannels(r),p(`%cXG master transpose. Volume: %c${r}`,I.info,I.recognized);break;case 126:p("%cXG system on",I.info),this.resetAllControllers(!1),this.setSystem("xg");break}else if(e[3]===8){if(!GA(this.system))return;let o=e[4]+A;if(o>=this.midiAudioChannels.length)return;let r=this.midiAudioChannels[o],g=e[6];switch(e[5]){case 1:r.controllerChange(y.bankSelect,g);break;case 2:r.controllerChange(y.lsbForControl0BankSelect,g);break;case 3:r.programChange(g);break;case 8:if(r.drumChannel)return;let i=g-64;r.channelTransposeKeyShift=i;break;case 11:r.controllerChange(y.mainVolume,g);break;case 14:let C=g;C===0?(r.randomPan=!0,p(`%cRandom pan is set to %cON%c for %c${o}`,I.info,I.recognized,I.info,I.value)):r.controllerChange(y.pan,C);break;case 19:r.controllerChange(y.reverbDepth,g);break;case 18:r.controllerChange(y.chorusDepth,g);break;default:J(`%cUnrecognized Yamaha XG Part Setup: %c${e[5].toString(16).toUpperCase()}`,I.warn,I.unrecognized)}}else if(e[3]===6&&e[4]===0){let o=new Uint8Array(e.slice(5,e.length-1));this.callEvent("synthdisplay",{displayData:o,displayType:Ut.XGText})}else GA(this.system)&&J(`%cUnrecognized Yamaha XG SysEx: %c${HA(e)}`,I.warn,I.unrecognized);else GA(this.system)&&J(`%cUnrecognized Yamaha SysEx: %c${HA(e)}`,I.warn,I.unrecognized);break}}function to(e=!0){e&&p("%cResetting all controllers!",I.info),this.callEvent("allcontrollerreset",void 0),this.setSystem(Lt);for(let A=0;A<this.midiAudioChannels.length;A++){this.midiAudioChannels[A].resetControllers();let t=this.midiAudioChannels[A];t.lockPreset?this.callEvent("drumchange",{channel:A,isDrumChannel:t.drumChannel}):(t.setBankSelect(zs(this.system)),A%16===9?(t.setPreset(this.drumPreset),t.drumChannel=!0,this.callEvent("drumchange",{channel:A,isDrumChannel:!0})):(t.drumChannel=!1,t.setPreset(this.defaultPreset),this.callEvent("drumchange",{channel:A,isDrumChannel:!1})));let n=t.preset.bank;this.callEvent("programchange",{channel:A,program:t.preset.program,bank:n});for(let s=0;s<128;s++)this.midiAudioChannels[A].lockedControllers[s]&&this.callEvent("controllerchange",{channel:A,controllerNumber:s,controllerValue:this.midiAudioChannels[A].midiControllers[s]>>7});if(this.midiAudioChannels[A].lockedControllers[lA+_.pitchWheel]===!1){let s=this.midiAudioChannels[A].midiControllers[lA+_.pitchWheel],o=s>>7,r=s&127;this.callEvent("pitchwheel",{channel:A,MSB:o,LSB:r})}}this.tunings=[],this.tunings=[];for(let A=0;A<128;A++)this.tunings.push([]);this.setMIDIVolume(1)}function no(){this.channelOctaveTuning.fill(0);for(let A=0;A<He.length;A++){if(this.lockedControllers[A])continue;let t=He[A];this.midiControllers[A]!==t&&A<127?A===y.portamentoControl?this.midiControllers[A]=an:this.controllerChange(A,t>>7):this.midiControllers[A]=t}this.channelVibrato={rate:0,depth:0,delay:0},this.holdPedal=!1,this.randomPan=!1,this.sysExModulators.resetModulators();let e=this.customControllers[EA.channelTransposeFine];this.customControllers.set(ns),this.setCustomController(EA.channelTransposeFine,e),this.resetParameters()}var os=new Set([y.bankSelect,y.lsbForControl0BankSelect,y.mainVolume,y.lsbForControl7MainVolume,y.pan,y.lsbForControl10Pan,y.reverbDepth,y.tremoloDepth,y.chorusDepth,y.detuneDepth,y.phaserDepth,y.soundVariation,y.filterResonance,y.releaseTime,y.attackTime,y.brightness,y.decayTime,y.vibratoRate,y.vibratoDepth,y.vibratoDelay,y.soundController10]);function so(){this.channelOctaveTuning.fill(0),this.pitchWheel(64,0),this.channelVibrato={rate:0,depth:0,delay:0};for(let e=0;e<128;e++){let A=He[e];!os.has(e)&&A!==this.midiControllers[e]&&(e===y.portamentoControl?this.midiControllers[e]=an:this.controllerChange(e,A>>7))}}function oo(){this.dataEntryState=vA.Idle,p("%cResetting Registered and Non-Registered Parameters!",I.info)}var lt=class{soundfontList=[];presetList=[];constructor(A){this.presetListChangeCallback=A}generatePresetList(){let A={};for(let t=this.soundfontList.length-1;t>=0;t--){let n=this.soundfontList[t],s=new Set;for(let o of n.soundfont.presets){let g=`${Math.min(128,o.bank+n.bankOffset)}-${o.program}`;s.has(g)||(s.add(g),A[g]=o.presetName)}}this.presetList=[];for(let[t,n]of Object.entries(A)){let s=t.split("-");this.presetList.push({presetName:n,program:parseInt(s[1]),bank:parseInt(s[0])})}this.presetListChangeCallback()}getPresetList(){return this.presetList.slice()}reloadManager(A){this.soundfontList=this.soundfontList.filter(t=>t.id===Xe),this.soundfontList.push({id:"main",bankOffset:0,soundfont:A}),this.generatePresetList()}deleteSoundFont(A){if(this.soundfontList.length===0){J("1 soundfont left. Aborting!");return}let t=this.soundfontList.findIndex(n=>n.id===A);if(t===-1){p(`No soundfont with id of "${A}" found. Aborting!`);return}delete this.soundfontList[t].soundfont.presets,delete this.soundfontList[t].soundfont.instruments,delete this.soundfontList[t].soundfont.samples,this.soundfontList.splice(t,1),this.generatePresetList()}addNewSoundFont(A,t,n){if(this.soundfontList.find(s=>s.id===t)!==void 0)throw new Error("Cannot overwrite the existing soundfont. Use soundfontManager.delete(id) instead.");this.soundfontList.push({id:t,soundfont:A,bankOffset:n}),this.generatePresetList()}getCurrentSoundFontOrder(){return this.soundfontList.map(A=>A.id)}rearrangeSoundFonts(A){this.soundfontList.sort((t,n)=>A.indexOf(t.id)-A.indexOf(n.id)),this.generatePresetList()}getPreset(A,t,n=!1){if(this.soundfontList.length<1)throw new Error("No soundfonts! Did you forget to add one?");let s=A===128||n&&Ae(A);for(let o of this.soundfontList){let r=o.soundfont.getPresetNoFallback(A===128?128:A-o.bankOffset,t,n);if(r!==void 0)return{preset:r,bankOffset:o.bankOffset}}if(s){for(let r of this.soundfontList){let g=r.soundfont.presets.find(C=>C.isDrumPreset(n)&&C.program===t);if(g)return{preset:g,bankOffset:r.bankOffset};let i=r.soundfont.presets.find(C=>C.isDrumPreset(n));if(i)return{preset:i,bankOffset:r.bankOffset}}let o=this.soundfontList[0];return{preset:o.soundfont.presets[0],bankOffset:o.bankOffset}}else{for(let r of this.soundfontList){let g=r.soundfont.presets.find(i=>i.program===t&&!i.isDrumPreset(n));if(g)return{preset:g,bankOffset:r.bankOffset}}let o=this.soundfontList[0];return{preset:o.soundfont.presets[0],bankOffset:o.bankOffset}}}destroyManager(){this.soundfontList.forEach(A=>{A.soundfont.destroySoundBank()}),delete this.soundfontList}};var gn=class{_keyMappings=[];addMapping(A,t,n){this._keyMappings[A]===void 0&&(this._keyMappings[A]=[]),this._keyMappings[A][t]=n}deleteMapping(A,t){this._keyMappings[A]?.[t]!==void 0&&(this._keyMappings[A][t]=void 0)}clearMappings(){this._keyMappings=[]}setMappings(A){this._keyMappings=A}getMappings(){return this._keyMappings}getVelocity(A,t){return this._keyMappings[A]?.[t]?.velocity??-1}getGain(A,t){return this._keyMappings[A]?.[t]?.gain??1}hasOverridePatch(A,t){let n=this._keyMappings[A]?.[t]?.patch?.bank;return n!==void 0&&n>=0}getPatch(A,t){let n=this._keyMappings[A]?.[t];if(n)return n.patch;throw new Error("No modifier.")}};var ro=.1,Ye=class e{static cachedCoefficients=[];a0=0;a1=0;a2=0;a3=0;a4=0;x1=0;x2=0;y1=0;y2=0;resonanceCb=0;currentInitialFc=13500;lastTargetCutoff=1/0;initialized=!1;sampleRate;constructor(A){this.sampleRate=A,this.maxCutoff=A*.45}static apply(A,t,n,s){let o=A.modulatedGenerators[a.initialFilterFc],r=A.filter;r.initialized?r.currentInitialFc+=(o-r.currentInitialFc)*s:(r.initialized=!0,r.currentInitialFc=o);let g=r.currentInitialFc+n,i=A.modulatedGenerators[a.initialFilterQ];if(r.currentInitialFc>13499&&g>13499&&i===0){r.currentInitialFc=13500;return}(Math.abs(r.lastTargetCutoff-g)>1||r.resonanceCb!==i)&&(r.lastTargetCutoff=g,r.resonanceCb=i,e.calculateCoefficients(r,g));for(let C=0;C<t.length;C++){let B=t[C],h=r.a0*B+r.a1*r.x1+r.a2*r.x2-r.a3*r.y1-r.a4*r.y2;r.x2=r.x1,r.x1=B,r.y2=r.y1,r.y1=h,t[C]=h}}static calculateCoefficients(A,t){t=~~t;let n=A.resonanceCb,s=e.cachedCoefficients?.[n]?.[t];if(s!==void 0){A.a0=s.a0,A.a1=s.a1,A.a2=s.a2,A.a3=s.a3,A.a4=s.a4;return}let o=Tt(t);o=Math.min(o,A.maxCutoff);let r=n/10,g=ge(-(r-3.01)),i=1/Math.sqrt(ge(-r)),C=2*Math.PI*o/A.sampleRate,B=Math.cos(C),h=Math.sin(C)/(2*g),c=(1-B)*i,l=c/2,m=l,f=1+h,S=-2*B,F=1-h,D={};D.a0=l/f,D.a1=c/f,D.a2=m/f,D.a3=S/f,D.a4=F/f,A.a0=D.a0,A.a1=D.a1,A.a2=D.a2,A.a3=D.a3,A.a4=D.a4,e.cachedCoefficients[n]===void 0&&(e.cachedCoefficients[n]=[]),e.cachedCoefficients[n][t]=D}},rs=new Ye(44100);rs.resonanceCb=0;for(let e=1500;e<13500;e++)rs.currentInitialFc=e,Ye.calculateCoefficients(rs,e);var bA=16384,we=new Float32Array(bA+1),ze=new Float32Array(bA+1);we[0]=0;we[we.length-1]=1;ze[0]=0;ze[ze.length-1]=1;for(let e=1;e<bA-1;e++){let A=-.4166666666666667*Math.log(e/(we.length-1))/Math.LN10;ze[e]=1-A,we[we.length-1-e]=A}function je(e,A,t,n){switch(e&&(t=1-t),A){case FA.linear:return n?t*2-1:t;case FA.switch:return t=t>.5?1:0,n?t*2-1:t;case FA.concave:return n?(t=t*2-1,t<0?-we[~~(t*-bA)]:we[~~(t*bA)]):we[~~(t*bA)];case FA.convex:return n?(t=t*2-1,t<0?-ze[~~(t*-bA)]:ze[~~(t*bA)]):ze[~~(t*bA)]}}var is=1,as=new Float32Array(1e3);for(let e=0;e<as.length;e++)as[e]=je(0,FA.convex,e/1e3,0);var he=class e{attackDuration=0;decayDuration=0;holdDuration=0;releaseDuration=0;sustainLevel=0;delayEnd=0;attackEnd=0;holdEnd=0;decayEnd=0;releaseStartLevel=0;currentValue=0;static startRelease(A){e.recalculate(A)}static recalculate(A){let t=A.modulationEnvelope;A.isInRelease&&(t.releaseStartLevel=e.getValue(A,A.releaseStartTime,!0)),t.sustainLevel=1-A.modulatedGenerators[a.sustainModEnv]/1e3,t.attackDuration=Ce(A.modulatedGenerators[a.attackModEnv]);let n=(60-A.midiNote)*A.modulatedGenerators[a.keyNumToModEnvDecay],s=Ce(A.modulatedGenerators[a.decayModEnv]+n);t.decayDuration=s*(1-t.sustainLevel);let o=(60-A.midiNote)*A.modulatedGenerators[a.keyNumToModEnvHold];t.holdDuration=Ce(o+A.modulatedGenerators[a.holdModEnv]);let r=Ce(A.modulatedGenerators[a.releaseModEnv]);t.releaseDuration=r*t.releaseStartLevel,t.delayEnd=A.startTime+Ce(A.modulatedGenerators[a.delayModEnv]),t.attackEnd=t.delayEnd+t.attackDuration,t.holdEnd=t.attackEnd+t.holdDuration,t.decayEnd=t.holdEnd+t.decayDuration}static getValue(A,t,n=!1){let s=A.modulationEnvelope;return A.isInRelease&&!n?s.releaseStartLevel===0?0:Math.max(0,(1-(t-A.releaseStartTime)/s.releaseDuration)*s.releaseStartLevel):(t<s.delayEnd?s.currentValue=0:t<s.attackEnd?s.currentValue=as[~~((1-(s.attackEnd-t)/s.attackDuration)*1e3)]:t<s.holdEnd?s.currentValue=is:t<s.decayEnd?s.currentValue=(1-(s.decayEnd-t)/s.decayDuration)*(s.sustainLevel-is)+is:s.currentValue=s.sustainLevel,s.currentValue)}};var ui=-2320,mi=-1130,In=class{sampleData;playbackStep=0;cursor=0;rootKey=0;loopStart=0;loopEnd=0;end=0;loopingMode=0;isLooping=!1;constructor(A,t,n,s,o,r,g,i){this.sampleData=A,this.playbackStep=t,this.cursor=n,this.rootKey=s,this.loopStart=o,this.loopEnd=r,this.end=g,this.loopingMode=i,this.isLooping=this.loopingMode===1||this.loopingMode===3}},vt=class e{sample;filter;gain=1;generators;modulators=[];modulatedGenerators;finished=!1;isInRelease=!1;velocity=0;midiNote=0;pressure=0;targetKey=0;modulationEnvelope=new he;volumeEnvelope;startTime=0;releaseStartTime=1/0;currentTuningCents=0;currentTuningCalculated=1;currentPan=0;realKey;portamentoFromKey=-1;portamentoDuration=0;overridePan=0;exclusiveClass=0;constructor(A,t,n,s,o,r,g,i,C){this.sample=t,this.generators=i,this.exclusiveClass=this.generators[a.exclusiveClass],this.modulatedGenerators=new Int16Array(i),this.modulators=C,this.filter=new Ye(A),this.velocity=s,this.midiNote=n,this.startTime=o,this.targetKey=r,this.realKey=g,this.volumeEnvelope=new Ee(A,i[a.sustainVolEnv])}static copy(A,t,n){let s=A.sample,o=new In(s.sampleData,s.playbackStep,s.cursor,s.rootKey,s.loopStart,s.loopEnd,s.end,s.loopingMode);return new e(A.volumeEnvelope.sampleRate,o,A.midiNote,A.velocity,t,A.targetKey,n,A.generators,A.modulators.map(r=>X.copy(r)))}exclusiveRelease(A){this.release(A,Io),this.modulatedGenerators[a.releaseVolEnv]=ui,this.modulatedGenerators[a.releaseModEnv]=mi,Ee.recalculate(this),he.recalculate(this)}release(A,t=go){this.releaseStartTime=A,this.releaseStartTime-this.startTime<t&&(this.releaseStartTime=this.startTime+t)}};function io(e,A,t,n,s,o){let r=e.getSamplesAndGenerators(n,s).reduce((g,i)=>{if(i.sample.getAudioData()===void 0)return J(`Discarding invalid sample: ${i.sample.sampleName}`),g;let C=new Int16Array(Vs);for(let S=0;S<60;S++)C[S]=Ps(S,i.presetGenerators,i.instrumentGenerators);C[a.initialAttenuation]=Math.floor(C[a.initialAttenuation]*.4);let B=i.sample.samplePitch;C[a.overridingRootKey]>-1&&(B=C[a.overridingRootKey]);let h=n;C[a.keyNum]>-1&&(h=C[a.keyNum]);let c=i.sample.sampleLoopStartIndex,l=i.sample.sampleLoopEndIndex,m=C[a.sampleModes],f=new In(i.sample.sampleData,i.sample.sampleRate/this.sampleRate*Math.pow(2,i.sample.samplePitchCorrection/1200),0,B,c,l,Math.floor(i.sample.sampleData.length)-1,m);return C[a.velocity]>-1&&(s=C[a.velocity]),g.push(new vt(this.sampleRate,f,n,s,this.currentSynthTime,h,o,C,i.modulators.map(S=>X.copy(S)))),g},[]);return this.setCachedVoice(A,t,n,s,r.map(g=>vt.copy(g,this.currentSynthTime,o))),r}function ao(e,A,t,n){let s=this.midiAudioChannels[e],o=this.keyModifierManager.hasOverridePatch(e,A),r=s.getBankSelect(),g=s.preset.program;if(o){let B=this.keyModifierManager.getPatch(e,A);r=B.bank,g=B.program}let i=this.getCachedVoice(r,g,A,t);if(i!==void 0)return i.map(B=>vt.copy(B,this.currentSynthTime,n));let C=s.preset;return o&&(C=this.getPreset(r,g)),this.getVoicesForPreset(C,r,g,A,t,n)}var Eo=.05,pi=4600,yi=2e3,Co=Math.PI/2,Cn=-500,ho=500,gs=ho-Cn,co=new Float32Array(gs+1),Bo=new Float32Array(gs+1);for(let e=Cn;e<=ho;e++){let A=(e-Cn)/gs,t=e-Cn;co[t]=Math.cos(Co*A),Bo[t]=Math.sin(Co*A)}function lo(e,A,t,n,s,o,r,g){if(isNaN(A[0]))return;let i;e.overridePan?i=e.overridePan:(e.currentPan+=(e.modulatedGenerators[a.pan]-e.currentPan)*this.synth.panSmoothingFactor,i=e.currentPan);let C=this.synth.currentGain*e.gain,B=~~(i+500),h=co[B]*C*this.synth.panLeft,c=Bo[B]*C*this.synth.panRight;if(this.synth.effectsEnabled){let l=e.modulatedGenerators[a.reverbEffectsSend];if(l>0){let f=this.synth.reverbGain*this.synth.reverbSend*C*(l/pi);for(let S=0;S<A.length;S++)s[S]+=f*A[S];o.set(s)}let m=e.modulatedGenerators[a.chorusEffectsSend];if(m>0){let f=this.synth.chorusGain*this.synth.chorusSend*(m/yi),S=h*f,F=c*f;for(let D=0;D<A.length;D++)r[D]+=S*A[D],g[D]+=F*A[D]}}if(h>0)for(let l=0;l<A.length;l++)t[l]+=h*A[l];if(c>0)for(let l=0;l<A.length;l++)n[l]+=c*A[l]}function Qo(e=!1){p("%cStop all received!",I.info);for(let A=0;A<this.midiAudioChannels.length;A++)this.midiAudioChannels[A].stopAllNotes(e);this.callEvent("stopall",void 0)}function b(e,A){let t=0;for(let n=0;n<A;n++)t|=e[e.currentIndex++]<<n*8;return t>>>0}function Je(e,A,t){for(let n=0;n<t;n++)e[e.currentIndex++]=A>>n*8&255}function Y(e,A){e[e.currentIndex++]=A&255,e[e.currentIndex++]=A>>8}function tA(e,A){Je(e,A,4)}function Oe(e,A){let t=A<<8|e;return t>32767?t-65536:t}function fo(e){return e>127?e-256:e}var BA=class{constructor(A,t,n){this.header=A,this.size=t,this.chunkData=n}};function IA(e,A=!0,t=!1){let n=AA(e,4),s=b(e,4),o;return A&&(o=new N(e.buffer.slice(e.currentIndex,e.currentIndex+s))),(A||t)&&(e.currentIndex+=s),s%2!==0&&e[e.currentIndex]===0&&e.currentIndex++,new BA(n,s,o)}function fA(e,A=void 0){let t=8+e.size;e.size%2!==0&&t++,A&&(t+=A.length);let n=new N(t);return A&&(n.set(A,n.currentIndex),n.currentIndex+=A.length),NA(n,e.header),tA(n,t-8-(A?.length||0)),n.set(e.chunkData,n.currentIndex),n}function j(e,A,t=!1,n=!1){if(t){let C=new Uint8Array(A.length+1);C.set(A),A=C}let s=8,o=s+A.length,r=A.length;o%2!==0&&o++;let g=e;n&&(o+=4,r+=4,s+=4,g="LIST");let i=new N(o);return NA(i,g),tA(i,r),n&&NA(i,e),i.set(A,s),i}function XA(e,A){return e.find(t=>t.header!=="LIST"?!1:(t.chunkData.currentIndex=0,AA(t.chunkData,4)===A))}function uo(){let e=4;for(let n of this.instruments)e+=n.instrumentZones.reduce((s,o)=>(o.generators=o.generators.filter(r=>r.generatorType!==a.sampleID&&r.generatorType!==a.keyRange&&r.generatorType!==a.velRange),(o.velRange.max!==127||o.velRange.min!==0)&&o.generators.unshift(new U(a.velRange,o.velRange.max<<8|Math.max(o.velRange.min,0),!1)),(o.keyRange.max!==127||o.keyRange.min!==0)&&o.generators.unshift(new U(a.keyRange,o.keyRange.max<<8|Math.max(o.keyRange.min,0),!1)),o.isGlobal||o.generators.push(new U(a.sampleID,this.samples.indexOf(o.sample),!1)),o.generators.length*4+s),0);let A=new N(e),t=0;for(let n of this.instruments)for(let s of n.instrumentZones){s.generatorZoneStartIndex=t;for(let o of s.generators)Y(A,o.generatorType),Y(A,o.generatorValue),t++}return tA(A,0),fA(new BA("igen",A.length,A))}function mo(e,A,t,n,s){let o=this.samples.map((C,B)=>{t&&C.compressSample(n,s);let h=C.getRawData();return p(`%cEncoded sample %c${B}. ${C.sampleName}%c of %c${this.samples.length}%c. Compressed: %c${C.isCompressed}%c.`,I.info,I.recognized,I.info,I.recognized,I.info,C.isCompressed?I.recognized:I.unrecognized,I.info),h}),r=this.samples.reduce((C,B,h)=>C+o[h].length+46,0),g=new N(r);this.samples.forEach((C,B)=>{let h=o[B],c,l,m=h.length;C.isCompressed?(c=g.currentIndex,l=c+h.length):(c=g.currentIndex/2,l=c+h.length/2,m+=46),e.push(c),g.set(h,g.currentIndex),g.currentIndex+=m,A.push(l)});let i=fA(new BA("smpl",g.length,g),new N([115,100,116,97]));return fA(new BA("LIST",i.length,i))}function po(e,A){let n=new N(46*(this.samples.length+1));return this.samples.forEach((s,o)=>{NA(n,s.sampleName,20);let r=e[o];tA(n,r);let g=A[o];tA(n,g);let i=s.sampleLoopStartIndex+r,C=s.sampleLoopEndIndex+r;s.isCompressed&&(i-=r,C-=r),tA(n,i),tA(n,C),tA(n,s.sampleRate),n[n.currentIndex++]=s.samplePitch,n[n.currentIndex++]=s.samplePitchCorrection,Y(n,s.sampleLink),Y(n,s.sampleType)}),NA(n,"EOS",46),fA(new BA("shdr",n.length,n))}function yo(){let e=10;for(let n of this.instruments)e+=n.instrumentZones.reduce((s,o)=>o.modulators.length*10+s,0);let A=new N(e),t=0;for(let n of this.instruments)for(let s of n.instrumentZones){s.modulatorZoneStartIndex=t;for(let o of s.modulators)Y(A,o.sourceEnum),Y(A,o.modulatorDestination),Y(A,o.transformAmount),Y(A,o.secondarySourceEnum),Y(A,o.transformType),t++}return Je(A,0,10),fA(new BA("imod",A.length,A))}function So(){let e=this.instruments.reduce((o,r)=>r.instrumentZones.length*4+o,4),A=new N(e),t=0,n=0,s=0;for(let o of this.instruments){o.instrumentZoneIndex=t;for(let r of o.instrumentZones)r.zoneID=t,Y(A,n),Y(A,s),n+=r.generators.length,s+=r.modulators.length,t++}return Y(A,n),Y(A,s),fA(new BA("ibag",A.length,A))}function Do(){let e=this.instruments.length*22+22,A=new N(e),t=0,n=0;for(let s of this.instruments)NA(A,s.instrumentName,20),Y(A,t),t+=s.instrumentZones.length,s.instrumentID=n,n++;return NA(A,"EOI",20),Y(A,t),fA(new BA("inst",A.length,A))}function ko(){let e=4;for(let n of this.presets)e+=n.presetZones.reduce((s,o)=>(o.generators=o.generators.filter(r=>r.generatorType!==a.instrument&&r.generatorType!==a.keyRange&&r.generatorType!==a.velRange),(o.velRange.max!==127||o.velRange.min!==0)&&o.generators.unshift(new U(a.velRange,o.velRange.max<<8|Math.max(o.velRange.min,0),!1)),(o.keyRange.max!==127||o.keyRange.min!==0)&&o.generators.unshift(new U(a.keyRange,o.keyRange.max<<8|Math.max(o.keyRange.min,0),!1)),o.isGlobal||o.generators.push(new U(a.instrument,this.instruments.indexOf(o.instrument),!1)),o.generators.length*4+s),0);let A=new N(e),t=0;for(let n of this.presets)for(let s of n.presetZones){s.generatorZoneStartIndex=t;for(let o of s.generators)Y(A,o.generatorType),Y(A,o.generatorValue);t+=s.generators.length}return Y(A,0),Y(A,0),fA(new BA("pgen",A.length,A))}function Fo(){let e=10;for(let n of this.presets)e+=n.presetZones.reduce((s,o)=>o.modulators.length*10+s,0);let A=new N(e),t=0;for(let n of this.presets)for(let s of n.presetZones){s.modulatorZoneStartIndex=t;for(let o of s.modulators)Y(A,o.sourceEnum),Y(A,o.modulatorDestination),Y(A,o.transformAmount),Y(A,o.secondarySourceEnum),Y(A,o.transformType),t++}return Je(A,0,10),fA(new BA("pmod",A.length,A))}function wo(){let e=this.presets.reduce((o,r)=>r.presetZones.length*4+o,4),A=new N(e),t=0,n=0,s=0;for(let o of this.presets){o.presetZoneStartIndex=t;for(let r of o.presetZones)r.zoneID=t,Y(A,n),Y(A,s),n+=r.generators.length,s+=r.modulators.length,t++}return Y(A,n),Y(A,s),fA(new BA("pbag",A.length,A))}function Ro(){let e=this.presets.length*38+38,A=new N(e),t=0;for(let n of this.presets)NA(A,n.presetName,20),Y(A,n.program),Y(A,n.bank),Y(A,t),tA(A,n.library),tA(A,n.genre),tA(A,n.morphology),t+=n.presetZones.length;return NA(A,"EOP",20),Y(A,0),Y(A,0),Y(A,t),tA(A,0),tA(A,0),tA(A,0),fA(new BA("phdr",A.length,A))}var Si={compress:!1,compressionQuality:.5,compressionFunction:void 0};function Go(e=Si){if(e.compress&&typeof e.compressionFunction!="function")throw new TypeError("No compression function supplied but compression enabled.");uA("%cSaving soundfont...",I.info),p(`%cCompression: %c${e?.compress||"false"}%c quality: %c${e?.compressionQuality||"none"}`,I.info,I.recognized,I.info,I.recognized),p("%cWriting INFO...",I.info);let A=[];this.soundFontInfo.ISFT="SpessaSynth",e?.compress&&(this.soundFontInfo.ifil="3.0");for(let[L,G]of Object.entries(this.soundFontInfo))if(L==="ifil"||L==="iver"){let E=parseInt(G.split(".")[0]),M=parseInt(G.split(".")[1]),v=new N(4);Y(v,E),Y(v,M),A.push(fA(new BA(L,4,v)))}else if(L==="DMOD")A.push(fA(new BA(L,G.length,G)));else{let E=new N(G.length);NA(E,G),A.push(fA(new BA(L,G.length,E)))}let t=kA([new N([73,78,70,79]),...A]),n=fA(new BA("LIST",t.length,t));p("%cWriting SDTA...",I.info);let s=[],o=[],r=mo.call(this,s,o,e?.compress,e?.compressionQuality??.5,e.compressionFunction);p("%cWriting PDTA...",I.info),p("%cWriting SHDR...",I.info);let g=po.call(this,s,o);p("%cWriting IGEN...",I.info);let i=uo.call(this);p("%cWriting IMOD...",I.info);let C=yo.call(this);p("%cWriting IBAG...",I.info);let B=So.call(this);p("%cWriting INST...",I.info);let h=Do.call(this),c=ko.call(this);p("%cWriting PMOD...",I.info);let l=Fo.call(this);p("%cWriting PBAG...",I.info);let m=wo.call(this);p("%cWriting PHDR...",I.info);let f=Ro.call(this),S=kA([new N([112,100,116,97]),f,m,l,c,h,B,C,i,g]),F=fA(new BA("LIST",S.length,S));p("%cWriting the output file...",I.info);let D=kA([new N([115,102,98,107]),n,r,F]),x=fA(new BA("RIFF",D.length,D));return p(`%cSaved succesfully! Final file size: %c${x.length}`,I.info,I.recognized),q(),x}var Ht=class{velRange={min:-1,max:127};keyRange={min:-1,max:127};isGlobal=!1;generators=[];modulators=[];get hasKeyRange(){return this.keyRange.min!==-1}get hasVelRange(){return this.velRange.min!==-1}getGeneratorValue(A,t){return this.generators.find(n=>n.generatorType===A)?.generatorValue??t}};var OA=class extends Ht{sample=void 0;useCount=0;deleteZone(){this.useCount--,!this.isGlobal&&this.sample.useCount--}},Re=class extends Ht{instrument=void 0;deleteZone(){this.isGlobal||this.instrument.removeUseCount()}};var Di=new Set([a.velRange,a.keyRange,a.instrument,a.exclusiveClass,a.endOper,a.sampleModes,a.startloopAddrsOffset,a.startloopAddrsCoarseOffset,a.endloopAddrsOffset,a.endloopAddrsCoarseOffset,a.startAddrsOffset,a.startAddrsCoarseOffset,a.endAddrOffset,a.endAddrsCoarseOffset,a.initialAttenuation,a.fineTune,a.coarseTune,a.keyNumToVolEnvHold,a.keyNumToVolEnvDecay,a.keyNumToModEnvHold,a.keyNumToModEnvDecay]);function Mo(e,A=!0){function t(h,c){h.push(...c.filter(l=>!h.find(m=>m.generatorType===l.generatorType)))}function n(h,c){return{min:Math.max(h.min,c.min),max:Math.min(h.max,c.max)}}function s(h,c){h.push(...c.filter(l=>!h.find(m=>X.isIdentical(l,m))))}let o=[],r=[],g=[],i={min:0,max:127},C={min:0,max:127},B=e.presetZones.find(h=>h.isGlobal);B&&(r.push(...B.generators),g.push(...B.modulators),i=B.keyRange,C=B.velRange);for(let h of e.presetZones){if(h.isGlobal)continue;let c=h.keyRange;h.hasKeyRange||(c=i);let l=h.velRange;h.hasVelRange||(l=C);let m=h.generators.map(E=>new U(E.generatorType,E.generatorValue));t(m,r);let f=[...h.modulators];s(f,g);let S=h.instrument.instrumentZones,F=[],D=[],x={min:0,max:127},L={min:0,max:127},G=S.find(E=>E.isGlobal);G&&(F.push(...G.generators),D.push(...G.modulators),x=G.keyRange,L=G.velRange);for(let E of S){if(E.isGlobal)continue;let M=E.keyRange;E.hasKeyRange||(M=x);let v=E.velRange;if(E.hasVelRange||(v=L),M=n(M,c),v=n(v,l),M.max<M.min||v.max<v.min)continue;let eA=E.generators.map(oA=>new U(oA.generatorType,oA.generatorValue));t(eA,F);let $=[...E.modulators];s($,D);let T=[...$];for(let oA of f){let rA=T.findIndex(hA=>X.isIdentical(oA,hA));rA!==-1?T[rA]=T[rA].sumTransform(oA):T.push(oA)}let nA=eA.map(oA=>new U(oA.generatorType,oA.generatorValue));for(let oA of m){if(oA.generatorType===a.velRange||oA.generatorType===a.keyRange||oA.generatorType===a.instrument||oA.generatorType===a.endOper||oA.generatorType===a.sampleModes)continue;let rA=eA.findIndex(hA=>hA.generatorType===oA.generatorType);if(rA!==-1){let hA=nA[rA].generatorValue+oA.generatorValue;nA[rA]=new U(oA.generatorType,hA)}else{let hA=Z[oA.generatorType].def+oA.generatorValue;nA.push(new U(oA.generatorType,hA))}}nA=nA.filter(oA=>oA.generatorType!==a.sampleID&&oA.generatorType!==a.keyRange&&oA.generatorType!==a.velRange&&oA.generatorType!==a.endOper&&oA.generatorType!==a.instrument&&oA.generatorValue!==Z[oA.generatorType].def);let cA=new OA;cA.keyRange=M,cA.velRange=v,cA.keyRange.min===0&&cA.keyRange.max===127&&(cA.keyRange.min=-1),cA.velRange.min===0&&cA.velRange.max===127&&(cA.velRange.min=-1),cA.isGlobal=!1,cA.sample=E.sample,cA.generators=nA,cA.modulators=T,o.push(cA)}}if(A){let h=new OA;h.isGlobal=!0;for(let m=0;m<58;m++){if(Di.has(m))continue;let f={},S=Z[m]?.def||0;f[S]=0;for(let F of o){let D=F.generators.find(G=>G.generatorType===m);if(D){let G=D.generatorValue;f[G]===void 0?f[G]=1:f[G]++}else f[S]++;let x;switch(m){default:continue;case a.decayVolEnv:x=a.keyNumToVolEnvDecay;break;case a.holdVolEnv:x=a.keyNumToVolEnvHold;break;case a.decayModEnv:x=a.keyNumToModEnvDecay;break;case a.holdModEnv:x=a.keyNumToModEnvHold}if(F.generators.find(G=>G.generatorType===x)!==void 0){f={};break}}if(Object.keys(f).length>0){let F=Object.entries(f).reduce((x,L)=>x[1]<L[1]?L:x,[0,0]),D=parseInt(F[0]);D!==S&&h.generators.push(new U(m,D)),o.forEach(x=>{let L=x.generators.findIndex(G=>G.generatorType===m);L!==-1?x.generators[L].generatorValue===D&&x.generators.splice(L,1):D!==S&&x.generators.push(new U(m,S))})}}let l=o.find(m=>!m.isGlobal).modulators.map(m=>X.copy(m));for(let m of l){let f=!0;for(let S of o){if(S.isGlobal||!f)continue;S.modulators.find(D=>X.isIdentical(D,m))||(f=!1)}if(f===!0){h.modulators.push(X.copy(m));for(let S of o){let F=S.modulators.find(D=>X.isIdentical(D,m));F.transformAmount===m.transformAmount&&S.modulators.splice(S.modulators.indexOf(F),1)}}}o.splice(0,0,h)}return o}var xo=20;function En(e,A,t,n,s,o,r){let g=r===0?0:1,i=new N(xo+g*16);tA(i,xo),Y(i,A),Y(i,t);let C=n*.4,B=Math.floor(C*-65536);tA(i,B),tA(i,2);let h=o-s,c=0;switch(r){default:case 0:g=0;break;case 1:c=0,g=1;break;case 3:c=1,g=1}return tA(i,g),g===1&&(tA(i,16),tA(i,c),tA(i,s),tA(i,h)),j("wsmp",i)}var P={none:0,modLfo:1,velocity:2,keyNum:3,volEnv:4,modEnv:5,pitchWheel:6,polyPressure:7,channelPressure:8,vibratoLfo:9,modulationWheel:129,volume:135,pan:138,expression:139,chorus:221,reverb:219,pitchWheelRange:256,fineTune:257,coarseTune:258},hn=new X(219,0,a.reverbEffectsSend,1e3,0),cn=new X(221,0,a.chorusEffectsSend,1e3,0),Bn=new X(129,0,a.vibLfoToPitch,0,0),ln=new X(13,0,a.vibLfoToPitch,0,0);var R={none:0,gain:1,reserved:2,pitch:3,pan:4,keyNum:5,chorusSend:128,reverbSend:129,modLfoFreq:260,modLfoDelay:261,vibLfoFreq:276,vibLfoDelay:277,volEnvAttack:518,volEnvDecay:519,volEnvRelease:521,volEnvSustain:522,volEnvDelay:523,volEnvHold:524,modEnvAttack:778,modEnvDecay:779,modEnvRelease:781,modEnvSustain:782,modEnvDelay:783,modEnvHold:784,filterCutoff:1280,filterQ:1281};var Yt=class{source;control;destination;scale;transform;constructor(A,t,n,s,o){this.source=A,this.control=t,this.destination=n,this.scale=s,this.transform=o}writeArticulator(){let A=new N(12);return Y(A,this.source),Y(A,this.control),Y(A,this.destination),Y(A,this.transform),tA(A,this.scale<<16),A}};function No(e,A){if(e)switch(A){default:return;case y.modulationWheel:return P.modulationWheel;case y.mainVolume:return P.volume;case y.pan:return P.pan;case y.expressionController:return P.expression;case y.chorusDepth:return P.chorus;case y.reverbDepth:return P.reverb}else switch(A){default:return;case _.noteOnKeyNum:return P.keyNum;case _.noteOnVelocity:return P.velocity;case _.noController:return P.none;case _.polyPressure:return P.polyPressure;case _.channelPressure:return P.channelPressure;case _.pitchWheel:return P.pitchWheel;case _.pitchWheelRange:return P.pitchWheelRange}}function bo(e,A){switch(e){default:return;case a.initialAttenuation:return{dest:R.gain,amount:-A};case a.fineTune:return R.pitch;case a.pan:return R.pan;case a.keyNum:return R.keyNum;case a.reverbEffectsSend:return R.reverbSend;case a.chorusEffectsSend:return R.chorusSend;case a.freqModLFO:return R.modLfoFreq;case a.delayModLFO:return R.modLfoDelay;case a.delayVibLFO:return R.vibLfoDelay;case a.freqVibLFO:return R.vibLfoFreq;case a.delayVolEnv:return R.volEnvDelay;case a.attackVolEnv:return R.volEnvAttack;case a.holdVolEnv:return R.volEnvHold;case a.decayVolEnv:return R.volEnvDecay;case a.sustainVolEnv:return{dest:R.volEnvSustain,amount:1e3-A};case a.releaseVolEnv:return R.volEnvRelease;case a.delayModEnv:return R.modEnvDelay;case a.attackModEnv:return R.modEnvAttack;case a.holdModEnv:return R.modEnvHold;case a.decayModEnv:return R.modEnvDecay;case a.sustainModEnv:return{dest:R.modEnvSustain,amount:1e3-A};case a.releaseModEnv:return R.modEnvRelease;case a.initialFilterFc:return R.filterCutoff;case a.initialFilterQ:return R.filterQ}}function Lo(e,A){switch(e){default:return;case a.modEnvToFilterFc:return{source:P.modEnv,dest:R.filterCutoff,amt:A,isBipolar:!1};case a.modEnvToPitch:return{source:P.modEnv,dest:R.pitch,amt:A,isBipolar:!1};case a.modLfoToFilterFc:return{source:P.modLfo,dest:R.filterCutoff,amt:A,isBipolar:!0};case a.modLfoToVolume:return{source:P.modLfo,dest:R.gain,amt:A,isBipolar:!0};case a.modLfoToPitch:return{source:P.modLfo,dest:R.pitch,amt:A,isBipolar:!0};case a.vibLfoToPitch:return{source:P.vibratoLfo,dest:R.pitch,amt:A,isBipolar:!0};case a.keyNumToVolEnvHold:return{source:P.keyNum,dest:R.volEnvHold,amt:A,isBipolar:!0};case a.keyNumToVolEnvDecay:return{source:P.keyNum,dest:R.volEnvDecay,amt:A,isBipolar:!0};case a.keyNumToModEnvHold:return{source:P.keyNum,dest:R.modEnvHold,amt:A,isBipolar:!0};case a.keyNumToModEnvDecay:return{source:P.keyNum,dest:R.modEnvDecay,amt:A,isBipolar:!0};case a.scaleTuning:return{source:P.keyNum,dest:R.pitch,amt:A*128,isBipolar:!1}}}function To(e){let A=bo(e.generatorType,e.generatorValue),t=A,n=0,s=e.generatorValue;A?.amount!==void 0&&(s=A.amount,t=A.dest);let o=Lo(e.generatorType,e.generatorValue);if(o!==void 0)s=o.amt,t=o.dest,n=o.source;else if(t===void 0){J(`Invalid generator type: ${e.generatorType}`);return}return new Yt(n,0,t,s,0)}function Uo(e){if(e.transformType!==0){J("Other transform types are not supported.");return}let A=No(e.sourceUsesCC,e.sourceIndex),t=e.sourceCurveType,n=e.sourcePolarity,s=e.sourceDirection;if(A===void 0){J(`Invalid source: ${e.sourceIndex}, CC: ${e.sourceUsesCC}`);return}e.modulatorDestination===a.initialAttenuation&&(s=s===1?0:1);let o=No(e.secSrcUsesCC,e.secSrcIndex),r=e.secSrcCurveType,g=e.secSrcPolarity,i=e.secSrcDirection;if(o===void 0){J(`Invalid secondary source: ${e.secSrcIndex}, CC: ${e.secSrcUsesCC}`);return}let C=bo(e.modulatorDestination,e.transformAmount),B=C,h=e.transformAmount;C?.dest!==void 0&&(B=C.dest,h=C.amount);let c=Lo(e.modulatorDestination,e.transformAmount);if(c!==void 0)h=c.amt,o=A,r=t,g=n,i=s,t=FA.linear,n=c.isBipolar?1:0,s=0,A=c.source,B=c.dest;else if(B===void 0){J(`Invalid destination: ${e.modulatorDestination}`);return}let l=0;return l|=r<<4,l|=g<<8,l|=i<<9,l|=t,l|=n<<14,l|=s<<15,new Yt(A,o,B,h,l)}var ki=new Set([a.sampleModes,a.initialAttenuation,a.keyRange,a.velRange,a.sampleID,a.fineTune,a.coarseTune,a.startAddrsOffset,a.startAddrsCoarseOffset,a.endAddrOffset,a.endAddrsCoarseOffset,a.startloopAddrsOffset,a.startloopAddrsCoarseOffset,a.endloopAddrsOffset,a.endloopAddrsCoarseOffset,a.overridingRootKey,a.exclusiveClass]);function Qn(e){for(let o=0;o<e.generators.length;o++){let r=e.generators[o];(r.generatorType===a.delayVolEnv||r.generatorType===a.attackVolEnv||r.generatorType===a.holdVolEnv||r.generatorType===a.decayVolEnv||r.generatorType===a.releaseVolEnv||r.generatorType===a.delayModEnv||r.generatorType===a.attackModEnv||r.generatorType===a.holdModEnv||r.generatorType===a.decayModEnv)&&(e.generators[o]=new U(r.generatorType,Math.min(r.generatorValue,6386),!1))}for(let o=0;o<e.generators.length;o++){let r=e.generators[o],g;switch(r.generatorType){default:continue;case a.keyNumToVolEnvDecay:g=a.decayVolEnv;break;case a.keyNumToVolEnvHold:g=a.holdVolEnv;break;case a.keyNumToModEnvDecay:g=a.decayModEnv;break;case a.keyNumToModEnvHold:g=a.holdModEnv}let i=e.generators.find(m=>m.generatorType===g);if(i===void 0)continue;let C=r.generatorValue*-128,B=60/128*C,h=i.generatorValue-B,c=e.generators.indexOf(r),l=e.generators.indexOf(i);e.generators[l]=new U(g,h,!1),e.generators[c]=new U(r.generatorType,C,!1)}let A=e.generators.reduce((o,r)=>{if(ki.has(r.generatorType))return o;let g=To(r);return g!==void 0?(o.push(g),p("%cSucceeded converting to DLS Articulator!",I.recognized)):J("Failed converting to DLS Articulator!"),o},[]),t=e.modulators.reduce((o,r)=>{if(X.isIdentical(r,cn,!0)||X.isIdentical(r,hn,!0)||X.isIdentical(r,Bn,!0)||X.isIdentical(r,ln,!0))return o;let g=Uo(r);return g!==void 0?(o.push(g),p("%cSucceeded converting to DLS Articulator!",I.recognized)):J("Failed converting to DLS Articulator!"),o},[]);A.push(...t);let n=new N(8);tA(n,8),tA(n,A.length);let s=A.map(o=>o.writeArticulator());return j("art2",kA([n,...s]))}function vo(e,A){let t=new N(12);Y(t,Math.max(e.keyRange.min,0)),Y(t,e.keyRange.max),Y(t,Math.max(e.velRange.min,0)),Y(t,e.velRange.max),Y(t,0);let n=e.getGeneratorValue(a.exclusiveClass,0);Y(t,n),Y(t,0);let s=j("rgnh",t),o=e.getGeneratorValue(a.overridingRootKey,e.sample.samplePitch);e.getGeneratorValue(a.scaleTuning,A.getGeneratorValue(a.scaleTuning,100))===0&&e.keyRange.max-e.keyRange.min===0&&(o=e.keyRange.min);let g=En(e.sample,o,e.getGeneratorValue(a.fineTune,0)+e.getGeneratorValue(a.coarseTune,0)*100+e.sample.samplePitchCorrection,e.getGeneratorValue(a.initialAttenuation,0),e.sample.sampleLoopStartIndex+e.getGeneratorValue(a.startloopAddrsOffset,0)+e.getGeneratorValue(a.startloopAddrsCoarseOffset,0)*32768,e.sample.sampleLoopEndIndex+e.getGeneratorValue(a.endloopAddrsOffset,0)+e.getGeneratorValue(a.endloopAddrsCoarseOffset,0)*32768,e.getGeneratorValue(a.sampleModes,0)),i=new N(12);Y(i,0),Y(i,0),tA(i,1),tA(i,this.samples.indexOf(e.sample));let C=j("wlnk",i),B=new N(0);if(e.modulators.length+e.generators.length>0){let h=Qn(e);B=j("lar2",h,!1,!0)}return j("rgn2",kA([s,g,C,B]),!1,!0)}function Ho(e){uA(`%cWriting %c${e.presetName}%c...`,I.info,I.recognized,I.info);let A=Mo(e),t=A.reduce((c,l)=>l.isGlobal?c:c+1,0),n=new N(12);tA(n,t);let s=(e.bank&127)<<8;e.bank===128&&(s|=1<<31),tA(n,s),tA(n,e.program&127);let o=j("insh",n),r=new N(0),g=A.find(c=>c.isGlobal===!0);if(g){let c=Qn(g);r=j("lar2",c,!1,!0)}let i=kA(A.reduce((c,l)=>(l.isGlobal||c.push(vo.apply(this,[l,g])),c),[])),C=j("lrgn",i,!1,!0),B=j("INAM",pe(e.presetName)),h=j("INFO",B,!1,!0);return q(),j("ins ",kA([o,C,r,h]),!1,!0)}function Yo(){let e=kA(this.presets.map(A=>Ho.apply(this,[A])));return j("lins",e,!1,!0)}function Jo(e){let A=new N(18);Y(A,1),Y(A,1),tA(A,e.sampleRate),tA(A,e.sampleRate*2),Y(A,2),Y(A,16);let t=j("fmt ",A),n=1;e.sampleLoopStartIndex+Math.abs(e.getAudioData().length-e.sampleLoopEndIndex)<2&&(n=0);let s=En(e,e.samplePitch,e.samplePitchCorrection,0,e.sampleLoopStartIndex,e.sampleLoopEndIndex,n),o=e.getAudioData(),r;if(e.isCompressed){let C=new Int16Array(o.length);for(let B=0;B<o.length;B++)C[B]=o[B]*32767;r=j("data",new N(C.buffer))}else r=j("data",e.getRawData());let g=j("INAM",pe(e.sampleName)),i=j("INFO",g,!1,!0);return p(`%cSaved %c${e.sampleName}%c succesfully!`,I.recognized,I.value,I.recognized),j("wave",kA([t,s,r,i]),!1,!0)}function Oo(){let e=0,A=[],t=this.samples.map(n=>{let s=Jo(n);return A.push(e),e+=s.length,s});return{data:j("wvpl",kA(t),!1,!0),indexes:A}}function Ko(){uA("%cSaving DLS...",I.info);let e=new N(4);tA(e,this.presets.length);let A=j("colh",e);uA("%cWriting instruments...",I.info);let t=Yo.apply(this);p("%cSuccess!",I.recognized),q(),uA("%cWriting WAVE samples...",I.info);let n=Oo.apply(this),s=n.data,o=n.indexes;p("%cSucceeded!",I.recognized),q();let r=new N(8+4*o.length);tA(r,8),tA(r,o.length);for(let h of o)tA(r,h);let g=j("ptbl",r);this.soundFontInfo.ICMT=(this.soundFontInfo.ICMT||"Soundfont")+`
10
+ `}sumTransform(A){return new e(this.sourceEnum,this.secondarySourceEnum,this.modulatorDestination,this.transformAmount+A.transformAmount,this.transformType)}},As=960,es=FA.concave;function ZA(e,A,t,n,s){return e<<10|A<<9|t<<8|n<<7|s}var di=[new X(ZA(es,0,1,0,_.noteOnVelocity),0,a.initialAttenuation,As,0),new X(129,0,a.vibLfoToPitch,50,0),new X(ZA(es,0,1,1,y.mainVolume),0,a.initialAttenuation,As,0),new X(13,0,a.vibLfoToPitch,50,0),new X(526,16,a.fineTune,12700,0),new X(650,0,a.pan,500,0),new X(ZA(es,0,1,1,y.expressionController),0,a.initialAttenuation,As,0),new X(219,0,a.reverbEffectsSend,200,0),new X(221,0,a.chorusEffectsSend,200,0)],fi=[new X(ZA(FA.linear,0,0,0,_.polyPressure),0,a.vibLfoToPitch,50,0),new X(ZA(FA.linear,0,0,1,y.tremoloDepth),0,a.modLfoToVolume,24,0),new X(ZA(FA.convex,1,0,1,y.attackTime),0,a.attackVolEnv,6e3,0),new X(ZA(FA.linear,1,0,1,y.releaseTime),0,a.releaseVolEnv,3600,0),new X(ZA(FA.linear,1,0,1,y.brightness),0,a.initialFilterFc,6e3,0),new X(ZA(FA.linear,1,0,1,y.filterResonance),0,a.initialFilterQ,250,0)],$s=di.concat(fi);var lA=128,rn=147,He=new Int16Array(rn).fill(0),MA=(e,A)=>He[e]=A<<7;MA(y.mainVolume,100);MA(y.balance,64);MA(y.expressionController,127);MA(y.pan,64);MA(y.portamentoOnOff,127);MA(y.filterResonance,64);MA(y.releaseTime,64);MA(y.attackTime,64);MA(y.brightness,64);MA(y.decayTime,64);MA(y.vibratoRate,64);MA(y.vibratoDepth,64);MA(y.vibratoDelay,64);MA(y.generalPurposeController6,64);MA(y.generalPurposeController8,64);MA(y.RPNLsb,127);MA(y.RPNMsb,127);MA(y.NRPNLsb,127);MA(y.NRPNMsb,127);var an=1;He[y.portamentoControl]=an;MA(lA+_.pitchWheel,64);MA(lA+_.pitchWheelRange,2);var EA={channelTuning:0,channelTransposeFine:1,modulationMultiplier:2,masterTuning:3,channelTuningSemitones:4,channelKeyShift:5},ts=Object.keys(EA).length,ns=new Float32Array(ts);ns[EA.modulationMultiplier]=1;var vA={Idle:0,RPCoarse:1,RPFine:2,NRPCoarse:3,NRPFine:4,DataCoarse:5,DataFine:6},ss={velocityOverride:128};function Ao(e,A,t){let n=e,s=A<<7|t;return e===127&&A===127&&t===127?{midiNote:-1,centTuning:null}:{midiNote:n,centTuning:s*.0061}}function eo(e,A=0){let t=e[0];if(this.deviceID!==-1&&e[1]!==127&&this.deviceID!==e[1])return;function n(o,r,g,i){p(`%cChannel %c${o}%c ${g}. %c${r} ${i}%c, with %c${HA(e)}`,I.info,I.recognized,I.info,I.value,I.info,I.value)}function s(){J(`%cUnrecognized Roland %cGS %cSysEx: %c${HA(e)}`,I.warn,I.recognized,I.warn,I.unrecognized)}switch(t){default:J(`%cUnrecognized SysEx: %c${HA(e)}`,I.warn,I.unrecognized);break;case 126:case 127:switch(e[2]){case 4:let o;switch(e[3]){case 1:let g=e[5]<<7|e[4];this.setMIDIVolume(g/16384),p(`%cMaster Volume. Volume: %c${g}`,I.info,I.value);break;case 2:let C=((e[5]<<7|e[4])-8192)/8192;this.setMasterParameter(JA.masterPan,C),p(`%cMaster Pan. Pan: %c${C}`,I.info,I.value);break;case 3:let B=(e[5]<<7|e[6])-8192;o=Math.floor(B/81.92),this.setMasterTuning(o),p(`%cMaster Fine Tuning. Cents: %c${o}`,I.info,I.value);break;case 4:o=(e[5]-64)*100,this.setMasterTuning(o),p(`%cMaster Coarse Tuning. Cents: %c${o}`,I.info,I.value);break;default:J(`%cUnrecognized MIDI Device Control Real-time message: %c${HA(e)}`,I.warn,I.unrecognized)}break;case 9:e[3]===1?(p("%cGM1 system on",I.info),this.setSystem("gm")):e[3]===3?(p("%cGM2 system on",I.info),this.setSystem("gm2")):(p("%cGM system off, defaulting to GS",I.info),this.setSystem("gs"));break;case 8:let r=4;switch(e[3]){case 1:let g=e[r++];e.currentIndex=r;let i=AA(e,16);if(r+=16,e.length<384){J(`The Bulk Tuning Dump is too short! (${e.length} bytes, at least 384 are expected)`);return}for(let c=0;c<128;c++)this.tunings[g][c]=Ao(e[r++],e[r++],e[r++]);p(`%cBulk Tuning Dump %c${i}%c Program: %c${g}`,I.info,I.value,I.info,I.recognized);break;case 2:case 7:e[3]===7&&r++;let C=e[r++],B=e[r++];for(let c=0;c<B;c++)this.tunings[C][e[r++]]=Ao(e[r++],e[r++],e[r++]);p(`%cSingle Note Tuning. Program: %c${C}%c Keys affected: %c${B}`,I.info,I.recognized,I.info,I.recognized);break;case 9:case 8:let h=new Int8Array(12);if(e[3]===8)for(let c=0;c<12;c++)h[c]=e[7+c]-64;else for(let c=0;c<24;c+=2){let l=(e[7+c]<<7|e[8+c])-8192;h[c/2]=Math.floor(l/81.92)}(e[4]&1)===1&&this.midiAudioChannels[14+A].setOctaveTuning(h),(e[4]>>1&1)===1&&this.midiAudioChannels[15+A].setOctaveTuning(h);for(let c=0;c<7;c++)(e[5]>>c&1)===1&&this.midiAudioChannels[7+c+A].setOctaveTuning(h);for(let c=0;c<7;c++)(e[6]>>c&1)===1&&this.midiAudioChannels[c+A].setOctaveTuning(h);p(`%cMIDI Octave Scale ${e[3]===8?"(1 byte)":"(2 bytes)"} tuning via Tuning: %c${h.join(" ")}`,I.info,I.value);break;default:J(`%cUnrecognized MIDI Tuning standard message: %c${HA(e)}`,I.warn,I.unrecognized);break}break;default:J(`%cUnrecognized MIDI Realtime/non realtime message: %c${HA(e)}`,I.warn,I.unrecognized)}break;case 65:if(e[2]===66&&e[3]===18){let o=e[7];if(e[4]===64||e[4]===0&&e[6]===127){if((e[5]&16)>0){let r=[9,0,1,2,3,4,5,6,7,8,10,11,12,13,14,15][e[5]&15]+A,g=this.midiAudioChannels[r];switch(e[6]){default:s();break;case 21:let i=o>0&&e[5]>>4;g.setDrums(i),p(`%cChannel %c${r}%c ${i?"is now a drum channel":"now isn't a drum channel"}%c via: %c${HA(e)}`,I.info,I.value,I.recognized,I.info,I.value);return;case 22:let C=o-64;g.setCustomController(EA.channelKeyShift,C),n(r,C,"key shift","keys");return;case 28:let B=o;B===0?(g.randomPan=!0,p(`%cRandom pan is set to %cON%c for %c${r}`,I.info,I.recognized,I.info,I.value)):(g.randomPan=!1,g.controllerChange(y.pan,B));break;case 33:g.controllerChange(y.chorusDepth,o);break;case 34:g.controllerChange(y.reverbDepth,o);break;case 64:case 65:case 66:case 67:case 68:case 69:case 70:case 71:case 72:case 73:case 74:case 75:let h=e.length-9,c=new Int8Array(12);for(let m=0;m<h;m++)c[m]=e[m+7]-64;g.setOctaveTuning(c);let l=o-64;n(r,c.join(" "),"octave scale tuning","cents"),g.setTuning(l);break}return}else if((e[5]&32)>0){let r=[9,0,1,2,3,4,5,6,7,8,10,11,12,13,14,15][e[5]&15]+A,g=this.midiAudioChannels[r],i=o-64,C=i/64,B=o/128,h=(c,l,m=!1)=>{switch(e[6]&15){case 0:g.sysExModulators.setModulator(c,a.fineTune,i*100,m),n(r,i,`${l} pitch control`,"semitones");break;case 1:g.sysExModulators.setModulator(c,a.initialFilterFc,C*9600,m),n(r,C*9600,`${l} pitch control`,"cents");break;case 2:g.sysExModulators.setModulator(c,a.initialAttenuation,C*960,m),n(r,C*960,`${l} amplitude`,"cB");break;case 4:g.sysExModulators.setModulator(c,a.vibLfoToPitch,B*600),n(r,B*600,`${l} LFO1 pitch depth`,"cents");break;case 5:g.sysExModulators.setModulator(c,a.vibLfoToFilterFc,B*2400),n(r,B*2400,`${l} LFO1 filter depth`,"cents");break;case 6:g.sysExModulators.setModulator(c,a.vibLfoToVolume,B*960),n(r,B*960,`${l} LFO1 amplitude depth`,"cB");break;case 8:g.sysExModulators.setModulator(c,a.modLfoToPitch,B*600),n(r,B*600,`${l} LFO2 pitch depth`,"cents");break;case 9:g.sysExModulators.setModulator(c,a.modLfoToFilterFc,B*2400),n(r,B*2400,`${l} LFO2 filter depth`,"cents");break;case 10:g.sysExModulators.setModulator(c,a.modLfoToVolume,B*960),n(r,B*960,`${l} LFO2 amplitude depth`,"cB");break}};switch(e[6]&240){default:s();break;case 0:h(y.modulationWheel,"mod wheel");break;case 16:h(lA+_.pitchWheel,"pitch bend",!0);break;case 32:h(lA+_.channelPressure,"channel pressure");break;case 48:h(lA+_.polyPressure,"poly pressure");break}return}else if(e[5]===0){switch(e[6]){default:s();break;case 127:o===0?(p("%cGS Reset received!",I.info),this.resetAllControllers(!1),this.setSystem("gs")):o===127&&(p("%cGS system off, switching to GM2",I.info),this.resetAllControllers(!1),this.setSystem("gm2"));break;case 6:p(`%cRoland GS Master Pan set to: %c${o}%c with: %c${HA(e)}`,I.info,I.value,I.info,I.value),this.setMasterParameter(JA.masterPan,(o-64)/64);break;case 4:p(`%cRoland GS Master Volume set to: %c${o}%c with: %c${HA(e)}`,I.info,I.value,I.info,I.value),this.setMIDIVolume(o/127);break;case 5:let r=o-64;p(`%cRoland GS Master Key-Shift set to: %c${r}%c with: %c${HA(e)}`,I.info,I.value,I.info,I.value),this.setMasterTuning(r*100);break}return}else if(e[5]===1)switch(e[6]){default:s();break;case 0:e.currentIndex=7;let r=AA(e,16);p(`%cGS Patch name: %c${r}`,I.info,I.value);break;case 51:p(`%cGS Reverb level: %c${o}`,I.info,I.value),this.reverbSend=o/64;break;case 48:case 49:case 50:case 52:case 53:case 55:p(`%cUnsupported GS Reverb Parameter: %c${e[6].toString(16)}`,I.warn,I.unrecognized);break;case 58:p(`%cGS Chorus level: %c${o}`,I.info,I.value),this.chorusSend=o/64;break;case 56:case 57:case 59:case 60:case 61:case 62:case 63:case 64:p(`%cUnsupported GS Chorus Parameter: %c${e[6].toString(16)}`,I.warn,I.unrecognized);break}}else s();return}else if(e[2]===69&&e[3]===18){if(e[4]===16&&e[6]===0)if(e[5]===0){let o=new Uint8Array(e.slice(7,e.length-2));this.callEvent("synthdisplay",{displayData:o,displayType:Ut.SoundCanvasText})}else if(e[5]===1){let o=new Uint8Array(e.slice(7,e.length-3));this.callEvent("synthdisplay",{displayData:o,displayType:Ut.SoundCanvasDotDisplay}),p(`%cRoland SC Display Dot Matrix via: %c${HA(e)}`,I.info,I.value)}else s()}else if(e[2]===22&&e[3]===18&&e[4]===16){this.setMIDIVolume(e[7]/100),p(`%cRoland Master Volume control set to: %c${e[7]}%c via: %c${HA(e)}`,I.info,I.value,I.info,I.value);return}else{J(`%cUnrecognized Roland SysEx: %c${HA(e)}`,I.warn,I.unrecognized);return}break;case 67:if(e[2]===76)if(e[3]===0&&e[4]===0)switch(e[5]){case 4:let o=e[6];this.setMIDIVolume(o/127),p(`%cXG master volume. Volume: %c${o}`,I.info,I.recognized);break;case 6:let r=e[6]-64;this.transposeAllChannels(r),p(`%cXG master transpose. Volume: %c${r}`,I.info,I.recognized);break;case 126:p("%cXG system on",I.info),this.resetAllControllers(!1),this.setSystem("xg");break}else if(e[3]===8){if(!GA(this.system))return;let o=e[4]+A;if(o>=this.midiAudioChannels.length)return;let r=this.midiAudioChannels[o],g=e[6];switch(e[5]){case 1:r.controllerChange(y.bankSelect,g);break;case 2:r.controllerChange(y.lsbForControl0BankSelect,g);break;case 3:r.programChange(g);break;case 8:if(r.drumChannel)return;let i=g-64;r.channelTransposeKeyShift=i;break;case 11:r.controllerChange(y.mainVolume,g);break;case 14:let C=g;C===0?(r.randomPan=!0,p(`%cRandom pan is set to %cON%c for %c${o}`,I.info,I.recognized,I.info,I.value)):r.controllerChange(y.pan,C);break;case 19:r.controllerChange(y.reverbDepth,g);break;case 18:r.controllerChange(y.chorusDepth,g);break;default:J(`%cUnrecognized Yamaha XG Part Setup: %c${e[5].toString(16).toUpperCase()}`,I.warn,I.unrecognized)}}else if(e[3]===6&&e[4]===0){let o=new Uint8Array(e.slice(5,e.length-1));this.callEvent("synthdisplay",{displayData:o,displayType:Ut.XGText})}else GA(this.system)&&J(`%cUnrecognized Yamaha XG SysEx: %c${HA(e)}`,I.warn,I.unrecognized);else GA(this.system)&&J(`%cUnrecognized Yamaha SysEx: %c${HA(e)}`,I.warn,I.unrecognized);break}}function to(e=!0){e&&p("%cResetting all controllers!",I.info),this.callEvent("allcontrollerreset",void 0),this.setSystem(Lt);for(let A=0;A<this.midiAudioChannels.length;A++){this.midiAudioChannels[A].resetControllers();let t=this.midiAudioChannels[A];t.lockPreset?this.callEvent("drumchange",{channel:A,isDrumChannel:t.drumChannel}):(t.setBankSelect(zs(this.system)),A%16===9?(t.setPreset(this.drumPreset),t.drumChannel=!0,this.callEvent("drumchange",{channel:A,isDrumChannel:!0})):(t.drumChannel=!1,t.setPreset(this.defaultPreset),this.callEvent("drumchange",{channel:A,isDrumChannel:!1})));let n=t.preset.bank;this.callEvent("programchange",{channel:A,program:t.preset.program,bank:n});for(let s=0;s<128;s++)this.midiAudioChannels[A].lockedControllers[s]&&this.callEvent("controllerchange",{channel:A,controllerNumber:s,controllerValue:this.midiAudioChannels[A].midiControllers[s]>>7});if(this.midiAudioChannels[A].lockedControllers[lA+_.pitchWheel]===!1){let s=this.midiAudioChannels[A].midiControllers[lA+_.pitchWheel],o=s>>7,r=s&127;this.callEvent("pitchwheel",{channel:A,MSB:o,LSB:r})}}this.tunings=[],this.tunings=[];for(let A=0;A<128;A++)this.tunings.push([]);this.setMIDIVolume(1)}function no(){this.channelOctaveTuning.fill(0);for(let A=0;A<He.length;A++){if(this.lockedControllers[A])continue;let t=He[A];this.midiControllers[A]!==t&&A<127?A===y.portamentoControl?this.midiControllers[A]=an:this.controllerChange(A,t>>7):this.midiControllers[A]=t}this.channelVibrato={rate:0,depth:0,delay:0},this.holdPedal=!1,this.randomPan=!1,this.sysExModulators.resetModulators();let e=this.customControllers[EA.channelTransposeFine];this.customControllers.set(ns),this.setCustomController(EA.channelTransposeFine,e),this.resetParameters()}var os=new Set([y.bankSelect,y.lsbForControl0BankSelect,y.mainVolume,y.lsbForControl7MainVolume,y.pan,y.lsbForControl10Pan,y.reverbDepth,y.tremoloDepth,y.chorusDepth,y.detuneDepth,y.phaserDepth,y.soundVariation,y.filterResonance,y.releaseTime,y.attackTime,y.brightness,y.decayTime,y.vibratoRate,y.vibratoDepth,y.vibratoDelay,y.soundController10]);function so(){this.channelOctaveTuning.fill(0),this.pitchWheel(64,0),this.channelVibrato={rate:0,depth:0,delay:0};for(let e=0;e<128;e++){let A=He[e];!os.has(e)&&A!==this.midiControllers[e]&&(e===y.portamentoControl?this.midiControllers[e]=an:this.controllerChange(e,A>>7))}}function oo(){this.dataEntryState=vA.Idle,p("%cResetting Registered and Non-Registered Parameters!",I.info)}var lt=class{soundfontList=[];presetList=[];constructor(A){this.presetListChangeCallback=A}generatePresetList(){let A={};for(let t=this.soundfontList.length-1;t>=0;t--){let n=this.soundfontList[t],s=new Set;for(let o of n.soundfont.presets){let g=`${Math.min(128,o.bank+n.bankOffset)}-${o.program}`;s.has(g)||(s.add(g),A[g]=o.presetName)}}this.presetList=[];for(let[t,n]of Object.entries(A)){let s=t.split("-");this.presetList.push({presetName:n,program:parseInt(s[1]),bank:parseInt(s[0])})}this.presetListChangeCallback()}getPresetList(){return this.presetList.slice()}reloadManager(A){this.soundfontList=this.soundfontList.filter(t=>t.id===Xe),this.soundfontList.push({id:"main",bankOffset:0,soundfont:A}),this.generatePresetList()}deleteSoundFont(A){if(this.soundfontList.length===0){J("1 soundfont left. Aborting!");return}let t=this.soundfontList.findIndex(n=>n.id===A);if(t===-1){p(`No soundfont with id of "${A}" found. Aborting!`);return}delete this.soundfontList[t].soundfont.presets,delete this.soundfontList[t].soundfont.instruments,delete this.soundfontList[t].soundfont.samples,this.soundfontList.splice(t,1),this.generatePresetList()}addNewSoundFont(A,t,n){if(this.soundfontList.find(s=>s.id===t)!==void 0){let s=this.soundfontList.find(o=>o.id===t);s.soundfont=A,s.bankOffset=n}else this.soundfontList.push({id:t,soundfont:A,bankOffset:n});this.generatePresetList()}getCurrentSoundFontOrder(){return this.soundfontList.map(A=>A.id)}rearrangeSoundFonts(A){this.soundfontList.sort((t,n)=>A.indexOf(t.id)-A.indexOf(n.id)),this.generatePresetList()}getPreset(A,t,n=!1){if(this.soundfontList.length<1)throw new Error("No soundfonts! Did you forget to add one?");let s=A===128||n&&Ae(A);for(let o of this.soundfontList){let r=o.soundfont.getPresetNoFallback(A===128?128:A-o.bankOffset,t,n);if(r!==void 0)return{preset:r,bankOffset:o.bankOffset}}if(s){for(let r of this.soundfontList){let g=r.soundfont.presets.find(C=>C.isDrumPreset(n)&&C.program===t);if(g)return{preset:g,bankOffset:r.bankOffset};let i=r.soundfont.presets.find(C=>C.isDrumPreset(n));if(i)return{preset:i,bankOffset:r.bankOffset}}let o=this.soundfontList[0];return{preset:o.soundfont.presets[0],bankOffset:o.bankOffset}}else{for(let r of this.soundfontList){let g=r.soundfont.presets.find(i=>i.program===t&&!i.isDrumPreset(n));if(g)return{preset:g,bankOffset:r.bankOffset}}let o=this.soundfontList[0];return{preset:o.soundfont.presets[0],bankOffset:o.bankOffset}}}destroyManager(){this.soundfontList.forEach(A=>{A.soundfont.destroySoundBank()}),delete this.soundfontList}};var gn=class{_keyMappings=[];addMapping(A,t,n){this._keyMappings[A]===void 0&&(this._keyMappings[A]=[]),this._keyMappings[A][t]=n}deleteMapping(A,t){this._keyMappings[A]?.[t]!==void 0&&(this._keyMappings[A][t]=void 0)}clearMappings(){this._keyMappings=[]}setMappings(A){this._keyMappings=A}getMappings(){return this._keyMappings}getVelocity(A,t){return this._keyMappings[A]?.[t]?.velocity??-1}getGain(A,t){return this._keyMappings[A]?.[t]?.gain??1}hasOverridePatch(A,t){let n=this._keyMappings[A]?.[t]?.patch?.bank;return n!==void 0&&n>=0}getPatch(A,t){let n=this._keyMappings[A]?.[t];if(n)return n.patch;throw new Error("No modifier.")}};var ro=.1,Ye=class e{static cachedCoefficients=[];a0=0;a1=0;a2=0;a3=0;a4=0;x1=0;x2=0;y1=0;y2=0;resonanceCb=0;currentInitialFc=13500;lastTargetCutoff=1/0;initialized=!1;sampleRate;constructor(A){this.sampleRate=A,this.maxCutoff=A*.45}static apply(A,t,n,s){let o=A.modulatedGenerators[a.initialFilterFc],r=A.filter;r.initialized?r.currentInitialFc+=(o-r.currentInitialFc)*s:(r.initialized=!0,r.currentInitialFc=o);let g=r.currentInitialFc+n,i=A.modulatedGenerators[a.initialFilterQ];if(r.currentInitialFc>13499&&g>13499&&i===0){r.currentInitialFc=13500;return}(Math.abs(r.lastTargetCutoff-g)>1||r.resonanceCb!==i)&&(r.lastTargetCutoff=g,r.resonanceCb=i,e.calculateCoefficients(r,g));for(let C=0;C<t.length;C++){let B=t[C],h=r.a0*B+r.a1*r.x1+r.a2*r.x2-r.a3*r.y1-r.a4*r.y2;r.x2=r.x1,r.x1=B,r.y2=r.y1,r.y1=h,t[C]=h}}static calculateCoefficients(A,t){t=~~t;let n=A.resonanceCb,s=e.cachedCoefficients?.[n]?.[t];if(s!==void 0){A.a0=s.a0,A.a1=s.a1,A.a2=s.a2,A.a3=s.a3,A.a4=s.a4;return}let o=Tt(t);o=Math.min(o,A.maxCutoff);let r=n/10,g=ge(-(r-3.01)),i=1/Math.sqrt(ge(-r)),C=2*Math.PI*o/A.sampleRate,B=Math.cos(C),h=Math.sin(C)/(2*g),c=(1-B)*i,l=c/2,m=l,f=1+h,S=-2*B,F=1-h,D={};D.a0=l/f,D.a1=c/f,D.a2=m/f,D.a3=S/f,D.a4=F/f,A.a0=D.a0,A.a1=D.a1,A.a2=D.a2,A.a3=D.a3,A.a4=D.a4,e.cachedCoefficients[n]===void 0&&(e.cachedCoefficients[n]=[]),e.cachedCoefficients[n][t]=D}},rs=new Ye(44100);rs.resonanceCb=0;for(let e=1500;e<13500;e++)rs.currentInitialFc=e,Ye.calculateCoefficients(rs,e);var bA=16384,we=new Float32Array(bA+1),ze=new Float32Array(bA+1);we[0]=0;we[we.length-1]=1;ze[0]=0;ze[ze.length-1]=1;for(let e=1;e<bA-1;e++){let A=-.4166666666666667*Math.log(e/(we.length-1))/Math.LN10;ze[e]=1-A,we[we.length-1-e]=A}function je(e,A,t,n){switch(e&&(t=1-t),A){case FA.linear:return n?t*2-1:t;case FA.switch:return t=t>.5?1:0,n?t*2-1:t;case FA.concave:return n?(t=t*2-1,t<0?-we[~~(t*-bA)]:we[~~(t*bA)]):we[~~(t*bA)];case FA.convex:return n?(t=t*2-1,t<0?-ze[~~(t*-bA)]:ze[~~(t*bA)]):ze[~~(t*bA)]}}var is=1,as=new Float32Array(1e3);for(let e=0;e<as.length;e++)as[e]=je(0,FA.convex,e/1e3,0);var he=class e{attackDuration=0;decayDuration=0;holdDuration=0;releaseDuration=0;sustainLevel=0;delayEnd=0;attackEnd=0;holdEnd=0;decayEnd=0;releaseStartLevel=0;currentValue=0;static startRelease(A){e.recalculate(A)}static recalculate(A){let t=A.modulationEnvelope;A.isInRelease&&(t.releaseStartLevel=e.getValue(A,A.releaseStartTime,!0)),t.sustainLevel=1-A.modulatedGenerators[a.sustainModEnv]/1e3,t.attackDuration=Ce(A.modulatedGenerators[a.attackModEnv]);let n=(60-A.midiNote)*A.modulatedGenerators[a.keyNumToModEnvDecay],s=Ce(A.modulatedGenerators[a.decayModEnv]+n);t.decayDuration=s*(1-t.sustainLevel);let o=(60-A.midiNote)*A.modulatedGenerators[a.keyNumToModEnvHold];t.holdDuration=Ce(o+A.modulatedGenerators[a.holdModEnv]);let r=Ce(A.modulatedGenerators[a.releaseModEnv]);t.releaseDuration=r*t.releaseStartLevel,t.delayEnd=A.startTime+Ce(A.modulatedGenerators[a.delayModEnv]),t.attackEnd=t.delayEnd+t.attackDuration,t.holdEnd=t.attackEnd+t.holdDuration,t.decayEnd=t.holdEnd+t.decayDuration}static getValue(A,t,n=!1){let s=A.modulationEnvelope;return A.isInRelease&&!n?s.releaseStartLevel===0?0:Math.max(0,(1-(t-A.releaseStartTime)/s.releaseDuration)*s.releaseStartLevel):(t<s.delayEnd?s.currentValue=0:t<s.attackEnd?s.currentValue=as[~~((1-(s.attackEnd-t)/s.attackDuration)*1e3)]:t<s.holdEnd?s.currentValue=is:t<s.decayEnd?s.currentValue=(1-(s.decayEnd-t)/s.decayDuration)*(s.sustainLevel-is)+is:s.currentValue=s.sustainLevel,s.currentValue)}};var ui=-2320,mi=-1130,In=class{sampleData;playbackStep=0;cursor=0;rootKey=0;loopStart=0;loopEnd=0;end=0;loopingMode=0;isLooping=!1;constructor(A,t,n,s,o,r,g,i){this.sampleData=A,this.playbackStep=t,this.cursor=n,this.rootKey=s,this.loopStart=o,this.loopEnd=r,this.end=g,this.loopingMode=i,this.isLooping=this.loopingMode===1||this.loopingMode===3}},vt=class e{sample;filter;gain=1;generators;modulators=[];modulatedGenerators;finished=!1;isInRelease=!1;velocity=0;midiNote=0;pressure=0;targetKey=0;modulationEnvelope=new he;volumeEnvelope;startTime=0;releaseStartTime=1/0;currentTuningCents=0;currentTuningCalculated=1;currentPan=0;realKey;portamentoFromKey=-1;portamentoDuration=0;overridePan=0;exclusiveClass=0;constructor(A,t,n,s,o,r,g,i,C){this.sample=t,this.generators=i,this.exclusiveClass=this.generators[a.exclusiveClass],this.modulatedGenerators=new Int16Array(i),this.modulators=C,this.filter=new Ye(A),this.velocity=s,this.midiNote=n,this.startTime=o,this.targetKey=r,this.realKey=g,this.volumeEnvelope=new Ee(A,i[a.sustainVolEnv])}static copy(A,t,n){let s=A.sample,o=new In(s.sampleData,s.playbackStep,s.cursor,s.rootKey,s.loopStart,s.loopEnd,s.end,s.loopingMode);return new e(A.volumeEnvelope.sampleRate,o,A.midiNote,A.velocity,t,A.targetKey,n,A.generators,A.modulators.map(r=>X.copy(r)))}exclusiveRelease(A){this.release(A,Io),this.modulatedGenerators[a.releaseVolEnv]=ui,this.modulatedGenerators[a.releaseModEnv]=mi,Ee.recalculate(this),he.recalculate(this)}release(A,t=go){this.releaseStartTime=A,this.releaseStartTime-this.startTime<t&&(this.releaseStartTime=this.startTime+t)}};function io(e,A,t,n,s,o){let r=e.getSamplesAndGenerators(n,s).reduce((g,i)=>{if(i.sample.getAudioData()===void 0)return J(`Discarding invalid sample: ${i.sample.sampleName}`),g;let C=new Int16Array(Vs);for(let S=0;S<60;S++)C[S]=Ps(S,i.presetGenerators,i.instrumentGenerators);C[a.initialAttenuation]=Math.floor(C[a.initialAttenuation]*.4);let B=i.sample.samplePitch;C[a.overridingRootKey]>-1&&(B=C[a.overridingRootKey]);let h=n;C[a.keyNum]>-1&&(h=C[a.keyNum]);let c=i.sample.sampleLoopStartIndex,l=i.sample.sampleLoopEndIndex,m=C[a.sampleModes],f=new In(i.sample.sampleData,i.sample.sampleRate/this.sampleRate*Math.pow(2,i.sample.samplePitchCorrection/1200),0,B,c,l,Math.floor(i.sample.sampleData.length)-1,m);return C[a.velocity]>-1&&(s=C[a.velocity]),g.push(new vt(this.sampleRate,f,n,s,this.currentSynthTime,h,o,C,i.modulators.map(S=>X.copy(S)))),g},[]);return this.setCachedVoice(A,t,n,s,r.map(g=>vt.copy(g,this.currentSynthTime,o))),r}function ao(e,A,t,n){let s=this.midiAudioChannels[e],o=this.keyModifierManager.hasOverridePatch(e,A),r=s.getBankSelect(),g=s.preset.program;if(o){let B=this.keyModifierManager.getPatch(e,A);r=B.bank,g=B.program}let i=this.getCachedVoice(r,g,A,t);if(i!==void 0)return i.map(B=>vt.copy(B,this.currentSynthTime,n));let C=s.preset;return o&&(C=this.getPreset(r,g)),this.getVoicesForPreset(C,r,g,A,t,n)}var Eo=.05,pi=4600,yi=2e3,Co=Math.PI/2,Cn=-500,ho=500,gs=ho-Cn,co=new Float32Array(gs+1),Bo=new Float32Array(gs+1);for(let e=Cn;e<=ho;e++){let A=(e-Cn)/gs,t=e-Cn;co[t]=Math.cos(Co*A),Bo[t]=Math.sin(Co*A)}function lo(e,A,t,n,s,o,r,g){if(isNaN(A[0]))return;let i;e.overridePan?i=e.overridePan:(e.currentPan+=(e.modulatedGenerators[a.pan]-e.currentPan)*this.synth.panSmoothingFactor,i=e.currentPan);let C=this.synth.currentGain*e.gain,B=~~(i+500),h=co[B]*C*this.synth.panLeft,c=Bo[B]*C*this.synth.panRight;if(this.synth.effectsEnabled){let l=e.modulatedGenerators[a.reverbEffectsSend];if(l>0){let f=this.synth.reverbGain*this.synth.reverbSend*C*(l/pi);for(let S=0;S<A.length;S++)s[S]+=f*A[S];o.set(s)}let m=e.modulatedGenerators[a.chorusEffectsSend];if(m>0){let f=this.synth.chorusGain*this.synth.chorusSend*(m/yi),S=h*f,F=c*f;for(let D=0;D<A.length;D++)r[D]+=S*A[D],g[D]+=F*A[D]}}if(h>0)for(let l=0;l<A.length;l++)t[l]+=h*A[l];if(c>0)for(let l=0;l<A.length;l++)n[l]+=c*A[l]}function Qo(e=!1){p("%cStop all received!",I.info);for(let A=0;A<this.midiAudioChannels.length;A++)this.midiAudioChannels[A].stopAllNotes(e);this.callEvent("stopall",void 0)}function b(e,A){let t=0;for(let n=0;n<A;n++)t|=e[e.currentIndex++]<<n*8;return t>>>0}function Je(e,A,t){for(let n=0;n<t;n++)e[e.currentIndex++]=A>>n*8&255}function Y(e,A){e[e.currentIndex++]=A&255,e[e.currentIndex++]=A>>8}function tA(e,A){Je(e,A,4)}function Oe(e,A){let t=A<<8|e;return t>32767?t-65536:t}function fo(e){return e>127?e-256:e}var BA=class{constructor(A,t,n){this.header=A,this.size=t,this.chunkData=n}};function IA(e,A=!0,t=!1){let n=AA(e,4),s=b(e,4),o;return A&&(o=new N(e.buffer.slice(e.currentIndex,e.currentIndex+s))),(A||t)&&(e.currentIndex+=s),s%2!==0&&e[e.currentIndex]===0&&e.currentIndex++,new BA(n,s,o)}function fA(e,A=void 0){let t=8+e.size;e.size%2!==0&&t++,A&&(t+=A.length);let n=new N(t);return A&&(n.set(A,n.currentIndex),n.currentIndex+=A.length),NA(n,e.header),tA(n,t-8-(A?.length||0)),n.set(e.chunkData,n.currentIndex),n}function j(e,A,t=!1,n=!1){if(t){let C=new Uint8Array(A.length+1);C.set(A),A=C}let s=8,o=s+A.length,r=A.length;o%2!==0&&o++;let g=e;n&&(o+=4,r+=4,s+=4,g="LIST");let i=new N(o);return NA(i,g),tA(i,r),n&&NA(i,e),i.set(A,s),i}function XA(e,A){return e.find(t=>t.header!=="LIST"?!1:(t.chunkData.currentIndex=0,AA(t.chunkData,4)===A))}function uo(){let e=4;for(let n of this.instruments)e+=n.instrumentZones.reduce((s,o)=>(o.generators=o.generators.filter(r=>r.generatorType!==a.sampleID&&r.generatorType!==a.keyRange&&r.generatorType!==a.velRange),(o.velRange.max!==127||o.velRange.min!==0)&&o.generators.unshift(new U(a.velRange,o.velRange.max<<8|Math.max(o.velRange.min,0),!1)),(o.keyRange.max!==127||o.keyRange.min!==0)&&o.generators.unshift(new U(a.keyRange,o.keyRange.max<<8|Math.max(o.keyRange.min,0),!1)),o.isGlobal||o.generators.push(new U(a.sampleID,this.samples.indexOf(o.sample),!1)),o.generators.length*4+s),0);let A=new N(e),t=0;for(let n of this.instruments)for(let s of n.instrumentZones){s.generatorZoneStartIndex=t;for(let o of s.generators)Y(A,o.generatorType),Y(A,o.generatorValue),t++}return tA(A,0),fA(new BA("igen",A.length,A))}function mo(e,A,t,n,s){let o=this.samples.map((C,B)=>{t&&C.compressSample(n,s);let h=C.getRawData();return p(`%cEncoded sample %c${B}. ${C.sampleName}%c of %c${this.samples.length}%c. Compressed: %c${C.isCompressed}%c.`,I.info,I.recognized,I.info,I.recognized,I.info,C.isCompressed?I.recognized:I.unrecognized,I.info),h}),r=this.samples.reduce((C,B,h)=>C+o[h].length+46,0),g=new N(r);this.samples.forEach((C,B)=>{let h=o[B],c,l,m=h.length;C.isCompressed?(c=g.currentIndex,l=c+h.length):(c=g.currentIndex/2,l=c+h.length/2,m+=46),e.push(c),g.set(h,g.currentIndex),g.currentIndex+=m,A.push(l)});let i=fA(new BA("smpl",g.length,g),new N([115,100,116,97]));return fA(new BA("LIST",i.length,i))}function po(e,A){let n=new N(46*(this.samples.length+1));return this.samples.forEach((s,o)=>{NA(n,s.sampleName,20);let r=e[o];tA(n,r);let g=A[o];tA(n,g);let i=s.sampleLoopStartIndex+r,C=s.sampleLoopEndIndex+r;s.isCompressed&&(i-=r,C-=r),tA(n,i),tA(n,C),tA(n,s.sampleRate),n[n.currentIndex++]=s.samplePitch,n[n.currentIndex++]=s.samplePitchCorrection,Y(n,s.sampleLink),Y(n,s.sampleType)}),NA(n,"EOS",46),fA(new BA("shdr",n.length,n))}function yo(){let e=10;for(let n of this.instruments)e+=n.instrumentZones.reduce((s,o)=>o.modulators.length*10+s,0);let A=new N(e),t=0;for(let n of this.instruments)for(let s of n.instrumentZones){s.modulatorZoneStartIndex=t;for(let o of s.modulators)Y(A,o.sourceEnum),Y(A,o.modulatorDestination),Y(A,o.transformAmount),Y(A,o.secondarySourceEnum),Y(A,o.transformType),t++}return Je(A,0,10),fA(new BA("imod",A.length,A))}function So(){let e=this.instruments.reduce((o,r)=>r.instrumentZones.length*4+o,4),A=new N(e),t=0,n=0,s=0;for(let o of this.instruments){o.instrumentZoneIndex=t;for(let r of o.instrumentZones)r.zoneID=t,Y(A,n),Y(A,s),n+=r.generators.length,s+=r.modulators.length,t++}return Y(A,n),Y(A,s),fA(new BA("ibag",A.length,A))}function Do(){let e=this.instruments.length*22+22,A=new N(e),t=0,n=0;for(let s of this.instruments)NA(A,s.instrumentName,20),Y(A,t),t+=s.instrumentZones.length,s.instrumentID=n,n++;return NA(A,"EOI",20),Y(A,t),fA(new BA("inst",A.length,A))}function ko(){let e=4;for(let n of this.presets)e+=n.presetZones.reduce((s,o)=>(o.generators=o.generators.filter(r=>r.generatorType!==a.instrument&&r.generatorType!==a.keyRange&&r.generatorType!==a.velRange),(o.velRange.max!==127||o.velRange.min!==0)&&o.generators.unshift(new U(a.velRange,o.velRange.max<<8|Math.max(o.velRange.min,0),!1)),(o.keyRange.max!==127||o.keyRange.min!==0)&&o.generators.unshift(new U(a.keyRange,o.keyRange.max<<8|Math.max(o.keyRange.min,0),!1)),o.isGlobal||o.generators.push(new U(a.instrument,this.instruments.indexOf(o.instrument),!1)),o.generators.length*4+s),0);let A=new N(e),t=0;for(let n of this.presets)for(let s of n.presetZones){s.generatorZoneStartIndex=t;for(let o of s.generators)Y(A,o.generatorType),Y(A,o.generatorValue);t+=s.generators.length}return Y(A,0),Y(A,0),fA(new BA("pgen",A.length,A))}function Fo(){let e=10;for(let n of this.presets)e+=n.presetZones.reduce((s,o)=>o.modulators.length*10+s,0);let A=new N(e),t=0;for(let n of this.presets)for(let s of n.presetZones){s.modulatorZoneStartIndex=t;for(let o of s.modulators)Y(A,o.sourceEnum),Y(A,o.modulatorDestination),Y(A,o.transformAmount),Y(A,o.secondarySourceEnum),Y(A,o.transformType),t++}return Je(A,0,10),fA(new BA("pmod",A.length,A))}function wo(){let e=this.presets.reduce((o,r)=>r.presetZones.length*4+o,4),A=new N(e),t=0,n=0,s=0;for(let o of this.presets){o.presetZoneStartIndex=t;for(let r of o.presetZones)r.zoneID=t,Y(A,n),Y(A,s),n+=r.generators.length,s+=r.modulators.length,t++}return Y(A,n),Y(A,s),fA(new BA("pbag",A.length,A))}function Ro(){let e=this.presets.length*38+38,A=new N(e),t=0;for(let n of this.presets)NA(A,n.presetName,20),Y(A,n.program),Y(A,n.bank),Y(A,t),tA(A,n.library),tA(A,n.genre),tA(A,n.morphology),t+=n.presetZones.length;return NA(A,"EOP",20),Y(A,0),Y(A,0),Y(A,t),tA(A,0),tA(A,0),tA(A,0),fA(new BA("phdr",A.length,A))}var Si={compress:!1,compressionQuality:.5,compressionFunction:void 0};function Go(e=Si){if(e.compress&&typeof e.compressionFunction!="function")throw new TypeError("No compression function supplied but compression enabled.");uA("%cSaving soundfont...",I.info),p(`%cCompression: %c${e?.compress||"false"}%c quality: %c${e?.compressionQuality||"none"}`,I.info,I.recognized,I.info,I.recognized),p("%cWriting INFO...",I.info);let A=[];this.soundFontInfo.ISFT="SpessaSynth",e?.compress&&(this.soundFontInfo.ifil="3.0");for(let[L,G]of Object.entries(this.soundFontInfo))if(L==="ifil"||L==="iver"){let E=parseInt(G.split(".")[0]),M=parseInt(G.split(".")[1]),v=new N(4);Y(v,E),Y(v,M),A.push(fA(new BA(L,4,v)))}else if(L==="DMOD")A.push(fA(new BA(L,G.length,G)));else{let E=new N(G.length);NA(E,G),A.push(fA(new BA(L,G.length,E)))}let t=kA([new N([73,78,70,79]),...A]),n=fA(new BA("LIST",t.length,t));p("%cWriting SDTA...",I.info);let s=[],o=[],r=mo.call(this,s,o,e?.compress,e?.compressionQuality??.5,e.compressionFunction);p("%cWriting PDTA...",I.info),p("%cWriting SHDR...",I.info);let g=po.call(this,s,o);p("%cWriting IGEN...",I.info);let i=uo.call(this);p("%cWriting IMOD...",I.info);let C=yo.call(this);p("%cWriting IBAG...",I.info);let B=So.call(this);p("%cWriting INST...",I.info);let h=Do.call(this),c=ko.call(this);p("%cWriting PMOD...",I.info);let l=Fo.call(this);p("%cWriting PBAG...",I.info);let m=wo.call(this);p("%cWriting PHDR...",I.info);let f=Ro.call(this),S=kA([new N([112,100,116,97]),f,m,l,c,h,B,C,i,g]),F=fA(new BA("LIST",S.length,S));p("%cWriting the output file...",I.info);let D=kA([new N([115,102,98,107]),n,r,F]),x=fA(new BA("RIFF",D.length,D));return p(`%cSaved succesfully! Final file size: %c${x.length}`,I.info,I.recognized),q(),x}var Ht=class{velRange={min:-1,max:127};keyRange={min:-1,max:127};isGlobal=!1;generators=[];modulators=[];get hasKeyRange(){return this.keyRange.min!==-1}get hasVelRange(){return this.velRange.min!==-1}getGeneratorValue(A,t){return this.generators.find(n=>n.generatorType===A)?.generatorValue??t}};var OA=class extends Ht{sample=void 0;useCount=0;deleteZone(){this.useCount--,!this.isGlobal&&this.sample.useCount--}},Re=class extends Ht{instrument=void 0;deleteZone(){this.isGlobal||this.instrument.removeUseCount()}};var Di=new Set([a.velRange,a.keyRange,a.instrument,a.exclusiveClass,a.endOper,a.sampleModes,a.startloopAddrsOffset,a.startloopAddrsCoarseOffset,a.endloopAddrsOffset,a.endloopAddrsCoarseOffset,a.startAddrsOffset,a.startAddrsCoarseOffset,a.endAddrOffset,a.endAddrsCoarseOffset,a.initialAttenuation,a.fineTune,a.coarseTune,a.keyNumToVolEnvHold,a.keyNumToVolEnvDecay,a.keyNumToModEnvHold,a.keyNumToModEnvDecay]);function Mo(e,A=!0){function t(h,c){h.push(...c.filter(l=>!h.find(m=>m.generatorType===l.generatorType)))}function n(h,c){return{min:Math.max(h.min,c.min),max:Math.min(h.max,c.max)}}function s(h,c){h.push(...c.filter(l=>!h.find(m=>X.isIdentical(l,m))))}let o=[],r=[],g=[],i={min:0,max:127},C={min:0,max:127},B=e.presetZones.find(h=>h.isGlobal);B&&(r.push(...B.generators),g.push(...B.modulators),i=B.keyRange,C=B.velRange);for(let h of e.presetZones){if(h.isGlobal)continue;let c=h.keyRange;h.hasKeyRange||(c=i);let l=h.velRange;h.hasVelRange||(l=C);let m=h.generators.map(E=>new U(E.generatorType,E.generatorValue));t(m,r);let f=[...h.modulators];s(f,g);let S=h.instrument.instrumentZones,F=[],D=[],x={min:0,max:127},L={min:0,max:127},G=S.find(E=>E.isGlobal);G&&(F.push(...G.generators),D.push(...G.modulators),x=G.keyRange,L=G.velRange);for(let E of S){if(E.isGlobal)continue;let M=E.keyRange;E.hasKeyRange||(M=x);let v=E.velRange;if(E.hasVelRange||(v=L),M=n(M,c),v=n(v,l),M.max<M.min||v.max<v.min)continue;let eA=E.generators.map(oA=>new U(oA.generatorType,oA.generatorValue));t(eA,F);let $=[...E.modulators];s($,D);let T=[...$];for(let oA of f){let rA=T.findIndex(hA=>X.isIdentical(oA,hA));rA!==-1?T[rA]=T[rA].sumTransform(oA):T.push(oA)}let nA=eA.map(oA=>new U(oA.generatorType,oA.generatorValue));for(let oA of m){if(oA.generatorType===a.velRange||oA.generatorType===a.keyRange||oA.generatorType===a.instrument||oA.generatorType===a.endOper||oA.generatorType===a.sampleModes)continue;let rA=eA.findIndex(hA=>hA.generatorType===oA.generatorType);if(rA!==-1){let hA=nA[rA].generatorValue+oA.generatorValue;nA[rA]=new U(oA.generatorType,hA)}else{let hA=Z[oA.generatorType].def+oA.generatorValue;nA.push(new U(oA.generatorType,hA))}}nA=nA.filter(oA=>oA.generatorType!==a.sampleID&&oA.generatorType!==a.keyRange&&oA.generatorType!==a.velRange&&oA.generatorType!==a.endOper&&oA.generatorType!==a.instrument&&oA.generatorValue!==Z[oA.generatorType].def);let cA=new OA;cA.keyRange=M,cA.velRange=v,cA.keyRange.min===0&&cA.keyRange.max===127&&(cA.keyRange.min=-1),cA.velRange.min===0&&cA.velRange.max===127&&(cA.velRange.min=-1),cA.isGlobal=!1,cA.sample=E.sample,cA.generators=nA,cA.modulators=T,o.push(cA)}}if(A){let h=new OA;h.isGlobal=!0;for(let m=0;m<58;m++){if(Di.has(m))continue;let f={},S=Z[m]?.def||0;f[S]=0;for(let F of o){let D=F.generators.find(G=>G.generatorType===m);if(D){let G=D.generatorValue;f[G]===void 0?f[G]=1:f[G]++}else f[S]++;let x;switch(m){default:continue;case a.decayVolEnv:x=a.keyNumToVolEnvDecay;break;case a.holdVolEnv:x=a.keyNumToVolEnvHold;break;case a.decayModEnv:x=a.keyNumToModEnvDecay;break;case a.holdModEnv:x=a.keyNumToModEnvHold}if(F.generators.find(G=>G.generatorType===x)!==void 0){f={};break}}if(Object.keys(f).length>0){let F=Object.entries(f).reduce((x,L)=>x[1]<L[1]?L:x,[0,0]),D=parseInt(F[0]);D!==S&&h.generators.push(new U(m,D)),o.forEach(x=>{let L=x.generators.findIndex(G=>G.generatorType===m);L!==-1?x.generators[L].generatorValue===D&&x.generators.splice(L,1):D!==S&&x.generators.push(new U(m,S))})}}let l=o.find(m=>!m.isGlobal).modulators.map(m=>X.copy(m));for(let m of l){let f=!0;for(let S of o){if(S.isGlobal||!f)continue;S.modulators.find(D=>X.isIdentical(D,m))||(f=!1)}if(f===!0){h.modulators.push(X.copy(m));for(let S of o){let F=S.modulators.find(D=>X.isIdentical(D,m));F.transformAmount===m.transformAmount&&S.modulators.splice(S.modulators.indexOf(F),1)}}}o.splice(0,0,h)}return o}var xo=20;function En(e,A,t,n,s,o,r){let g=r===0?0:1,i=new N(xo+g*16);tA(i,xo),Y(i,A),Y(i,t);let C=n*.4,B=Math.floor(C*-65536);tA(i,B),tA(i,2);let h=o-s,c=0;switch(r){default:case 0:g=0;break;case 1:c=0,g=1;break;case 3:c=1,g=1}return tA(i,g),g===1&&(tA(i,16),tA(i,c),tA(i,s),tA(i,h)),j("wsmp",i)}var P={none:0,modLfo:1,velocity:2,keyNum:3,volEnv:4,modEnv:5,pitchWheel:6,polyPressure:7,channelPressure:8,vibratoLfo:9,modulationWheel:129,volume:135,pan:138,expression:139,chorus:221,reverb:219,pitchWheelRange:256,fineTune:257,coarseTune:258},hn=new X(219,0,a.reverbEffectsSend,1e3,0),cn=new X(221,0,a.chorusEffectsSend,1e3,0),Bn=new X(129,0,a.vibLfoToPitch,0,0),ln=new X(13,0,a.vibLfoToPitch,0,0);var R={none:0,gain:1,reserved:2,pitch:3,pan:4,keyNum:5,chorusSend:128,reverbSend:129,modLfoFreq:260,modLfoDelay:261,vibLfoFreq:276,vibLfoDelay:277,volEnvAttack:518,volEnvDecay:519,volEnvRelease:521,volEnvSustain:522,volEnvDelay:523,volEnvHold:524,modEnvAttack:778,modEnvDecay:779,modEnvRelease:781,modEnvSustain:782,modEnvDelay:783,modEnvHold:784,filterCutoff:1280,filterQ:1281};var Yt=class{source;control;destination;scale;transform;constructor(A,t,n,s,o){this.source=A,this.control=t,this.destination=n,this.scale=s,this.transform=o}writeArticulator(){let A=new N(12);return Y(A,this.source),Y(A,this.control),Y(A,this.destination),Y(A,this.transform),tA(A,this.scale<<16),A}};function No(e,A){if(e)switch(A){default:return;case y.modulationWheel:return P.modulationWheel;case y.mainVolume:return P.volume;case y.pan:return P.pan;case y.expressionController:return P.expression;case y.chorusDepth:return P.chorus;case y.reverbDepth:return P.reverb}else switch(A){default:return;case _.noteOnKeyNum:return P.keyNum;case _.noteOnVelocity:return P.velocity;case _.noController:return P.none;case _.polyPressure:return P.polyPressure;case _.channelPressure:return P.channelPressure;case _.pitchWheel:return P.pitchWheel;case _.pitchWheelRange:return P.pitchWheelRange}}function bo(e,A){switch(e){default:return;case a.initialAttenuation:return{dest:R.gain,amount:-A};case a.fineTune:return R.pitch;case a.pan:return R.pan;case a.keyNum:return R.keyNum;case a.reverbEffectsSend:return R.reverbSend;case a.chorusEffectsSend:return R.chorusSend;case a.freqModLFO:return R.modLfoFreq;case a.delayModLFO:return R.modLfoDelay;case a.delayVibLFO:return R.vibLfoDelay;case a.freqVibLFO:return R.vibLfoFreq;case a.delayVolEnv:return R.volEnvDelay;case a.attackVolEnv:return R.volEnvAttack;case a.holdVolEnv:return R.volEnvHold;case a.decayVolEnv:return R.volEnvDecay;case a.sustainVolEnv:return{dest:R.volEnvSustain,amount:1e3-A};case a.releaseVolEnv:return R.volEnvRelease;case a.delayModEnv:return R.modEnvDelay;case a.attackModEnv:return R.modEnvAttack;case a.holdModEnv:return R.modEnvHold;case a.decayModEnv:return R.modEnvDecay;case a.sustainModEnv:return{dest:R.modEnvSustain,amount:1e3-A};case a.releaseModEnv:return R.modEnvRelease;case a.initialFilterFc:return R.filterCutoff;case a.initialFilterQ:return R.filterQ}}function Lo(e,A){switch(e){default:return;case a.modEnvToFilterFc:return{source:P.modEnv,dest:R.filterCutoff,amt:A,isBipolar:!1};case a.modEnvToPitch:return{source:P.modEnv,dest:R.pitch,amt:A,isBipolar:!1};case a.modLfoToFilterFc:return{source:P.modLfo,dest:R.filterCutoff,amt:A,isBipolar:!0};case a.modLfoToVolume:return{source:P.modLfo,dest:R.gain,amt:A,isBipolar:!0};case a.modLfoToPitch:return{source:P.modLfo,dest:R.pitch,amt:A,isBipolar:!0};case a.vibLfoToPitch:return{source:P.vibratoLfo,dest:R.pitch,amt:A,isBipolar:!0};case a.keyNumToVolEnvHold:return{source:P.keyNum,dest:R.volEnvHold,amt:A,isBipolar:!0};case a.keyNumToVolEnvDecay:return{source:P.keyNum,dest:R.volEnvDecay,amt:A,isBipolar:!0};case a.keyNumToModEnvHold:return{source:P.keyNum,dest:R.modEnvHold,amt:A,isBipolar:!0};case a.keyNumToModEnvDecay:return{source:P.keyNum,dest:R.modEnvDecay,amt:A,isBipolar:!0};case a.scaleTuning:return{source:P.keyNum,dest:R.pitch,amt:A*128,isBipolar:!1}}}function To(e){let A=bo(e.generatorType,e.generatorValue),t=A,n=0,s=e.generatorValue;A?.amount!==void 0&&(s=A.amount,t=A.dest);let o=Lo(e.generatorType,e.generatorValue);if(o!==void 0)s=o.amt,t=o.dest,n=o.source;else if(t===void 0){J(`Invalid generator type: ${e.generatorType}`);return}return new Yt(n,0,t,s,0)}function Uo(e){if(e.transformType!==0){J("Other transform types are not supported.");return}let A=No(e.sourceUsesCC,e.sourceIndex),t=e.sourceCurveType,n=e.sourcePolarity,s=e.sourceDirection;if(A===void 0){J(`Invalid source: ${e.sourceIndex}, CC: ${e.sourceUsesCC}`);return}e.modulatorDestination===a.initialAttenuation&&(s=s===1?0:1);let o=No(e.secSrcUsesCC,e.secSrcIndex),r=e.secSrcCurveType,g=e.secSrcPolarity,i=e.secSrcDirection;if(o===void 0){J(`Invalid secondary source: ${e.secSrcIndex}, CC: ${e.secSrcUsesCC}`);return}let C=bo(e.modulatorDestination,e.transformAmount),B=C,h=e.transformAmount;C?.dest!==void 0&&(B=C.dest,h=C.amount);let c=Lo(e.modulatorDestination,e.transformAmount);if(c!==void 0)h=c.amt,o=A,r=t,g=n,i=s,t=FA.linear,n=c.isBipolar?1:0,s=0,A=c.source,B=c.dest;else if(B===void 0){J(`Invalid destination: ${e.modulatorDestination}`);return}let l=0;return l|=r<<4,l|=g<<8,l|=i<<9,l|=t,l|=n<<14,l|=s<<15,new Yt(A,o,B,h,l)}var ki=new Set([a.sampleModes,a.initialAttenuation,a.keyRange,a.velRange,a.sampleID,a.fineTune,a.coarseTune,a.startAddrsOffset,a.startAddrsCoarseOffset,a.endAddrOffset,a.endAddrsCoarseOffset,a.startloopAddrsOffset,a.startloopAddrsCoarseOffset,a.endloopAddrsOffset,a.endloopAddrsCoarseOffset,a.overridingRootKey,a.exclusiveClass]);function Qn(e){for(let o=0;o<e.generators.length;o++){let r=e.generators[o];(r.generatorType===a.delayVolEnv||r.generatorType===a.attackVolEnv||r.generatorType===a.holdVolEnv||r.generatorType===a.decayVolEnv||r.generatorType===a.releaseVolEnv||r.generatorType===a.delayModEnv||r.generatorType===a.attackModEnv||r.generatorType===a.holdModEnv||r.generatorType===a.decayModEnv)&&(e.generators[o]=new U(r.generatorType,Math.min(r.generatorValue,6386),!1))}for(let o=0;o<e.generators.length;o++){let r=e.generators[o],g;switch(r.generatorType){default:continue;case a.keyNumToVolEnvDecay:g=a.decayVolEnv;break;case a.keyNumToVolEnvHold:g=a.holdVolEnv;break;case a.keyNumToModEnvDecay:g=a.decayModEnv;break;case a.keyNumToModEnvHold:g=a.holdModEnv}let i=e.generators.find(m=>m.generatorType===g);if(i===void 0)continue;let C=r.generatorValue*-128,B=60/128*C,h=i.generatorValue-B,c=e.generators.indexOf(r),l=e.generators.indexOf(i);e.generators[l]=new U(g,h,!1),e.generators[c]=new U(r.generatorType,C,!1)}let A=e.generators.reduce((o,r)=>{if(ki.has(r.generatorType))return o;let g=To(r);return g!==void 0?(o.push(g),p("%cSucceeded converting to DLS Articulator!",I.recognized)):J("Failed converting to DLS Articulator!"),o},[]),t=e.modulators.reduce((o,r)=>{if(X.isIdentical(r,cn,!0)||X.isIdentical(r,hn,!0)||X.isIdentical(r,Bn,!0)||X.isIdentical(r,ln,!0))return o;let g=Uo(r);return g!==void 0?(o.push(g),p("%cSucceeded converting to DLS Articulator!",I.recognized)):J("Failed converting to DLS Articulator!"),o},[]);A.push(...t);let n=new N(8);tA(n,8),tA(n,A.length);let s=A.map(o=>o.writeArticulator());return j("art2",kA([n,...s]))}function vo(e,A){let t=new N(12);Y(t,Math.max(e.keyRange.min,0)),Y(t,e.keyRange.max),Y(t,Math.max(e.velRange.min,0)),Y(t,e.velRange.max),Y(t,0);let n=e.getGeneratorValue(a.exclusiveClass,0);Y(t,n),Y(t,0);let s=j("rgnh",t),o=e.getGeneratorValue(a.overridingRootKey,e.sample.samplePitch);e.getGeneratorValue(a.scaleTuning,A.getGeneratorValue(a.scaleTuning,100))===0&&e.keyRange.max-e.keyRange.min===0&&(o=e.keyRange.min);let g=En(e.sample,o,e.getGeneratorValue(a.fineTune,0)+e.getGeneratorValue(a.coarseTune,0)*100+e.sample.samplePitchCorrection,e.getGeneratorValue(a.initialAttenuation,0),e.sample.sampleLoopStartIndex+e.getGeneratorValue(a.startloopAddrsOffset,0)+e.getGeneratorValue(a.startloopAddrsCoarseOffset,0)*32768,e.sample.sampleLoopEndIndex+e.getGeneratorValue(a.endloopAddrsOffset,0)+e.getGeneratorValue(a.endloopAddrsCoarseOffset,0)*32768,e.getGeneratorValue(a.sampleModes,0)),i=new N(12);Y(i,0),Y(i,0),tA(i,1),tA(i,this.samples.indexOf(e.sample));let C=j("wlnk",i),B=new N(0);if(e.modulators.length+e.generators.length>0){let h=Qn(e);B=j("lar2",h,!1,!0)}return j("rgn2",kA([s,g,C,B]),!1,!0)}function Ho(e){uA(`%cWriting %c${e.presetName}%c...`,I.info,I.recognized,I.info);let A=Mo(e),t=A.reduce((c,l)=>l.isGlobal?c:c+1,0),n=new N(12);tA(n,t);let s=(e.bank&127)<<8;e.bank===128&&(s|=1<<31),tA(n,s),tA(n,e.program&127);let o=j("insh",n),r=new N(0),g=A.find(c=>c.isGlobal===!0);if(g){let c=Qn(g);r=j("lar2",c,!1,!0)}let i=kA(A.reduce((c,l)=>(l.isGlobal||c.push(vo.apply(this,[l,g])),c),[])),C=j("lrgn",i,!1,!0),B=j("INAM",pe(e.presetName)),h=j("INFO",B,!1,!0);return q(),j("ins ",kA([o,C,r,h]),!1,!0)}function Yo(){let e=kA(this.presets.map(A=>Ho.apply(this,[A])));return j("lins",e,!1,!0)}function Jo(e){let A=new N(18);Y(A,1),Y(A,1),tA(A,e.sampleRate),tA(A,e.sampleRate*2),Y(A,2),Y(A,16);let t=j("fmt ",A),n=1;e.sampleLoopStartIndex+Math.abs(e.getAudioData().length-e.sampleLoopEndIndex)<2&&(n=0);let s=En(e,e.samplePitch,e.samplePitchCorrection,0,e.sampleLoopStartIndex,e.sampleLoopEndIndex,n),o=e.getAudioData(),r;if(e.isCompressed){let C=new Int16Array(o.length);for(let B=0;B<o.length;B++)C[B]=o[B]*32767;r=j("data",new N(C.buffer))}else r=j("data",e.getRawData());let g=j("INAM",pe(e.sampleName)),i=j("INFO",g,!1,!0);return p(`%cSaved %c${e.sampleName}%c succesfully!`,I.recognized,I.value,I.recognized),j("wave",kA([t,s,r,i]),!1,!0)}function Oo(){let e=0,A=[],t=this.samples.map(n=>{let s=Jo(n);return A.push(e),e+=s.length,s});return{data:j("wvpl",kA(t),!1,!0),indexes:A}}function Ko(){uA("%cSaving DLS...",I.info);let e=new N(4);tA(e,this.presets.length);let A=j("colh",e);uA("%cWriting instruments...",I.info);let t=Yo.apply(this);p("%cSuccess!",I.recognized),q(),uA("%cWriting WAVE samples...",I.info);let n=Oo.apply(this),s=n.data,o=n.indexes;p("%cSucceeded!",I.recognized),q();let r=new N(8+4*o.length);tA(r,8),tA(r,o.length);for(let h of o)tA(r,h);let g=j("ptbl",r);this.soundFontInfo.ICMT=(this.soundFontInfo.ICMT||"Soundfont")+`
11
11
  Converted from SF2 to DLS using SpessaSynth`,this.soundFontInfo.ISFT="SpessaSynth";let i=[];for(let[h,c]of Object.entries(this.soundFontInfo))h!=="ICMT"&&h!=="INAM"&&h!=="ICRD"&&h!=="IENG"&&h!=="ICOP"&&h!=="ISFT"&&h!=="ISBJ"||i.push(j(h,pe(c),!0));let C=j("INFO",kA(i),!1,!0),B=new N(A.length+t.length+g.length+s.length+C.length+4);return NA(B,"DLS "),B.set(kA([A,t,g,s,C]),4),p("%cSaved succesfully!",I.recognized),q(),j("RIFF",B)}var Fi=48e3,Ge=class{sampleName;sampleRate;samplePitch;samplePitchCorrection;sampleLink;sampleType;sampleLoopStartIndex;sampleLoopEndIndex;isCompressed;compressedData=void 0;useCount=0;sampleData=void 0;constructor(A,t,n,s,o,r,g,i){this.sampleName=A,this.sampleRate=t,this.samplePitch=n,this.samplePitchCorrection=s,this.sampleLink=o,this.sampleType=r,this.sampleLoopStartIndex=g,this.sampleLoopEndIndex=i,this.isCompressed=(r&16)>0}getRawData(){let A=new Uint8Array(this.sampleData.length*2);for(let t=0;t<this.sampleData.length;t++){let n=Math.floor(this.sampleData[t]*32768);A[t*2]=n&255,A[t*2+1]=n>>8&255}return A}resampleData(A){let t=this.getAudioData(),n=A/this.sampleRate,s=new Float32Array(Math.floor(t.length*n));for(let o=0;o<s.length;o++)s[o]=t[Math.floor(o*(1/n))];t=s,this.sampleRate=A,this.sampleLoopStartIndex=Math.floor(this.sampleLoopStartIndex*n),this.sampleLoopEndIndex=Math.floor(this.sampleLoopEndIndex*n),this.sampleData=t}compressSample(A,t){if(!this.isCompressed)try{let n=this.getAudioData();(this.sampleRate<8e3||this.sampleRate>96e3)&&(this.resampleData(Fi),n=this.getAudioData()),this.compressedData=t([n],1,this.sampleRate,A),this.sampleType|=16,this.isCompressed=!0}catch{J(`Failed to compress ${this.sampleName}. Leaving as uncompressed!`),this.isCompressed=!1,this.compressedData=void 0,this.sampleType&=239}}getAudioData(){return this.sampleData}};var Me=class{instrumentName="";instrumentZones=[];_useCount=0;get useCount(){return this._useCount}addUseCount(){this._useCount++,this.instrumentZones.forEach(A=>A.useCount++)}removeUseCount(){this._useCount--;for(let A=0;A<this.instrumentZones.length;A++)this.safeDeleteZone(A)&&A--}deleteInstrument(){this.instrumentZones.forEach(A=>A.deleteZone()),this.instrumentZones.length=0}safeDeleteZone(A){return this.instrumentZones[A].useCount--,this.instrumentZones[A].useCount<1?(this.deleteZone(A),!0):!1}deleteZone(A){this.instrumentZones[A].deleteZone(),this.instrumentZones.splice(A,1)}};var xe=class{parentSoundBank;presetName="";program=0;bank=0;presetZones=[];library=0;genre=0;morphology=0;constructor(A){this.parentSoundBank=A}isDrumPreset(A,t=!1){let n=A&&this.parentSoundBank.isXGBank;return this.bank===128||n&&Ae(this.bank)&&(this.bank!==126||t)}deletePreset(){this.presetZones.forEach(A=>A.deleteZone()),this.presetZones.length=0}deleteZone(A){this.presetZones[A].deleteZone(),this.presetZones.splice(A,1)}preload(A,t){for(let n=A;n<t+1;n++)for(let s=0;s<128;s++)this.getSamplesAndGenerators(n,s).forEach(o=>{o.sample.isSampleLoaded||o.sample.getAudioData()})}getSamplesAndGenerators(A,t){if(this.presetZones.length<1)return[];function n(c,l){return l>=c.min&&l<=c.max}function s(c,l){c.push(...l.filter(m=>!c.find(f=>f.generatorType===m.generatorType)))}function o(c,l){c.push(...l.filter(m=>!c.find(f=>X.isIdentical(m,f))))}let r=[],g=this.presetZones[0].isGlobal?[...this.presetZones[0].generators]:[],i=this.presetZones[0].isGlobal?[...this.presetZones[0].modulators]:[],C=this.presetZones[0].isGlobal?this.presetZones[0].keyRange:{min:0,max:127},B=this.presetZones[0].isGlobal?this.presetZones[0].velRange:{min:0,max:127};return this.presetZones.filter(c=>n(c.hasKeyRange?c.keyRange:C,A)&&n(c.hasVelRange?c.velRange:B,t)&&!c.isGlobal).forEach(c=>{if(c.instrument.instrumentZones.length<1)return;let l=c.generators,m=c.modulators,f=c.instrument.instrumentZones[0],S=f.isGlobal?[...f.generators]:[],F=f.isGlobal?[...f.modulators]:[],D=f.isGlobal?f.keyRange:{min:0,max:127},x=f.isGlobal?f.velRange:{min:0,max:127};c.instrument.instrumentZones.filter(G=>n(G.hasKeyRange?G.keyRange:D,A)&&n(G.hasVelRange?G.velRange:x,t)&&!G.isGlobal).forEach(G=>{let E=[...G.generators],M=[...G.modulators];s(l,g),s(E,S),o(m,i),o(M,F),o(M,this.parentSoundBank.defaultModulators);let v=[...M];for(let eA=0;eA<m.length;eA++){let $=m[eA],T=v.findIndex(nA=>X.isIdentical($,nA));T!==-1?v[T]=v[T].sumTransform($):v.push($)}r.push({instrumentGenerators:E,presetGenerators:l,modulators:v,sample:G.sample,sampleID:G.generators.find(eA=>eA.generatorType===a.sampleID).generatorValue})})}),r}};var Ne=class e{soundFontInfo={};presets=[];samples=[];instruments=[];defaultModulators=$s.map(A=>X.copy(A));isXGBank=!1;constructor(A=void 0){A?.presets&&(this.presets.push(...A.presets),this.soundFontInfo=A.info)}static mergeSoundBanks(...A){let t=A.shift(),n=t.presets;for(;A.length;)A.shift().presets.forEach(o=>{n.find(r=>r.bank===o.bank&&r.program===o.program)===void 0&&n.push(o)});return new e({presets:n,info:t.soundFontInfo})}static getDummySoundfontFile(){let A=new e,t=new Ge("Saw",44100,65,20,0,0,0,127);t.sampleData=new Float32Array(128);for(let C=0;C<128;C++)t.sampleData[C]=C/128*2-1;A.samples.push(t);let n=new OA;n.isGlobal=!0,n.generators.push(new U(a.initialAttenuation,375)),n.generators.push(new U(a.releaseVolEnv,-1e3)),n.generators.push(new U(a.sampleModes,1));let s=new OA;s.sample=t;let o=new OA;o.sample=t,o.generators.push(new U(a.fineTune,-9));let r=new Me;r.instrumentName="Saw Wave",r.instrumentZones.push(n),r.instrumentZones.push(s),r.instrumentZones.push(o),A.instruments.push(r);let g=new Re;g.instrument=r;let i=new xe(A);return i.presetName="Saw Wave",i.presetZones.push(g),A.presets.push(i),A.soundFontInfo.ifil="2.1",A.soundFontInfo.isng="EMU8000",A.soundFontInfo.INAM="Dummy",A._parseInternal(),A.write().buffer}_parseInternal(){this.isXGBank=!1;let A=new Set([0,1,2,3,4,5,6,7,8,9,16,17,24,25,27,28,29,30,31,32,33,40,41,48,56,57,58,64,65,66,126,127]);for(let t of this.presets)if(Ae(t.bank)&&(this.isXGBank=!0,!A.has(t.program))){this.isXGBank=!1,p(`%cThis bank is not valid XG. Preset %c${t.bank}:${t.program}%c is not a valid XG drum. XG mode will use presets on bank 128.`,I.info,I.value,I.info);break}}trimSoundBank(A){let t=this;function n(o,r){let g=0;for(let i=0;i<o.instrumentZones.length;i++){let C=o.instrumentZones[i];if(C.isGlobal)continue;let B=C.keyRange,h=C.velRange,c=!1;for(let l of r)if(l.key>=B.min&&l.key<=B.max&&l.velocity>=h.min&&l.velocity<=h.max){c=!0;break}c||(p(`%c${C.sample.sampleName} %cremoved from %c${o.instrumentName}%c. Use count: %c${C.useCount-1}`,I.recognized,I.info,I.recognized,I.info,I.recognized),o.safeDeleteZone(i)&&(g++,i--,p(`%c${C.sample.sampleName} %cdeleted`,I.recognized,I.info)),C.sample.useCount<1&&t.deleteSample(C.sample))}return g}ae("%cTrimming soundfont...",I.info);let s=A.getUsedProgramsAndKeys(t);uA("%cModifying soundfont...",I.info),p("Detected keys for midi:",s);for(let o=0;o<t.presets.length;o++){let r=t.presets[o],g=r.bank+":"+r.program,i=s[g];if(i===void 0)p(`%cDeleting preset %c${r.presetName}%c and its zones`,I.info,I.recognized,I.info),t.deletePreset(r),o--;else{let C=[...i].map(h=>{let c=h.split("-");return{key:parseInt(c[0]),velocity:parseInt(c[1])}});uA(`%cTrimming %c${r.presetName}`,I.info,I.recognized),p(`Keys for ${r.presetName}:`,C);let B=0;for(let h=0;h<r.presetZones.length;h++){let c=r.presetZones[h];if(c.isGlobal)continue;let l=c.keyRange,m=c.velRange,f=!1;for(let S of C)if(S.key>=l.min&&S.key<=l.max&&S.velocity>=m.min&&S.velocity<=m.max){f=!0;let F=n(c.instrument,C);p(`%cTrimmed off %c${F}%c zones from %c${c.instrument.instrumentName}`,I.info,I.recognized,I.info,I.recognized);break}f||(B++,r.deleteZone(h),c.instrument.useCount<1&&t.deleteInstrument(c.instrument),h--)}p(`%cTrimmed off %c${B}%c zones from %c${r.presetName}`,I.info,I.recognized,I.info,I.recognized),q()}}t.removeUnusedElements(),t.soundFontInfo.ICMT=`NOTE: This soundfont was trimmed by SpessaSynth to only contain presets used in "${A.midiName}"
12
12
 
13
13
  `+t.soundFontInfo.ICMT,p("%cSoundfont modified!",I.recognized),q(),q()}removeUnusedElements(){this.instruments.forEach(A=>{A.useCount<1&&A.instrumentZones.forEach(t=>{t.isGlobal||t.sample.useCount--})}),this.instruments=this.instruments.filter(A=>A.useCount>0),this.samples=this.samples.filter(A=>A.useCount>0)}deleteInstrument(A){if(A.useCount>0)throw new Error(`Cannot delete an instrument that has ${A.useCount} usages.`);this.instruments.splice(this.instruments.indexOf(A),1),A.deleteInstrument(),this.removeUnusedElements()}deletePreset(A){A.deletePreset(),this.presets.splice(this.presets.indexOf(A),1),this.removeUnusedElements()}deleteSample(A){if(A.useCount>0)throw new Error(`Cannot delete sample that has ${A.useCount} usages.`);this.samples.splice(this.samples.indexOf(A),1),this.removeUnusedElements()}getPresetNoFallback(A,t,n=!1){let s=A===128||n&&Ae(A),o;if(s?o=this.presets.find(r=>r.bank===A&&r.isDrumPreset(n)&&r.program===t):o=this.presets.find(r=>r.bank===A&&r.program===t),o)return o;if(s&&n){let r=this.presets.find(g=>g.isDrumPreset(n)&&g.program===t);if(r)return r}}getPreset(A,t,n=!1){let s=A===128||n&&Ae(A),o;return s?o=this.presets.find(r=>r.bank===A&&r.isDrumPreset(n)&&r.program===t):o=this.presets.find(r=>r.bank===A&&r.program===t),o||(s?(o=this.presets.find(r=>r.isDrumPreset(n)&&r.program===t),o||(o=this.presets.find(r=>r.isDrumPreset(n)))):o=this.presets.find(r=>r.program===t&&!r.isDrumPreset(n)),o&&J(`%cPreset ${A}.${t} not found. Replaced with %c${o.presetName} (${o.bank}.${o.program})`,I.warn,I.recognized),o||(J(`Preset ${t} not found. Defaulting to`,this.presets[0].presetName),o=this.presets[0]),o)}getPresetByName(A){let t=this.presets.find(n=>n.presetName===A);return t||(J("Preset not found. Defaulting to:",this.presets[0].presetName),t=this.presets[0]),t}parsingError(A){throw new Error(`SF parsing error: ${A} The file may be corrupted.`)}destroySoundBank(){delete this.presets,delete this.instruments,delete this.samples}clearCache(){this.presets.forEach(A=>A.clearCache())}};Ne.prototype.write=Go;Ne.prototype.writeDLS=Ko;function qo(e){uA("%cLoading instruments...",I.info);for(let A=0;A<this.instrumentAmount;A++)this.readDLSInstrument(IA(e.chunkData));q()}var dn=class extends xe{constructor(A,t,n){super(A),this.program=n&127;let s=t>>8&127,o=t&127;s>0?this.bank=s:this.bank=o,t>>31&&(this.bank=128),this.DLSInstrument=new Me,this.DLSInstrument.addUseCount();let g=new Re;g.instrument=this.DLSInstrument,this.presetZones=[g]}};function Vo(e){this.verifyHeader(e,"LIST"),this.verifyText(AA(e.chunkData,4),"ins ");let A=[];for(;e.chunkData.length>e.chunkData.currentIndex;)A.push(IA(e.chunkData));let t=A.find(l=>l.header==="insh");if(!t)throw q(),new Error("No instrument header!");let n=b(t.chunkData,4),s=b(t.chunkData,4),o=b(t.chunkData,4),r=new dn(this,s,o),g="unnamedPreset",i=XA(A,"INFO");if(i){let l=IA(i.chunkData);for(;l.header!=="INAM";)l=IA(i.chunkData);g=AA(l.chunkData,l.chunkData.length).trim()}r.presetName=g,r.DLSInstrument.instrumentName=g,uA(`%cParsing %c"${g}"%c...`,I.info,I.recognized,I.info);let C=XA(A,"lrgn");if(!C)throw q(),new Error("No region list!");let B=new OA;B.isGlobal=!0;let h=XA(A,"lart"),c=XA(A,"lar2");(c!==void 0||h!==void 0)&&this.readLart(h,c,B),B.generators=B.generators.filter(l=>l.generatorValue!==Z[l.generatorType].def),B.modulators.find(l=>l.modulatorDestination===a.reverbEffectsSend)===void 0&&B.modulators.push(X.copy(hn)),B.modulators.find(l=>l.modulatorDestination===a.chorusEffectsSend)===void 0&&B.modulators.push(X.copy(cn)),r.DLSInstrument.instrumentZones.push(B);for(let l=0;l<n;l++){let m=IA(C.chunkData);this.verifyHeader(m,"LIST");let f=AA(m.chunkData,4);f!=="rgn "&&f!=="rgn2"&&(q(),this.parsingError(`Invalid DLS region! Expected "rgn " or "rgn2" got "${f}"`));let S=this.readRegion(m);S&&r.DLSInstrument.instrumentZones.push(S)}this.presets.push(r),this.instruments.push(r.DLSInstrument),q()}function Po(e){let A,t=!1;switch(e){default:case P.modLfo:case P.vibratoLfo:case P.coarseTune:case P.fineTune:case P.modEnv:return;case P.keyNum:A=_.noteOnKeyNum;break;case P.none:A=_.noController;break;case P.modulationWheel:A=y.modulationWheel,t=!0;break;case P.pan:A=y.pan,t=!0;break;case P.reverb:A=y.reverbDepth,t=!0;break;case P.chorus:A=y.chorusDepth,t=!0;break;case P.expression:A=y.expressionController,t=!0;break;case P.volume:A=y.mainVolume,t=!0;break;case P.velocity:A=_.noteOnVelocity;break;case P.polyPressure:A=_.polyPressure;break;case P.channelPressure:A=_.channelPressure;break;case P.pitchWheel:A=_.pitchWheel;break;case P.pitchWheelRange:A=_.pitchWheelRange;break}if(A===void 0)throw new Error(`Unknown DLS Source: ${e}`);return{enum:A,isCC:t}}function wi(e,A){switch(e){default:case R.none:return;case R.pan:return a.pan;case R.gain:return{gen:a.initialAttenuation,newAmount:A*-1};case R.pitch:return a.fineTune;case R.keyNum:return a.overridingRootKey;case R.volEnvDelay:return a.delayVolEnv;case R.volEnvAttack:return a.attackVolEnv;case R.volEnvHold:return a.holdVolEnv;case R.volEnvDecay:return a.decayVolEnv;case R.volEnvSustain:return{gen:a.sustainVolEnv,newAmount:1e3-A};case R.volEnvRelease:return a.releaseVolEnv;case R.modEnvDelay:return a.delayModEnv;case R.modEnvAttack:return a.attackModEnv;case R.modEnvHold:return a.holdModEnv;case R.modEnvDecay:return a.decayModEnv;case R.modEnvSustain:return{gen:a.sustainModEnv,newAmount:(1e3-A)/10};case R.modEnvRelease:return a.releaseModEnv;case R.filterCutoff:return a.initialFilterFc;case R.filterQ:return a.initialFilterQ;case R.chorusSend:return a.chorusEffectsSend;case R.reverbSend:return a.reverbEffectsSend;case R.modLfoFreq:return a.freqModLFO;case R.modLfoDelay:return a.delayModLFO;case R.vibLfoFreq:return a.freqVibLFO;case R.vibLfoDelay:return a.delayVibLFO}}function Ri(e,A){return e===P.vibratoLfo&&A===R.pitch?a.vibLfoToPitch:e===P.modLfo&&A===R.pitch?a.modLfoToPitch:e===P.modLfo&&A===R.filterCutoff?a.modLfoToFilterFc:e===P.modLfo&&A===R.gain?a.modLfoToVolume:e===P.modEnv&&A===R.filterCutoff?a.modEnvToFilterFc:e===P.modEnv&&A===R.pitch?a.modEnvToPitch:void 0}function Zo(e,A,t,n,s){let o=Ri(e,t),r,g,i=!1,C=!1,B=s;if(o===void 0){let F=wi(t,s);if(F===void 0){J(`Invalid destination: ${t}`);return}if(r=F,F.newAmount!==void 0&&(B=F.newAmount,r=F.gen),g=Po(e),g===void 0){J(`Invalid source: ${e}`);return}}else r=o,i=!0,g={enum:_.noController,isCC:!1},C=!0;let h=Po(A);if(h===void 0){J(`Invalid control: ${A}`);return}let c;if(C)c=0;else{let F=n&15,D=n>>10&15;D===FA.linear&&F!==FA.linear&&(D=F);let x=n>>14&1,L=n>>15&1;r===a.initialAttenuation&&s<0&&(L=1),c=ZA(D,x,L,g.isCC,g.enum)}r===a.initialAttenuation&&(B=Math.max(960,Math.min(0,B)));let l=n>>4&15,m=n>>8&1,f=n>>9&1,S=ZA(l,m,f,h.isCC,h.enum);if(i){let F=S;S=c,c=F}return new X(c,S,r,B,0)}function Is(e,A){let t=e.chunkData,n=[],s=[];b(t,4);let o=b(t,4);for(let r=0;r<o;r++){let g=b(t,2),i=b(t,2),C=b(t,2),B=b(t,2),c=(b(t,4)|0)>>16;if(g===0&&i===0&&B===0){let l;switch(C){case R.pan:l=new U(a.pan,c);break;case R.gain:l=new U(a.initialAttenuation,-c*10/.4);break;case R.filterCutoff:l=new U(a.initialFilterFc,c);break;case R.filterQ:l=new U(a.initialFilterQ,c);break;case R.modLfoFreq:l=new U(a.freqModLFO,c);break;case R.modLfoDelay:l=new U(a.delayModLFO,c);break;case R.vibLfoFreq:l=new U(a.freqVibLFO,c);break;case R.vibLfoDelay:l=new U(a.delayVibLFO,c);break;case R.volEnvDelay:l=new U(a.delayVolEnv,c);break;case R.volEnvAttack:l=new U(a.attackVolEnv,c);break;case R.volEnvHold:l=new U(a.holdVolEnv,c,!1);break;case R.volEnvDecay:l=new U(a.decayVolEnv,c,!1);break;case R.volEnvRelease:l=new U(a.releaseVolEnv,c);break;case R.volEnvSustain:let m=1e3-c;l=new U(a.sustainVolEnv,m);break;case R.modEnvDelay:l=new U(a.delayModEnv,c);break;case R.modEnvAttack:l=new U(a.attackModEnv,c);break;case R.modEnvHold:l=new U(a.holdModEnv,c,!1);break;case R.modEnvDecay:l=new U(a.decayModEnv,c,!1);break;case R.modEnvRelease:l=new U(a.releaseModEnv,c);break;case R.modEnvSustain:let f=1e3-c;l=new U(a.sustainModEnv,f);break;case R.reverbSend:l=new U(a.reverbEffectsSend,c);break;case R.chorusSend:l=new U(a.chorusEffectsSend,c);break;case R.pitch:let S=Math.floor(c/100),F=Math.floor(c-S*100);l=new U(a.fineTune,F),n.push(new U(a.coarseTune,S));break}l&&n.push(l)}else{let l=!0,m=(f,S,F)=>{let D=f/-128;if(n.push(new U(S,D)),D<=120){let x=Math.round(.46875*f);n.forEach(L=>{L.generatorType===F&&(L.generatorValue+=x)})}};if(i===P.none?g===P.modLfo&&C===R.pitch?n.push(new U(a.modLfoToPitch,c)):g===P.modLfo&&C===R.gain?n.push(new U(a.modLfoToVolume,c)):g===P.modLfo&&C===R.filterCutoff?n.push(new U(a.modLfoToFilterFc,c)):g===P.vibratoLfo&&C===R.pitch?n.push(new U(a.vibLfoToPitch,c)):g===P.modEnv&&C===R.pitch?n.push(new U(a.modEnvToPitch,c)):g===P.modEnv&&C===R.filterCutoff?n.push(new U(a.modEnvToFilterFc,c)):g===P.keyNum&&C===R.pitch?n.push(new U(a.scaleTuning,c/128)):g===P.keyNum&&C===R.volEnvHold?m(c,a.keyNumToVolEnvHold,a.holdVolEnv):g===P.keyNum&&C===R.volEnvDecay?m(c,a.keyNumToVolEnvDecay,a.decayVolEnv):g===P.keyNum&&C===R.modEnvHold?m(c,a.keyNumToModEnvHold,a.holdModEnv):g===P.keyNum&&C===R.modEnvDecay?m(c,a.keyNumToModEnvDecay,a.decayModEnv):l=!1:l=!1,l===!1){let f=Zo(g,i,C,B,c);f?(s.push(f),p("%cSucceeded converting to SF2 Modulator!",I.recognized)):J("Failed converting to SF2 Modulator!")}}}return A&&s.push(X.copy(Bn),X.copy(ln)),{modulators:s,generators:n}}function Xo(e,A,t){if(e)for(;e.chunkData.currentIndex<e.chunkData.length;){let n=IA(e.chunkData);this.verifyHeader(n,"art1","art2");let s=Is(n,!0);t.generators.push(...s.generators),t.modulators.push(...s.modulators)}if(A)for(;A.chunkData.currentIndex<A.chunkData.length;){let n=IA(A.chunkData);this.verifyHeader(n,"art2","art1");let s=Is(n,!1);t.generators.push(...s.generators),t.modulators.push(...s.modulators)}}var fn=class extends OA{constructor(A,t){super(),this.keyRange=A,this.velRange=t,this.isGlobal=!0}setWavesample(A,t,n,s,o,r,g){t!==0&&this.generators.push(new U(a.sampleModes,t)),this.generators.push(new U(a.initialAttenuation,A)),this.isGlobal=!1,g-=o.samplePitchCorrection;let i=Math.trunc(g/100);i!==0&&this.generators.push(new U(a.coarseTune,i));let C=g-i*100;if(C!==0&&this.generators.push(new U(a.fineTune,C)),t!==0){let B=n.start-o.sampleLoopStartIndex,h=n.end-o.sampleLoopEndIndex;if(B!==0){let c=B%32768;this.generators.push(new U(a.startloopAddrsOffset,c));let l=Math.trunc(B/32768);l!==0&&this.generators.push(new U(a.startloopAddrsCoarseOffset,l))}if(h!==0){let c=h%32768;this.generators.push(new U(a.endloopAddrsOffset,c));let l=Math.trunc(h/32768);l!==0&&this.generators.push(new U(a.endloopAddrsCoarseOffset,l))}}s!==o.samplePitch&&this.generators.push(new U(a.overridingRootKey,s)),this.generators.push(new U(a.sampleID,r)),this.sample=o,o.useCount++}};function Wo(e){let A=[];for(;e.chunkData.length>e.chunkData.currentIndex;)A.push(IA(e.chunkData));let t=A.find(v=>v.header==="rgnh"),n=b(t.chunkData,2),s=b(t.chunkData,2),o=b(t.chunkData,2),r=b(t.chunkData,2);o===0&&r===0&&(r=127,o=0);let g=new fn({min:n,max:s},{min:o,max:r});b(t.chunkData,2);let i=b(t.chunkData,2);i!==0&&g.generators.push(new U(a.exclusiveClass,i));let C=XA(A,"lart"),B=XA(A,"lar2");this.readLart(C,B,g),g.isGlobal=!1;let h=A.find(v=>v.header==="wsmp");b(h.chunkData,4);let c=b(h.chunkData,2),l=Oe(h.chunkData[h.chunkData.currentIndex++],h.chunkData[h.chunkData.currentIndex++]),f=(b(h.chunkData,4)|0)/-655360;b(h.chunkData,4);let S=b(h.chunkData,4),F,D={start:0,end:0};if(S===0)F=0;else{b(h.chunkData,4),b(h.chunkData,4)===0?F=1:F=3,D.start=b(h.chunkData,4);let eA=b(h.chunkData,4);D.end=D.start+eA}let x=A.find(v=>v.header==="wlnk");if(x===void 0)return;b(x.chunkData,2),b(x.chunkData,2),b(x.chunkData,4);let L=b(x.chunkData,4),G=this.samples[L];if(G===void 0)throw new Error("Invalid sample ID!");let M=(f||G.sampleDbAttenuation)*10/.4;return g.setWavesample(M,F,D,c,G,L,l),g}var un=class extends Ge{sampleDbAttenuation;sampleData;constructor(A,t,n,s,o,r,g,i){super(A,t,n,s,0,1,o,r),this.sampleData=g,this.sampleDbAttenuation=i}getAudioData(){return this.sampleData}getRawData(){if(this.isCompressed){if(!this.compressedData)throw new Error("Compressed but no data?? This shouldn't happen!!");return this.compressedData}return super.getRawData()}};var _o={PCM:1,ALAW:6};function Gi(e,A){let t=Math.pow(2,A*8-1),n=Math.pow(2,A*8),s,o=!1;A===1?(s=255,o=!0):s=t;let r=e.size/A,g=new Float32Array(r);for(let i=0;i<g.length;i++){let C=b(e.chunkData,A);o?g[i]=C/s-.5:(C>=t&&(C-=n),g[i]=C/s)}return g}function Mi(e,A){let t=e.size/A,n=new Float32Array(t);for(let s=0;s<n.length;s++){let o=b(e.chunkData,A),r=o^85;r&=127;let g=r>>4,i=r&15;g>0&&(i+=16),i=(i<<4)+8,g>1&&(i=i<<g-1);let C=o>127?i:-i;n[s]=C/32678}return n}function zo(e){uA("%cLoading Wave samples...",I.recognized);let A=0;for(;e.chunkData.currentIndex<e.chunkData.length;){let t=IA(e.chunkData);this.verifyHeader(t,"LIST"),this.verifyText(AA(t.chunkData,4),"wave");let n=[];for(;t.chunkData.currentIndex<t.chunkData.length;)n.push(IA(t.chunkData));let s=n.find(G=>G.header==="fmt ");if(!s)throw new Error("No fmt chunk in the wave file!");let o=b(s.chunkData,2),r=b(s.chunkData,2);if(r!==1)throw new Error(`Only mono samples are supported. Fmt reports ${r} channels`);let g=b(s.chunkData,4);b(s.chunkData,4),b(s.chunkData,2);let C=b(s.chunkData,2)/8,B=!1,h=n.find(G=>G.header==="data");h||this.parsingError("No data chunk in the WAVE chunk!");let c;switch(o){default:B=!0,c=new Float32Array(h.size/C);break;case _o.PCM:c=Gi(h,C);break;case _o.ALAW:c=Mi(h,C);break}let l=XA(n,"INFO"),m=`Unnamed ${A}`;if(l){let G=IA(l.chunkData);for(;G.header!=="INAM"&&l.chunkData.currentIndex<l.chunkData.length;)G=IA(l.chunkData);G.header==="INAM"&&(m=AA(G.chunkData,G.size).trim())}let f=60,S=0,F=0,D=c.length-1,x=0,L=n.find(G=>G.header==="wsmp");if(L){b(L.chunkData,4),f=b(L.chunkData,2),S=Oe(L.chunkData[L.chunkData.currentIndex++],L.chunkData[L.chunkData.currentIndex++]);let G=Math.trunc(S/100);if(f+=G,S-=G*100,x=(b(L.chunkData,4)|0)/-655360,b(L.chunkData,4),b(L.chunkData,4)===1){b(L.chunkData,8),F=b(L.chunkData,4);let v=b(L.chunkData,4);D=F+v}}else J("No wsmp chunk in wave... using sane defaults.");B&&console.error(`Failed to load '${m}': Unsupported format: (${o})`),this.samples.push(new un(m,g,f,S,F,D,c,x)),A++,p(`%cLoaded sample %c${m}`,I.info,I.recognized)}q()}var be=class extends Ne{constructor(A){super(),this.dataArray=new N(A),ae("%cParsing DLS...",I.info),this.dataArray||(q(),this.parsingError("No data provided!"));let t=IA(this.dataArray,!1);this.verifyHeader(t,"riff"),this.verifyText(AA(this.dataArray,4).toLowerCase(),"dls ");let n=[];for(;this.dataArray.currentIndex<this.dataArray.length;)n.push(IA(this.dataArray));this.soundFontInfo.ifil="2.1",this.soundFontInfo.isng="EMU8000",this.soundFontInfo.INAM="Unnamed DLS",this.soundFontInfo.IENG="Unknown",this.soundFontInfo.IPRD="SpessaSynth DLS",this.soundFontInfo.ICRD=new Date().toDateString();let s=XA(n,"INFO");if(s)for(;s.chunkData.currentIndex<s.chunkData.length;){let i=IA(s.chunkData);this.soundFontInfo[i.header]=AA(i.chunkData,i.size)}this.soundFontInfo.ICMT=this.soundFontInfo.ICMT||"(No description)",this.soundFontInfo.ISBJ&&(this.soundFontInfo.ICMT+=`