midiwire 0.11.2 → 0.12.0
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/dist/midiwire.es.js +1293 -1427
- package/dist/midiwire.umd.js +1 -1
- package/dist/utils/dx7/DX7Voice.d.ts +2 -0
- package/dist/utils/dx7/DX7Voice.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/utils/dx7/DX7Voice.js +4 -2
package/dist/midiwire.umd.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(l,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(l=typeof globalThis<"u"?globalThis:l||self,p(l.MIDIControls={}))})(this,(function(l){"use strict";class p{constructor(t,s="[data-midi-cc]"){this.controller=t,this.selector=s,this.observer=null}bindAll(){document.querySelectorAll(this.selector==="[data-midi-cc]"?"[data-midi-cc], [data-midi-msb][data-midi-lsb]":this.selector).forEach(s=>{if(s.hasAttribute("data-midi-bound"))return;const e=this._parseAttributes(s);e&&(this.controller.bind(s,e),s.setAttribute("data-midi-bound","true"))})}enableAutoBinding(){if(this.observer)return;const t=this.selector==="[data-midi-cc]"?"[data-midi-cc], [data-midi-msb][data-midi-lsb]":this.selector;this.observer=new MutationObserver(s=>{s.forEach(e=>{e.addedNodes.forEach(E=>{if(E.nodeType===Node.ELEMENT_NODE){if(E.matches?.(t)){const _=this._parseAttributes(E);_&&!E.hasAttribute("data-midi-bound")&&(this.controller.bind(E,_),E.setAttribute("data-midi-bound","true"))}E.querySelectorAll&&E.querySelectorAll(t).forEach(i=>{if(!i.hasAttribute("data-midi-bound")){const r=this._parseAttributes(i);r&&(this.controller.bind(i,r),i.setAttribute("data-midi-bound","true"))}})}}),e.removedNodes.forEach(E=>{E.nodeType===Node.ELEMENT_NODE&&(E.hasAttribute?.("data-midi-bound")&&this.controller.unbind(E),E.querySelectorAll&&E.querySelectorAll("[data-midi-bound]").forEach(i=>{this.controller.unbind(i)}))})})}),this.observer.observe(document.body,{childList:!0,subtree:!0})}disableAutoBinding(){this.observer&&(this.observer.disconnect(),this.observer=null)}_parseAttributes(t){const s=parseInt(t.dataset.midiMsb,10),e=parseInt(t.dataset.midiLsb,10);if(!Number.isNaN(s)&&!Number.isNaN(e)&&s>=0&&s<=127&&e>=0&&e<=127){const _=parseInt(t.dataset.midiCc,10);return!Number.isNaN(_)&&_>=0&&_<=127&&console.warn(`Element has both 7-bit (data-midi-cc="${_}") and 14-bit (data-midi-msb="${s}" data-midi-lsb="${e}") CC attributes. 14-bit takes precedence.`,t),{msb:s,lsb:e,is14Bit:!0,channel:parseInt(t.dataset.midiChannel,10)||void 0,min:parseFloat(t.getAttribute("min"))||0,max:parseFloat(t.getAttribute("max"))||127,invert:t.dataset.midiInvert==="true",label:t.dataset.midiLabel}}const E=parseInt(t.dataset.midiCc,10);return!Number.isNaN(E)&&E>=0&&E<=127?{cc:E,channel:parseInt(t.dataset.midiChannel,10)||void 0,min:parseFloat(t.getAttribute("min"))||0,max:parseFloat(t.getAttribute("max"))||127,invert:t.dataset.midiInvert==="true",label:t.dataset.midiLabel}:((t.dataset.midiCc!==void 0||t.dataset.midiMsb!==void 0&&t.dataset.midiLsb!==void 0)&&console.warn("Invalid MIDI configuration on element:",t),null)}destroy(){this.disableAutoBinding()}}class m extends Error{constructor(t,s){super(t),this.name="MIDIError",this.code=s,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}}class g extends m{constructor(t,s){super(t,"MIDI_ACCESS_ERROR"),this.name="MIDIAccessError",this.reason=s}}class G extends m{constructor(t){super(t,"MIDI_CONNECTION_ERROR"),this.name="MIDIConnectionError"}}class L extends m{constructor(t,s,e){super(t,"MIDI_DEVICE_ERROR"),this.name="MIDIDeviceError",this.deviceType=s,this.deviceId=e}}class R extends m{constructor(t,s){super(t,"MIDI_VALIDATION_ERROR"),this.name="MIDIValidationError",this.validationType=s}}class F extends Error{constructor(t,s){super(t),this.name="DX7Error",this.code=s,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}}class M extends F{constructor(t,s,e){super(t,"DX7_PARSE_ERROR"),this.name="DX7ParseError",this.parseType=s,this.offset=e}}class d extends F{constructor(t,s,e){super(t,"DX7_VALIDATION_ERROR"),this.name="DX7ValidationError",this.validationType=s,this.value=e}}function c(C,t,s){return Math.max(t,Math.min(s,C))}function H(C,t,s,e=!1){const E=(C-t)/(s-t),i=(e?1-E:E)*127;return c(Math.round(i),0,127)}function W(C,t,s,e=!1){let E=c(C,0,127)/127;return e&&(E=1-E),t+E*(s-t)}function Q(C){const t={C:0,"C#":1,DB:1,D:2,"D#":3,EB:3,E:4,F:5,"F#":6,GB:6,G:7,"G#":8,AB:8,A:9,"A#":10,BB:10,B:11},s=C.match(/^([A-G][#b]?)(-?\d+)$/i);if(!s)throw new R(`Invalid note name: ${C}`,"note",C);const[,e,E]=s,_=t[e.toUpperCase()];if(_===void 0)throw new R(`Invalid note: ${e}`,"note",e);const i=(parseInt(E,10)+1)*12+_;return c(i,0,127)}function J(C,t=!1){const E=t?["C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"]:["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],_=Math.floor(C/12)-1;return`${E[C%12]}${_}`}function j(C){const t=69+12*Math.log2(C/440);return c(Math.round(t),0,127)}function k(C){return 440*2**((C-69)/12)}function V(C){return{0:"Bank Select",1:"Modulation",2:"Breath Controller",4:"Foot Controller",5:"Portamento Time",7:"Volume",8:"Balance",10:"Pan",11:"Expression",64:"Sustain Pedal",65:"Portamento",66:"Sostenuto",67:"Soft Pedal",68:"Legato",71:"Resonance",72:"Release Time",73:"Attack Time",74:"Cutoff",75:"Decay Time",76:"Vibrato Rate",77:"Vibrato Depth",78:"Vibrato Delay",84:"Portamento Control",91:"Reverb",92:"Tremolo",93:"Chorus",94:"Detune",95:"Phaser",120:"All Sound Off",121:"Reset All Controllers",123:"All Notes Off"}[C]||`CC ${C}`}function v(C){const t=c(Math.round(C),0,16383);return{msb:t>>7&127,lsb:t&127}}function w(C,t){return c(C,0,127)<<7|c(t,0,127)}function B(C,t,s,e=!1){const E=(C-t)/(s-t),i=(e?1-E:E)*16383;return v(i)}function X(C,t,s,e,E=!1){let i=w(C,t)/16383;return E&&(i=1-i),s+i*(e-s)}class b{constructor(){this.events=new Map}on(t,s){return this.events.has(t)||this.events.set(t,[]),this.events.get(t).push(s),()=>this.off(t,s)}once(t,s){const e=(...E)=>{s(...E),this.off(t,e)};this.on(t,e)}off(t,s){if(!this.events.has(t))return;const e=this.events.get(t),E=e.indexOf(s);E>-1&&e.splice(E,1),e.length===0&&this.events.delete(t)}emit(t,s){if(!this.events.has(t))return;[...this.events.get(t)].forEach(E=>{try{E(s)}catch(_){console.error(`Error in event handler for "${t}":`,_)}})}removeAllListeners(t){t?this.events.delete(t):this.events.clear()}}const U={DEVICE_CHANGE:"device-change",IN_DEV_CONNECTED:"in-dev-connected",IN_DEV_DISCONNECTED:"in-dev-disconnected",OUT_DEV_CONNECTED:"out-dev-connected",OUT_DEV_DISCONNECTED:"out-dev-disconnected"};class Y extends b{constructor(t={}){super(),this.options={sysex:!1,...t},this.midiAccess=null,this.output=null,this.input=null}async requestAccess(){if(!navigator.requestMIDIAccess)throw new g("Web MIDI API is not supported in this browser","unsupported");try{this.midiAccess=await navigator.requestMIDIAccess({sysex:this.options.sysex}),this.midiAccess.onstatechange=t=>{const s=t.port,e=t.port.state;this.emit(U.DEVICE_CHANGE,{port:s,state:e,type:s.type,device:{id:s.id,name:s.name,manufacturer:s.manufacturer||"Unknown"}}),e==="disconnected"?s.type==="input"?(this.emit(U.IN_DEV_DISCONNECTED,{device:s}),this.input&&this.input.id===s.id&&(this.input=null)):s.type==="output"&&(this.emit(U.OUT_DEV_DISCONNECTED,{device:s}),this.output&&this.output.id===s.id&&(this.output=null)):e==="connected"&&(s.type==="input"?this.emit(U.IN_DEV_CONNECTED,{device:s}):s.type==="output"&&this.emit(U.OUT_DEV_CONNECTED,{device:s}))}}catch(t){throw t.name==="SecurityError"?new g("MIDI access denied. SysEx requires user permission.","denied"):new g(`Failed to get MIDI access: ${t.message}`,"failed")}}async connect(t){if(!this.midiAccess)throw new G("MIDI access not initialized. Call requestAccess() first.");const s=Array.from(this.midiAccess.outputs.values());if(s.length===0)throw new L("No MIDI output devices available","output");if(t===void 0){this.output=s[0];return}if(typeof t=="number"){if(t<0||t>=s.length)throw new L(`Output index ${t} out of range (0-${s.length-1})`,"output",t);this.output=s[t];return}if(this.output=s.find(e=>e.name===t||e.id===t),!this.output){const e=s.map(E=>E.name).join(", ");throw new L(`MIDI output "${t}" not found. Available: ${e}`,"output",t)}}async connectInput(t,s){if(!this.midiAccess)throw new G("MIDI access not initialized. Call requestAccess() first.");if(typeof s!="function")throw new R("onMessage callback must be a function","callback");const e=Array.from(this.midiAccess.inputs.values());if(e.length===0)throw new L("No MIDI input devices available","input");if(this.input&&(this.input.onmidimessage=null),t===void 0)this.input=e[0];else if(typeof t=="number"){if(t<0||t>=e.length)throw new L(`Input index ${t} out of range (0-${e.length-1})`,"input",t);this.input=e[t]}else if(this.input=e.find(E=>E.name===t||E.id===t),!this.input){const E=e.map(_=>_.name).join(", ");throw new L(`MIDI input "${t}" not found. Available: ${E}`,"input",t)}this.input.onmidimessage=E=>{s(E)}}disconnectOutput(){this.output=null}disconnectInput(){this.input&&(this.input.onmidimessage=null,this.input=null)}disconnect(){this.disconnectOutput(),this.disconnectInput()}isConnected(){return this.output!==null}getCurrentOutput(){return this.output?{id:this.output.id,name:this.output.name,manufacturer:this.output.manufacturer||"Unknown"}:null}getCurrentInput(){return this.input?{id:this.input.id,name:this.input.name,manufacturer:this.input.manufacturer||"Unknown"}:null}getOutputs(){if(!this.midiAccess)return[];const t=[];return this.midiAccess.outputs.forEach(s=>{s.state==="connected"&&t.push({id:s.id,name:s.name,manufacturer:s.manufacturer||"Unknown"})}),t}getInputs(){if(!this.midiAccess)return[];const t=[];return this.midiAccess.inputs.forEach(s=>{s.state==="connected"&&t.push({id:s.id,name:s.name,manufacturer:s.manufacturer||"Unknown"})}),t}send(t,s=null){if(!this.output){console.warn("No MIDI output connected. Call connect() first.");return}try{const e=new Uint8Array(t);s===null?this.output.send(e):this.output.send(e,s)}catch(e){console.error("Failed to send MIDI message:",e)}}sendSysEx(t,s=!1){if(!this.options.sysex){console.warn("SysEx not enabled. Initialize with sysex: true");return}let e;s?e=[240,...t,247]:e=t,this.send(e)}}const o={READY:"ready",ERROR:"error",DESTROYED:"destroyed",DEV_OUT_CONNECTED:"dev-out-connected",DEV_OUT_DISCONNECTED:"dev-out-disconnected",DEV_IN_CONNECTED:"dev-in-connected",DEV_IN_DISCONNECTED:"dev-in-disconnected",CH_CC_SEND:"ch-cc-send",CH_CC_RECV:"ch-cc-recv",CH_NOTE_ON_SEND:"ch-note-on-send",CH_NOTE_ON_RECV:"ch-note-on-recv",CH_NOTE_OFF_SEND:"ch-note-off-send",CH_NOTE_OFF_RECV:"ch-note-off-recv",CH_PC_SEND:"ch-pc-send",CH_PC_RECV:"ch-pc-recv",CH_PITCH_BEND_SEND:"ch-pitch-bend-send",CH_PITCH_BEND_RECV:"ch-pitch-bend-recv",CH_MONO_PRESS_SEND:"ch-mono-press-send",CH_MONO_PRESS_RECV:"ch-mono-press-recv",CH_POLY_PRESS_SEND:"ch-poly-press-send",CH_POLY_PRESS_RECV:"ch-poly-press-recv",CH_ALL_SOUNDS_OFF_SEND:"ch-all-sounds-off-send",CH_RESET_CONTROLLERS_SEND:"ch-reset-controllers-send",CH_LOCAL_CONTROL_SEND:"ch-local-control-send",CH_ALL_NOTES_OFF_SEND:"ch-all-notes-off-send",CH_OMNI_OFF_SEND:"ch-omni-off-send",CH_OMNI_ON_SEND:"ch-omni-on-send",CH_MONO_ON_SEND:"ch-mono-on-send",CH_POLY_ON_SEND:"ch-poly-on-send",SYS_EX_SEND:"sys-ex-send",SYS_EX_RECV:"sys-ex-recv",SYS_CLOCK_RECV:"sys-clock-recv",SYS_START_RECV:"sys-start-recv",SYS_CONTINUE_RECV:"sys-continue-recv",SYS_STOP_RECV:"sys-stop-recv",SYS_MTC_RECV:"sys-mtc-recv",SYS_SONG_POS_RECV:"sys-song-pos-recv",SYS_SONG_SEL_RECV:"sys-song-sel-recv",SYS_TUNE_REQ_RECV:"sys-tune-req-recv",SYS_ACT_SENSE_RECV:"sys-act-sense-recv",SYS_RESET_RECV:"sys-reset-recv",MIDI_RAW:"midi-raw",PATCH_SAVED:"patch-saved",PATCH_LOADED:"patch-loaded",PATCH_DELETED:"patch-deleted"};class x extends b{constructor(t={}){super(),this.options={inputChannel:1,outputChannel:1,autoConnect:!0,sysex:!1,...t},this.connection=null,this.bindings=new Map,this.state={controlChange:new Map,programChange:new Map,pitchBend:new Map,monoPressure:new Map,polyPressure:new Map},this.initialized=!1,this._initNamespaces()}async init(){if(this.initialized){console.warn("MIDI Controller already initialized");return}try{this.connection=new Y({sysex:this.options.sysex}),await this.connection.requestAccess(),this.connection.on(U.DEVICE_CHANGE,async({state:t,type:s,device:e})=>{try{if(t==="connected")s==="output"?this.emit(o.DEV_OUT_CONNECTED,e):s==="input"&&this.emit(o.DEV_IN_CONNECTED,e);else if(t==="disconnected"){if(s==="output"&&e){const E=this.connection.getCurrentOutput();E&&E.id===e.id?await this._disconnectOutput():this.emit(o.DEV_OUT_DISCONNECTED,e)}if(s==="input"&&e){const E=this.connection.getCurrentInput();E&&E.id===e.id?await this._disconnectInput():this.emit(o.DEV_IN_DISCONNECTED,e)}}}catch(E){console.error("Error in device change handler:",E),this.emit(o.ERROR,E)}}),this.options.autoConnect&&await this.connection.connect(this.options.output),this.options.input!==void 0&&await this._connectInput(this.options.input),this.initialized=!0,this.emit(o.READY,this),this.options.onReady?.(this)}catch(t){throw this.emit(o.ERROR,t),this.options.onError?.(t),t}}_initNamespaces(){this.device={connect:this._connect.bind(this),disconnect:this._disconnect.bind(this),connectInput:this._connectInput.bind(this),disconnectInput:this._disconnectInput.bind(this),connectOutput:this._connectOutput.bind(this),disconnectOutput:this._disconnectOutput.bind(this),getCurrentOutput:this._getCurrentOutput.bind(this),getCurrentInput:this._getCurrentInput.bind(this),getOutputs:this._getOutputs.bind(this),getInputs:this._getInputs.bind(this)},this.channel={sendNoteOn:this._sendNoteOn.bind(this),sendNoteOff:this._sendNoteOff.bind(this),sendCC:this._sendCC.bind(this),getCC:this._getCC.bind(this),sendPC:this._sendPC.bind(this),getPC:this._getPC.bind(this),sendPitchBend:this._sendPitchBend.bind(this),getPitchBend:this._getPitchBend.bind(this),sendMonoPressure:this._sendMonoPressure.bind(this),getMonoPressure:this._getMonoPressure.bind(this),sendPolyPressure:this._sendPolyPressure.bind(this),getPolyPressure:this._getPolyPressure.bind(this),allSoundsOff:this._allSoundsOff.bind(this),resetControllers:this._resetControllers.bind(this),localControl:this._localControl.bind(this),allNotesOff:this._allNotesOff.bind(this),omniOff:this._omniOff.bind(this),omniOn:this._omniOn.bind(this),monoOn:this._monoOn.bind(this),polyOn:this._polyOn.bind(this)},this.system={sendEx:(function(t,s=!1){return this._sendSysEx(t,s)}).bind(this),sendClock:this._sendClock.bind(this),start:this._sendStart.bind(this),continue:this._sendContinue.bind(this),stop:this._sendStop.bind(this),sendMTC:this._sendMTC.bind(this),sendSongPosition:this._sendSongPosition.bind(this),sendSongSelect:this._sendSongSelect.bind(this),sendTuneRequest:this._sendTuneRequest.bind(this),sendActiveSensing:this._sendActiveSensing.bind(this),sendSystemReset:this._sendSystemReset.bind(this)},this.patch={get:this._getPatch.bind(this),set:this._setPatch.bind(this),save:this._savePatch.bind(this),load:this._loadPatch.bind(this),delete:this._deletePatch.bind(this),list:this._listPatches.bind(this)}}bind(t,s,e={}){if(!t)return console.warn("Cannot bind: element is null or undefined"),()=>{};const E=this._createBinding(t,s,e);return this.bindings.set(t,E),this.initialized&&this.connection?.isConnected()&&E.handler({target:t}),()=>this.unbind(t)}_createBinding(t,s,e={}){const{min:E=parseFloat(t.getAttribute("min"))||0,max:_=parseFloat(t.getAttribute("max"))||127,channel:i,invert:r=!1,onInput:h=void 0}=s,{debounce:S=0}=e,P={...s,min:E,max:_,invert:r,onInput:h};if(i!==void 0&&(P.channel=i),s.is14Bit){const{msb:T,lsb:I}=s,f=y=>{const z=parseFloat(y.target.value);if(Number.isNaN(z))return;const{msb:ht,lsb:Nt}=B(z,E,_,r),q=i||this.options.outputChannel;this._sendCC(T,ht,q),this._sendCC(I,Nt,q)};let K=null;const D=S>0?y=>{K&&clearTimeout(K),K=setTimeout(()=>{f(y),K=null},S)}:f;return t.addEventListener("input",D),t.addEventListener("change",D),{element:t,config:P,handler:f,destroy:()=>{K&&clearTimeout(K),t.removeEventListener("input",D),t.removeEventListener("change",D)}}}const{cc:a}=s,A=T=>{const I=parseFloat(T.target.value);if(Number.isNaN(I))return;const f=H(I,E,_,r),K=i===void 0?this.options.outputChannel:i;this._sendCC(a,f,K)};let N=null;const O=S>0?T=>{N&&clearTimeout(N),N=setTimeout(()=>{A(T),N=null},S)}:A;return t.addEventListener("input",O),t.addEventListener("change",O),{element:t,config:P,handler:A,destroy:()=>{N&&clearTimeout(N),t.removeEventListener("input",O),t.removeEventListener("change",O)}}}unbind(t){const s=this.bindings.get(t);s&&(s.destroy(),this.bindings.delete(t))}async destroy(){for(const t of this.bindings.values())t.destroy();this.bindings.clear(),this.state.controlChange.clear(),this.state.programChange.clear(),this.state.pitchBend.clear(),this.state.monoPressure.clear(),this.state.polyPressure.clear(),await this._disconnect(),this.initialized=!1,this.emit(o.DESTROYED),this.removeAllListeners()}async _connect(t){t?await this.connection.connect(t):this.options.output!==void 0?await this.connection.connect(this.options.output):this.options.autoConnect&&await this.connection.connect()}async _disconnect(){this.connection.disconnect()}async _connectInput(t){await this.connection.connectInput(t,s=>{this._handleMIDIMessage(s)}),this.emit(o.DEV_IN_CONNECTED,this.connection.getCurrentInput())}async _disconnectInput(){const t=this.connection.getCurrentInput();this.connection.disconnectInput(),this.emit(o.DEV_IN_DISCONNECTED,t)}async _connectOutput(t){await this.connection.connect(t),this.emit(o.DEV_OUT_CONNECTED,this.connection.getCurrentOutput())}async _disconnectOutput(){const t=this.connection.getCurrentOutput();this.connection.disconnectOutput(),this.emit(o.DEV_OUT_DISCONNECTED,t)}_getCurrentOutput(){return this.connection?.getCurrentOutput()||null}_getCurrentInput(){return this.connection?.getCurrentInput()||null}_getOutputs(){return this.connection?.getOutputs()||[]}_getInputs(){return this.connection?.getInputs()||[]}send(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send(t)}_sendNoteOn(t,s=64,e=this.options.outputChannel){if(!this.initialized)return;t=c(Math.round(t),0,127),s=c(Math.round(s),0,127),e=c(Math.round(e),1,16);const E=144+(e-1);this.connection.send([E,t,s]),this.emit(o.CH_NOTE_ON_SEND,{note:t,velocity:s,channel:e})}_sendNoteOff(t,s=this.options.outputChannel,e=0){if(!this.initialized)return;t=c(Math.round(t),0,127),e=c(Math.round(e),0,127),s=c(Math.round(s),1,16);const E=144+(s-1);this.connection.send([E,t,e]),this.emit(o.CH_NOTE_OFF_SEND,{note:t,channel:s,velocity:e})}_sendCC(t,s,e=this.options.outputChannel){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}t=c(Math.round(t),0,127),s=c(Math.round(s),0,127),e=c(Math.round(e),1,16);const E=176+(e-1);this.connection.send([E,t,s]);const _=`${e}:${t}`;this.state.controlChange.set(_,s),this.emit(o.CH_CC_SEND,{cc:t,value:s,channel:e})}_sendPC(t,s=this.options.outputChannel){if(!this.initialized)return;t=c(Math.round(t),0,127),s=c(Math.round(s),1,16);const e=192+(s-1);this.connection.send([e,t]),this.state.programChange.set(s.toString(),t),this.emit(o.CH_PC_SEND,{program:t,channel:s})}_getPC(t=this.options.inputChannel){return this.state.programChange.get(t.toString())}_getCC(t,s=this.options.inputChannel){const e=`${s}:${t}`;return this.state.controlChange.get(e)}_sendPitchBend(t,s=this.options.outputChannel){if(!this.initialized)return;t=c(Math.round(t),0,16383),s=c(Math.round(s),1,16);const e=224+(s-1),E=t&127,_=t>>7&127;this.connection.send([e,E,_]),this.state.pitchBend.set(s.toString(),t),this.emit(o.CH_PITCH_BEND_SEND,{value:t,channel:s})}_getPitchBend(t=this.options.inputChannel){return this.state.pitchBend.get(t.toString())}_sendMonoPressure(t,s=this.options.outputChannel){if(!this.initialized)return;t=c(Math.round(t),0,127),s=c(Math.round(s),1,16);const e=208+(s-1);this.connection.send([e,t]),this.state.monoPressure.set(s.toString(),t),this.emit(o.CH_MONO_PRESS_SEND,{pressure:t,channel:s})}_getMonoPressure(t=this.options.inputChannel){return this.state.monoPressure.get(t.toString())}_sendPolyPressure(t,s,e=this.options.outputChannel){if(!this.initialized)return;t=c(Math.round(t),0,127),s=c(Math.round(s),0,127),e=c(Math.round(e),1,16);const E=160+(e-1);this.connection.send([E,t,s]);const _=`${e}:${t}`;this.state.polyPressure.set(_,s),this.emit(o.CH_POLY_PRESS_SEND,{note:t,pressure:s,channel:e})}_getPolyPressure(t,s=this.options.inputChannel){const e=`${s}:${t}`;return this.state.polyPressure.get(e)}_allSoundsOff(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(t!==void 0){t=c(Math.round(t),1,16);const s=176+(t-1);this.connection.send([s,120,0]),this.emit(o.CH_ALL_SOUNDS_OFF_SEND,{channel:t})}else{for(let s=1;s<=16;s++){const e=176+(s-1);this.connection.send([e,120,0])}this.emit(o.CH_ALL_SOUNDS_OFF_SEND,{channel:null})}}_resetControllers(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(t!==void 0){t=c(Math.round(t),1,16);const s=176+(t-1);this.connection.send([s,121,0]),this.emit(o.CH_RESET_CONTROLLERS_SEND,{channel:t})}else{for(let s=1;s<=16;s++){const e=176+(s-1);this.connection.send([e,121,0])}this.emit(o.CH_RESET_CONTROLLERS_SEND,{channel:null})}}_localControl(t,s){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}const e=t?127:0;if(s!==void 0){s=c(Math.round(s),1,16);const E=176+(s-1);this.connection.send([E,122,e]),this.emit(o.CH_LOCAL_CONTROL_SEND,{enabled:t,channel:s})}else{for(let E=1;E<=16;E++){const _=176+(E-1);this.connection.send([_,122,e])}this.emit(o.CH_LOCAL_CONTROL_SEND,{enabled:t,channel:null})}}_allNotesOff(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(t!==void 0){t=c(Math.round(t),1,16);const s=176+(t-1);this.connection.send([s,123,0]),this.emit(o.CH_ALL_NOTES_OFF_SEND,{channel:t})}else{for(let s=1;s<=16;s++){const e=176+(s-1);this.connection.send([e,123,0])}this.emit(o.CH_ALL_NOTES_OFF_SEND,{channel:null})}}_omniOff(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(t!==void 0){t=c(Math.round(t),1,16);const s=176+(t-1);this.connection.send([s,124,0]),this.emit(o.CH_OMNI_OFF_SEND,{channel:t})}else{for(let s=1;s<=16;s++){const e=176+(s-1);this.connection.send([e,124,0])}this.emit(o.CH_OMNI_OFF_SEND,{channel:null})}}_omniOn(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(t!==void 0){t=c(Math.round(t),1,16);const s=176+(t-1);this.connection.send([s,125,0]),this.emit(o.CH_OMNI_ON_SEND,{channel:t})}else{for(let s=1;s<=16;s++){const e=176+(s-1);this.connection.send([e,125,0])}this.emit(o.CH_OMNI_ON_SEND,{channel:null})}}_monoOn(t=1,s){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(t=Math.max(0,Math.min(16,Math.round(t))),s!==void 0){s=c(Math.round(s),1,16);const e=176+(s-1);this.connection.send([e,126,t]),this.emit(o.CH_MONO_ON_SEND,{channels:t,channel:s})}else{for(let e=1;e<=16;e++){const E=176+(e-1);this.connection.send([E,126,t])}this.emit(o.CH_MONO_ON_SEND,{channels:t,channel:null})}}_polyOn(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(t!==void 0){t=c(Math.round(t),1,16);const s=176+(t-1);this.connection.send([s,127,0]),this.emit(o.CH_POLY_ON_SEND,{channel:t})}else{for(let s=1;s<=16;s++){const e=176+(s-1);this.connection.send([e,127,0])}this.emit(o.CH_POLY_ON_SEND,{channel:null})}}_sendSysEx(t,s=!1){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(!this.options.sysex){console.warn("SysEx not enabled. Initialize with sysex: true");return}this.connection.sendSysEx(t,s),this.emit(o.SYS_EX_SEND,{data:t,includeWrapper:s})}_sendClock(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([248])}_sendStart(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([250])}_sendContinue(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([251])}_sendStop(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([252])}_sendMTC(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}t=c(Math.round(t),0,127),this.connection.send([241,t])}_sendSongPosition(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}t=c(Math.round(t),0,16383);const s=t&127,e=t>>7&127;this.connection.send([242,s,e])}_sendSongSelect(t){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}t=c(Math.round(t),0,127),this.connection.send([243,t])}_sendTuneRequest(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([246])}_sendActiveSensing(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([254])}_sendSystemReset(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([255])}_handleMIDIMessage(t){const[s,e,E]=t.data,_=s&240,i=(s&15)+1;if(s===248){this.emit(o.SYS_CLOCK_RECV,{timestamp:t.midiwire});return}if(s===250){this.emit(o.SYS_START_RECV,{timestamp:t.midiwire});return}if(s===251){this.emit(o.SYS_CONTINUE_RECV,{timestamp:t.midiwire});return}if(s===252){this.emit(o.SYS_STOP_RECV,{timestamp:t.midiwire});return}if(s===254){this.emit(o.SYS_ACT_SENSE_RECV,{timestamp:t.midiwire});return}if(s===255){this.emit(o.SYS_RESET_RECV,{timestamp:t.midiwire});return}if(s===240){this.emit(o.SYS_EX_RECV,{data:Array.from(t.data),timestamp:t.midiwire});return}if(s===241){this.emit(o.SYS_MTC_RECV,{data:e,timestamp:t.midiwire});return}if(s===242){const r=e+(E<<7);this.emit(o.SYS_SONG_POS_RECV,{position:r,timestamp:t.midiwire});return}if(s===243){this.emit(o.SYS_SONG_SEL_RECV,{song:e,timestamp:t.midiwire});return}if(s===246){this.emit(o.SYS_TUNE_REQ_RECV,{timestamp:t.midiwire});return}if(s===247){this.emit(o.MIDI_RAW,{status:s,data:[e,E],channel:i,timestamp:t.midiwire});return}if(_===176){const r=`${i}:${e}`;this.state.controlChange.set(r,E),this.emit(o.CH_CC_RECV,{cc:e,value:E,channel:i});return}if(_===192){this.state.programChange.set(i.toString(),e),this.emit(o.CH_PC_RECV,{program:e,channel:i});return}if(_===224){const r=e+(E<<7);this.state.pitchBend.set(i.toString(),r),this.emit(o.CH_PITCH_BEND_RECV,{value:r,channel:i});return}if(_===208){this.state.monoPressure.set(i.toString(),e),this.emit(o.CH_MONO_PRESS_RECV,{pressure:e,channel:i});return}if(_===160){const r=`${i}:${e}`;this.state.polyPressure.set(r,E),this.emit(o.CH_POLY_PRESS_RECV,{note:e,pressure:E,channel:i});return}if(_===144&&E>0){this.emit(o.CH_NOTE_ON_RECV,{note:e,velocity:E,channel:i});return}if(_===128||_===144&&E===0){this.emit(o.CH_NOTE_OFF_RECV,{note:e,channel:i});return}this.emit(o.MIDI_RAW,{status:s,data:[e,E],channel:i,timestamp:t.midiwire})}_getPatch(t="Unnamed Patch"){const s={name:t,device:this._getCurrentOutput()?.name||null,timestamp:new Date().toISOString(),version:"1.0",channels:{},settings:{}};for(const[e,E]of this.state.controlChange.entries()){const[_,i]=e.split(":").map(Number);s.channels[_]||(s.channels[_]={ccs:{},notes:{}}),s.channels[_].ccs[i]=E}for(const[e,E]of this.state.programChange.entries()){const _=parseInt(e,10);s.channels[_]||(s.channels[_]={ccs:{},notes:{}}),s.channels[_].program=E}for(const[e,E]of this.state.pitchBend.entries()){const _=parseInt(e,10);s.channels[_]||(s.channels[_]={ccs:{},notes:{}}),s.channels[_].pitchBend=E}for(const[e,E]of this.state.monoPressure.entries()){const _=parseInt(e,10);s.channels[_]||(s.channels[_]={ccs:{},notes:{}}),s.channels[_].monoPressure=E}for(const[e,E]of this.state.polyPressure.entries()){const[_,i]=e.split(":").map(Number),r=parseInt(_,10);s.channels[r]||(s.channels[r]={ccs:{},notes:{}}),s.channels[r].polyPressure||(s.channels[r].polyPressure={}),s.channels[r].polyPressure[i]=E}for(const[e,E]of this.bindings.entries()){const{config:_}=E;if(_.cc){const i=`cc${_.cc}`;s.settings[i]={min:_.min,max:_.max,invert:_.invert||!1,is14Bit:_.is14Bit||!1,label:e.getAttribute?.("data-midi-label")||null,elementId:e.id||null}}}return s}async _setPatch(t){if(!t||!t.channels)throw new R("Invalid patch format","patch");const s=t.version||"1.0";s==="1.0"?await this._applyPatchV1(t):(console.warn(`Unknown patch version: ${s}. Attempting to apply as v1.0`),await this._applyPatchV1(t)),this.emit(o.PATCH_LOADED,{patch:t})}async _applyPatchV1(t){for(const[s,e]of Object.entries(t.channels)){const E=parseInt(s,10);if(e.ccs)for(const[_,i]of Object.entries(e.ccs)){const r=parseInt(_,10);this._sendCC(r,i,E)}if(e.program!==void 0&&this._sendPC(e.program,E),e.pitchBend!==void 0&&this._sendPitchBend(e.pitchBend,E),e.monoPressure!==void 0&&this._sendMonoPressure(e.monoPressure,E),e.polyPressure)for(const[_,i]of Object.entries(e.polyPressure)){const r=parseInt(_,10);this._sendPolyPressure(r,i,E)}if(e.notes)for(const[_,i]of Object.entries(e.notes)){const r=parseInt(_,10);i>0?this._sendNoteOn(r,i,E):this._sendNoteOff(r,E)}}if(t.settings)for(const[s,e]of Object.entries(t.settings))for(const[E,_]of this.bindings.entries())_.config.cc?.toString()===s.replace("cc","")&&(E.min!==void 0&&e.min!==void 0&&(E.min=String(e.min)),E.max!==void 0&&e.max!==void 0&&(E.max=String(e.max)));for(const[s,e]of this.bindings.entries()){const{config:E}=e;if(E.cc!==void 0){const _=E.channel||this.options.inputChannel,i=t.channels[_];if(i?.ccs){const r=i.ccs[E.cc];if(r!==void 0){const h=E.min!==void 0?E.min:parseFloat(s.getAttribute?.("min"))||0,S=E.max!==void 0?E.max:parseFloat(s.getAttribute?.("max"))||127,P=E.invert||!1;let a;P?a=S-r/127*(S-h):a=h+r/127*(S-h),E.onInput&&typeof E.onInput=="function"?E.onInput(a):(s.value=a,s.dispatchEvent(new Event("input",{bubbles:!0})))}}}}}_savePatch(t,s=null){const e=s||this._getPatch(t),E=`midiwire_patch_${t}`;try{return localStorage.setItem(E,JSON.stringify(e)),this.emit(o.PATCH_SAVED,{name:t,patch:e}),E}catch(_){throw console.error("Failed to save patch:",_),_}}_loadPatch(t){const s=`midiwire_patch_${t}`;try{const e=localStorage.getItem(s);if(!e)return null;const E=JSON.parse(e);return this.emit(o.PATCH_LOADED,{name:t,patch:E}),E}catch(e){return console.error("Failed to load patch:",e),null}}_deletePatch(t){const s=`midiwire_patch_${t}`;try{return localStorage.removeItem(s),this.emit(o.PATCH_DELETED,{name:t}),!0}catch(e){return console.error("Failed to delete patch:",e),!1}}_listPatches(){const t=[];try{for(let s=0;s<localStorage.length;s++){const e=localStorage.key(s);if(e?.startsWith("midiwire_patch_")){const E=e.replace("midiwire_patch_",""),_=this._loadPatch(E);_&&t.push({name:E,patch:_})}}}catch(s){console.error("Failed to list patches:",s)}return t.sort((s,e)=>s.name.localeCompare(e.name))}}class ${constructor(t={}){this.midi=t.midiController||null,this.onStatusUpdate=t.onStatusUpdate||(()=>{}),this.onConnectionUpdate=t.onConnectionUpdate||(()=>{}),this.channel=t.channel||1,this.currentOutput=null,this.currentInput=null,this.isConnecting=!1}async setupSelectors(t={},s={}){if(!this.midi)throw new Error("MIDI controller not initialized. Pass midiController in constructor options.");const{output:e,input:E,channel:_}=t,i=this._resolveSelector(e),r=this._resolveSelector(E),h=this._resolveSelector(_);if(this._setupDeviceChangeListeners({output:i,input:r},s.onDeviceListChange),i){await this._populateOutputDeviceList(i);const S=s.onConnect?async(a,A)=>s.onConnect({midi:a,device:A,type:"output"}):void 0,P=s.onDisconnect?async a=>s.onDisconnect({midi:a,type:"output"}):void 0;this._connectOutputDeviceSelection(i,S,P)}if(r){await this._populateInputDeviceList(r);const S=s.onConnect?async(a,A)=>s.onConnect({midi:a,device:A,type:"input"}):void 0,P=s.onDisconnect?async a=>s.onDisconnect({midi:a,type:"input"}):void 0;this._connectInputDeviceSelection(r,S,P)}return h&&this._connectChannelSelection(h,"output"),this.midi}updateStatus(t,s=""){this.onStatusUpdate(t,s)}updateConnectionStatus(){this.onConnectionUpdate(this.currentOutput,this.currentInput,this.midi)}_setupDeviceChangeListeners(t={},s){!this.midi||this._listenersInitialized||(this._listenersInitialized=!0,this.midi.on(o.DEV_OUT_CONNECTED,async e=>{this.updateStatus(`Output device connected: ${e?.name||"Unknown"}`,"connected"),t.output&&await this._populateOutputDeviceList(t.output),s&&s()}),this.midi.on(o.DEV_OUT_DISCONNECTED,async e=>{this.updateStatus(`Output device disconnected: ${e?.name||"Unknown"}`,"error"),this.currentOutput&&e?.name===this.currentOutput.name&&(this.currentOutput=null,this.updateConnectionStatus(),t.output&&(t.output.value="")),t.output&&await this._populateOutputDeviceList(t.output),s&&s()}),this.midi.on(o.DEV_IN_CONNECTED,async e=>{this.updateStatus(`Input device connected: ${e?.name||"Unknown"}`,"connected"),t.input&&await this._populateInputDeviceList(t.input),s&&s()}),this.midi.on(o.DEV_IN_DISCONNECTED,async e=>{this.updateStatus(`Input device disconnected: ${e?.name||"Unknown"}`,"error"),t.input&&(t.input.value="",await this._populateInputDeviceList(t.input)),s&&s()}))}_resolveSelector(t){if(typeof t=="string"){const s=document.querySelector(t);return s||console.warn(`MIDIDeviceManager: Selector "${t}" not found`),s}return t||null}_getOutputDevices(){return this.midi?this.midi.device.getOutputs():[]}_getInputDevices(){return this.midi?this.midi.device.getInputs():[]}_connectOutputDeviceSelection(t,s,e){!t||!this.midi||t.addEventListener("change",async E=>{if(this.isConnecting)return;this.isConnecting=!0;const _=E.target.value;if(!_){this.currentOutput&&this.midi&&(await this.midi.device.disconnectOutput(),this.currentOutput=null,this.updateStatus("Output device disconnected",""),this.updateConnectionStatus()),this.isConnecting=!1,e&&await e(this.midi);return}try{if(await this.midi.device.connectOutput(parseInt(_,10)),this.currentOutput=this.midi.device.getCurrentOutput(),this.currentOutput){const r=this.midi.device.getOutputs().findIndex(h=>h.id===this.currentOutput.id);r!==-1&&(t.value=r.toString())}this.updateConnectionStatus(),s&&await s(this.midi,this.currentOutput)}catch(i){this.updateStatus(`Output connection failed: ${i.message}`,"error")}finally{this.isConnecting=!1}})}_connectInputDeviceSelection(t,s,e){!t||!this.midi||t.addEventListener("change",async E=>{const _=E.target.value;if(!_){this.midi&&(await this.midi.device.disconnectInput(),this.updateStatus("Input device disconnected",""),this.updateConnectionStatus()),e&&await e(this.midi);return}if(!this.isConnecting){this.isConnecting=!0;try{await this.midi.device.connectInput(parseInt(_,10));const i=this.midi.device.getCurrentInput();this.updateConnectionStatus(),s&&await s(this.midi,i)}catch(i){this.updateStatus(`Input connection failed: ${i.message}`,"error")}finally{this.isConnecting=!1}}})}_populateDeviceList(t,s,e,E,_){if(s.length>0){if(t.innerHTML='<option value="">Select a device</option>'+s.map((i,r)=>`<option value="${r}">${i.name}</option>`).join(""),e){const i=s.findIndex(r=>r.name===e.name);i!==-1?t.value=i.toString():(t.value="",_&&(this.currentOutput=null,this.updateConnectionStatus()))}else t.value="";t.disabled=!1,_&&!this.currentOutput&&this.updateStatus("Select a device")}else t.innerHTML='<option value="">No devices connected</option>',t.disabled=!0,_&&this.updateStatus("No devices connected","error");E&&E()}async _populateOutputDeviceList(t,s){if(!t||!this.midi)return;const e=this._getOutputDevices();this._populateDeviceList(t,e,this.currentOutput,s,!0)}async _populateInputDeviceList(t,s){if(!t||!this.midi)return;const e=this._getInputDevices(),E=this.midi.device.getCurrentInput();this._populateDeviceList(t,e,E,s,!1)}_connectChannelSelection(t,s){if(!t||!this.midi)return;const e=s==="input"?"inputChannel":"outputChannel";t.addEventListener("change",E=>{this.midi&&(this.midi.options[e]=parseInt(E.target.value,10),this.updateConnectionStatus())})}}class n{static PACKED_SIZE=128;static PACKED_OP_SIZE=17;static NUM_OPERATORS=6;static PACKED_OP_EG_RATE_1=0;static PACKED_OP_EG_RATE_2=1;static PACKED_OP_EG_RATE_3=2;static PACKED_OP_EG_RATE_4=3;static PACKED_OP_EG_LEVEL_1=4;static PACKED_OP_EG_LEVEL_2=5;static PACKED_OP_EG_LEVEL_3=6;static PACKED_OP_EG_LEVEL_4=7;static PACKED_OP_BREAK_POINT=8;static PACKED_OP_L_SCALE_DEPTH=9;static PACKED_OP_R_SCALE_DEPTH=10;static PACKED_OP_CURVES=11;static PACKED_OP_RATE_SCALING=12;static PACKED_OP_MOD_SENS=13;static PACKED_OP_OUTPUT_LEVEL=14;static PACKED_OP_MODE_FREQ=15;static PACKED_OP_DETUNE_FINE=16;static PACKED_PITCH_EG_RATE_1=102;static PACKED_PITCH_EG_RATE_2=103;static PACKED_PITCH_EG_RATE_3=104;static PACKED_PITCH_EG_RATE_4=105;static PACKED_PITCH_EG_LEVEL_1=106;static PACKED_PITCH_EG_LEVEL_2=107;static PACKED_PITCH_EG_LEVEL_3=108;static PACKED_PITCH_EG_LEVEL_4=109;static OFFSET_ALGORITHM=110;static OFFSET_FEEDBACK=111;static OFFSET_LFO_SPEED=112;static OFFSET_LFO_DELAY=113;static OFFSET_LFO_PM_DEPTH=114;static OFFSET_LFO_AM_DEPTH=115;static OFFSET_LFO_SYNC_WAVE=116;static OFFSET_TRANSPOSE=117;static OFFSET_AMP_MOD_SENS=118;static OFFSET_EG_BIAS_SENS=119;static PACKED_NAME_START=118;static NAME_LENGTH=10;static UNPACKED_SIZE=169;static UNPACKED_OP_SIZE=23;static UNPACKED_OP_EG_RATE_1=0;static UNPACKED_OP_EG_RATE_2=1;static UNPACKED_OP_EG_RATE_3=2;static UNPACKED_OP_EG_RATE_4=3;static UNPACKED_OP_EG_LEVEL_1=4;static UNPACKED_OP_EG_LEVEL_2=5;static UNPACKED_OP_EG_LEVEL_3=6;static UNPACKED_OP_EG_LEVEL_4=7;static UNPACKED_OP_BREAK_POINT=8;static UNPACKED_OP_L_SCALE_DEPTH=9;static UNPACKED_OP_R_SCALE_DEPTH=10;static UNPACKED_OP_L_CURVE=11;static UNPACKED_OP_R_CURVE=12;static UNPACKED_OP_RATE_SCALING=13;static UNPACKED_OP_DETUNE=14;static UNPACKED_OP_AMP_MOD_SENS=15;static UNPACKED_OP_OUTPUT_LEVEL=16;static UNPACKED_OP_MODE=17;static UNPACKED_OP_KEY_VEL_SENS=18;static UNPACKED_OP_FREQ_COARSE=19;static UNPACKED_OP_OSC_DETUNE=20;static UNPACKED_OP_FREQ_FINE=21;static UNPACKED_PITCH_EG_RATE_1=138;static UNPACKED_PITCH_EG_RATE_2=139;static UNPACKED_PITCH_EG_RATE_3=140;static UNPACKED_PITCH_EG_RATE_4=141;static UNPACKED_PITCH_EG_LEVEL_1=142;static UNPACKED_PITCH_EG_LEVEL_2=143;static UNPACKED_PITCH_EG_LEVEL_3=144;static UNPACKED_PITCH_EG_LEVEL_4=145;static UNPACKED_ALGORITHM=146;static UNPACKED_FEEDBACK=147;static UNPACKED_OSC_SYNC=148;static UNPACKED_LFO_SPEED=149;static UNPACKED_LFO_DELAY=150;static UNPACKED_LFO_PM_DEPTH=151;static UNPACKED_LFO_AM_DEPTH=152;static UNPACKED_LFO_KEY_SYNC=153;static UNPACKED_LFO_WAVE=154;static UNPACKED_LFO_PM_SENS=155;static UNPACKED_AMP_MOD_SENS=156;static UNPACKED_TRANSPOSE=157;static UNPACKED_EG_BIAS_SENS=158;static UNPACKED_NAME_START=159;static VCED_SIZE=163;static VCED_HEADER_SIZE=6;static VCED_DATA_SIZE=155;static VCED_SYSEX_START=240;static VCED_YAMAHA_ID=67;static VCED_SUB_STATUS=0;static VCED_FORMAT_SINGLE=0;static VCED_BYTE_COUNT_MSB=1;static VCED_BYTE_COUNT_LSB=27;static VCED_SYSEX_END=247;static MASK_7BIT=127;static MASK_2BIT=3;static MASK_3BIT=7;static MASK_4BIT=15;static MASK_5BIT=31;static MASK_1BIT=1;static TRANSPOSE_CENTER=24;static CHAR_YEN=92;static CHAR_ARROW_RIGHT=126;static CHAR_ARROW_LEFT=127;static CHAR_REPLACEMENT_Y=89;static CHAR_REPLACEMENT_GT=62;static CHAR_REPLACEMENT_LT=60;static CHAR_SPACE=32;static CHAR_MIN_PRINTABLE=32;static CHAR_MAX_PRINTABLE=126;static DEFAULT_EG_RATE=99;static DEFAULT_EG_LEVEL_MAX=99;static DEFAULT_EG_LEVEL_MIN=0;static DEFAULT_BREAK_POINT=0;static DEFAULT_OUTPUT_LEVEL=99;static DEFAULT_PITCH_EG_LEVEL=50;static DEFAULT_LFO_SPEED=35;static DEFAULT_LFO_PM_SENS=3;static DEFAULT_DETUNE=7;static DEFAULT_FREQ_COARSE=1;static DEFAULT_ALGORITHM=0;static DEFAULT_FEEDBACK=0;static MIDI_OCTAVE_OFFSET=-2;static MIDI_BREAK_POINT_OFFSET=21;constructor(t,s=0){if(t.length!==n.PACKED_SIZE)throw new d(`Invalid voice data length: expected ${n.PACKED_SIZE} bytes, got ${t.length}`,"length",t.length);this.index=s,this.data=new Uint8Array(t),this.name=this._extractName(),this._unpackedCache=null}_extractName(){const t=this.data.subarray(n.PACKED_NAME_START,n.PACKED_NAME_START+n.NAME_LENGTH);return Array.from(t).map(e=>{let E=e&n.MASK_7BIT;return E===n.CHAR_YEN&&(E=n.CHAR_REPLACEMENT_Y),E===n.CHAR_ARROW_RIGHT&&(E=n.CHAR_REPLACEMENT_GT),E===n.CHAR_ARROW_LEFT&&(E=n.CHAR_REPLACEMENT_LT),(E<n.CHAR_MIN_PRINTABLE||E>n.CHAR_MAX_PRINTABLE)&&(E=n.CHAR_SPACE),String.fromCharCode(E)}).join("").trim()}getParameter(t){if(t<0||t>=n.PACKED_SIZE)throw new d(`Parameter offset out of range: ${t} (must be 0-${n.PACKED_SIZE-1})`,"offset",t);return this.data[t]&n.MASK_7BIT}getUnpackedParameter(t){if(t<0||t>=n.UNPACKED_SIZE)throw new d(`Unpacked parameter offset out of range: ${t} (must be 0-${n.UNPACKED_SIZE-1})`,"offset",t);return this._unpackedCache||(this._unpackedCache=this.unpack()),this._unpackedCache[t]&n.MASK_7BIT}setParameter(t,s){if(t<0||t>=n.PACKED_SIZE)throw new d(`Parameter offset out of range: ${t} (must be 0-${n.PACKED_SIZE-1})`,"offset",t);this.data[t]=s&n.MASK_7BIT,this._unpackedCache=null,t>=n.PACKED_NAME_START&&t<n.PACKED_NAME_START+n.NAME_LENGTH&&(this.name=this._extractName())}unpack(){const t=this.data,s=new Uint8Array(n.UNPACKED_SIZE);return this._unpackOperators(t,s),this._unpackPitchEG(t,s),this._unpackGlobalParams(t,s),this._unpackName(t,s),s}_unpackOperators(t,s){for(let e=0;e<n.NUM_OPERATORS;e++){const E=(n.NUM_OPERATORS-1-e)*n.PACKED_OP_SIZE,_=e*n.UNPACKED_OP_SIZE;this._unpackOperator(t,s,E,_)}}_unpackOperator(t,s,e,E){this._unpackOperatorEG(t,s,e,E),this._unpackOperatorScaling(t,s,e,E),this._unpackOperatorPackedParams(t,s,e,E),this._unpackOperatorFrequency(t,s,e,E)}_unpackOperatorEG(t,s,e,E){s[E+n.UNPACKED_OP_EG_RATE_1]=t[e+n.PACKED_OP_EG_RATE_1]&n.MASK_7BIT,s[E+n.UNPACKED_OP_EG_RATE_2]=t[e+n.PACKED_OP_EG_RATE_2]&n.MASK_7BIT,s[E+n.UNPACKED_OP_EG_RATE_3]=t[e+n.PACKED_OP_EG_RATE_3]&n.MASK_7BIT,s[E+n.UNPACKED_OP_EG_RATE_4]=t[e+n.PACKED_OP_EG_RATE_4]&n.MASK_7BIT,s[E+n.UNPACKED_OP_EG_LEVEL_1]=t[e+n.PACKED_OP_EG_LEVEL_1]&n.MASK_7BIT,s[E+n.UNPACKED_OP_EG_LEVEL_2]=t[e+n.PACKED_OP_EG_LEVEL_2]&n.MASK_7BIT,s[E+n.UNPACKED_OP_EG_LEVEL_3]=t[e+n.PACKED_OP_EG_LEVEL_3]&n.MASK_7BIT,s[E+n.UNPACKED_OP_EG_LEVEL_4]=t[e+n.PACKED_OP_EG_LEVEL_4]&n.MASK_7BIT}_unpackOperatorScaling(t,s,e,E){s[E+n.UNPACKED_OP_BREAK_POINT]=t[e+n.PACKED_OP_BREAK_POINT]&n.MASK_7BIT,s[E+n.UNPACKED_OP_L_SCALE_DEPTH]=t[e+n.PACKED_OP_L_SCALE_DEPTH]&n.MASK_7BIT,s[E+n.UNPACKED_OP_R_SCALE_DEPTH]=t[e+n.PACKED_OP_R_SCALE_DEPTH]&n.MASK_7BIT}_unpackOperatorPackedParams(t,s,e,E){const _=t[e+n.PACKED_OP_CURVES]&n.MASK_7BIT;s[E+n.UNPACKED_OP_L_CURVE]=_&n.MASK_2BIT,s[E+n.UNPACKED_OP_R_CURVE]=_>>2&n.MASK_2BIT;const i=t[e+n.PACKED_OP_RATE_SCALING]&n.MASK_7BIT;s[E+n.UNPACKED_OP_RATE_SCALING]=i&n.MASK_3BIT,s[E+n.UNPACKED_OP_DETUNE]=i>>3&n.MASK_4BIT;const r=t[e+n.PACKED_OP_MOD_SENS]&n.MASK_7BIT;s[E+n.UNPACKED_OP_AMP_MOD_SENS]=r&n.MASK_2BIT,s[E+n.UNPACKED_OP_KEY_VEL_SENS]=r>>2&n.MASK_3BIT,s[E+n.UNPACKED_OP_OUTPUT_LEVEL]=t[e+n.PACKED_OP_OUTPUT_LEVEL]&n.MASK_7BIT}_unpackOperatorFrequency(t,s,e,E){const _=t[e+n.PACKED_OP_MODE_FREQ]&n.MASK_7BIT;s[E+n.UNPACKED_OP_MODE]=_&n.MASK_1BIT,s[E+n.UNPACKED_OP_FREQ_COARSE]=_>>1&n.MASK_5BIT;const i=t[e+n.PACKED_OP_DETUNE_FINE]&n.MASK_7BIT;s[E+n.UNPACKED_OP_OSC_DETUNE]=i&n.MASK_3BIT,s[E+n.UNPACKED_OP_FREQ_FINE]=i>>3&n.MASK_4BIT}_unpackPitchEG(t,s){s[n.UNPACKED_PITCH_EG_RATE_1]=t[n.PACKED_PITCH_EG_RATE_1]&n.MASK_7BIT,s[n.UNPACKED_PITCH_EG_RATE_2]=t[n.PACKED_PITCH_EG_RATE_2]&n.MASK_7BIT,s[n.UNPACKED_PITCH_EG_RATE_3]=t[n.PACKED_PITCH_EG_RATE_3]&n.MASK_7BIT,s[n.UNPACKED_PITCH_EG_RATE_4]=t[n.PACKED_PITCH_EG_RATE_4]&n.MASK_7BIT,s[n.UNPACKED_PITCH_EG_LEVEL_1]=t[n.PACKED_PITCH_EG_LEVEL_1]&n.MASK_7BIT,s[n.UNPACKED_PITCH_EG_LEVEL_2]=t[n.PACKED_PITCH_EG_LEVEL_2]&n.MASK_7BIT,s[n.UNPACKED_PITCH_EG_LEVEL_3]=t[n.PACKED_PITCH_EG_LEVEL_3]&n.MASK_7BIT,s[n.UNPACKED_PITCH_EG_LEVEL_4]=t[n.PACKED_PITCH_EG_LEVEL_4]&n.MASK_7BIT}_unpackGlobalParams(t,s){s[n.UNPACKED_ALGORITHM]=t[n.OFFSET_ALGORITHM]&n.MASK_5BIT;const e=t[n.OFFSET_FEEDBACK]&n.MASK_7BIT;s[n.UNPACKED_FEEDBACK]=e&n.MASK_3BIT,s[n.UNPACKED_OSC_SYNC]=e>>3&n.MASK_1BIT,s[n.UNPACKED_LFO_SPEED]=t[n.OFFSET_LFO_SPEED]&n.MASK_7BIT,s[n.UNPACKED_LFO_DELAY]=t[n.OFFSET_LFO_DELAY]&n.MASK_7BIT,s[n.UNPACKED_LFO_PM_DEPTH]=t[n.OFFSET_LFO_PM_DEPTH]&n.MASK_7BIT,s[n.UNPACKED_LFO_AM_DEPTH]=t[n.OFFSET_LFO_AM_DEPTH]&n.MASK_7BIT;const E=t[n.OFFSET_LFO_SYNC_WAVE]&n.MASK_7BIT;s[n.UNPACKED_LFO_KEY_SYNC]=E&n.MASK_1BIT,s[n.UNPACKED_LFO_WAVE]=E>>1&n.MASK_3BIT,s[n.UNPACKED_LFO_PM_SENS]=E>>4&n.MASK_3BIT,s[n.UNPACKED_AMP_MOD_SENS]=t[n.OFFSET_AMP_MOD_SENS]&n.MASK_7BIT,s[n.UNPACKED_TRANSPOSE]=t[n.OFFSET_TRANSPOSE]&n.MASK_7BIT,s[n.UNPACKED_EG_BIAS_SENS]=t[n.OFFSET_EG_BIAS_SENS]&n.MASK_7BIT}_unpackName(t,s){for(let e=0;e<n.NAME_LENGTH;e++)s[n.UNPACKED_NAME_START+e]=t[n.PACKED_NAME_START+e]&n.MASK_7BIT}static pack(t){if(t.length!==n.UNPACKED_SIZE)throw new d(`Invalid unpacked data length: expected ${n.UNPACKED_SIZE} bytes, got ${t.length}`,"length",t.length);const s=new Uint8Array(n.PACKED_SIZE);return n._packOperators(t,s),n._packPitchEG(t,s),n._packGlobalParams(t,s),n._packName(t,s),s}static _packOperators(t,s){for(let e=0;e<n.NUM_OPERATORS;e++){const E=e*n.UNPACKED_OP_SIZE,_=(n.NUM_OPERATORS-1-e)*n.PACKED_OP_SIZE;n._packOperator(t,s,E,_)}}static _packOperator(t,s,e,E){n._packOperatorEG(t,s,e,E),n._packOperatorScaling(t,s,e,E),n._packOperatorPackedParams(t,s,e,E),n._packOperatorFrequency(t,s,e,E)}static _packOperatorEG(t,s,e,E){s[E+n.PACKED_OP_EG_RATE_1]=t[e+n.UNPACKED_OP_EG_RATE_1],s[E+n.PACKED_OP_EG_RATE_2]=t[e+n.UNPACKED_OP_EG_RATE_2],s[E+n.PACKED_OP_EG_RATE_3]=t[e+n.UNPACKED_OP_EG_RATE_3],s[E+n.PACKED_OP_EG_RATE_4]=t[e+n.UNPACKED_OP_EG_RATE_4],s[E+n.PACKED_OP_EG_LEVEL_1]=t[e+n.UNPACKED_OP_EG_LEVEL_1],s[E+n.PACKED_OP_EG_LEVEL_2]=t[e+n.UNPACKED_OP_EG_LEVEL_2],s[E+n.PACKED_OP_EG_LEVEL_3]=t[e+n.UNPACKED_OP_EG_LEVEL_3],s[E+n.PACKED_OP_EG_LEVEL_4]=t[e+n.UNPACKED_OP_EG_LEVEL_4]}static _packOperatorScaling(t,s,e,E){s[E+n.PACKED_OP_BREAK_POINT]=t[e+n.UNPACKED_OP_BREAK_POINT],s[E+n.PACKED_OP_L_SCALE_DEPTH]=t[e+n.UNPACKED_OP_L_SCALE_DEPTH],s[E+n.PACKED_OP_R_SCALE_DEPTH]=t[e+n.UNPACKED_OP_R_SCALE_DEPTH]}static _packOperatorPackedParams(t,s,e,E){const _=t[e+n.UNPACKED_OP_L_CURVE]&n.MASK_2BIT,i=t[e+n.UNPACKED_OP_R_CURVE]&n.MASK_2BIT;s[E+n.PACKED_OP_CURVES]=_|i<<2;const r=t[e+n.UNPACKED_OP_RATE_SCALING]&n.MASK_3BIT,h=t[e+n.UNPACKED_OP_DETUNE]&n.MASK_4BIT;s[E+n.PACKED_OP_RATE_SCALING]=r|h<<3;const S=t[e+n.UNPACKED_OP_AMP_MOD_SENS]&n.MASK_2BIT,P=t[e+n.UNPACKED_OP_KEY_VEL_SENS]&n.MASK_3BIT;s[E+n.PACKED_OP_MOD_SENS]=S|P<<2,s[E+n.PACKED_OP_OUTPUT_LEVEL]=t[e+n.UNPACKED_OP_OUTPUT_LEVEL]}static _packOperatorFrequency(t,s,e,E){const _=t[e+n.UNPACKED_OP_MODE]&n.MASK_1BIT,i=t[e+n.UNPACKED_OP_FREQ_COARSE]&n.MASK_5BIT;s[E+n.PACKED_OP_MODE_FREQ]=_|i<<1;const r=t[e+n.UNPACKED_OP_OSC_DETUNE]&n.MASK_3BIT,h=t[e+n.UNPACKED_OP_FREQ_FINE]&n.MASK_4BIT;s[E+n.PACKED_OP_DETUNE_FINE]=r|h<<3}static _packPitchEG(t,s){s[n.PACKED_PITCH_EG_RATE_1]=t[n.UNPACKED_PITCH_EG_RATE_1],s[n.PACKED_PITCH_EG_RATE_2]=t[n.UNPACKED_PITCH_EG_RATE_2],s[n.PACKED_PITCH_EG_RATE_3]=t[n.UNPACKED_PITCH_EG_RATE_3],s[n.PACKED_PITCH_EG_RATE_4]=t[n.UNPACKED_PITCH_EG_RATE_4],s[n.PACKED_PITCH_EG_LEVEL_1]=t[n.UNPACKED_PITCH_EG_LEVEL_1],s[n.PACKED_PITCH_EG_LEVEL_2]=t[n.UNPACKED_PITCH_EG_LEVEL_2],s[n.PACKED_PITCH_EG_LEVEL_3]=t[n.UNPACKED_PITCH_EG_LEVEL_3],s[n.PACKED_PITCH_EG_LEVEL_4]=t[n.UNPACKED_PITCH_EG_LEVEL_4]}static _packGlobalParams(t,s){s[n.OFFSET_ALGORITHM]=t[n.UNPACKED_ALGORITHM];const e=t[n.UNPACKED_FEEDBACK]&n.MASK_3BIT,E=t[n.UNPACKED_OSC_SYNC]&n.MASK_1BIT;s[n.OFFSET_FEEDBACK]=e|E<<3,s[n.OFFSET_LFO_SPEED]=t[n.UNPACKED_LFO_SPEED],s[n.OFFSET_LFO_DELAY]=t[n.UNPACKED_LFO_DELAY],s[n.OFFSET_LFO_PM_DEPTH]=t[n.UNPACKED_LFO_PM_DEPTH],s[n.OFFSET_LFO_AM_DEPTH]=t[n.UNPACKED_LFO_AM_DEPTH];const _=t[n.UNPACKED_LFO_KEY_SYNC]&n.MASK_1BIT,i=t[n.UNPACKED_LFO_WAVE]&n.MASK_3BIT,r=t[n.UNPACKED_LFO_PM_SENS]&n.MASK_3BIT;s[n.OFFSET_LFO_SYNC_WAVE]=_|i<<1|r<<4,s[n.OFFSET_AMP_MOD_SENS]=t[n.UNPACKED_AMP_MOD_SENS],s[n.OFFSET_TRANSPOSE]=t[n.UNPACKED_TRANSPOSE],s[n.OFFSET_EG_BIAS_SENS]=t[n.UNPACKED_EG_BIAS_SENS]}static _packName(t,s){for(let e=0;e<n.NAME_LENGTH;e++)s[n.PACKED_NAME_START+e]=t[n.UNPACKED_NAME_START+e]}static createDefault(t=0){const s=new Uint8Array(n.UNPACKED_SIZE);for(let _=0;_<n.NUM_OPERATORS;_++){const i=_*n.UNPACKED_OP_SIZE;s[i+n.UNPACKED_OP_EG_RATE_1]=n.DEFAULT_EG_RATE,s[i+n.UNPACKED_OP_EG_RATE_2]=n.DEFAULT_EG_RATE,s[i+n.UNPACKED_OP_EG_RATE_3]=n.DEFAULT_EG_RATE,s[i+n.UNPACKED_OP_EG_RATE_4]=n.DEFAULT_EG_RATE,s[i+n.UNPACKED_OP_EG_LEVEL_1]=n.DEFAULT_EG_LEVEL_MAX,s[i+n.UNPACKED_OP_EG_LEVEL_2]=n.DEFAULT_EG_LEVEL_MAX,s[i+n.UNPACKED_OP_EG_LEVEL_3]=n.DEFAULT_EG_LEVEL_MAX,s[i+n.UNPACKED_OP_EG_LEVEL_4]=n.DEFAULT_EG_LEVEL_MIN,s[i+n.UNPACKED_OP_BREAK_POINT]=n.DEFAULT_BREAK_POINT,s[i+n.UNPACKED_OP_L_SCALE_DEPTH]=0,s[i+n.UNPACKED_OP_R_SCALE_DEPTH]=0,s[i+n.UNPACKED_OP_L_CURVE]=0,s[i+n.UNPACKED_OP_R_CURVE]=0,s[i+n.UNPACKED_OP_RATE_SCALING]=0,s[i+n.UNPACKED_OP_DETUNE]=n.DEFAULT_DETUNE,s[i+n.UNPACKED_OP_AMP_MOD_SENS]=0,s[i+n.UNPACKED_OP_KEY_VEL_SENS]=0,s[i+n.UNPACKED_OP_OUTPUT_LEVEL]=n.DEFAULT_OUTPUT_LEVEL,s[i+n.UNPACKED_OP_MODE]=0,s[i+n.UNPACKED_OP_FREQ_COARSE]=n.DEFAULT_FREQ_COARSE,s[i+n.UNPACKED_OP_OSC_DETUNE]=0,s[i+n.UNPACKED_OP_FREQ_FINE]=0}s[n.UNPACKED_PITCH_EG_RATE_1]=n.DEFAULT_EG_RATE,s[n.UNPACKED_PITCH_EG_RATE_2]=n.DEFAULT_EG_RATE,s[n.UNPACKED_PITCH_EG_RATE_3]=n.DEFAULT_EG_RATE,s[n.UNPACKED_PITCH_EG_RATE_4]=n.DEFAULT_EG_RATE,s[n.UNPACKED_PITCH_EG_LEVEL_1]=n.DEFAULT_PITCH_EG_LEVEL,s[n.UNPACKED_PITCH_EG_LEVEL_2]=n.DEFAULT_PITCH_EG_LEVEL,s[n.UNPACKED_PITCH_EG_LEVEL_3]=n.DEFAULT_PITCH_EG_LEVEL,s[n.UNPACKED_PITCH_EG_LEVEL_4]=n.DEFAULT_PITCH_EG_LEVEL,s[n.UNPACKED_ALGORITHM]=n.DEFAULT_ALGORITHM,s[n.UNPACKED_FEEDBACK]=n.DEFAULT_FEEDBACK,s[n.UNPACKED_OSC_SYNC]=0,s[n.UNPACKED_LFO_SPEED]=n.DEFAULT_LFO_SPEED,s[n.UNPACKED_LFO_DELAY]=0,s[n.UNPACKED_LFO_PM_DEPTH]=0,s[n.UNPACKED_LFO_AM_DEPTH]=0,s[n.UNPACKED_LFO_KEY_SYNC]=0,s[n.UNPACKED_LFO_WAVE]=0,s[n.UNPACKED_LFO_PM_SENS]=n.DEFAULT_LFO_PM_SENS,s[n.UNPACKED_AMP_MOD_SENS]=0,s[n.UNPACKED_TRANSPOSE]=n.TRANSPOSE_CENTER,s[n.UNPACKED_EG_BIAS_SENS]=0;const e="Init Voice";for(let _=0;_<n.NAME_LENGTH;_++)s[n.UNPACKED_NAME_START+_]=_<e.length?e.charCodeAt(_):n.CHAR_SPACE;const E=n.pack(s);return new n(E,t)}static fromUnpacked(t,s=0){const e=n.pack(t);return new n(e,s)}static async fromFile(t){return new Promise((s,e)=>{const E=new FileReader;E.onload=_=>{try{const i=new Uint8Array(_.target.result);if(i[0]!==n.VCED_SYSEX_START||i[1]!==n.VCED_YAMAHA_ID||i[2]!==n.VCED_SUB_STATUS||i[3]!==n.VCED_FORMAT_SINGLE||i[4]!==n.VCED_BYTE_COUNT_MSB||i[5]!==n.VCED_BYTE_COUNT_LSB)throw new M("Invalid VCED header","header",0);const r=i.subarray(n.VCED_HEADER_SIZE,n.VCED_HEADER_SIZE+n.VCED_DATA_SIZE),h=i[n.VCED_HEADER_SIZE+n.VCED_DATA_SIZE],S=u._calculateChecksum(r,n.VCED_DATA_SIZE);h!==S&&console.warn(`DX7 VCED checksum mismatch (expected ${S.toString(16)}, got ${h.toString(16)}). This is common with vintage SysEx files.`);const P=new Uint8Array(n.UNPACKED_SIZE);let a=0;for(let N=0;N<n.NUM_OPERATORS;N++){const O=(n.NUM_OPERATORS-1-N)*n.UNPACKED_OP_SIZE;P[O+n.UNPACKED_OP_EG_RATE_1]=r[a++],P[O+n.UNPACKED_OP_EG_RATE_2]=r[a++],P[O+n.UNPACKED_OP_EG_RATE_3]=r[a++],P[O+n.UNPACKED_OP_EG_RATE_4]=r[a++],P[O+n.UNPACKED_OP_EG_LEVEL_1]=r[a++],P[O+n.UNPACKED_OP_EG_LEVEL_2]=r[a++],P[O+n.UNPACKED_OP_EG_LEVEL_3]=r[a++],P[O+n.UNPACKED_OP_EG_LEVEL_4]=r[a++],P[O+n.UNPACKED_OP_BREAK_POINT]=r[a++],P[O+n.UNPACKED_OP_L_SCALE_DEPTH]=r[a++],P[O+n.UNPACKED_OP_R_SCALE_DEPTH]=r[a++],P[O+n.UNPACKED_OP_L_CURVE]=r[a++],P[O+n.UNPACKED_OP_R_CURVE]=r[a++],P[O+n.UNPACKED_OP_RATE_SCALING]=r[a++],P[O+n.UNPACKED_OP_DETUNE]=r[a++];const T=r[a++];P[O+n.UNPACKED_OP_AMP_MOD_SENS]=T&n.MASK_2BIT,P[O+n.UNPACKED_OP_KEY_VEL_SENS]=T>>2&n.MASK_3BIT,P[O+n.UNPACKED_OP_OUTPUT_LEVEL]=r[a++],P[O+n.UNPACKED_OP_MODE]=r[a++],P[O+n.UNPACKED_OP_FREQ_COARSE]=r[a++],P[O+n.UNPACKED_OP_FREQ_FINE]=r[a++],P[O+n.UNPACKED_OP_OSC_DETUNE]=r[a++]}P[n.UNPACKED_PITCH_EG_RATE_1]=r[a++],P[n.UNPACKED_PITCH_EG_RATE_2]=r[a++],P[n.UNPACKED_PITCH_EG_RATE_3]=r[a++],P[n.UNPACKED_PITCH_EG_RATE_4]=r[a++],P[n.UNPACKED_PITCH_EG_LEVEL_1]=r[a++],P[n.UNPACKED_PITCH_EG_LEVEL_2]=r[a++],P[n.UNPACKED_PITCH_EG_LEVEL_3]=r[a++],P[n.UNPACKED_PITCH_EG_LEVEL_4]=r[a++],P[n.UNPACKED_ALGORITHM]=r[a++],P[n.UNPACKED_FEEDBACK]=r[a++],P[n.UNPACKED_OSC_SYNC]=r[a++],P[n.UNPACKED_LFO_SPEED]=r[a++],P[n.UNPACKED_LFO_DELAY]=r[a++],P[n.UNPACKED_LFO_PM_DEPTH]=r[a++],P[n.UNPACKED_LFO_AM_DEPTH]=r[a++],P[n.UNPACKED_LFO_KEY_SYNC]=r[a++],P[n.UNPACKED_LFO_WAVE]=r[a++],P[n.UNPACKED_LFO_PM_SENS]=r[a++],P[n.UNPACKED_TRANSPOSE]=r[a++];for(let N=0;N<n.NAME_LENGTH;N++)P[n.UNPACKED_NAME_START+N]=r[a++];const A=n.pack(P);s(new n(A,0))}catch(i){e(i)}},E.onerror=()=>e(new Error("Failed to read file")),E.readAsArrayBuffer(t)})}static fromSysEx(t,s=0){const e=t instanceof Uint8Array?t:new Uint8Array(t);let E;if(e[0]===n.VCED_SYSEX_START){if(e[0]!==n.VCED_SYSEX_START||e[1]!==n.VCED_YAMAHA_ID||e[2]!==n.VCED_SUB_STATUS||e[3]!==n.VCED_FORMAT_SINGLE||e[4]!==n.VCED_BYTE_COUNT_MSB||e[5]!==n.VCED_BYTE_COUNT_LSB)throw new M("Invalid VCED header","header",0);E=e.subarray(n.VCED_HEADER_SIZE,n.VCED_HEADER_SIZE+n.VCED_DATA_SIZE)}else if(e.length===n.PACKED_SIZE)E=e;else throw new d(`Invalid data length: expected ${n.PACKED_SIZE} or ${n.VCED_SIZE} bytes, got ${e.length}`,"length",e.length);if(E.length!==n.VCED_DATA_SIZE&&E.length!==n.PACKED_SIZE)throw new d(`Invalid voice data length: expected ${n.VCED_DATA_SIZE} or ${n.PACKED_SIZE} bytes, got ${E.length}`,"length",E.length);if(E.length===n.VCED_DATA_SIZE){const _=new Uint8Array(n.UNPACKED_SIZE);let i=0;for(let h=0;h<n.NUM_OPERATORS;h++){const S=(n.NUM_OPERATORS-1-h)*n.UNPACKED_OP_SIZE;_[S+n.UNPACKED_OP_EG_RATE_1]=E[i++],_[S+n.UNPACKED_OP_EG_RATE_2]=E[i++],_[S+n.UNPACKED_OP_EG_RATE_3]=E[i++],_[S+n.UNPACKED_OP_EG_RATE_4]=E[i++],_[S+n.UNPACKED_OP_EG_LEVEL_1]=E[i++],_[S+n.UNPACKED_OP_EG_LEVEL_2]=E[i++],_[S+n.UNPACKED_OP_EG_LEVEL_3]=E[i++],_[S+n.UNPACKED_OP_EG_LEVEL_4]=E[i++],_[S+n.UNPACKED_OP_BREAK_POINT]=E[i++],_[S+n.UNPACKED_OP_L_SCALE_DEPTH]=E[i++],_[S+n.UNPACKED_OP_R_SCALE_DEPTH]=E[i++],_[S+n.UNPACKED_OP_L_CURVE]=E[i++],_[S+n.UNPACKED_OP_R_CURVE]=E[i++],_[S+n.UNPACKED_OP_RATE_SCALING]=E[i++],_[S+n.UNPACKED_OP_DETUNE]=E[i++];const P=E[i++];_[S+n.UNPACKED_OP_AMP_MOD_SENS]=P&n.MASK_2BIT,_[S+n.UNPACKED_OP_KEY_VEL_SENS]=P>>2&n.MASK_3BIT,_[S+n.UNPACKED_OP_OUTPUT_LEVEL]=E[i++],_[S+n.UNPACKED_OP_MODE]=E[i++],_[S+n.UNPACKED_OP_FREQ_COARSE]=E[i++],_[S+n.UNPACKED_OP_FREQ_FINE]=E[i++],_[S+n.UNPACKED_OP_OSC_DETUNE]=E[i++]}_[n.UNPACKED_PITCH_EG_RATE_1]=E[i++],_[n.UNPACKED_PITCH_EG_RATE_2]=E[i++],_[n.UNPACKED_PITCH_EG_RATE_3]=E[i++],_[n.UNPACKED_PITCH_EG_RATE_4]=E[i++],_[n.UNPACKED_PITCH_EG_LEVEL_1]=E[i++],_[n.UNPACKED_PITCH_EG_LEVEL_2]=E[i++],_[n.UNPACKED_PITCH_EG_LEVEL_3]=E[i++],_[n.UNPACKED_PITCH_EG_LEVEL_4]=E[i++],_[n.UNPACKED_ALGORITHM]=E[i++],_[n.UNPACKED_FEEDBACK]=E[i++],_[n.UNPACKED_OSC_SYNC]=E[i++],_[n.UNPACKED_LFO_SPEED]=E[i++],_[n.UNPACKED_LFO_DELAY]=E[i++],_[n.UNPACKED_LFO_PM_DEPTH]=E[i++],_[n.UNPACKED_LFO_AM_DEPTH]=E[i++],_[n.UNPACKED_LFO_KEY_SYNC]=E[i++],_[n.UNPACKED_LFO_WAVE]=E[i++],_[n.UNPACKED_LFO_PM_SENS]=E[i++],_[n.UNPACKED_TRANSPOSE]=E[i++];for(let h=0;h<n.NAME_LENGTH;h++)_[n.UNPACKED_NAME_START+h]=E[i++];const r=n.pack(_);return new n(r,s)}return new n(E,s)}static fromJSON(t,s=0){if(!t||typeof t!="object")throw new d("Invalid JSON: expected object","json",t);const e=new Uint8Array(n.UNPACKED_SIZE),E=(a,A,N,O=0,T=127)=>{if(A==null)throw new d(`Missing required parameter: ${N}`,N,A);const I=Number(A);if(Number.isNaN(I))throw new d(`Invalid parameter value for ${N}: ${A}`,N,A);if(I<O||I>T)throw new d(`Parameter ${N} out of range: ${I} (must be ${O}-${T})`,N,I);e[a]=Math.floor(I)},_=a=>{const A={"-LN":0,"-EX":1,"+EX":2,"+LN":3};return A[a]!==void 0?A[a]:0},i=a=>{const A={TRIANGLE:0,"SAW DOWN":1,"SAW UP":2,SQUARE:3,SINE:4,"SAMPLE & HOLD":5};return A[a]!==void 0?A[a]:0},r=a=>{if(!a||typeof a!="string")return 60;const A=a.trim().match(/^([A-G]#?)(-?\d+)$/);if(!A)return 60;const[,N,O]=A,T=parseInt(O,10),f={C:0,"C#":1,D:2,"D#":3,E:4,F:5,"F#":6,G:7,"G#":8,A:9,"A#":10,B:11}[N.toUpperCase()];return f===void 0?60:(T-n.MIDI_OCTAVE_OFFSET)*12+f};if(!Array.isArray(t.operators))throw new d("Invalid operators array: expected array","operators",t.operators);for(let a=0;a<t.operators.length;a++){const A=t.operators[a];if(!A||typeof A!="object")throw new d(`Invalid operator data at index ${a}`,`operators[${a}]`,A);if(!A.eg||!Array.isArray(A.eg.rates)||A.eg.rates.length!==4)throw new d(`Invalid EG rates for operator ${a}`,`operators[${a}].eg.rates`,A.eg?.rates);if(!A.eg||!Array.isArray(A.eg.levels)||A.eg.levels.length!==4)throw new d(`Invalid EG levels for operator ${a}`,`operators[${a}].eg.levels`,A.eg?.levels)}if(t.operators.length!==n.NUM_OPERATORS)throw new d(`Invalid operators array: expected ${n.NUM_OPERATORS} operators`,"operators",t.operators);for(let a=0;a<n.NUM_OPERATORS;a++){const A=t.operators[a],N=a*n.UNPACKED_OP_SIZE;if(!A.eg||!Array.isArray(A.eg.rates)||A.eg.rates.length!==4)throw new d(`Invalid EG rates for operator ${a}`,`operators[${a}].eg.rates`,A.eg?.rates);if(E(N+n.UNPACKED_OP_EG_RATE_1,A.eg.rates[0],`operators[${a}].eg.rates[0]`,0,99),E(N+n.UNPACKED_OP_EG_RATE_2,A.eg.rates[1],`operators[${a}].eg.rates[1]`,0,99),E(N+n.UNPACKED_OP_EG_RATE_3,A.eg.rates[2],`operators[${a}].eg.rates[2]`,0,99),E(N+n.UNPACKED_OP_EG_RATE_4,A.eg.rates[3],`operators[${a}].eg.rates[3]`,0,99),!A.eg||!Array.isArray(A.eg.levels)||A.eg.levels.length!==4)throw new d(`Invalid EG levels for operator ${a}`,`operators[${a}].eg.levels`,A.eg?.levels);E(N+n.UNPACKED_OP_EG_LEVEL_1,A.eg.levels[0],`operators[${a}].eg.levels[0]`,0,99),E(N+n.UNPACKED_OP_EG_LEVEL_2,A.eg.levels[1],`operators[${a}].eg.levels[1]`,0,99),E(N+n.UNPACKED_OP_EG_LEVEL_3,A.eg.levels[2],`operators[${a}].eg.levels[2]`,0,99),E(N+n.UNPACKED_OP_EG_LEVEL_4,A.eg.levels[3],`operators[${a}].eg.levels[3]`,0,99);const O=r(A.key?.breakPoint)-n.MIDI_BREAK_POINT_OFFSET;E(N+n.UNPACKED_OP_BREAK_POINT,O,`operators[${a}].key.breakPoint`,0,127),E(N+n.UNPACKED_OP_L_SCALE_DEPTH,A.scale?.left?.depth||0,`operators[${a}].scale.left.depth`,0,99),E(N+n.UNPACKED_OP_R_SCALE_DEPTH,A.scale?.right?.depth||0,`operators[${a}].scale.right.depth`,0,99),e[N+n.UNPACKED_OP_L_CURVE]=_(A.scale?.left?.curve||"-LN"),e[N+n.UNPACKED_OP_R_CURVE]=_(A.scale?.right?.curve||"-LN"),E(N+n.UNPACKED_OP_RATE_SCALING,A.key?.scaling||0,`operators[${a}].key.scaling`,0,7);const T=Number(A.osc?.detune)||0;E(N+n.UNPACKED_OP_DETUNE,T+7,`operators[${a}].osc.detune`,0,14),E(N+n.UNPACKED_OP_AMP_MOD_SENS,A.output?.ampModSens||0,`operators[${a}].output.ampModSens`,0,3),E(N+n.UNPACKED_OP_OUTPUT_LEVEL,A.output?.level||0,`operators[${a}].output.level`,0,99);const I=A.osc?.freq?.mode?.toUpperCase()==="FIXED"?1:0,f=Number(A.osc?.freq?.coarse)||0,K=Number(A.osc?.freq?.fine)||0;e[N+n.UNPACKED_OP_MODE]=I,E(N+n.UNPACKED_OP_FREQ_COARSE,f,`operators[${a}].osc.freq.coarse`,0,31),E(N+n.UNPACKED_OP_FREQ_FINE,K,`operators[${a}].osc.freq.fine`,0,15),E(N+n.UNPACKED_OP_KEY_VEL_SENS,A.key?.velocity||0,`operators[${a}].key.velocity`,0,7)}if(!t.pitchEG||!Array.isArray(t.pitchEG.rates)||t.pitchEG.rates.length!==4)throw new d("Invalid pitch EG rates","pitchEG.rates",t.pitchEG?.rates);if(!t.pitchEG||!Array.isArray(t.pitchEG.levels)||t.pitchEG.levels.length!==4)throw new d("Invalid pitch EG levels","pitchEG.levels",t.pitchEG?.levels);if(E(n.UNPACKED_PITCH_EG_RATE_1,t.pitchEG.rates[0],"pitchEG.rates[0]",0,99),E(n.UNPACKED_PITCH_EG_RATE_2,t.pitchEG.rates[1],"pitchEG.rates[1]",0,99),E(n.UNPACKED_PITCH_EG_RATE_3,t.pitchEG.rates[2],"pitchEG.rates[2]",0,99),E(n.UNPACKED_PITCH_EG_RATE_4,t.pitchEG.rates[3],"pitchEG.rates[3]",0,99),E(n.UNPACKED_PITCH_EG_LEVEL_1,t.pitchEG.levels[0],"pitchEG.levels[0]",0,99),E(n.UNPACKED_PITCH_EG_LEVEL_2,t.pitchEG.levels[1],"pitchEG.levels[1]",0,99),E(n.UNPACKED_PITCH_EG_LEVEL_3,t.pitchEG.levels[2],"pitchEG.levels[2]",0,99),E(n.UNPACKED_PITCH_EG_LEVEL_4,t.pitchEG.levels[3],"pitchEG.levels[3]",0,99),!t.lfo||typeof t.lfo!="object")throw new d("Invalid LFO data","lfo",t.lfo);if(E(n.UNPACKED_LFO_SPEED,t.lfo.speed,"lfo.speed",0,99),E(n.UNPACKED_LFO_DELAY,t.lfo.delay,"lfo.delay",0,99),E(n.UNPACKED_LFO_PM_DEPTH,t.lfo.pmDepth,"lfo.pmDepth",0,99),E(n.UNPACKED_LFO_AM_DEPTH,t.lfo.amDepth,"lfo.amDepth",0,99),e[n.UNPACKED_LFO_KEY_SYNC]=t.lfo.keySync?1:0,e[n.UNPACKED_LFO_WAVE]=i(t.lfo.wave),!t.global||typeof t.global!="object")throw new d("Invalid global data","global",t.global);const h=Number(t.global.algorithm)||1;E(n.UNPACKED_ALGORITHM,h-1,"global.algorithm",0,31),E(n.UNPACKED_FEEDBACK,t.global.feedback,"global.feedback",0,7),e[n.UNPACKED_OSC_SYNC]=t.global.oscKeySync?1:0,E(n.UNPACKED_LFO_PM_SENS,t.global.pitchModSens,"global.pitchModSens",0,7);const S=Number(t.global.transpose)||0;E(n.UNPACKED_TRANSPOSE,S+n.TRANSPOSE_CENTER,"global.transpose",0,127),E(n.UNPACKED_AMP_MOD_SENS,t.global.ampModSens||0,"global.ampModSens",0,3),E(n.UNPACKED_EG_BIAS_SENS,t.global.egBiasSens||0,"global.egBiasSens",0,7);const P=t.name||"";for(let a=0;a<n.NAME_LENGTH;a++)e[n.UNPACKED_NAME_START+a]=a<P.length?P.charCodeAt(a):n.CHAR_SPACE;return n.fromUnpacked(e,s)}toSysEx(){const t=this.unpack(),s=new Uint8Array(n.VCED_SIZE);let e=0;s[e++]=n.VCED_SYSEX_START,s[e++]=n.VCED_YAMAHA_ID,s[e++]=n.VCED_SUB_STATUS,s[e++]=n.VCED_FORMAT_SINGLE,s[e++]=n.VCED_BYTE_COUNT_MSB,s[e++]=n.VCED_BYTE_COUNT_LSB;for(let _=n.NUM_OPERATORS-1;_>=0;_--){const i=_*n.UNPACKED_OP_SIZE;s[e++]=t[i+n.UNPACKED_OP_EG_RATE_1],s[e++]=t[i+n.UNPACKED_OP_EG_RATE_2],s[e++]=t[i+n.UNPACKED_OP_EG_RATE_3],s[e++]=t[i+n.UNPACKED_OP_EG_RATE_4],s[e++]=t[i+n.UNPACKED_OP_EG_LEVEL_1],s[e++]=t[i+n.UNPACKED_OP_EG_LEVEL_2],s[e++]=t[i+n.UNPACKED_OP_EG_LEVEL_3],s[e++]=t[i+n.UNPACKED_OP_EG_LEVEL_4],s[e++]=t[i+n.UNPACKED_OP_BREAK_POINT],s[e++]=t[i+n.UNPACKED_OP_L_SCALE_DEPTH],s[e++]=t[i+n.UNPACKED_OP_R_SCALE_DEPTH],s[e++]=t[i+n.UNPACKED_OP_L_CURVE],s[e++]=t[i+n.UNPACKED_OP_R_CURVE],s[e++]=t[i+n.UNPACKED_OP_RATE_SCALING],s[e++]=t[i+n.UNPACKED_OP_DETUNE];const r=t[i+n.UNPACKED_OP_AMP_MOD_SENS]&n.MASK_2BIT,h=t[i+n.UNPACKED_OP_KEY_VEL_SENS]&n.MASK_3BIT;s[e++]=r|h<<2,s[e++]=t[i+n.UNPACKED_OP_OUTPUT_LEVEL],s[e++]=t[i+n.UNPACKED_OP_MODE],s[e++]=t[i+n.UNPACKED_OP_FREQ_COARSE],s[e++]=t[i+n.UNPACKED_OP_OSC_DETUNE],s[e++]=t[i+n.UNPACKED_OP_FREQ_FINE]}s[e++]=t[n.UNPACKED_PITCH_EG_RATE_1],s[e++]=t[n.UNPACKED_PITCH_EG_RATE_2],s[e++]=t[n.UNPACKED_PITCH_EG_RATE_3],s[e++]=t[n.UNPACKED_PITCH_EG_RATE_4],s[e++]=t[n.UNPACKED_PITCH_EG_LEVEL_1],s[e++]=t[n.UNPACKED_PITCH_EG_LEVEL_2],s[e++]=t[n.UNPACKED_PITCH_EG_LEVEL_3],s[e++]=t[n.UNPACKED_PITCH_EG_LEVEL_4],s[e++]=t[n.UNPACKED_ALGORITHM],s[e++]=t[n.UNPACKED_FEEDBACK],s[e++]=t[n.UNPACKED_OSC_SYNC],s[e++]=t[n.UNPACKED_LFO_SPEED],s[e++]=t[n.UNPACKED_LFO_DELAY],s[e++]=t[n.UNPACKED_LFO_PM_DEPTH],s[e++]=t[n.UNPACKED_LFO_AM_DEPTH],s[e++]=t[n.UNPACKED_LFO_KEY_SYNC],s[e++]=t[n.UNPACKED_LFO_WAVE],s[e++]=t[n.UNPACKED_LFO_PM_SENS],s[e++]=t[n.UNPACKED_TRANSPOSE];for(let _=0;_<n.NAME_LENGTH;_++)s[e++]=t[n.UNPACKED_NAME_START+_];const E=s.subarray(n.VCED_HEADER_SIZE,n.VCED_HEADER_SIZE+n.VCED_DATA_SIZE);return s[e++]=u._calculateChecksum(E,n.VCED_DATA_SIZE),s[e++]=n.VCED_SYSEX_END,s}toJSON(){const t=this.unpack(),s=[],e=i=>["-LN","-EX","+EX","+LN"][i]||"UNKNOWN",E=i=>["TRIANGLE","SAW DOWN","SAW UP","SQUARE","SINE","SAMPLE & HOLD"][i]||"UNKNOWN",_=i=>{const r=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],h=Math.floor(i/12)+n.MIDI_OCTAVE_OFFSET;return`${r[i%12]}${h}`};for(let i=0;i<n.NUM_OPERATORS;i++){const r=i*n.UNPACKED_OP_SIZE,h=t[r+n.UNPACKED_OP_MODE]===0?"RATIO":"FIXED";s.push({id:i+1,osc:{detune:t[r+n.UNPACKED_OP_OSC_DETUNE],freq:{coarse:t[r+n.UNPACKED_OP_FREQ_COARSE],fine:t[r+n.UNPACKED_OP_FREQ_FINE],mode:h}},eg:{rates:[t[r+n.UNPACKED_OP_EG_RATE_1],t[r+n.UNPACKED_OP_EG_RATE_2],t[r+n.UNPACKED_OP_EG_RATE_3],t[r+n.UNPACKED_OP_EG_RATE_4]],levels:[t[r+n.UNPACKED_OP_EG_LEVEL_1],t[r+n.UNPACKED_OP_EG_LEVEL_2],t[r+n.UNPACKED_OP_EG_LEVEL_3],t[r+n.UNPACKED_OP_EG_LEVEL_4]]},key:{velocity:t[r+n.UNPACKED_OP_KEY_VEL_SENS],scaling:t[r+n.UNPACKED_OP_RATE_SCALING],breakPoint:_(t[r+n.UNPACKED_OP_BREAK_POINT]+n.MIDI_BREAK_POINT_OFFSET)},output:{level:t[r+n.UNPACKED_OP_OUTPUT_LEVEL],ampModSens:t[r+n.UNPACKED_OP_AMP_MOD_SENS]},scale:{left:{depth:t[r+n.UNPACKED_OP_L_SCALE_DEPTH],curve:e(t[r+n.UNPACKED_OP_L_CURVE])},right:{depth:t[r+n.UNPACKED_OP_R_SCALE_DEPTH],curve:e(t[r+n.UNPACKED_OP_R_CURVE])}}})}return{name:this.name||"(Empty)",operators:s,pitchEG:{rates:[t[n.UNPACKED_PITCH_EG_RATE_1],t[n.UNPACKED_PITCH_EG_RATE_2],t[n.UNPACKED_PITCH_EG_RATE_3],t[n.UNPACKED_PITCH_EG_RATE_4]],levels:[t[n.UNPACKED_PITCH_EG_LEVEL_1],t[n.UNPACKED_PITCH_EG_LEVEL_2],t[n.UNPACKED_PITCH_EG_LEVEL_3],t[n.UNPACKED_PITCH_EG_LEVEL_4]]},lfo:{speed:t[n.UNPACKED_LFO_SPEED],delay:t[n.UNPACKED_LFO_DELAY],pmDepth:t[n.UNPACKED_LFO_PM_DEPTH],amDepth:t[n.UNPACKED_LFO_AM_DEPTH],keySync:t[n.UNPACKED_LFO_KEY_SYNC]===1,wave:E(t[n.UNPACKED_LFO_WAVE])},global:{algorithm:t[n.UNPACKED_ALGORITHM]+1,feedback:t[n.UNPACKED_FEEDBACK],oscKeySync:t[n.UNPACKED_OSC_SYNC]===1,pitchModSens:t[n.UNPACKED_LFO_PM_SENS],transpose:t[n.UNPACKED_TRANSPOSE]-n.TRANSPOSE_CENTER}}}}class u{static SYSEX_START=240;static SYSEX_END=247;static SYSEX_YAMAHA_ID=67;static SYSEX_SUB_STATUS=0;static SYSEX_FORMAT_32_VOICES=9;static SYSEX_BYTE_COUNT_MSB=32;static SYSEX_BYTE_COUNT_LSB=0;static SYSEX_HEADER=[u.SYSEX_START,u.SYSEX_YAMAHA_ID,u.SYSEX_SUB_STATUS,u.SYSEX_FORMAT_32_VOICES,u.SYSEX_BYTE_COUNT_MSB,u.SYSEX_BYTE_COUNT_LSB];static SYSEX_HEADER_SIZE=6;static VOICE_DATA_SIZE=4096;static SYSEX_SIZE=4104;static VOICE_SIZE=128;static NUM_VOICES=32;static CHECKSUM_MODULO=128;static MASK_7BIT=127;constructor(t,s=""){if(this.voices=new Array(u.NUM_VOICES),this.name=s,t)this._load(t);else for(let e=0;e<u.NUM_VOICES;e++)this.voices[e]=n.createDefault(e)}static _calculateChecksum(t,s){let e=0;for(let E=0;E<s;E++)e+=t[E];return u.CHECKSUM_MODULO-e%u.CHECKSUM_MODULO&u.MASK_7BIT}_load(t){const s=t instanceof Uint8Array?t:new Uint8Array(t);let e,E=0;if(s[0]===u.SYSEX_START){const i=s.subarray(0,u.SYSEX_HEADER_SIZE),r=u.SYSEX_HEADER;for(let h=0;h<u.SYSEX_HEADER_SIZE;h++)if(i[h]!==r[h])throw new M(`Invalid SysEx header at position ${h}: expected ${r[h].toString(16)}, got ${i[h].toString(16)}`,"header",h);e=s.subarray(u.SYSEX_HEADER_SIZE,u.SYSEX_HEADER_SIZE+u.VOICE_DATA_SIZE),E=u.SYSEX_HEADER_SIZE}else if(s.length===u.VOICE_DATA_SIZE)e=s;else throw new d(`Invalid data length: expected ${u.VOICE_DATA_SIZE} or ${u.SYSEX_SIZE} bytes, got ${s.length}`,"length",s.length);if(e.length!==u.VOICE_DATA_SIZE)throw new d(`Invalid voice data length: expected ${u.VOICE_DATA_SIZE} bytes, got ${e.length}`,"length",e.length);const _=u.SYSEX_HEADER_SIZE+u.VOICE_DATA_SIZE;if(E>0&&s.length>=_+1){const i=s[_],r=u._calculateChecksum(e,u.VOICE_DATA_SIZE);i!==r&&console.warn(`DX7 checksum mismatch (expected ${r.toString(16)}, got ${i.toString(16)}). This is common with vintage SysEx files and the data is likely still valid.`)}this.voices=new Array(u.NUM_VOICES);for(let i=0;i<u.NUM_VOICES;i++){const r=i*u.VOICE_SIZE,h=e.subarray(r,r+u.VOICE_SIZE);this.voices[i]=new n(h,i)}}replaceVoice(t,s){if(t<0||t>=u.NUM_VOICES)throw new d(`Invalid voice index: ${t}`,"index",t);const e=new Uint8Array(s.data);this.voices[t]=new n(e,t)}addVoice(t){for(let s=0;s<this.voices.length;s++){const e=this.voices[s];if(e.name===""||e.name==="Init Voice")return this.replaceVoice(s,t),s}return-1}getVoices(){return this.voices}getVoice(t){return t<0||t>=this.voices.length?null:this.voices[t]}getVoiceNames(){return this.voices.map(t=>t.name)}findVoiceByName(t){const s=t.toLowerCase();return this.voices.find(e=>e.name.toLowerCase().includes(s))||null}static async fromFile(t){return new Promise((s,e)=>{const E=new FileReader;E.onload=async _=>{try{const i=t.name||"",r=new Uint8Array(_.target.result);if(r[0]===u.SYSEX_START&&r[3]===n.VCED_FORMAT_SINGLE)e(new M("This is a single voice file. Use DX7Voice.fromFile() instead.","format",3));else{const h=i.replace(/\.[^/.]+$/,""),S=new u(_.target.result,h);s(S)}}catch(i){e(i)}},E.onerror=()=>e(new Error("Failed to read file")),E.readAsArrayBuffer(t)})}static fromSysEx(t,s=""){return new u(t,s)}static fromJSON(t){if(!t||typeof t!="object")throw new d("Invalid JSON: expected object","json",t);const s=new u;if(s.name=t.name||"",!Array.isArray(t.voices))throw new d("Invalid voices array","voices",t.voices);t.voices.length!==u.NUM_VOICES&&console.warn(`Bank JSON has ${t.voices.length} voices, expected ${u.NUM_VOICES}. Missing voices will be filled with defaults.`);const e=Math.min(t.voices.length,u.NUM_VOICES);for(let E=0;E<e;E++){const _=t.voices[E];if(!_||typeof _!="object"){console.warn(`Invalid voice data at index ${E}, using default voice`);continue}try{const{index:i,...r}=_,h=n.fromJSON(r,E);s.replaceVoice(E,h)}catch(i){console.warn(`Failed to load voice at index ${E}: ${i.message}, using default voice`)}}return s}toSysEx(){const t=new Uint8Array(u.SYSEX_SIZE);let s=0;u.SYSEX_HEADER.forEach(E=>{t[s++]=E});for(const E of this.voices)for(let _=0;_<u.VOICE_SIZE;_++)t[s++]=E.data[_];const e=t.subarray(u.SYSEX_HEADER_SIZE,u.SYSEX_HEADER_SIZE+u.VOICE_DATA_SIZE);return t[s++]=u._calculateChecksum(e,u.VOICE_DATA_SIZE),t[s++]=u.SYSEX_END,t}toJSON(){const t=this.voices.map((s,e)=>{const E=s.toJSON();return{index:e+1,...E}});return{version:"1.0",name:this.name||"",voices:t}}}function tt(C){return C[0]!==240||C[C.length-1]!==247?null:{manufacturerId:C[1],payload:C.slice(2,-1),raw:C}}function nt(C,t){return[240,C,...t,247]}function st(C){return C.length>=2&&C[0]===240&&C[C.length-1]===247}function et(C){const t=[];for(let s=0;s<C.length;s+=7){const e=C.slice(s,s+7);let E=0;const _=[];for(let i=0;i<e.length;i++){const r=e[i];r&128&&(E|=1<<i),_.push(r&127)}t.push(E,..._)}return t}function Et(C){const t=[];for(let s=0;s<C.length;s+=8){const e=C[s],E=Math.min(7,C.length-s-1);for(let _=0;_<E;_++){let i=C[s+1+_];e&1<<_&&(i|=128),t.push(i)}}return t}function it(C){return Number.isInteger(C)&&C>=1&&C<=16}function _t(C){return Number.isInteger(C)&&C>=0&&C<=127}function rt(C){return Number.isInteger(C)&&C>=0&&C<=31}function at(C){return Number.isInteger(C)&&C>=0&&C<=127}function Ct(C){return Number.isInteger(C)&&C>=0&&C<=127}function At(C){return Number.isInteger(C)&&C>=0&&C<=127}function ot(C){return Number.isInteger(C)&&C>=0&&C<=127}function Pt(C){return Number.isInteger(C)&&C>=0&&C<=16383}function ut(C,t){return Number.isInteger(C)&&C>=0&&C<=127&&Number.isInteger(t)&&t>=0&&t<=127}async function lt(C={}){const{onStatusUpdate:t,onConnectionUpdate:s,inputChannel:e=1,outputChannel:E=1,output:_,sysex:i,onReady:r,onError:h,selector:S,watchDOM:P,...a}=C,N=await Z({autoConnect:!1,sysex:i,inputChannel:e,outputChannel:E,selector:S||"[data-midi-cc]",watchDOM:P,onError:h,...a}),O=new $({midiController:N,onStatusUpdate:t||(()=>{}),onConnectionUpdate:s||(()=>{}),channel:E});if(_)try{await N.device.connectOutput(_),O.currentOutput=N.device.getCurrentOutput(),O.updateConnectionStatus()}catch(T){h?h(T):console.error("Failed to connect to MIDI device:",T.message)}return r&&r(N,O),O}async function Z(C={}){const t=new x(C);await t.init();const s=C.selector||"[data-midi-cc]";{const e=new p(t,s);e.bindAll(),C.watchDOM&&e.enableAutoBinding(),t._binder=e}return t}l.CONN=U,l.CONNECTION_EVENTS=U,l.CONTROLLER_EVENTS=o,l.CTRL=o,l.DX7Bank=u,l.DX7Error=F,l.DX7ParseError=M,l.DX7ValidationError=d,l.DX7Voice=n,l.DataAttributeBinder=p,l.EventEmitter=b,l.MIDIAccessError=g,l.MIDIConnection=Y,l.MIDIConnectionError=G,l.MIDIController=x,l.MIDIDeviceError=L,l.MIDIDeviceManager=$,l.MIDIError=m,l.MIDIValidationError=R,l.clamp=c,l.createMIDIController=Z,l.createMIDIDeviceManager=lt,l.createSysEx=nt,l.decode14BitValue=w,l.decode7Bit=Et,l.denormalize14BitValue=X,l.denormalizeValue=W,l.encode14BitValue=v,l.encode7Bit=et,l.frequencyToNote=j,l.getCCName=V,l.isSysEx=st,l.isValid14BitCC=rt,l.isValidCC=_t,l.isValidChannel=it,l.isValidMIDIValue=at,l.isValidNote=Ct,l.isValidPitchBend=Pt,l.isValidPitchBendBytes=ut,l.isValidProgramChange=ot,l.isValidVelocity=At,l.normalize14BitValue=B,l.normalizeValue=H,l.noteNameToNumber=Q,l.noteNumberToName=J,l.noteToFrequency=k,l.parseSysEx=tt,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
|
|
1
|
+
(function(l,I){typeof exports=="object"&&typeof module<"u"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(l=typeof globalThis<"u"?globalThis:l||self,I(l.MIDIControls={}))})(this,(function(l){"use strict";var Ot=Object.defineProperty;var dt=(l,I,K)=>I in l?Ot(l,I,{enumerable:!0,configurable:!0,writable:!0,value:K}):l[I]=K;var _=(l,I,K)=>dt(l,typeof I!="symbol"?I+"":I,K);class I{constructor(e,i="[data-midi-cc]"){this.controller=e,this.selector=i,this.observer=null}bindAll(){document.querySelectorAll(this.selector==="[data-midi-cc]"?"[data-midi-cc], [data-midi-msb][data-midi-lsb]":this.selector).forEach(i=>{if(i.hasAttribute("data-midi-bound"))return;const n=this._parseAttributes(i);n&&(this.controller.bind(i,n),i.setAttribute("data-midi-bound","true"))})}enableAutoBinding(){if(this.observer)return;const e=this.selector==="[data-midi-cc]"?"[data-midi-cc], [data-midi-msb][data-midi-lsb]":this.selector;this.observer=new MutationObserver(i=>{i.forEach(n=>{n.addedNodes.forEach(s=>{if(s.nodeType===Node.ELEMENT_NODE){if(s.matches?.(e)){const r=this._parseAttributes(s);r&&!s.hasAttribute("data-midi-bound")&&(this.controller.bind(s,r),s.setAttribute("data-midi-bound","true"))}s.querySelectorAll&&s.querySelectorAll(e).forEach(E=>{if(!E.hasAttribute("data-midi-bound")){const o=this._parseAttributes(E);o&&(this.controller.bind(E,o),E.setAttribute("data-midi-bound","true"))}})}}),n.removedNodes.forEach(s=>{s.nodeType===Node.ELEMENT_NODE&&(s.hasAttribute?.("data-midi-bound")&&this.controller.unbind(s),s.querySelectorAll&&s.querySelectorAll("[data-midi-bound]").forEach(E=>{this.controller.unbind(E)}))})})}),this.observer.observe(document.body,{childList:!0,subtree:!0})}disableAutoBinding(){this.observer&&(this.observer.disconnect(),this.observer=null)}_parseAttributes(e){const i=parseInt(e.dataset.midiMsb,10),n=parseInt(e.dataset.midiLsb,10);if(!Number.isNaN(i)&&!Number.isNaN(n)&&i>=0&&i<=127&&n>=0&&n<=127){const r=parseInt(e.dataset.midiCc,10);return!Number.isNaN(r)&&r>=0&&r<=127&&console.warn(`Element has both 7-bit (data-midi-cc="${r}") and 14-bit (data-midi-msb="${i}" data-midi-lsb="${n}") CC attributes. 14-bit takes precedence.`,e),{msb:i,lsb:n,is14Bit:!0,channel:parseInt(e.dataset.midiChannel,10)||void 0,min:parseFloat(e.getAttribute("min"))||0,max:parseFloat(e.getAttribute("max"))||127,invert:e.dataset.midiInvert==="true",label:e.dataset.midiLabel}}const s=parseInt(e.dataset.midiCc,10);return!Number.isNaN(s)&&s>=0&&s<=127?{cc:s,channel:parseInt(e.dataset.midiChannel,10)||void 0,min:parseFloat(e.getAttribute("min"))||0,max:parseFloat(e.getAttribute("max"))||127,invert:e.dataset.midiInvert==="true",label:e.dataset.midiLabel}:((e.dataset.midiCc!==void 0||e.dataset.midiMsb!==void 0&&e.dataset.midiLsb!==void 0)&&console.warn("Invalid MIDI configuration on element:",e),null)}destroy(){this.disableAutoBinding()}}class K extends Error{constructor(e,i){super(e),this.name="MIDIError",this.code=i,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}}class F extends K{constructor(e,i){super(e,"MIDI_ACCESS_ERROR"),this.name="MIDIAccessError",this.reason=i}}class y extends K{constructor(e){super(e,"MIDI_CONNECTION_ERROR"),this.name="MIDIConnectionError"}}class R extends K{constructor(e,i,n){super(e,"MIDI_DEVICE_ERROR"),this.name="MIDIDeviceError",this.deviceType=i,this.deviceId=n}}class M extends K{constructor(e,i){super(e,"MIDI_VALIDATION_ERROR"),this.name="MIDIValidationError",this.validationType=i}}class H extends Error{constructor(e,i){super(e),this.name="DX7Error",this.code=i,Error.captureStackTrace&&Error.captureStackTrace(this,this.constructor)}}class g extends H{constructor(e,i,n){super(e,"DX7_PARSE_ERROR"),this.name="DX7ParseError",this.parseType=i,this.offset=n}}class d extends H{constructor(e,i,n){super(e,"DX7_VALIDATION_ERROR"),this.name="DX7ValidationError",this.validationType=i,this.value=n}}function O(c,e,i){return Math.max(e,Math.min(i,c))}function w(c,e,i,n=!1){const s=(c-e)/(i-e),E=(n?1-s:s)*127;return O(Math.round(E),0,127)}function J(c,e,i,n=!1){let s=O(c,0,127)/127;return n&&(s=1-s),e+s*(i-e)}function k(c){const e={C:0,"C#":1,DB:1,D:2,"D#":3,EB:3,E:4,F:5,"F#":6,GB:6,G:7,"G#":8,AB:8,A:9,"A#":10,BB:10,B:11},i=c.match(/^([A-G][#b]?)(-?\d+)$/i);if(!i)throw new M(`Invalid note name: ${c}`,"note",c);const[,n,s]=i,r=e[n.toUpperCase()];if(r===void 0)throw new M(`Invalid note: ${n}`,"note",n);const E=(parseInt(s,10)+1)*12+r;return O(E,0,127)}function j(c,e=!1){const s=e?["C","Db","D","Eb","E","F","Gb","G","Ab","A","Bb","B"]:["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],r=Math.floor(c/12)-1;return`${s[c%12]}${r}`}function X(c){const e=69+12*Math.log2(c/440);return O(Math.round(e),0,127)}function tt(c){return 440*2**((c-69)/12)}function et(c){return{0:"Bank Select",1:"Modulation",2:"Breath Controller",4:"Foot Controller",5:"Portamento Time",7:"Volume",8:"Balance",10:"Pan",11:"Expression",64:"Sustain Pedal",65:"Portamento",66:"Sostenuto",67:"Soft Pedal",68:"Legato",71:"Resonance",72:"Release Time",73:"Attack Time",74:"Cutoff",75:"Decay Time",76:"Vibrato Rate",77:"Vibrato Depth",78:"Vibrato Delay",84:"Portamento Control",91:"Reverb",92:"Tremolo",93:"Chorus",94:"Detune",95:"Phaser",120:"All Sound Off",121:"Reset All Controllers",123:"All Notes Off"}[c]||`CC ${c}`}function B(c){const e=O(Math.round(c),0,16383);return{msb:e>>7&127,lsb:e&127}}function Y(c,e){return O(c,0,127)<<7|O(e,0,127)}function x(c,e,i,n=!1){const s=(c-e)/(i-e),E=(n?1-s:s)*16383;return B(E)}function it(c,e,i,n,s=!1){let E=Y(c,e)/16383;return s&&(E=1-E),i+E*(n-i)}class v{constructor(){this.events=new Map}on(e,i){return this.events.has(e)||this.events.set(e,[]),this.events.get(e).push(i),()=>this.off(e,i)}once(e,i){const n=(...s)=>{i(...s),this.off(e,n)};this.on(e,n)}off(e,i){if(!this.events.has(e))return;const n=this.events.get(e),s=n.indexOf(i);s>-1&&n.splice(s,1),n.length===0&&this.events.delete(e)}emit(e,i){if(!this.events.has(e))return;[...this.events.get(e)].forEach(s=>{try{s(i)}catch(r){console.error(`Error in event handler for "${e}":`,r)}})}removeAllListeners(e){e?this.events.delete(e):this.events.clear()}}const p={DEVICE_CHANGE:"device-change",IN_DEV_CONNECTED:"in-dev-connected",IN_DEV_DISCONNECTED:"in-dev-disconnected",OUT_DEV_CONNECTED:"out-dev-connected",OUT_DEV_DISCONNECTED:"out-dev-disconnected"};class $ extends v{constructor(e={}){super(),this.options={sysex:!1,...e},this.midiAccess=null,this.output=null,this.input=null}async requestAccess(){if(!navigator.requestMIDIAccess)throw new F("Web MIDI API is not supported in this browser","unsupported");try{this.midiAccess=await navigator.requestMIDIAccess({sysex:this.options.sysex}),this.midiAccess.onstatechange=e=>{const i=e.port,n=e.port.state;this.emit(p.DEVICE_CHANGE,{port:i,state:n,type:i.type,device:{id:i.id,name:i.name,manufacturer:i.manufacturer||"Unknown"}}),n==="disconnected"?i.type==="input"?(this.emit(p.IN_DEV_DISCONNECTED,{device:i}),this.input&&this.input.id===i.id&&(this.input=null)):i.type==="output"&&(this.emit(p.OUT_DEV_DISCONNECTED,{device:i}),this.output&&this.output.id===i.id&&(this.output=null)):n==="connected"&&(i.type==="input"?this.emit(p.IN_DEV_CONNECTED,{device:i}):i.type==="output"&&this.emit(p.OUT_DEV_CONNECTED,{device:i}))}}catch(e){throw e.name==="SecurityError"?new F("MIDI access denied. SysEx requires user permission.","denied"):new F(`Failed to get MIDI access: ${e.message}`,"failed")}}async connect(e){if(!this.midiAccess)throw new y("MIDI access not initialized. Call requestAccess() first.");const i=Array.from(this.midiAccess.outputs.values());if(i.length===0)throw new R("No MIDI output devices available","output");if(e===void 0){this.output=i[0];return}if(typeof e=="number"){if(e<0||e>=i.length)throw new R(`Output index ${e} out of range (0-${i.length-1})`,"output",e);this.output=i[e];return}if(this.output=i.find(n=>n.name===e||n.id===e),!this.output){const n=i.map(s=>s.name).join(", ");throw new R(`MIDI output "${e}" not found. Available: ${n}`,"output",e)}}async connectInput(e,i){if(!this.midiAccess)throw new y("MIDI access not initialized. Call requestAccess() first.");if(typeof i!="function")throw new M("onMessage callback must be a function","callback");const n=Array.from(this.midiAccess.inputs.values());if(n.length===0)throw new R("No MIDI input devices available","input");if(this.input&&(this.input.onmidimessage=null),e===void 0)this.input=n[0];else if(typeof e=="number"){if(e<0||e>=n.length)throw new R(`Input index ${e} out of range (0-${n.length-1})`,"input",e);this.input=n[e]}else if(this.input=n.find(s=>s.name===e||s.id===e),!this.input){const s=n.map(r=>r.name).join(", ");throw new R(`MIDI input "${e}" not found. Available: ${s}`,"input",e)}this.input.onmidimessage=s=>{i(s)}}disconnectOutput(){this.output=null}disconnectInput(){this.input&&(this.input.onmidimessage=null,this.input=null)}disconnect(){this.disconnectOutput(),this.disconnectInput()}isConnected(){return this.output!==null}getCurrentOutput(){return this.output?{id:this.output.id,name:this.output.name,manufacturer:this.output.manufacturer||"Unknown"}:null}getCurrentInput(){return this.input?{id:this.input.id,name:this.input.name,manufacturer:this.input.manufacturer||"Unknown"}:null}getOutputs(){if(!this.midiAccess)return[];const e=[];return this.midiAccess.outputs.forEach(i=>{i.state==="connected"&&e.push({id:i.id,name:i.name,manufacturer:i.manufacturer||"Unknown"})}),e}getInputs(){if(!this.midiAccess)return[];const e=[];return this.midiAccess.inputs.forEach(i=>{i.state==="connected"&&e.push({id:i.id,name:i.name,manufacturer:i.manufacturer||"Unknown"})}),e}send(e,i=null){if(!this.output){console.warn("No MIDI output connected. Call connect() first.");return}try{const n=new Uint8Array(e);i===null?this.output.send(n):this.output.send(n,i)}catch(n){console.error("Failed to send MIDI message:",n)}}sendSysEx(e,i=!1){if(!this.options.sysex){console.warn("SysEx not enabled. Initialize with sysex: true");return}let n;i?n=[240,...e,247]:n=e,this.send(n)}}const A={READY:"ready",ERROR:"error",DESTROYED:"destroyed",DEV_OUT_CONNECTED:"dev-out-connected",DEV_OUT_DISCONNECTED:"dev-out-disconnected",DEV_IN_CONNECTED:"dev-in-connected",DEV_IN_DISCONNECTED:"dev-in-disconnected",CH_CC_SEND:"ch-cc-send",CH_CC_RECV:"ch-cc-recv",CH_NOTE_ON_SEND:"ch-note-on-send",CH_NOTE_ON_RECV:"ch-note-on-recv",CH_NOTE_OFF_SEND:"ch-note-off-send",CH_NOTE_OFF_RECV:"ch-note-off-recv",CH_PC_SEND:"ch-pc-send",CH_PC_RECV:"ch-pc-recv",CH_PITCH_BEND_SEND:"ch-pitch-bend-send",CH_PITCH_BEND_RECV:"ch-pitch-bend-recv",CH_MONO_PRESS_SEND:"ch-mono-press-send",CH_MONO_PRESS_RECV:"ch-mono-press-recv",CH_POLY_PRESS_SEND:"ch-poly-press-send",CH_POLY_PRESS_RECV:"ch-poly-press-recv",CH_ALL_SOUNDS_OFF_SEND:"ch-all-sounds-off-send",CH_RESET_CONTROLLERS_SEND:"ch-reset-controllers-send",CH_LOCAL_CONTROL_SEND:"ch-local-control-send",CH_ALL_NOTES_OFF_SEND:"ch-all-notes-off-send",CH_OMNI_OFF_SEND:"ch-omni-off-send",CH_OMNI_ON_SEND:"ch-omni-on-send",CH_MONO_ON_SEND:"ch-mono-on-send",CH_POLY_ON_SEND:"ch-poly-on-send",SYS_EX_SEND:"sys-ex-send",SYS_EX_RECV:"sys-ex-recv",SYS_CLOCK_RECV:"sys-clock-recv",SYS_START_RECV:"sys-start-recv",SYS_CONTINUE_RECV:"sys-continue-recv",SYS_STOP_RECV:"sys-stop-recv",SYS_MTC_RECV:"sys-mtc-recv",SYS_SONG_POS_RECV:"sys-song-pos-recv",SYS_SONG_SEL_RECV:"sys-song-sel-recv",SYS_TUNE_REQ_RECV:"sys-tune-req-recv",SYS_ACT_SENSE_RECV:"sys-act-sense-recv",SYS_RESET_RECV:"sys-reset-recv",MIDI_RAW:"midi-raw",PATCH_SAVED:"patch-saved",PATCH_LOADED:"patch-loaded",PATCH_DELETED:"patch-deleted"};class Z extends v{constructor(e={}){super(),this.options={inputChannel:1,outputChannel:1,autoConnect:!0,sysex:!1,...e},this.connection=null,this.bindings=new Map,this.state={controlChange:new Map,programChange:new Map,pitchBend:new Map,monoPressure:new Map,polyPressure:new Map},this.initialized=!1,this._initNamespaces()}async init(){if(this.initialized){console.warn("MIDI Controller already initialized");return}try{this.connection=new $({sysex:this.options.sysex}),await this.connection.requestAccess(),this.connection.on(p.DEVICE_CHANGE,async({state:e,type:i,device:n})=>{try{if(e==="connected")i==="output"?this.emit(A.DEV_OUT_CONNECTED,n):i==="input"&&this.emit(A.DEV_IN_CONNECTED,n);else if(e==="disconnected"){if(i==="output"&&n){const s=this.connection.getCurrentOutput();s&&s.id===n.id?await this._disconnectOutput():this.emit(A.DEV_OUT_DISCONNECTED,n)}if(i==="input"&&n){const s=this.connection.getCurrentInput();s&&s.id===n.id?await this._disconnectInput():this.emit(A.DEV_IN_DISCONNECTED,n)}}}catch(s){console.error("Error in device change handler:",s),this.emit(A.ERROR,s)}}),this.options.autoConnect&&await this.connection.connect(this.options.output),this.options.input!==void 0&&await this._connectInput(this.options.input),this.initialized=!0,this.emit(A.READY,this),this.options.onReady?.(this)}catch(e){throw this.emit(A.ERROR,e),this.options.onError?.(e),e}}_initNamespaces(){this.device={connect:this._connect.bind(this),disconnect:this._disconnect.bind(this),connectInput:this._connectInput.bind(this),disconnectInput:this._disconnectInput.bind(this),connectOutput:this._connectOutput.bind(this),disconnectOutput:this._disconnectOutput.bind(this),getCurrentOutput:this._getCurrentOutput.bind(this),getCurrentInput:this._getCurrentInput.bind(this),getOutputs:this._getOutputs.bind(this),getInputs:this._getInputs.bind(this)},this.channel={sendNoteOn:this._sendNoteOn.bind(this),sendNoteOff:this._sendNoteOff.bind(this),sendCC:this._sendCC.bind(this),getCC:this._getCC.bind(this),sendPC:this._sendPC.bind(this),getPC:this._getPC.bind(this),sendPitchBend:this._sendPitchBend.bind(this),getPitchBend:this._getPitchBend.bind(this),sendMonoPressure:this._sendMonoPressure.bind(this),getMonoPressure:this._getMonoPressure.bind(this),sendPolyPressure:this._sendPolyPressure.bind(this),getPolyPressure:this._getPolyPressure.bind(this),allSoundsOff:this._allSoundsOff.bind(this),resetControllers:this._resetControllers.bind(this),localControl:this._localControl.bind(this),allNotesOff:this._allNotesOff.bind(this),omniOff:this._omniOff.bind(this),omniOn:this._omniOn.bind(this),monoOn:this._monoOn.bind(this),polyOn:this._polyOn.bind(this)},this.system={sendEx:function(e,i=!1){return this._sendSysEx(e,i)}.bind(this),sendClock:this._sendClock.bind(this),start:this._sendStart.bind(this),continue:this._sendContinue.bind(this),stop:this._sendStop.bind(this),sendMTC:this._sendMTC.bind(this),sendSongPosition:this._sendSongPosition.bind(this),sendSongSelect:this._sendSongSelect.bind(this),sendTuneRequest:this._sendTuneRequest.bind(this),sendActiveSensing:this._sendActiveSensing.bind(this),sendSystemReset:this._sendSystemReset.bind(this)},this.patch={get:this._getPatch.bind(this),set:this._setPatch.bind(this),save:this._savePatch.bind(this),load:this._loadPatch.bind(this),delete:this._deletePatch.bind(this),list:this._listPatches.bind(this)}}bind(e,i,n={}){if(!e)return console.warn("Cannot bind: element is null or undefined"),()=>{};const s=this._createBinding(e,i,n);return this.bindings.set(e,s),this.initialized&&this.connection?.isConnected()&&s.handler({target:e}),()=>this.unbind(e)}_createBinding(e,i,n={}){const{min:s=parseFloat(e.getAttribute("min"))||0,max:r=parseFloat(e.getAttribute("max"))||127,channel:E,invert:o=!1,onInput:h=void 0}=i,{debounce:N=0}=n,u={...i,min:s,max:r,invert:o,onInput:h};if(E!==void 0&&(u.channel=E),i.is14Bit){const{msb:T,lsb:f}=i,U=V=>{const W=parseFloat(V.target.value);if(Number.isNaN(W))return;const{msb:Nt,lsb:Dt}=x(W,s,r,o),Q=E||this.options.outputChannel;this._sendCC(T,Nt,Q),this._sendCC(f,Dt,Q)};let L=null;const b=N>0?V=>{L&&clearTimeout(L),L=setTimeout(()=>{U(V),L=null},N)}:U;return e.addEventListener("input",b),e.addEventListener("change",b),{element:e,config:u,handler:U,destroy:()=>{L&&clearTimeout(L),e.removeEventListener("input",b),e.removeEventListener("change",b)}}}const{cc:a}=i,C=T=>{const f=parseFloat(T.target.value);if(Number.isNaN(f))return;const U=w(f,s,r,o),L=E===void 0?this.options.outputChannel:E;this._sendCC(a,U,L)};let S=null;const D=N>0?T=>{S&&clearTimeout(S),S=setTimeout(()=>{C(T),S=null},N)}:C;return e.addEventListener("input",D),e.addEventListener("change",D),{element:e,config:u,handler:C,destroy:()=>{S&&clearTimeout(S),e.removeEventListener("input",D),e.removeEventListener("change",D)}}}unbind(e){const i=this.bindings.get(e);i&&(i.destroy(),this.bindings.delete(e))}async destroy(){for(const e of this.bindings.values())e.destroy();this.bindings.clear(),this.state.controlChange.clear(),this.state.programChange.clear(),this.state.pitchBend.clear(),this.state.monoPressure.clear(),this.state.polyPressure.clear(),await this._disconnect(),this.initialized=!1,this.emit(A.DESTROYED),this.removeAllListeners()}async _connect(e){e?await this.connection.connect(e):this.options.output!==void 0?await this.connection.connect(this.options.output):this.options.autoConnect&&await this.connection.connect()}async _disconnect(){this.connection.disconnect()}async _connectInput(e){await this.connection.connectInput(e,i=>{this._handleMIDIMessage(i)}),this.emit(A.DEV_IN_CONNECTED,this.connection.getCurrentInput())}async _disconnectInput(){const e=this.connection.getCurrentInput();this.connection.disconnectInput(),this.emit(A.DEV_IN_DISCONNECTED,e)}async _connectOutput(e){await this.connection.connect(e),this.emit(A.DEV_OUT_CONNECTED,this.connection.getCurrentOutput())}async _disconnectOutput(){const e=this.connection.getCurrentOutput();this.connection.disconnectOutput(),this.emit(A.DEV_OUT_DISCONNECTED,e)}_getCurrentOutput(){return this.connection?.getCurrentOutput()||null}_getCurrentInput(){return this.connection?.getCurrentInput()||null}_getOutputs(){return this.connection?.getOutputs()||[]}_getInputs(){return this.connection?.getInputs()||[]}send(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send(e)}_sendNoteOn(e,i=64,n=this.options.outputChannel){if(!this.initialized)return;e=O(Math.round(e),0,127),i=O(Math.round(i),0,127),n=O(Math.round(n),1,16);const s=144+(n-1);this.connection.send([s,e,i]),this.emit(A.CH_NOTE_ON_SEND,{note:e,velocity:i,channel:n})}_sendNoteOff(e,i=this.options.outputChannel,n=0){if(!this.initialized)return;e=O(Math.round(e),0,127),n=O(Math.round(n),0,127),i=O(Math.round(i),1,16);const s=144+(i-1);this.connection.send([s,e,n]),this.emit(A.CH_NOTE_OFF_SEND,{note:e,channel:i,velocity:n})}_sendCC(e,i,n=this.options.outputChannel){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}e=O(Math.round(e),0,127),i=O(Math.round(i),0,127),n=O(Math.round(n),1,16);const s=176+(n-1);this.connection.send([s,e,i]);const r=`${n}:${e}`;this.state.controlChange.set(r,i),this.emit(A.CH_CC_SEND,{cc:e,value:i,channel:n})}_sendPC(e,i=this.options.outputChannel){if(!this.initialized)return;e=O(Math.round(e),0,127),i=O(Math.round(i),1,16);const n=192+(i-1);this.connection.send([n,e]),this.state.programChange.set(i.toString(),e),this.emit(A.CH_PC_SEND,{program:e,channel:i})}_getPC(e=this.options.inputChannel){return this.state.programChange.get(e.toString())}_getCC(e,i=this.options.inputChannel){const n=`${i}:${e}`;return this.state.controlChange.get(n)}_sendPitchBend(e,i=this.options.outputChannel){if(!this.initialized)return;e=O(Math.round(e),0,16383),i=O(Math.round(i),1,16);const n=224+(i-1),s=e&127,r=e>>7&127;this.connection.send([n,s,r]),this.state.pitchBend.set(i.toString(),e),this.emit(A.CH_PITCH_BEND_SEND,{value:e,channel:i})}_getPitchBend(e=this.options.inputChannel){return this.state.pitchBend.get(e.toString())}_sendMonoPressure(e,i=this.options.outputChannel){if(!this.initialized)return;e=O(Math.round(e),0,127),i=O(Math.round(i),1,16);const n=208+(i-1);this.connection.send([n,e]),this.state.monoPressure.set(i.toString(),e),this.emit(A.CH_MONO_PRESS_SEND,{pressure:e,channel:i})}_getMonoPressure(e=this.options.inputChannel){return this.state.monoPressure.get(e.toString())}_sendPolyPressure(e,i,n=this.options.outputChannel){if(!this.initialized)return;e=O(Math.round(e),0,127),i=O(Math.round(i),0,127),n=O(Math.round(n),1,16);const s=160+(n-1);this.connection.send([s,e,i]);const r=`${n}:${e}`;this.state.polyPressure.set(r,i),this.emit(A.CH_POLY_PRESS_SEND,{note:e,pressure:i,channel:n})}_getPolyPressure(e,i=this.options.inputChannel){const n=`${i}:${e}`;return this.state.polyPressure.get(n)}_allSoundsOff(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(e!==void 0){e=O(Math.round(e),1,16);const i=176+(e-1);this.connection.send([i,120,0]),this.emit(A.CH_ALL_SOUNDS_OFF_SEND,{channel:e})}else{for(let i=1;i<=16;i++){const n=176+(i-1);this.connection.send([n,120,0])}this.emit(A.CH_ALL_SOUNDS_OFF_SEND,{channel:null})}}_resetControllers(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(e!==void 0){e=O(Math.round(e),1,16);const i=176+(e-1);this.connection.send([i,121,0]),this.emit(A.CH_RESET_CONTROLLERS_SEND,{channel:e})}else{for(let i=1;i<=16;i++){const n=176+(i-1);this.connection.send([n,121,0])}this.emit(A.CH_RESET_CONTROLLERS_SEND,{channel:null})}}_localControl(e,i){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}const n=e?127:0;if(i!==void 0){i=O(Math.round(i),1,16);const s=176+(i-1);this.connection.send([s,122,n]),this.emit(A.CH_LOCAL_CONTROL_SEND,{enabled:e,channel:i})}else{for(let s=1;s<=16;s++){const r=176+(s-1);this.connection.send([r,122,n])}this.emit(A.CH_LOCAL_CONTROL_SEND,{enabled:e,channel:null})}}_allNotesOff(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(e!==void 0){e=O(Math.round(e),1,16);const i=176+(e-1);this.connection.send([i,123,0]),this.emit(A.CH_ALL_NOTES_OFF_SEND,{channel:e})}else{for(let i=1;i<=16;i++){const n=176+(i-1);this.connection.send([n,123,0])}this.emit(A.CH_ALL_NOTES_OFF_SEND,{channel:null})}}_omniOff(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(e!==void 0){e=O(Math.round(e),1,16);const i=176+(e-1);this.connection.send([i,124,0]),this.emit(A.CH_OMNI_OFF_SEND,{channel:e})}else{for(let i=1;i<=16;i++){const n=176+(i-1);this.connection.send([n,124,0])}this.emit(A.CH_OMNI_OFF_SEND,{channel:null})}}_omniOn(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(e!==void 0){e=O(Math.round(e),1,16);const i=176+(e-1);this.connection.send([i,125,0]),this.emit(A.CH_OMNI_ON_SEND,{channel:e})}else{for(let i=1;i<=16;i++){const n=176+(i-1);this.connection.send([n,125,0])}this.emit(A.CH_OMNI_ON_SEND,{channel:null})}}_monoOn(e=1,i){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(e=Math.max(0,Math.min(16,Math.round(e))),i!==void 0){i=O(Math.round(i),1,16);const n=176+(i-1);this.connection.send([n,126,e]),this.emit(A.CH_MONO_ON_SEND,{channels:e,channel:i})}else{for(let n=1;n<=16;n++){const s=176+(n-1);this.connection.send([s,126,e])}this.emit(A.CH_MONO_ON_SEND,{channels:e,channel:null})}}_polyOn(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(e!==void 0){e=O(Math.round(e),1,16);const i=176+(e-1);this.connection.send([i,127,0]),this.emit(A.CH_POLY_ON_SEND,{channel:e})}else{for(let i=1;i<=16;i++){const n=176+(i-1);this.connection.send([n,127,0])}this.emit(A.CH_POLY_ON_SEND,{channel:null})}}_sendSysEx(e,i=!1){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}if(!this.options.sysex){console.warn("SysEx not enabled. Initialize with sysex: true");return}this.connection.sendSysEx(e,i),this.emit(A.SYS_EX_SEND,{data:e,includeWrapper:i})}_sendClock(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([248])}_sendStart(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([250])}_sendContinue(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([251])}_sendStop(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([252])}_sendMTC(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}e=O(Math.round(e),0,127),this.connection.send([241,e])}_sendSongPosition(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}e=O(Math.round(e),0,16383);const i=e&127,n=e>>7&127;this.connection.send([242,i,n])}_sendSongSelect(e){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}e=O(Math.round(e),0,127),this.connection.send([243,e])}_sendTuneRequest(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([246])}_sendActiveSensing(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([254])}_sendSystemReset(){if(!this.initialized){console.warn("MIDI not initialized. Call init() first.");return}this.connection.send([255])}_handleMIDIMessage(e){const[i,n,s]=e.data,r=i&240,E=(i&15)+1;if(i===248){this.emit(A.SYS_CLOCK_RECV,{timestamp:e.midiwire});return}if(i===250){this.emit(A.SYS_START_RECV,{timestamp:e.midiwire});return}if(i===251){this.emit(A.SYS_CONTINUE_RECV,{timestamp:e.midiwire});return}if(i===252){this.emit(A.SYS_STOP_RECV,{timestamp:e.midiwire});return}if(i===254){this.emit(A.SYS_ACT_SENSE_RECV,{timestamp:e.midiwire});return}if(i===255){this.emit(A.SYS_RESET_RECV,{timestamp:e.midiwire});return}if(i===240){this.emit(A.SYS_EX_RECV,{data:Array.from(e.data),timestamp:e.midiwire});return}if(i===241){this.emit(A.SYS_MTC_RECV,{data:n,timestamp:e.midiwire});return}if(i===242){const o=n+(s<<7);this.emit(A.SYS_SONG_POS_RECV,{position:o,timestamp:e.midiwire});return}if(i===243){this.emit(A.SYS_SONG_SEL_RECV,{song:n,timestamp:e.midiwire});return}if(i===246){this.emit(A.SYS_TUNE_REQ_RECV,{timestamp:e.midiwire});return}if(i===247){this.emit(A.MIDI_RAW,{status:i,data:[n,s],channel:E,timestamp:e.midiwire});return}if(r===176){const o=`${E}:${n}`;this.state.controlChange.set(o,s),this.emit(A.CH_CC_RECV,{cc:n,value:s,channel:E});return}if(r===192){this.state.programChange.set(E.toString(),n),this.emit(A.CH_PC_RECV,{program:n,channel:E});return}if(r===224){const o=n+(s<<7);this.state.pitchBend.set(E.toString(),o),this.emit(A.CH_PITCH_BEND_RECV,{value:o,channel:E});return}if(r===208){this.state.monoPressure.set(E.toString(),n),this.emit(A.CH_MONO_PRESS_RECV,{pressure:n,channel:E});return}if(r===160){const o=`${E}:${n}`;this.state.polyPressure.set(o,s),this.emit(A.CH_POLY_PRESS_RECV,{note:n,pressure:s,channel:E});return}if(r===144&&s>0){this.emit(A.CH_NOTE_ON_RECV,{note:n,velocity:s,channel:E});return}if(r===128||r===144&&s===0){this.emit(A.CH_NOTE_OFF_RECV,{note:n,channel:E});return}this.emit(A.MIDI_RAW,{status:i,data:[n,s],channel:E,timestamp:e.midiwire})}_getPatch(e="Unnamed Patch"){const i={name:e,device:this._getCurrentOutput()?.name||null,timestamp:new Date().toISOString(),version:"1.0",channels:{},settings:{}};for(const[n,s]of this.state.controlChange.entries()){const[r,E]=n.split(":").map(Number);i.channels[r]||(i.channels[r]={ccs:{},notes:{}}),i.channels[r].ccs[E]=s}for(const[n,s]of this.state.programChange.entries()){const r=parseInt(n,10);i.channels[r]||(i.channels[r]={ccs:{},notes:{}}),i.channels[r].program=s}for(const[n,s]of this.state.pitchBend.entries()){const r=parseInt(n,10);i.channels[r]||(i.channels[r]={ccs:{},notes:{}}),i.channels[r].pitchBend=s}for(const[n,s]of this.state.monoPressure.entries()){const r=parseInt(n,10);i.channels[r]||(i.channels[r]={ccs:{},notes:{}}),i.channels[r].monoPressure=s}for(const[n,s]of this.state.polyPressure.entries()){const[r,E]=n.split(":").map(Number),o=parseInt(r,10);i.channels[o]||(i.channels[o]={ccs:{},notes:{}}),i.channels[o].polyPressure||(i.channels[o].polyPressure={}),i.channels[o].polyPressure[E]=s}for(const[n,s]of this.bindings.entries()){const{config:r}=s;if(r.cc){const E=`cc${r.cc}`;i.settings[E]={min:r.min,max:r.max,invert:r.invert||!1,is14Bit:r.is14Bit||!1,label:n.getAttribute?.("data-midi-label")||null,elementId:n.id||null}}}return i}async _setPatch(e){if(!e||!e.channels)throw new M("Invalid patch format","patch");const i=e.version||"1.0";i==="1.0"?await this._applyPatchV1(e):(console.warn(`Unknown patch version: ${i}. Attempting to apply as v1.0`),await this._applyPatchV1(e)),this.emit(A.PATCH_LOADED,{patch:e})}async _applyPatchV1(e){for(const[i,n]of Object.entries(e.channels)){const s=parseInt(i,10);if(n.ccs)for(const[r,E]of Object.entries(n.ccs)){const o=parseInt(r,10);this._sendCC(o,E,s)}if(n.program!==void 0&&this._sendPC(n.program,s),n.pitchBend!==void 0&&this._sendPitchBend(n.pitchBend,s),n.monoPressure!==void 0&&this._sendMonoPressure(n.monoPressure,s),n.polyPressure)for(const[r,E]of Object.entries(n.polyPressure)){const o=parseInt(r,10);this._sendPolyPressure(o,E,s)}if(n.notes)for(const[r,E]of Object.entries(n.notes)){const o=parseInt(r,10);E>0?this._sendNoteOn(o,E,s):this._sendNoteOff(o,s)}}if(e.settings)for(const[i,n]of Object.entries(e.settings))for(const[s,r]of this.bindings.entries())r.config.cc?.toString()===i.replace("cc","")&&(s.min!==void 0&&n.min!==void 0&&(s.min=String(n.min)),s.max!==void 0&&n.max!==void 0&&(s.max=String(n.max)));for(const[i,n]of this.bindings.entries()){const{config:s}=n;if(s.cc!==void 0){const r=s.channel||this.options.inputChannel,E=e.channels[r];if(E?.ccs){const o=E.ccs[s.cc];if(o!==void 0){const h=s.min!==void 0?s.min:parseFloat(i.getAttribute?.("min"))||0,N=s.max!==void 0?s.max:parseFloat(i.getAttribute?.("max"))||127,u=s.invert||!1;let a;u?a=N-o/127*(N-h):a=h+o/127*(N-h),s.onInput&&typeof s.onInput=="function"?s.onInput(a):(i.value=a,i.dispatchEvent(new Event("input",{bubbles:!0})))}}}}}_savePatch(e,i=null){const n=i||this._getPatch(e),s=`midiwire_patch_${e}`;try{return localStorage.setItem(s,JSON.stringify(n)),this.emit(A.PATCH_SAVED,{name:e,patch:n}),s}catch(r){throw console.error("Failed to save patch:",r),r}}_loadPatch(e){const i=`midiwire_patch_${e}`;try{const n=localStorage.getItem(i);if(!n)return null;const s=JSON.parse(n);return this.emit(A.PATCH_LOADED,{name:e,patch:s}),s}catch(n){return console.error("Failed to load patch:",n),null}}_deletePatch(e){const i=`midiwire_patch_${e}`;try{return localStorage.removeItem(i),this.emit(A.PATCH_DELETED,{name:e}),!0}catch(n){return console.error("Failed to delete patch:",n),!1}}_listPatches(){const e=[];try{for(let i=0;i<localStorage.length;i++){const n=localStorage.key(i);if(n?.startsWith("midiwire_patch_")){const s=n.replace("midiwire_patch_",""),r=this._loadPatch(s);r&&e.push({name:s,patch:r})}}}catch(i){console.error("Failed to list patches:",i)}return e.sort((i,n)=>i.name.localeCompare(n.name))}}class z{constructor(e={}){this.midi=e.midiController||null,this.onStatusUpdate=e.onStatusUpdate||(()=>{}),this.onConnectionUpdate=e.onConnectionUpdate||(()=>{}),this.channel=e.channel||1,this.currentOutput=null,this.currentInput=null,this.isConnecting=!1}async setupSelectors(e={},i={}){if(!this.midi)throw new Error("MIDI controller not initialized. Pass midiController in constructor options.");const{output:n,input:s,channel:r}=e,E=this._resolveSelector(n),o=this._resolveSelector(s),h=this._resolveSelector(r);if(this._setupDeviceChangeListeners({output:E,input:o},i.onDeviceListChange),E){await this._populateOutputDeviceList(E);const N=i.onConnect?async(a,C)=>i.onConnect({midi:a,device:C,type:"output"}):void 0,u=i.onDisconnect?async a=>i.onDisconnect({midi:a,type:"output"}):void 0;this._connectOutputDeviceSelection(E,N,u)}if(o){await this._populateInputDeviceList(o);const N=i.onConnect?async(a,C)=>i.onConnect({midi:a,device:C,type:"input"}):void 0,u=i.onDisconnect?async a=>i.onDisconnect({midi:a,type:"input"}):void 0;this._connectInputDeviceSelection(o,N,u)}return h&&this._connectChannelSelection(h,"output"),this.midi}updateStatus(e,i=""){this.onStatusUpdate(e,i)}updateConnectionStatus(){this.onConnectionUpdate(this.currentOutput,this.currentInput,this.midi)}_setupDeviceChangeListeners(e={},i){!this.midi||this._listenersInitialized||(this._listenersInitialized=!0,this.midi.on(A.DEV_OUT_CONNECTED,async n=>{this.updateStatus(`Output device connected: ${n?.name||"Unknown"}`,"connected"),e.output&&await this._populateOutputDeviceList(e.output),i&&i()}),this.midi.on(A.DEV_OUT_DISCONNECTED,async n=>{this.updateStatus(`Output device disconnected: ${n?.name||"Unknown"}`,"error"),this.currentOutput&&n?.name===this.currentOutput.name&&(this.currentOutput=null,this.updateConnectionStatus(),e.output&&(e.output.value="")),e.output&&await this._populateOutputDeviceList(e.output),i&&i()}),this.midi.on(A.DEV_IN_CONNECTED,async n=>{this.updateStatus(`Input device connected: ${n?.name||"Unknown"}`,"connected"),e.input&&await this._populateInputDeviceList(e.input),i&&i()}),this.midi.on(A.DEV_IN_DISCONNECTED,async n=>{this.updateStatus(`Input device disconnected: ${n?.name||"Unknown"}`,"error"),e.input&&(e.input.value="",await this._populateInputDeviceList(e.input)),i&&i()}))}_resolveSelector(e){if(typeof e=="string"){const i=document.querySelector(e);return i||console.warn(`MIDIDeviceManager: Selector "${e}" not found`),i}return e||null}_getOutputDevices(){return this.midi?this.midi.device.getOutputs():[]}_getInputDevices(){return this.midi?this.midi.device.getInputs():[]}_connectOutputDeviceSelection(e,i,n){!e||!this.midi||e.addEventListener("change",async s=>{if(this.isConnecting)return;this.isConnecting=!0;const r=s.target.value;if(!r){this.currentOutput&&this.midi&&(await this.midi.device.disconnectOutput(),this.currentOutput=null,this.updateStatus("Output device disconnected",""),this.updateConnectionStatus()),this.isConnecting=!1,n&&await n(this.midi);return}try{if(await this.midi.device.connectOutput(parseInt(r,10)),this.currentOutput=this.midi.device.getCurrentOutput(),this.currentOutput){const o=this.midi.device.getOutputs().findIndex(h=>h.id===this.currentOutput.id);o!==-1&&(e.value=o.toString())}this.updateConnectionStatus(),i&&await i(this.midi,this.currentOutput)}catch(E){this.updateStatus(`Output connection failed: ${E.message}`,"error")}finally{this.isConnecting=!1}})}_connectInputDeviceSelection(e,i,n){!e||!this.midi||e.addEventListener("change",async s=>{const r=s.target.value;if(!r){this.midi&&(await this.midi.device.disconnectInput(),this.updateStatus("Input device disconnected",""),this.updateConnectionStatus()),n&&await n(this.midi);return}if(!this.isConnecting){this.isConnecting=!0;try{await this.midi.device.connectInput(parseInt(r,10));const E=this.midi.device.getCurrentInput();this.updateConnectionStatus(),i&&await i(this.midi,E)}catch(E){this.updateStatus(`Input connection failed: ${E.message}`,"error")}finally{this.isConnecting=!1}}})}_populateDeviceList(e,i,n,s,r){if(i.length>0){if(e.innerHTML='<option value="">Select a device</option>'+i.map((E,o)=>`<option value="${o}">${E.name}</option>`).join(""),n){const E=i.findIndex(o=>o.name===n.name);E!==-1?e.value=E.toString():(e.value="",r&&(this.currentOutput=null,this.updateConnectionStatus()))}else e.value="";e.disabled=!1,r&&!this.currentOutput&&this.updateStatus("Select a device")}else e.innerHTML='<option value="">No devices connected</option>',e.disabled=!0,r&&this.updateStatus("No devices connected","error");s&&s()}async _populateOutputDeviceList(e,i){if(!e||!this.midi)return;const n=this._getOutputDevices();this._populateDeviceList(e,n,this.currentOutput,i,!0)}async _populateInputDeviceList(e,i){if(!e||!this.midi)return;const n=this._getInputDevices(),s=this.midi.device.getCurrentInput();this._populateDeviceList(e,n,s,i,!1)}_connectChannelSelection(e,i){if(!e||!this.midi)return;const n=i==="input"?"inputChannel":"outputChannel";e.addEventListener("change",s=>{this.midi&&(this.midi.options[n]=parseInt(s.target.value,10),this.updateConnectionStatus())})}}const t=class t{constructor(e,i=0){if(e.length!==t.PACKED_SIZE)throw new d(`Invalid voice data length: expected ${t.PACKED_SIZE} bytes, got ${e.length}`,"length",e.length);this.index=i,this.data=new Uint8Array(e),this.name=this._extractName(),this._unpackedCache=null}_extractName(){const e=this.data.subarray(t.PACKED_NAME_START,t.PACKED_NAME_START+t.NAME_LENGTH);return Array.from(e).map(n=>{let s=n&t.MASK_7BIT;return s===t.CHAR_YEN&&(s=t.CHAR_REPLACEMENT_Y),s===t.CHAR_ARROW_RIGHT&&(s=t.CHAR_REPLACEMENT_GT),s===t.CHAR_ARROW_LEFT&&(s=t.CHAR_REPLACEMENT_LT),(s<t.CHAR_MIN_PRINTABLE||s>t.CHAR_MAX_PRINTABLE)&&(s=t.CHAR_SPACE),String.fromCharCode(s)}).join("").trim()}getParameter(e){if(e<0||e>=t.PACKED_SIZE)throw new d(`Parameter offset out of range: ${e} (must be 0-${t.PACKED_SIZE-1})`,"offset",e);return this.data[e]&t.MASK_7BIT}getUnpackedParameter(e){if(e<0||e>=t.UNPACKED_SIZE)throw new d(`Unpacked parameter offset out of range: ${e} (must be 0-${t.UNPACKED_SIZE-1})`,"offset",e);return this._unpackedCache||(this._unpackedCache=this.unpack()),this._unpackedCache[e]&t.MASK_7BIT}setParameter(e,i){if(e<0||e>=t.PACKED_SIZE)throw new d(`Parameter offset out of range: ${e} (must be 0-${t.PACKED_SIZE-1})`,"offset",e);this.data[e]=i&t.MASK_7BIT,this._unpackedCache=null,e>=t.PACKED_NAME_START&&e<t.PACKED_NAME_START+t.NAME_LENGTH&&(this.name=this._extractName())}unpack(){const e=this.data,i=new Uint8Array(t.UNPACKED_SIZE);return this._unpackOperators(e,i),this._unpackPitchEG(e,i),this._unpackGlobalParams(e,i),this._unpackName(e,i),i}_unpackOperators(e,i){for(let n=0;n<t.NUM_OPERATORS;n++){const s=(t.NUM_OPERATORS-1-n)*t.PACKED_OP_SIZE,r=n*t.UNPACKED_OP_SIZE;this._unpackOperator(e,i,s,r)}}_unpackOperator(e,i,n,s){this._unpackOperatorEG(e,i,n,s),this._unpackOperatorScaling(e,i,n,s),this._unpackOperatorPackedParams(e,i,n,s),this._unpackOperatorFrequency(e,i,n,s)}_unpackOperatorEG(e,i,n,s){i[s+t.UNPACKED_OP_EG_RATE_1]=e[n+t.PACKED_OP_EG_RATE_1]&t.MASK_7BIT,i[s+t.UNPACKED_OP_EG_RATE_2]=e[n+t.PACKED_OP_EG_RATE_2]&t.MASK_7BIT,i[s+t.UNPACKED_OP_EG_RATE_3]=e[n+t.PACKED_OP_EG_RATE_3]&t.MASK_7BIT,i[s+t.UNPACKED_OP_EG_RATE_4]=e[n+t.PACKED_OP_EG_RATE_4]&t.MASK_7BIT,i[s+t.UNPACKED_OP_EG_LEVEL_1]=e[n+t.PACKED_OP_EG_LEVEL_1]&t.MASK_7BIT,i[s+t.UNPACKED_OP_EG_LEVEL_2]=e[n+t.PACKED_OP_EG_LEVEL_2]&t.MASK_7BIT,i[s+t.UNPACKED_OP_EG_LEVEL_3]=e[n+t.PACKED_OP_EG_LEVEL_3]&t.MASK_7BIT,i[s+t.UNPACKED_OP_EG_LEVEL_4]=e[n+t.PACKED_OP_EG_LEVEL_4]&t.MASK_7BIT}_unpackOperatorScaling(e,i,n,s){i[s+t.UNPACKED_OP_BREAK_POINT]=e[n+t.PACKED_OP_BREAK_POINT]&t.MASK_7BIT,i[s+t.UNPACKED_OP_L_SCALE_DEPTH]=e[n+t.PACKED_OP_L_SCALE_DEPTH]&t.MASK_7BIT,i[s+t.UNPACKED_OP_R_SCALE_DEPTH]=e[n+t.PACKED_OP_R_SCALE_DEPTH]&t.MASK_7BIT}_unpackOperatorPackedParams(e,i,n,s){const r=e[n+t.PACKED_OP_CURVES]&t.MASK_7BIT;i[s+t.UNPACKED_OP_L_CURVE]=r&t.MASK_2BIT,i[s+t.UNPACKED_OP_R_CURVE]=r>>2&t.MASK_2BIT;const E=e[n+t.PACKED_OP_RATE_SCALING]&t.MASK_7BIT;i[s+t.UNPACKED_OP_RATE_SCALING]=E&t.MASK_3BIT,i[s+t.UNPACKED_OP_DETUNE]=E>>3&t.MASK_4BIT;const o=e[n+t.PACKED_OP_MOD_SENS]&t.MASK_7BIT;i[s+t.UNPACKED_OP_AMP_MOD_SENS]=o&t.MASK_2BIT,i[s+t.UNPACKED_OP_KEY_VEL_SENS]=o>>2&t.MASK_3BIT,i[s+t.UNPACKED_OP_OUTPUT_LEVEL]=e[n+t.PACKED_OP_OUTPUT_LEVEL]&t.MASK_7BIT}_unpackOperatorFrequency(e,i,n,s){const r=e[n+t.PACKED_OP_MODE_FREQ]&t.MASK_7BIT;i[s+t.UNPACKED_OP_MODE]=r&t.MASK_1BIT,i[s+t.UNPACKED_OP_FREQ_COARSE]=r>>1&t.MASK_5BIT;const E=e[n+t.PACKED_OP_DETUNE_FINE]&t.MASK_7BIT;i[s+t.UNPACKED_OP_OSC_DETUNE]=E&t.MASK_3BIT,i[s+t.UNPACKED_OP_FREQ_FINE]=E>>3&t.MASK_4BIT}_unpackPitchEG(e,i){i[t.UNPACKED_PITCH_EG_RATE_1]=e[t.PACKED_PITCH_EG_RATE_1]&t.MASK_7BIT,i[t.UNPACKED_PITCH_EG_RATE_2]=e[t.PACKED_PITCH_EG_RATE_2]&t.MASK_7BIT,i[t.UNPACKED_PITCH_EG_RATE_3]=e[t.PACKED_PITCH_EG_RATE_3]&t.MASK_7BIT,i[t.UNPACKED_PITCH_EG_RATE_4]=e[t.PACKED_PITCH_EG_RATE_4]&t.MASK_7BIT,i[t.UNPACKED_PITCH_EG_LEVEL_1]=e[t.PACKED_PITCH_EG_LEVEL_1]&t.MASK_7BIT,i[t.UNPACKED_PITCH_EG_LEVEL_2]=e[t.PACKED_PITCH_EG_LEVEL_2]&t.MASK_7BIT,i[t.UNPACKED_PITCH_EG_LEVEL_3]=e[t.PACKED_PITCH_EG_LEVEL_3]&t.MASK_7BIT,i[t.UNPACKED_PITCH_EG_LEVEL_4]=e[t.PACKED_PITCH_EG_LEVEL_4]&t.MASK_7BIT}_unpackGlobalParams(e,i){i[t.UNPACKED_ALGORITHM]=e[t.OFFSET_ALGORITHM]&t.MASK_5BIT;const n=e[t.OFFSET_FEEDBACK]&t.MASK_7BIT;i[t.UNPACKED_FEEDBACK]=n&t.MASK_3BIT,i[t.UNPACKED_OSC_SYNC]=n>>3&t.MASK_1BIT,i[t.UNPACKED_LFO_SPEED]=e[t.OFFSET_LFO_SPEED]&t.MASK_7BIT,i[t.UNPACKED_LFO_DELAY]=e[t.OFFSET_LFO_DELAY]&t.MASK_7BIT,i[t.UNPACKED_LFO_PM_DEPTH]=e[t.OFFSET_LFO_PM_DEPTH]&t.MASK_7BIT,i[t.UNPACKED_LFO_AM_DEPTH]=e[t.OFFSET_LFO_AM_DEPTH]&t.MASK_7BIT;const s=e[t.OFFSET_LFO_SYNC_WAVE]&t.MASK_7BIT;i[t.UNPACKED_LFO_KEY_SYNC]=s&t.MASK_1BIT,i[t.UNPACKED_LFO_WAVE]=s>>1&t.MASK_3BIT,i[t.UNPACKED_LFO_PM_SENS]=s>>4&t.MASK_3BIT,i[t.UNPACKED_AMP_MOD_SENS]=e[t.OFFSET_AMP_MOD_SENS]&t.MASK_7BIT,i[t.UNPACKED_TRANSPOSE]=e[t.OFFSET_TRANSPOSE]&t.MASK_7BIT,i[t.UNPACKED_EG_BIAS_SENS]=e[t.OFFSET_EG_BIAS_SENS]&t.MASK_7BIT}_unpackName(e,i){for(let n=0;n<t.NAME_LENGTH;n++)i[t.UNPACKED_NAME_START+n]=e[t.PACKED_NAME_START+n]&t.MASK_7BIT}static pack(e){if(e.length!==t.UNPACKED_SIZE)throw new d(`Invalid unpacked data length: expected ${t.UNPACKED_SIZE} bytes, got ${e.length}`,"length",e.length);const i=new Uint8Array(t.PACKED_SIZE);return t._packOperators(e,i),t._packPitchEG(e,i),t._packGlobalParams(e,i),t._packName(e,i),i}static _packOperators(e,i){for(let n=0;n<t.NUM_OPERATORS;n++){const s=n*t.UNPACKED_OP_SIZE,r=(t.NUM_OPERATORS-1-n)*t.PACKED_OP_SIZE;t._packOperator(e,i,s,r)}}static _packOperator(e,i,n,s){t._packOperatorEG(e,i,n,s),t._packOperatorScaling(e,i,n,s),t._packOperatorPackedParams(e,i,n,s),t._packOperatorFrequency(e,i,n,s)}static _packOperatorEG(e,i,n,s){i[s+t.PACKED_OP_EG_RATE_1]=e[n+t.UNPACKED_OP_EG_RATE_1],i[s+t.PACKED_OP_EG_RATE_2]=e[n+t.UNPACKED_OP_EG_RATE_2],i[s+t.PACKED_OP_EG_RATE_3]=e[n+t.UNPACKED_OP_EG_RATE_3],i[s+t.PACKED_OP_EG_RATE_4]=e[n+t.UNPACKED_OP_EG_RATE_4],i[s+t.PACKED_OP_EG_LEVEL_1]=e[n+t.UNPACKED_OP_EG_LEVEL_1],i[s+t.PACKED_OP_EG_LEVEL_2]=e[n+t.UNPACKED_OP_EG_LEVEL_2],i[s+t.PACKED_OP_EG_LEVEL_3]=e[n+t.UNPACKED_OP_EG_LEVEL_3],i[s+t.PACKED_OP_EG_LEVEL_4]=e[n+t.UNPACKED_OP_EG_LEVEL_4]}static _packOperatorScaling(e,i,n,s){i[s+t.PACKED_OP_BREAK_POINT]=e[n+t.UNPACKED_OP_BREAK_POINT],i[s+t.PACKED_OP_L_SCALE_DEPTH]=e[n+t.UNPACKED_OP_L_SCALE_DEPTH],i[s+t.PACKED_OP_R_SCALE_DEPTH]=e[n+t.UNPACKED_OP_R_SCALE_DEPTH]}static _packOperatorPackedParams(e,i,n,s){const r=e[n+t.UNPACKED_OP_L_CURVE]&t.MASK_2BIT,E=e[n+t.UNPACKED_OP_R_CURVE]&t.MASK_2BIT;i[s+t.PACKED_OP_CURVES]=r|E<<2;const o=e[n+t.UNPACKED_OP_RATE_SCALING]&t.MASK_3BIT,h=e[n+t.UNPACKED_OP_DETUNE]&t.MASK_4BIT;i[s+t.PACKED_OP_RATE_SCALING]=o|h<<3;const N=e[n+t.UNPACKED_OP_AMP_MOD_SENS]&t.MASK_2BIT,u=e[n+t.UNPACKED_OP_KEY_VEL_SENS]&t.MASK_3BIT;i[s+t.PACKED_OP_MOD_SENS]=N|u<<2,i[s+t.PACKED_OP_OUTPUT_LEVEL]=e[n+t.UNPACKED_OP_OUTPUT_LEVEL]}static _packOperatorFrequency(e,i,n,s){const r=e[n+t.UNPACKED_OP_MODE]&t.MASK_1BIT,E=e[n+t.UNPACKED_OP_FREQ_COARSE]&t.MASK_5BIT;i[s+t.PACKED_OP_MODE_FREQ]=r|E<<1;const o=e[n+t.UNPACKED_OP_OSC_DETUNE]&t.MASK_3BIT,h=e[n+t.UNPACKED_OP_FREQ_FINE]&t.MASK_4BIT;i[s+t.PACKED_OP_DETUNE_FINE]=o|h<<3}static _packPitchEG(e,i){i[t.PACKED_PITCH_EG_RATE_1]=e[t.UNPACKED_PITCH_EG_RATE_1],i[t.PACKED_PITCH_EG_RATE_2]=e[t.UNPACKED_PITCH_EG_RATE_2],i[t.PACKED_PITCH_EG_RATE_3]=e[t.UNPACKED_PITCH_EG_RATE_3],i[t.PACKED_PITCH_EG_RATE_4]=e[t.UNPACKED_PITCH_EG_RATE_4],i[t.PACKED_PITCH_EG_LEVEL_1]=e[t.UNPACKED_PITCH_EG_LEVEL_1],i[t.PACKED_PITCH_EG_LEVEL_2]=e[t.UNPACKED_PITCH_EG_LEVEL_2],i[t.PACKED_PITCH_EG_LEVEL_3]=e[t.UNPACKED_PITCH_EG_LEVEL_3],i[t.PACKED_PITCH_EG_LEVEL_4]=e[t.UNPACKED_PITCH_EG_LEVEL_4]}static _packGlobalParams(e,i){i[t.OFFSET_ALGORITHM]=e[t.UNPACKED_ALGORITHM];const n=e[t.UNPACKED_FEEDBACK]&t.MASK_3BIT,s=e[t.UNPACKED_OSC_SYNC]&t.MASK_1BIT;i[t.OFFSET_FEEDBACK]=n|s<<3,i[t.OFFSET_LFO_SPEED]=e[t.UNPACKED_LFO_SPEED],i[t.OFFSET_LFO_DELAY]=e[t.UNPACKED_LFO_DELAY],i[t.OFFSET_LFO_PM_DEPTH]=e[t.UNPACKED_LFO_PM_DEPTH],i[t.OFFSET_LFO_AM_DEPTH]=e[t.UNPACKED_LFO_AM_DEPTH];const r=e[t.UNPACKED_LFO_KEY_SYNC]&t.MASK_1BIT,E=e[t.UNPACKED_LFO_WAVE]&t.MASK_3BIT,o=e[t.UNPACKED_LFO_PM_SENS]&t.MASK_3BIT;i[t.OFFSET_LFO_SYNC_WAVE]=r|E<<1|o<<4,i[t.OFFSET_AMP_MOD_SENS]=e[t.UNPACKED_AMP_MOD_SENS],i[t.OFFSET_TRANSPOSE]=e[t.UNPACKED_TRANSPOSE],i[t.OFFSET_EG_BIAS_SENS]=e[t.UNPACKED_EG_BIAS_SENS]}static _packName(e,i){for(let n=0;n<t.NAME_LENGTH;n++)i[t.PACKED_NAME_START+n]=e[t.UNPACKED_NAME_START+n]}static createDefault(e=0){const i=new Uint8Array(t.UNPACKED_SIZE);for(let r=0;r<t.NUM_OPERATORS;r++){const E=r*t.UNPACKED_OP_SIZE;i[E+t.UNPACKED_OP_EG_RATE_1]=t.DEFAULT_EG_RATE,i[E+t.UNPACKED_OP_EG_RATE_2]=t.DEFAULT_EG_RATE,i[E+t.UNPACKED_OP_EG_RATE_3]=t.DEFAULT_EG_RATE,i[E+t.UNPACKED_OP_EG_RATE_4]=t.DEFAULT_EG_RATE,i[E+t.UNPACKED_OP_EG_LEVEL_1]=t.DEFAULT_EG_LEVEL_MAX,i[E+t.UNPACKED_OP_EG_LEVEL_2]=t.DEFAULT_EG_LEVEL_MAX,i[E+t.UNPACKED_OP_EG_LEVEL_3]=t.DEFAULT_EG_LEVEL_MAX,i[E+t.UNPACKED_OP_EG_LEVEL_4]=t.DEFAULT_EG_LEVEL_MIN,i[E+t.UNPACKED_OP_BREAK_POINT]=t.DEFAULT_BREAK_POINT,i[E+t.UNPACKED_OP_L_SCALE_DEPTH]=0,i[E+t.UNPACKED_OP_R_SCALE_DEPTH]=0,i[E+t.UNPACKED_OP_L_CURVE]=0,i[E+t.UNPACKED_OP_R_CURVE]=0,i[E+t.UNPACKED_OP_RATE_SCALING]=0,i[E+t.UNPACKED_OP_DETUNE]=t.DEFAULT_DETUNE,i[E+t.UNPACKED_OP_AMP_MOD_SENS]=0,i[E+t.UNPACKED_OP_KEY_VEL_SENS]=0,i[E+t.UNPACKED_OP_OUTPUT_LEVEL]=t.DEFAULT_OUTPUT_LEVEL,i[E+t.UNPACKED_OP_MODE]=0,i[E+t.UNPACKED_OP_FREQ_COARSE]=t.DEFAULT_FREQ_COARSE,i[E+t.UNPACKED_OP_OSC_DETUNE]=0,i[E+t.UNPACKED_OP_FREQ_FINE]=0}i[t.UNPACKED_PITCH_EG_RATE_1]=t.DEFAULT_EG_RATE,i[t.UNPACKED_PITCH_EG_RATE_2]=t.DEFAULT_EG_RATE,i[t.UNPACKED_PITCH_EG_RATE_3]=t.DEFAULT_EG_RATE,i[t.UNPACKED_PITCH_EG_RATE_4]=t.DEFAULT_EG_RATE,i[t.UNPACKED_PITCH_EG_LEVEL_1]=t.DEFAULT_PITCH_EG_LEVEL,i[t.UNPACKED_PITCH_EG_LEVEL_2]=t.DEFAULT_PITCH_EG_LEVEL,i[t.UNPACKED_PITCH_EG_LEVEL_3]=t.DEFAULT_PITCH_EG_LEVEL,i[t.UNPACKED_PITCH_EG_LEVEL_4]=t.DEFAULT_PITCH_EG_LEVEL,i[t.UNPACKED_ALGORITHM]=t.DEFAULT_ALGORITHM,i[t.UNPACKED_FEEDBACK]=t.DEFAULT_FEEDBACK,i[t.UNPACKED_OSC_SYNC]=t.DEFAULT_OSC_SYNC,i[t.UNPACKED_LFO_SPEED]=t.DEFAULT_LFO_SPEED,i[t.UNPACKED_LFO_DELAY]=0,i[t.UNPACKED_LFO_PM_DEPTH]=0,i[t.UNPACKED_LFO_AM_DEPTH]=0,i[t.UNPACKED_LFO_KEY_SYNC]=t.DEFAULT_LFO_KEY_SYNC,i[t.UNPACKED_LFO_WAVE]=0,i[t.UNPACKED_LFO_PM_SENS]=t.DEFAULT_LFO_PM_SENS,i[t.UNPACKED_AMP_MOD_SENS]=0,i[t.UNPACKED_TRANSPOSE]=t.TRANSPOSE_CENTER,i[t.UNPACKED_EG_BIAS_SENS]=0;const n="Init Voice";for(let r=0;r<t.NAME_LENGTH;r++)i[t.UNPACKED_NAME_START+r]=r<n.length?n.charCodeAt(r):t.CHAR_SPACE;const s=t.pack(i);return new t(s,e)}static fromUnpacked(e,i=0){const n=t.pack(e);return new t(n,i)}static async fromFile(e){return new Promise((i,n)=>{const s=new FileReader;s.onload=r=>{try{const E=new Uint8Array(r.target.result);if(E[0]!==t.VCED_SYSEX_START||E[1]!==t.VCED_YAMAHA_ID||E[2]!==t.VCED_SUB_STATUS||E[3]!==t.VCED_FORMAT_SINGLE||E[4]!==t.VCED_BYTE_COUNT_MSB||E[5]!==t.VCED_BYTE_COUNT_LSB)throw new g("Invalid VCED header","header",0);const o=E.subarray(t.VCED_HEADER_SIZE,t.VCED_HEADER_SIZE+t.VCED_DATA_SIZE),h=E[t.VCED_HEADER_SIZE+t.VCED_DATA_SIZE],N=G._calculateChecksum(o,t.VCED_DATA_SIZE);h!==N&&console.warn(`DX7 VCED checksum mismatch (expected ${N.toString(16)}, got ${h.toString(16)}). This is common with vintage SysEx files.`);const u=new Uint8Array(t.UNPACKED_SIZE);let a=0;for(let S=0;S<t.NUM_OPERATORS;S++){const D=(t.NUM_OPERATORS-1-S)*t.UNPACKED_OP_SIZE;u[D+t.UNPACKED_OP_EG_RATE_1]=o[a++],u[D+t.UNPACKED_OP_EG_RATE_2]=o[a++],u[D+t.UNPACKED_OP_EG_RATE_3]=o[a++],u[D+t.UNPACKED_OP_EG_RATE_4]=o[a++],u[D+t.UNPACKED_OP_EG_LEVEL_1]=o[a++],u[D+t.UNPACKED_OP_EG_LEVEL_2]=o[a++],u[D+t.UNPACKED_OP_EG_LEVEL_3]=o[a++],u[D+t.UNPACKED_OP_EG_LEVEL_4]=o[a++],u[D+t.UNPACKED_OP_BREAK_POINT]=o[a++],u[D+t.UNPACKED_OP_L_SCALE_DEPTH]=o[a++],u[D+t.UNPACKED_OP_R_SCALE_DEPTH]=o[a++],u[D+t.UNPACKED_OP_L_CURVE]=o[a++],u[D+t.UNPACKED_OP_R_CURVE]=o[a++],u[D+t.UNPACKED_OP_RATE_SCALING]=o[a++],u[D+t.UNPACKED_OP_DETUNE]=o[a++];const T=o[a++];u[D+t.UNPACKED_OP_AMP_MOD_SENS]=T&t.MASK_2BIT,u[D+t.UNPACKED_OP_KEY_VEL_SENS]=T>>2&t.MASK_3BIT,u[D+t.UNPACKED_OP_OUTPUT_LEVEL]=o[a++],u[D+t.UNPACKED_OP_MODE]=o[a++],u[D+t.UNPACKED_OP_FREQ_COARSE]=o[a++],u[D+t.UNPACKED_OP_FREQ_FINE]=o[a++],u[D+t.UNPACKED_OP_OSC_DETUNE]=o[a++]}u[t.UNPACKED_PITCH_EG_RATE_1]=o[a++],u[t.UNPACKED_PITCH_EG_RATE_2]=o[a++],u[t.UNPACKED_PITCH_EG_RATE_3]=o[a++],u[t.UNPACKED_PITCH_EG_RATE_4]=o[a++],u[t.UNPACKED_PITCH_EG_LEVEL_1]=o[a++],u[t.UNPACKED_PITCH_EG_LEVEL_2]=o[a++],u[t.UNPACKED_PITCH_EG_LEVEL_3]=o[a++],u[t.UNPACKED_PITCH_EG_LEVEL_4]=o[a++],u[t.UNPACKED_ALGORITHM]=o[a++],u[t.UNPACKED_FEEDBACK]=o[a++],u[t.UNPACKED_OSC_SYNC]=o[a++],u[t.UNPACKED_LFO_SPEED]=o[a++],u[t.UNPACKED_LFO_DELAY]=o[a++],u[t.UNPACKED_LFO_PM_DEPTH]=o[a++],u[t.UNPACKED_LFO_AM_DEPTH]=o[a++],u[t.UNPACKED_LFO_KEY_SYNC]=o[a++],u[t.UNPACKED_LFO_WAVE]=o[a++],u[t.UNPACKED_LFO_PM_SENS]=o[a++],u[t.UNPACKED_TRANSPOSE]=o[a++];for(let S=0;S<t.NAME_LENGTH;S++)u[t.UNPACKED_NAME_START+S]=o[a++];const C=t.pack(u);i(new t(C,0))}catch(E){n(E)}},s.onerror=()=>n(new Error("Failed to read file")),s.readAsArrayBuffer(e)})}static fromSysEx(e,i=0){const n=e instanceof Uint8Array?e:new Uint8Array(e);let s;if(n[0]===t.VCED_SYSEX_START){if(n[0]!==t.VCED_SYSEX_START||n[1]!==t.VCED_YAMAHA_ID||n[2]!==t.VCED_SUB_STATUS||n[3]!==t.VCED_FORMAT_SINGLE||n[4]!==t.VCED_BYTE_COUNT_MSB||n[5]!==t.VCED_BYTE_COUNT_LSB)throw new g("Invalid VCED header","header",0);s=n.subarray(t.VCED_HEADER_SIZE,t.VCED_HEADER_SIZE+t.VCED_DATA_SIZE)}else if(n.length===t.PACKED_SIZE)s=n;else throw new d(`Invalid data length: expected ${t.PACKED_SIZE} or ${t.VCED_SIZE} bytes, got ${n.length}`,"length",n.length);if(s.length!==t.VCED_DATA_SIZE&&s.length!==t.PACKED_SIZE)throw new d(`Invalid voice data length: expected ${t.VCED_DATA_SIZE} or ${t.PACKED_SIZE} bytes, got ${s.length}`,"length",s.length);if(s.length===t.VCED_DATA_SIZE){const r=new Uint8Array(t.UNPACKED_SIZE);let E=0;for(let h=0;h<t.NUM_OPERATORS;h++){const N=(t.NUM_OPERATORS-1-h)*t.UNPACKED_OP_SIZE;r[N+t.UNPACKED_OP_EG_RATE_1]=s[E++],r[N+t.UNPACKED_OP_EG_RATE_2]=s[E++],r[N+t.UNPACKED_OP_EG_RATE_3]=s[E++],r[N+t.UNPACKED_OP_EG_RATE_4]=s[E++],r[N+t.UNPACKED_OP_EG_LEVEL_1]=s[E++],r[N+t.UNPACKED_OP_EG_LEVEL_2]=s[E++],r[N+t.UNPACKED_OP_EG_LEVEL_3]=s[E++],r[N+t.UNPACKED_OP_EG_LEVEL_4]=s[E++],r[N+t.UNPACKED_OP_BREAK_POINT]=s[E++],r[N+t.UNPACKED_OP_L_SCALE_DEPTH]=s[E++],r[N+t.UNPACKED_OP_R_SCALE_DEPTH]=s[E++],r[N+t.UNPACKED_OP_L_CURVE]=s[E++],r[N+t.UNPACKED_OP_R_CURVE]=s[E++],r[N+t.UNPACKED_OP_RATE_SCALING]=s[E++],r[N+t.UNPACKED_OP_DETUNE]=s[E++];const u=s[E++];r[N+t.UNPACKED_OP_AMP_MOD_SENS]=u&t.MASK_2BIT,r[N+t.UNPACKED_OP_KEY_VEL_SENS]=u>>2&t.MASK_3BIT,r[N+t.UNPACKED_OP_OUTPUT_LEVEL]=s[E++],r[N+t.UNPACKED_OP_MODE]=s[E++],r[N+t.UNPACKED_OP_FREQ_COARSE]=s[E++],r[N+t.UNPACKED_OP_FREQ_FINE]=s[E++],r[N+t.UNPACKED_OP_OSC_DETUNE]=s[E++]}r[t.UNPACKED_PITCH_EG_RATE_1]=s[E++],r[t.UNPACKED_PITCH_EG_RATE_2]=s[E++],r[t.UNPACKED_PITCH_EG_RATE_3]=s[E++],r[t.UNPACKED_PITCH_EG_RATE_4]=s[E++],r[t.UNPACKED_PITCH_EG_LEVEL_1]=s[E++],r[t.UNPACKED_PITCH_EG_LEVEL_2]=s[E++],r[t.UNPACKED_PITCH_EG_LEVEL_3]=s[E++],r[t.UNPACKED_PITCH_EG_LEVEL_4]=s[E++],r[t.UNPACKED_ALGORITHM]=s[E++],r[t.UNPACKED_FEEDBACK]=s[E++],r[t.UNPACKED_OSC_SYNC]=s[E++],r[t.UNPACKED_LFO_SPEED]=s[E++],r[t.UNPACKED_LFO_DELAY]=s[E++],r[t.UNPACKED_LFO_PM_DEPTH]=s[E++],r[t.UNPACKED_LFO_AM_DEPTH]=s[E++],r[t.UNPACKED_LFO_KEY_SYNC]=s[E++],r[t.UNPACKED_LFO_WAVE]=s[E++],r[t.UNPACKED_LFO_PM_SENS]=s[E++],r[t.UNPACKED_TRANSPOSE]=s[E++];for(let h=0;h<t.NAME_LENGTH;h++)r[t.UNPACKED_NAME_START+h]=s[E++];const o=t.pack(r);return new t(o,i)}return new t(s,i)}static fromJSON(e,i=0){if(!e||typeof e!="object")throw new d("Invalid JSON: expected object","json",e);const n=new Uint8Array(t.UNPACKED_SIZE),s=(a,C,S,D=0,T=127)=>{if(C==null)throw new d(`Missing required parameter: ${S}`,S,C);const f=Number(C);if(Number.isNaN(f))throw new d(`Invalid parameter value for ${S}: ${C}`,S,C);if(f<D||f>T)throw new d(`Parameter ${S} out of range: ${f} (must be ${D}-${T})`,S,f);n[a]=Math.floor(f)},r=a=>{const C={"-LN":0,"-EX":1,"+EX":2,"+LN":3};return C[a]!==void 0?C[a]:0},E=a=>{const C={TRIANGLE:0,"SAW DOWN":1,"SAW UP":2,SQUARE:3,SINE:4,"SAMPLE & HOLD":5};return C[a]!==void 0?C[a]:0},o=a=>{if(!a||typeof a!="string")return 60;const C=a.trim().match(/^([A-G]#?)(-?\d+)$/);if(!C)return 60;const[,S,D]=C,T=parseInt(D,10),U={C:0,"C#":1,D:2,"D#":3,E:4,F:5,"F#":6,G:7,"G#":8,A:9,"A#":10,B:11}[S.toUpperCase()];return U===void 0?60:(T-t.MIDI_OCTAVE_OFFSET)*12+U};if(!Array.isArray(e.operators))throw new d("Invalid operators array: expected array","operators",e.operators);for(let a=0;a<e.operators.length;a++){const C=e.operators[a];if(!C||typeof C!="object")throw new d(`Invalid operator data at index ${a}`,`operators[${a}]`,C);if(!C.eg||!Array.isArray(C.eg.rates)||C.eg.rates.length!==4)throw new d(`Invalid EG rates for operator ${a}`,`operators[${a}].eg.rates`,C.eg?.rates);if(!C.eg||!Array.isArray(C.eg.levels)||C.eg.levels.length!==4)throw new d(`Invalid EG levels for operator ${a}`,`operators[${a}].eg.levels`,C.eg?.levels)}if(e.operators.length!==t.NUM_OPERATORS)throw new d(`Invalid operators array: expected ${t.NUM_OPERATORS} operators`,"operators",e.operators);for(let a=0;a<t.NUM_OPERATORS;a++){const C=e.operators[a],S=a*t.UNPACKED_OP_SIZE;if(!C.eg||!Array.isArray(C.eg.rates)||C.eg.rates.length!==4)throw new d(`Invalid EG rates for operator ${a}`,`operators[${a}].eg.rates`,C.eg?.rates);if(s(S+t.UNPACKED_OP_EG_RATE_1,C.eg.rates[0],`operators[${a}].eg.rates[0]`,0,99),s(S+t.UNPACKED_OP_EG_RATE_2,C.eg.rates[1],`operators[${a}].eg.rates[1]`,0,99),s(S+t.UNPACKED_OP_EG_RATE_3,C.eg.rates[2],`operators[${a}].eg.rates[2]`,0,99),s(S+t.UNPACKED_OP_EG_RATE_4,C.eg.rates[3],`operators[${a}].eg.rates[3]`,0,99),!C.eg||!Array.isArray(C.eg.levels)||C.eg.levels.length!==4)throw new d(`Invalid EG levels for operator ${a}`,`operators[${a}].eg.levels`,C.eg?.levels);s(S+t.UNPACKED_OP_EG_LEVEL_1,C.eg.levels[0],`operators[${a}].eg.levels[0]`,0,99),s(S+t.UNPACKED_OP_EG_LEVEL_2,C.eg.levels[1],`operators[${a}].eg.levels[1]`,0,99),s(S+t.UNPACKED_OP_EG_LEVEL_3,C.eg.levels[2],`operators[${a}].eg.levels[2]`,0,99),s(S+t.UNPACKED_OP_EG_LEVEL_4,C.eg.levels[3],`operators[${a}].eg.levels[3]`,0,99);const D=o(C.key?.breakPoint)-t.MIDI_BREAK_POINT_OFFSET;s(S+t.UNPACKED_OP_BREAK_POINT,D,`operators[${a}].key.breakPoint`,0,127),s(S+t.UNPACKED_OP_L_SCALE_DEPTH,C.scale?.left?.depth||0,`operators[${a}].scale.left.depth`,0,99),s(S+t.UNPACKED_OP_R_SCALE_DEPTH,C.scale?.right?.depth||0,`operators[${a}].scale.right.depth`,0,99),n[S+t.UNPACKED_OP_L_CURVE]=r(C.scale?.left?.curve||"-LN"),n[S+t.UNPACKED_OP_R_CURVE]=r(C.scale?.right?.curve||"-LN"),s(S+t.UNPACKED_OP_RATE_SCALING,C.key?.scaling||0,`operators[${a}].key.scaling`,0,7);const T=Number(C.osc?.detune)||0;s(S+t.UNPACKED_OP_DETUNE,T+7,`operators[${a}].osc.detune`,0,14),s(S+t.UNPACKED_OP_AMP_MOD_SENS,C.output?.ampModSens||0,`operators[${a}].output.ampModSens`,0,3),s(S+t.UNPACKED_OP_OUTPUT_LEVEL,C.output?.level||0,`operators[${a}].output.level`,0,99);const f=C.osc?.freq?.mode?.toUpperCase()==="FIXED"?1:0,U=Number(C.osc?.freq?.coarse)||0,L=Number(C.osc?.freq?.fine)||0;n[S+t.UNPACKED_OP_MODE]=f,s(S+t.UNPACKED_OP_FREQ_COARSE,U,`operators[${a}].osc.freq.coarse`,0,31),s(S+t.UNPACKED_OP_FREQ_FINE,L,`operators[${a}].osc.freq.fine`,0,15),s(S+t.UNPACKED_OP_KEY_VEL_SENS,C.key?.velocity||0,`operators[${a}].key.velocity`,0,7)}if(!e.pitchEG||!Array.isArray(e.pitchEG.rates)||e.pitchEG.rates.length!==4)throw new d("Invalid pitch EG rates","pitchEG.rates",e.pitchEG?.rates);if(!e.pitchEG||!Array.isArray(e.pitchEG.levels)||e.pitchEG.levels.length!==4)throw new d("Invalid pitch EG levels","pitchEG.levels",e.pitchEG?.levels);if(s(t.UNPACKED_PITCH_EG_RATE_1,e.pitchEG.rates[0],"pitchEG.rates[0]",0,99),s(t.UNPACKED_PITCH_EG_RATE_2,e.pitchEG.rates[1],"pitchEG.rates[1]",0,99),s(t.UNPACKED_PITCH_EG_RATE_3,e.pitchEG.rates[2],"pitchEG.rates[2]",0,99),s(t.UNPACKED_PITCH_EG_RATE_4,e.pitchEG.rates[3],"pitchEG.rates[3]",0,99),s(t.UNPACKED_PITCH_EG_LEVEL_1,e.pitchEG.levels[0],"pitchEG.levels[0]",0,99),s(t.UNPACKED_PITCH_EG_LEVEL_2,e.pitchEG.levels[1],"pitchEG.levels[1]",0,99),s(t.UNPACKED_PITCH_EG_LEVEL_3,e.pitchEG.levels[2],"pitchEG.levels[2]",0,99),s(t.UNPACKED_PITCH_EG_LEVEL_4,e.pitchEG.levels[3],"pitchEG.levels[3]",0,99),!e.lfo||typeof e.lfo!="object")throw new d("Invalid LFO data","lfo",e.lfo);if(s(t.UNPACKED_LFO_SPEED,e.lfo.speed,"lfo.speed",0,99),s(t.UNPACKED_LFO_DELAY,e.lfo.delay,"lfo.delay",0,99),s(t.UNPACKED_LFO_PM_DEPTH,e.lfo.pmDepth,"lfo.pmDepth",0,99),s(t.UNPACKED_LFO_AM_DEPTH,e.lfo.amDepth,"lfo.amDepth",0,99),n[t.UNPACKED_LFO_KEY_SYNC]=e.lfo.keySync?1:0,n[t.UNPACKED_LFO_WAVE]=E(e.lfo.wave),!e.global||typeof e.global!="object")throw new d("Invalid global data","global",e.global);const h=Number(e.global.algorithm)||1;s(t.UNPACKED_ALGORITHM,h-1,"global.algorithm",0,31),s(t.UNPACKED_FEEDBACK,e.global.feedback,"global.feedback",0,7),n[t.UNPACKED_OSC_SYNC]=e.global.oscKeySync?1:0,s(t.UNPACKED_LFO_PM_SENS,e.global.pitchModSens,"global.pitchModSens",0,7);const N=Number(e.global.transpose)||0;s(t.UNPACKED_TRANSPOSE,N+t.TRANSPOSE_CENTER,"global.transpose",0,127),s(t.UNPACKED_AMP_MOD_SENS,e.global.ampModSens||0,"global.ampModSens",0,3),s(t.UNPACKED_EG_BIAS_SENS,e.global.egBiasSens||0,"global.egBiasSens",0,7);const u=e.name||"";for(let a=0;a<t.NAME_LENGTH;a++)n[t.UNPACKED_NAME_START+a]=a<u.length?u.charCodeAt(a):t.CHAR_SPACE;return t.fromUnpacked(n,i)}toSysEx(){const e=this.unpack(),i=new Uint8Array(t.VCED_SIZE);let n=0;i[n++]=t.VCED_SYSEX_START,i[n++]=t.VCED_YAMAHA_ID,i[n++]=t.VCED_SUB_STATUS,i[n++]=t.VCED_FORMAT_SINGLE,i[n++]=t.VCED_BYTE_COUNT_MSB,i[n++]=t.VCED_BYTE_COUNT_LSB;for(let r=t.NUM_OPERATORS-1;r>=0;r--){const E=r*t.UNPACKED_OP_SIZE;i[n++]=e[E+t.UNPACKED_OP_EG_RATE_1],i[n++]=e[E+t.UNPACKED_OP_EG_RATE_2],i[n++]=e[E+t.UNPACKED_OP_EG_RATE_3],i[n++]=e[E+t.UNPACKED_OP_EG_RATE_4],i[n++]=e[E+t.UNPACKED_OP_EG_LEVEL_1],i[n++]=e[E+t.UNPACKED_OP_EG_LEVEL_2],i[n++]=e[E+t.UNPACKED_OP_EG_LEVEL_3],i[n++]=e[E+t.UNPACKED_OP_EG_LEVEL_4],i[n++]=e[E+t.UNPACKED_OP_BREAK_POINT],i[n++]=e[E+t.UNPACKED_OP_L_SCALE_DEPTH],i[n++]=e[E+t.UNPACKED_OP_R_SCALE_DEPTH],i[n++]=e[E+t.UNPACKED_OP_L_CURVE],i[n++]=e[E+t.UNPACKED_OP_R_CURVE],i[n++]=e[E+t.UNPACKED_OP_RATE_SCALING],i[n++]=e[E+t.UNPACKED_OP_DETUNE];const o=e[E+t.UNPACKED_OP_AMP_MOD_SENS]&t.MASK_2BIT,h=e[E+t.UNPACKED_OP_KEY_VEL_SENS]&t.MASK_3BIT;i[n++]=o|h<<2,i[n++]=e[E+t.UNPACKED_OP_OUTPUT_LEVEL],i[n++]=e[E+t.UNPACKED_OP_MODE],i[n++]=e[E+t.UNPACKED_OP_FREQ_COARSE],i[n++]=e[E+t.UNPACKED_OP_OSC_DETUNE],i[n++]=e[E+t.UNPACKED_OP_FREQ_FINE]}i[n++]=e[t.UNPACKED_PITCH_EG_RATE_1],i[n++]=e[t.UNPACKED_PITCH_EG_RATE_2],i[n++]=e[t.UNPACKED_PITCH_EG_RATE_3],i[n++]=e[t.UNPACKED_PITCH_EG_RATE_4],i[n++]=e[t.UNPACKED_PITCH_EG_LEVEL_1],i[n++]=e[t.UNPACKED_PITCH_EG_LEVEL_2],i[n++]=e[t.UNPACKED_PITCH_EG_LEVEL_3],i[n++]=e[t.UNPACKED_PITCH_EG_LEVEL_4],i[n++]=e[t.UNPACKED_ALGORITHM],i[n++]=e[t.UNPACKED_FEEDBACK],i[n++]=e[t.UNPACKED_OSC_SYNC],i[n++]=e[t.UNPACKED_LFO_SPEED],i[n++]=e[t.UNPACKED_LFO_DELAY],i[n++]=e[t.UNPACKED_LFO_PM_DEPTH],i[n++]=e[t.UNPACKED_LFO_AM_DEPTH],i[n++]=e[t.UNPACKED_LFO_KEY_SYNC],i[n++]=e[t.UNPACKED_LFO_WAVE],i[n++]=e[t.UNPACKED_LFO_PM_SENS],i[n++]=e[t.UNPACKED_TRANSPOSE];for(let r=0;r<t.NAME_LENGTH;r++)i[n++]=e[t.UNPACKED_NAME_START+r];const s=i.subarray(t.VCED_HEADER_SIZE,t.VCED_HEADER_SIZE+t.VCED_DATA_SIZE);return i[n++]=G._calculateChecksum(s,t.VCED_DATA_SIZE),i[n++]=t.VCED_SYSEX_END,i}toJSON(){const e=this.unpack(),i=[],n=E=>["-LN","-EX","+EX","+LN"][E]||"UNKNOWN",s=E=>["TRIANGLE","SAW DOWN","SAW UP","SQUARE","SINE","SAMPLE & HOLD"][E]||"UNKNOWN",r=E=>{const o=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],h=Math.floor(E/12)+t.MIDI_OCTAVE_OFFSET;return`${o[E%12]}${h}`};for(let E=0;E<t.NUM_OPERATORS;E++){const o=E*t.UNPACKED_OP_SIZE,h=e[o+t.UNPACKED_OP_MODE]===0?"RATIO":"FIXED";i.push({id:E+1,osc:{detune:e[o+t.UNPACKED_OP_OSC_DETUNE],freq:{coarse:e[o+t.UNPACKED_OP_FREQ_COARSE],fine:e[o+t.UNPACKED_OP_FREQ_FINE],mode:h}},eg:{rates:[e[o+t.UNPACKED_OP_EG_RATE_1],e[o+t.UNPACKED_OP_EG_RATE_2],e[o+t.UNPACKED_OP_EG_RATE_3],e[o+t.UNPACKED_OP_EG_RATE_4]],levels:[e[o+t.UNPACKED_OP_EG_LEVEL_1],e[o+t.UNPACKED_OP_EG_LEVEL_2],e[o+t.UNPACKED_OP_EG_LEVEL_3],e[o+t.UNPACKED_OP_EG_LEVEL_4]]},key:{velocity:e[o+t.UNPACKED_OP_KEY_VEL_SENS],scaling:e[o+t.UNPACKED_OP_RATE_SCALING],breakPoint:r(e[o+t.UNPACKED_OP_BREAK_POINT]+t.MIDI_BREAK_POINT_OFFSET)},output:{level:e[o+t.UNPACKED_OP_OUTPUT_LEVEL],ampModSens:e[o+t.UNPACKED_OP_AMP_MOD_SENS]},scale:{left:{depth:e[o+t.UNPACKED_OP_L_SCALE_DEPTH],curve:n(e[o+t.UNPACKED_OP_L_CURVE])},right:{depth:e[o+t.UNPACKED_OP_R_SCALE_DEPTH],curve:n(e[o+t.UNPACKED_OP_R_CURVE])}}})}return{name:this.name||"(Empty)",operators:i,pitchEG:{rates:[e[t.UNPACKED_PITCH_EG_RATE_1],e[t.UNPACKED_PITCH_EG_RATE_2],e[t.UNPACKED_PITCH_EG_RATE_3],e[t.UNPACKED_PITCH_EG_RATE_4]],levels:[e[t.UNPACKED_PITCH_EG_LEVEL_1],e[t.UNPACKED_PITCH_EG_LEVEL_2],e[t.UNPACKED_PITCH_EG_LEVEL_3],e[t.UNPACKED_PITCH_EG_LEVEL_4]]},lfo:{speed:e[t.UNPACKED_LFO_SPEED],delay:e[t.UNPACKED_LFO_DELAY],pmDepth:e[t.UNPACKED_LFO_PM_DEPTH],amDepth:e[t.UNPACKED_LFO_AM_DEPTH],keySync:e[t.UNPACKED_LFO_KEY_SYNC]===1,wave:s(e[t.UNPACKED_LFO_WAVE])},global:{algorithm:e[t.UNPACKED_ALGORITHM]+1,feedback:e[t.UNPACKED_FEEDBACK],oscKeySync:e[t.UNPACKED_OSC_SYNC]===1,pitchModSens:e[t.UNPACKED_LFO_PM_SENS],transpose:e[t.UNPACKED_TRANSPOSE]-t.TRANSPOSE_CENTER}}}};_(t,"PACKED_SIZE",128),_(t,"PACKED_OP_SIZE",17),_(t,"NUM_OPERATORS",6),_(t,"PACKED_OP_EG_RATE_1",0),_(t,"PACKED_OP_EG_RATE_2",1),_(t,"PACKED_OP_EG_RATE_3",2),_(t,"PACKED_OP_EG_RATE_4",3),_(t,"PACKED_OP_EG_LEVEL_1",4),_(t,"PACKED_OP_EG_LEVEL_2",5),_(t,"PACKED_OP_EG_LEVEL_3",6),_(t,"PACKED_OP_EG_LEVEL_4",7),_(t,"PACKED_OP_BREAK_POINT",8),_(t,"PACKED_OP_L_SCALE_DEPTH",9),_(t,"PACKED_OP_R_SCALE_DEPTH",10),_(t,"PACKED_OP_CURVES",11),_(t,"PACKED_OP_RATE_SCALING",12),_(t,"PACKED_OP_MOD_SENS",13),_(t,"PACKED_OP_OUTPUT_LEVEL",14),_(t,"PACKED_OP_MODE_FREQ",15),_(t,"PACKED_OP_DETUNE_FINE",16),_(t,"PACKED_PITCH_EG_RATE_1",102),_(t,"PACKED_PITCH_EG_RATE_2",103),_(t,"PACKED_PITCH_EG_RATE_3",104),_(t,"PACKED_PITCH_EG_RATE_4",105),_(t,"PACKED_PITCH_EG_LEVEL_1",106),_(t,"PACKED_PITCH_EG_LEVEL_2",107),_(t,"PACKED_PITCH_EG_LEVEL_3",108),_(t,"PACKED_PITCH_EG_LEVEL_4",109),_(t,"OFFSET_ALGORITHM",110),_(t,"OFFSET_FEEDBACK",111),_(t,"OFFSET_LFO_SPEED",112),_(t,"OFFSET_LFO_DELAY",113),_(t,"OFFSET_LFO_PM_DEPTH",114),_(t,"OFFSET_LFO_AM_DEPTH",115),_(t,"OFFSET_LFO_SYNC_WAVE",116),_(t,"OFFSET_TRANSPOSE",117),_(t,"OFFSET_AMP_MOD_SENS",118),_(t,"OFFSET_EG_BIAS_SENS",119),_(t,"PACKED_NAME_START",118),_(t,"NAME_LENGTH",10),_(t,"UNPACKED_SIZE",169),_(t,"UNPACKED_OP_SIZE",23),_(t,"UNPACKED_OP_EG_RATE_1",0),_(t,"UNPACKED_OP_EG_RATE_2",1),_(t,"UNPACKED_OP_EG_RATE_3",2),_(t,"UNPACKED_OP_EG_RATE_4",3),_(t,"UNPACKED_OP_EG_LEVEL_1",4),_(t,"UNPACKED_OP_EG_LEVEL_2",5),_(t,"UNPACKED_OP_EG_LEVEL_3",6),_(t,"UNPACKED_OP_EG_LEVEL_4",7),_(t,"UNPACKED_OP_BREAK_POINT",8),_(t,"UNPACKED_OP_L_SCALE_DEPTH",9),_(t,"UNPACKED_OP_R_SCALE_DEPTH",10),_(t,"UNPACKED_OP_L_CURVE",11),_(t,"UNPACKED_OP_R_CURVE",12),_(t,"UNPACKED_OP_RATE_SCALING",13),_(t,"UNPACKED_OP_DETUNE",14),_(t,"UNPACKED_OP_AMP_MOD_SENS",15),_(t,"UNPACKED_OP_OUTPUT_LEVEL",16),_(t,"UNPACKED_OP_MODE",17),_(t,"UNPACKED_OP_KEY_VEL_SENS",18),_(t,"UNPACKED_OP_FREQ_COARSE",19),_(t,"UNPACKED_OP_OSC_DETUNE",20),_(t,"UNPACKED_OP_FREQ_FINE",21),_(t,"UNPACKED_PITCH_EG_RATE_1",138),_(t,"UNPACKED_PITCH_EG_RATE_2",139),_(t,"UNPACKED_PITCH_EG_RATE_3",140),_(t,"UNPACKED_PITCH_EG_RATE_4",141),_(t,"UNPACKED_PITCH_EG_LEVEL_1",142),_(t,"UNPACKED_PITCH_EG_LEVEL_2",143),_(t,"UNPACKED_PITCH_EG_LEVEL_3",144),_(t,"UNPACKED_PITCH_EG_LEVEL_4",145),_(t,"UNPACKED_ALGORITHM",146),_(t,"UNPACKED_FEEDBACK",147),_(t,"UNPACKED_OSC_SYNC",148),_(t,"UNPACKED_LFO_SPEED",149),_(t,"UNPACKED_LFO_DELAY",150),_(t,"UNPACKED_LFO_PM_DEPTH",151),_(t,"UNPACKED_LFO_AM_DEPTH",152),_(t,"UNPACKED_LFO_KEY_SYNC",153),_(t,"UNPACKED_LFO_WAVE",154),_(t,"UNPACKED_LFO_PM_SENS",155),_(t,"UNPACKED_AMP_MOD_SENS",156),_(t,"UNPACKED_TRANSPOSE",157),_(t,"UNPACKED_EG_BIAS_SENS",158),_(t,"UNPACKED_NAME_START",159),_(t,"VCED_SIZE",163),_(t,"VCED_HEADER_SIZE",6),_(t,"VCED_DATA_SIZE",155),_(t,"VCED_SYSEX_START",240),_(t,"VCED_YAMAHA_ID",67),_(t,"VCED_SUB_STATUS",0),_(t,"VCED_FORMAT_SINGLE",0),_(t,"VCED_BYTE_COUNT_MSB",1),_(t,"VCED_BYTE_COUNT_LSB",27),_(t,"VCED_SYSEX_END",247),_(t,"MASK_7BIT",127),_(t,"MASK_2BIT",3),_(t,"MASK_3BIT",7),_(t,"MASK_4BIT",15),_(t,"MASK_5BIT",31),_(t,"MASK_1BIT",1),_(t,"TRANSPOSE_CENTER",24),_(t,"CHAR_YEN",92),_(t,"CHAR_ARROW_RIGHT",126),_(t,"CHAR_ARROW_LEFT",127),_(t,"CHAR_REPLACEMENT_Y",89),_(t,"CHAR_REPLACEMENT_GT",62),_(t,"CHAR_REPLACEMENT_LT",60),_(t,"CHAR_SPACE",32),_(t,"CHAR_MIN_PRINTABLE",32),_(t,"CHAR_MAX_PRINTABLE",126),_(t,"DEFAULT_EG_RATE",99),_(t,"DEFAULT_EG_LEVEL_MAX",99),_(t,"DEFAULT_EG_LEVEL_MIN",0),_(t,"DEFAULT_BREAK_POINT",0),_(t,"DEFAULT_OUTPUT_LEVEL",99),_(t,"DEFAULT_PITCH_EG_LEVEL",50),_(t,"DEFAULT_LFO_SPEED",35),_(t,"DEFAULT_LFO_PM_SENS",3),_(t,"DEFAULT_DETUNE",7),_(t,"DEFAULT_FREQ_COARSE",1),_(t,"DEFAULT_ALGORITHM",0),_(t,"DEFAULT_FEEDBACK",0),_(t,"DEFAULT_OSC_SYNC",1),_(t,"DEFAULT_LFO_KEY_SYNC",1),_(t,"MIDI_OCTAVE_OFFSET",-2),_(t,"MIDI_BREAK_POINT_OFFSET",21);let m=t;const P=class P{constructor(e,i=""){if(this.voices=new Array(P.NUM_VOICES),this.name=i,e)this._load(e);else for(let n=0;n<P.NUM_VOICES;n++)this.voices[n]=m.createDefault(n)}static _calculateChecksum(e,i){let n=0;for(let s=0;s<i;s++)n+=e[s];return P.CHECKSUM_MODULO-n%P.CHECKSUM_MODULO&P.MASK_7BIT}_load(e){const i=e instanceof Uint8Array?e:new Uint8Array(e);let n,s=0;if(i[0]===P.SYSEX_START){const E=i.subarray(0,P.SYSEX_HEADER_SIZE),o=P.SYSEX_HEADER;for(let h=0;h<P.SYSEX_HEADER_SIZE;h++)if(E[h]!==o[h])throw new g(`Invalid SysEx header at position ${h}: expected ${o[h].toString(16)}, got ${E[h].toString(16)}`,"header",h);n=i.subarray(P.SYSEX_HEADER_SIZE,P.SYSEX_HEADER_SIZE+P.VOICE_DATA_SIZE),s=P.SYSEX_HEADER_SIZE}else if(i.length===P.VOICE_DATA_SIZE)n=i;else throw new d(`Invalid data length: expected ${P.VOICE_DATA_SIZE} or ${P.SYSEX_SIZE} bytes, got ${i.length}`,"length",i.length);if(n.length!==P.VOICE_DATA_SIZE)throw new d(`Invalid voice data length: expected ${P.VOICE_DATA_SIZE} bytes, got ${n.length}`,"length",n.length);const r=P.SYSEX_HEADER_SIZE+P.VOICE_DATA_SIZE;if(s>0&&i.length>=r+1){const E=i[r],o=P._calculateChecksum(n,P.VOICE_DATA_SIZE);E!==o&&console.warn(`DX7 checksum mismatch (expected ${o.toString(16)}, got ${E.toString(16)}). This is common with vintage SysEx files and the data is likely still valid.`)}this.voices=new Array(P.NUM_VOICES);for(let E=0;E<P.NUM_VOICES;E++){const o=E*P.VOICE_SIZE,h=n.subarray(o,o+P.VOICE_SIZE);this.voices[E]=new m(h,E)}}replaceVoice(e,i){if(e<0||e>=P.NUM_VOICES)throw new d(`Invalid voice index: ${e}`,"index",e);const n=new Uint8Array(i.data);this.voices[e]=new m(n,e)}addVoice(e){for(let i=0;i<this.voices.length;i++){const n=this.voices[i];if(n.name===""||n.name==="Init Voice")return this.replaceVoice(i,e),i}return-1}getVoices(){return this.voices}getVoice(e){return e<0||e>=this.voices.length?null:this.voices[e]}getVoiceNames(){return this.voices.map(e=>e.name)}findVoiceByName(e){const i=e.toLowerCase();return this.voices.find(n=>n.name.toLowerCase().includes(i))||null}static async fromFile(e){return new Promise((i,n)=>{const s=new FileReader;s.onload=async r=>{try{const E=e.name||"",o=new Uint8Array(r.target.result);if(o[0]===P.SYSEX_START&&o[3]===m.VCED_FORMAT_SINGLE)n(new g("This is a single voice file. Use DX7Voice.fromFile() instead.","format",3));else{const h=E.replace(/\.[^/.]+$/,""),N=new P(r.target.result,h);i(N)}}catch(E){n(E)}},s.onerror=()=>n(new Error("Failed to read file")),s.readAsArrayBuffer(e)})}static fromSysEx(e,i=""){return new P(e,i)}static fromJSON(e){if(!e||typeof e!="object")throw new d("Invalid JSON: expected object","json",e);const i=new P;if(i.name=e.name||"",!Array.isArray(e.voices))throw new d("Invalid voices array","voices",e.voices);e.voices.length!==P.NUM_VOICES&&console.warn(`Bank JSON has ${e.voices.length} voices, expected ${P.NUM_VOICES}. Missing voices will be filled with defaults.`);const n=Math.min(e.voices.length,P.NUM_VOICES);for(let s=0;s<n;s++){const r=e.voices[s];if(!r||typeof r!="object"){console.warn(`Invalid voice data at index ${s}, using default voice`);continue}try{const{index:E,...o}=r,h=m.fromJSON(o,s);i.replaceVoice(s,h)}catch(E){console.warn(`Failed to load voice at index ${s}: ${E.message}, using default voice`)}}return i}toSysEx(){const e=new Uint8Array(P.SYSEX_SIZE);let i=0;P.SYSEX_HEADER.forEach(s=>{e[i++]=s});for(const s of this.voices)for(let r=0;r<P.VOICE_SIZE;r++)e[i++]=s.data[r];const n=e.subarray(P.SYSEX_HEADER_SIZE,P.SYSEX_HEADER_SIZE+P.VOICE_DATA_SIZE);return e[i++]=P._calculateChecksum(n,P.VOICE_DATA_SIZE),e[i++]=P.SYSEX_END,e}toJSON(){const e=this.voices.map((i,n)=>{const s=i.toJSON();return{index:n+1,...s}});return{version:"1.0",name:this.name||"",voices:e}}};_(P,"SYSEX_START",240),_(P,"SYSEX_END",247),_(P,"SYSEX_YAMAHA_ID",67),_(P,"SYSEX_SUB_STATUS",0),_(P,"SYSEX_FORMAT_32_VOICES",9),_(P,"SYSEX_BYTE_COUNT_MSB",32),_(P,"SYSEX_BYTE_COUNT_LSB",0),_(P,"SYSEX_HEADER",[P.SYSEX_START,P.SYSEX_YAMAHA_ID,P.SYSEX_SUB_STATUS,P.SYSEX_FORMAT_32_VOICES,P.SYSEX_BYTE_COUNT_MSB,P.SYSEX_BYTE_COUNT_LSB]),_(P,"SYSEX_HEADER_SIZE",6),_(P,"VOICE_DATA_SIZE",4096),_(P,"SYSEX_SIZE",4104),_(P,"VOICE_SIZE",128),_(P,"NUM_VOICES",32),_(P,"CHECKSUM_MODULO",128),_(P,"MASK_7BIT",127);let G=P;function nt(c){return c[0]!==240||c[c.length-1]!==247?null:{manufacturerId:c[1],payload:c.slice(2,-1),raw:c}}function st(c,e){return[240,c,...e,247]}function Et(c){return c.length>=2&&c[0]===240&&c[c.length-1]===247}function rt(c){const e=[];for(let i=0;i<c.length;i+=7){const n=c.slice(i,i+7);let s=0;const r=[];for(let E=0;E<n.length;E++){const o=n[E];o&128&&(s|=1<<E),r.push(o&127)}e.push(s,...r)}return e}function ot(c){const e=[];for(let i=0;i<c.length;i+=8){const n=c[i],s=Math.min(7,c.length-i-1);for(let r=0;r<s;r++){let E=c[i+1+r];n&1<<r&&(E|=128),e.push(E)}}return e}function _t(c){return Number.isInteger(c)&&c>=1&&c<=16}function at(c){return Number.isInteger(c)&&c>=0&&c<=127}function ct(c){return Number.isInteger(c)&&c>=0&&c<=31}function Ct(c){return Number.isInteger(c)&&c>=0&&c<=127}function At(c){return Number.isInteger(c)&&c>=0&&c<=127}function Pt(c){return Number.isInteger(c)&&c>=0&&c<=127}function ut(c){return Number.isInteger(c)&&c>=0&&c<=127}function lt(c){return Number.isInteger(c)&&c>=0&&c<=16383}function ht(c,e){return Number.isInteger(c)&&c>=0&&c<=127&&Number.isInteger(e)&&e>=0&&e<=127}async function St(c={}){const{onStatusUpdate:e,onConnectionUpdate:i,inputChannel:n=1,outputChannel:s=1,output:r,sysex:E,onReady:o,onError:h,selector:N,watchDOM:u,...a}=c,S=await q({autoConnect:!1,sysex:E,inputChannel:n,outputChannel:s,selector:N||"[data-midi-cc]",watchDOM:u,onError:h,...a}),D=new z({midiController:S,onStatusUpdate:e||(()=>{}),onConnectionUpdate:i||(()=>{}),channel:s});if(r)try{await S.device.connectOutput(r),D.currentOutput=S.device.getCurrentOutput(),D.updateConnectionStatus()}catch(T){h?h(T):console.error("Failed to connect to MIDI device:",T.message)}return o&&o(S,D),D}async function q(c={}){const e=new Z(c);await e.init();const i=c.selector||"[data-midi-cc]";{const n=new I(e,i);n.bindAll(),c.watchDOM&&n.enableAutoBinding(),e._binder=n}return e}l.CONN=p,l.CONNECTION_EVENTS=p,l.CONTROLLER_EVENTS=A,l.CTRL=A,l.DX7Bank=G,l.DX7Error=H,l.DX7ParseError=g,l.DX7ValidationError=d,l.DX7Voice=m,l.DataAttributeBinder=I,l.EventEmitter=v,l.MIDIAccessError=F,l.MIDIConnection=$,l.MIDIConnectionError=y,l.MIDIController=Z,l.MIDIDeviceError=R,l.MIDIDeviceManager=z,l.MIDIError=K,l.MIDIValidationError=M,l.clamp=O,l.createMIDIController=q,l.createMIDIDeviceManager=St,l.createSysEx=st,l.decode14BitValue=Y,l.decode7Bit=ot,l.denormalize14BitValue=it,l.denormalizeValue=J,l.encode14BitValue=B,l.encode7Bit=rt,l.frequencyToNote=X,l.getCCName=et,l.isSysEx=Et,l.isValid14BitCC=ct,l.isValidCC=at,l.isValidChannel=_t,l.isValidMIDIValue=Ct,l.isValidNote=At,l.isValidPitchBend=lt,l.isValidPitchBendBytes=ht,l.isValidProgramChange=ut,l.isValidVelocity=Pt,l.normalize14BitValue=x,l.normalizeValue=w,l.noteNameToNumber=k,l.noteNumberToName=j,l.noteToFrequency=tt,l.parseSysEx=nt,Object.defineProperty(l,Symbol.toStringTag,{value:"Module"})}));
|
|
@@ -209,6 +209,8 @@ export class DX7Voice {
|
|
|
209
209
|
static DEFAULT_FREQ_COARSE: number;
|
|
210
210
|
static DEFAULT_ALGORITHM: number;
|
|
211
211
|
static DEFAULT_FEEDBACK: number;
|
|
212
|
+
static DEFAULT_OSC_SYNC: number;
|
|
213
|
+
static DEFAULT_LFO_KEY_SYNC: number;
|
|
212
214
|
static MIDI_OCTAVE_OFFSET: number;
|
|
213
215
|
static MIDI_BREAK_POINT_OFFSET: number;
|
|
214
216
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DX7Voice.d.ts","sourceRoot":"","sources":["../../../src/utils/dx7/DX7Voice.js"],"names":[],"mappings":"AAwBA;;;;;;;;;;GAUG;AAEH;;;;;;;;;GASG;AAEH;;;;;;;;;;GAUG;AAEH;;;;;;GAMG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH;IAGE,2BAAwB;IACxB,8BAA0B;IAC1B,6BAAwB;IAGxB,mCAA8B;IAC9B,mCAA8B;IAC9B,mCAA8B;IAC9B,mCAA8B;IAC9B,oCAA+B;IAC/B,oCAA+B;IAC/B,oCAA+B;IAC/B,oCAA+B;IAC/B,qCAAgC;IAChC,uCAAkC;IAClC,uCAAmC;IACnC,gCAA4B;IAC5B,sCAAkC;IAClC,kCAA8B;IAC9B,sCAAkC;IAClC,mCAA+B;IAC/B,qCAAiC;IAGjC,sCAAmC;IACnC,sCAAmC;IACnC,sCAAmC;IACnC,sCAAmC;IACnC,uCAAoC;IACpC,uCAAoC;IACpC,uCAAoC;IACpC,uCAAoC;IACpC,gCAA6B;IAC7B,+BAA4B;IAC5B,gCAA6B;IAC7B,gCAA6B;IAC7B,mCAAgC;IAChC,mCAAgC;IAChC,oCAAiC;IACjC,gCAA6B;IAC7B,mCAAgC;IAChC,mCAAgC;IAOhC,iCAA8B;IAC9B,2BAAuB;IAGvB,6BAA0B;IAC1B,gCAA4B;IAG5B,qCAAgC;IAChC,qCAAgC;IAChC,qCAAgC;IAChC,qCAAgC;IAChC,sCAAiC;IACjC,sCAAiC;IACjC,sCAAiC;IACjC,sCAAiC;IACjC,uCAAkC;IAClC,yCAAoC;IACpC,yCAAqC;IACrC,mCAA+B;IAC/B,mCAA+B;IAC/B,wCAAoC;IACpC,kCAA8B;IAC9B,wCAAoC;IACpC,wCAAoC;IACpC,gCAA4B;IAC5B,wCAAoC;IACpC,uCAAmC;IACnC,sCAAkC;IAClC,qCAAiC;IAGjC,wCAAqC;IACrC,wCAAqC;IACrC,wCAAqC;IACrC,wCAAqC;IACrC,yCAAsC;IACtC,yCAAsC;IACtC,yCAAsC;IACtC,yCAAsC;IAGtC,kCAA+B;IAC/B,iCAA8B;IAC9B,iCAA8B;IAC9B,kCAA+B;IAC/B,kCAA+B;IAC/B,qCAAkC;IAClC,qCAAkC;IAClC,qCAAkC;IAClC,iCAA8B;IAC9B,oCAAiC;IACjC,qCAAkC;IAClC,kCAA+B;IAC/B,qCAAkC;IAClC,mCAAgC;IAGhC,yBAAsB;IACtB,gCAA2B;IAC3B,8BAA2B;IAG3B,gCAA8B;IAC9B,8BAA4B;IAC5B,+BAA6B;IAC7B,kCAAgC;IAChC,mCAAiC;IACjC,mCAAiC;IACjC,8BAA4B;IAG5B,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IAGvB,gCAA4B;IAG5B,wBAAoB;IACpB,gCAA6B;IAC7B,+BAA4B;IAC5B,kCAA8B;IAC9B,mCAA+B;IAC/B,mCAA+B;IAC/B,0BAAsB;IACtB,kCAA8B;IAC9B,kCAA+B;IAG/B,+BAA2B;IAC3B,oCAAgC;IAChC,oCAA+B;IAC/B,mCAA8B;IAC9B,oCAAgC;IAChC,sCAAkC;IAClC,iCAA6B;IAC7B,mCAA8B;IAC9B,8BAAyB;IACzB,mCAA8B;IAC9B,iCAA4B;IAC5B,gCAA2B;
|
|
1
|
+
{"version":3,"file":"DX7Voice.d.ts","sourceRoot":"","sources":["../../../src/utils/dx7/DX7Voice.js"],"names":[],"mappings":"AAwBA;;;;;;;;;;GAUG;AAEH;;;;;;;;;GASG;AAEH;;;;;;;;;;GAUG;AAEH;;;;;;GAMG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH;IAGE,2BAAwB;IACxB,8BAA0B;IAC1B,6BAAwB;IAGxB,mCAA8B;IAC9B,mCAA8B;IAC9B,mCAA8B;IAC9B,mCAA8B;IAC9B,oCAA+B;IAC/B,oCAA+B;IAC/B,oCAA+B;IAC/B,oCAA+B;IAC/B,qCAAgC;IAChC,uCAAkC;IAClC,uCAAmC;IACnC,gCAA4B;IAC5B,sCAAkC;IAClC,kCAA8B;IAC9B,sCAAkC;IAClC,mCAA+B;IAC/B,qCAAiC;IAGjC,sCAAmC;IACnC,sCAAmC;IACnC,sCAAmC;IACnC,sCAAmC;IACnC,uCAAoC;IACpC,uCAAoC;IACpC,uCAAoC;IACpC,uCAAoC;IACpC,gCAA6B;IAC7B,+BAA4B;IAC5B,gCAA6B;IAC7B,gCAA6B;IAC7B,mCAAgC;IAChC,mCAAgC;IAChC,oCAAiC;IACjC,gCAA6B;IAC7B,mCAAgC;IAChC,mCAAgC;IAOhC,iCAA8B;IAC9B,2BAAuB;IAGvB,6BAA0B;IAC1B,gCAA4B;IAG5B,qCAAgC;IAChC,qCAAgC;IAChC,qCAAgC;IAChC,qCAAgC;IAChC,sCAAiC;IACjC,sCAAiC;IACjC,sCAAiC;IACjC,sCAAiC;IACjC,uCAAkC;IAClC,yCAAoC;IACpC,yCAAqC;IACrC,mCAA+B;IAC/B,mCAA+B;IAC/B,wCAAoC;IACpC,kCAA8B;IAC9B,wCAAoC;IACpC,wCAAoC;IACpC,gCAA4B;IAC5B,wCAAoC;IACpC,uCAAmC;IACnC,sCAAkC;IAClC,qCAAiC;IAGjC,wCAAqC;IACrC,wCAAqC;IACrC,wCAAqC;IACrC,wCAAqC;IACrC,yCAAsC;IACtC,yCAAsC;IACtC,yCAAsC;IACtC,yCAAsC;IAGtC,kCAA+B;IAC/B,iCAA8B;IAC9B,iCAA8B;IAC9B,kCAA+B;IAC/B,kCAA+B;IAC/B,qCAAkC;IAClC,qCAAkC;IAClC,qCAAkC;IAClC,iCAA8B;IAC9B,oCAAiC;IACjC,qCAAkC;IAClC,kCAA+B;IAC/B,qCAAkC;IAClC,mCAAgC;IAGhC,yBAAsB;IACtB,gCAA2B;IAC3B,8BAA2B;IAG3B,gCAA8B;IAC9B,8BAA4B;IAC5B,+BAA6B;IAC7B,kCAAgC;IAChC,mCAAiC;IACjC,mCAAiC;IACjC,8BAA4B;IAG5B,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IACvB,yBAAuB;IAGvB,gCAA4B;IAG5B,wBAAoB;IACpB,gCAA6B;IAC7B,+BAA4B;IAC5B,kCAA8B;IAC9B,mCAA+B;IAC/B,mCAA+B;IAC/B,0BAAsB;IACtB,kCAA8B;IAC9B,kCAA+B;IAG/B,+BAA2B;IAC3B,oCAAgC;IAChC,oCAA+B;IAC/B,mCAA8B;IAC9B,oCAAgC;IAChC,sCAAkC;IAClC,iCAA6B;IAC7B,mCAA8B;IAC9B,8BAAyB;IACzB,mCAA8B;IAC9B,iCAA4B;IAC5B,gCAA2B;IAC3B,gCAA2B;IAC3B,oCAA+B;IAG/B,kCAA8B;IAC9B,uCAAmC;IA8QnC;;;;OAIG;IACH,sBAHW,KAAK,CAAC,MAAM,CAAC,GAAC,UAAU,GACtB,UAAU,CAmBtB;IAED;;;OAGG;IACH,8BAQC;IAED;;;OAGG;IACH,6BAKC;IAED;;;OAGG;IACH,+BAYC;IAED;;;OAGG;IACH,oCAIC;IAED;;;OAGG;IACH,yCAkBC;IAED;;;OAGG;IACH,sCAUC;IAED;;;OAGG;IACH,4BAUC;IAED;;;OAGG;IACH,iCAsBC;IAED;;;OAGG;IACH,yBAIC;IAED;;;;OAIG;IACH,6BAHW,MAAM,GACJ,QAAQ,CA2EpB;IAED;;;;;OAKG;IACH,8BAJW,KAAK,CAAC,MAAM,CAAC,GAAC,UAAU,UACxB,MAAM,GACJ,QAAQ,CAKpB;IAED;;;;;;OAMG;IACH,sBALW,IAAI,GAAC,IAAI,GACP,OAAO,CAAC,QAAQ,CAAC,CAiH7B;IAED;;;;;;;OAOG;IACH,uBANW,KAAK,CAAC,MAAM,CAAC,GAAC,WAAW,GAAC,UAAU,UACpC,MAAM,GACJ,QAAQ,CAwHpB;IAED;;;;;;OAMG;IACH,sBALW,YAAY,UACZ,MAAM,GACJ,QAAQ,CA8QpB;IAxhCD;;;;;OAKG;IACH,kBAJW,KAAK,CAAC,MAAM,CAAC,GAAC,UAAU,UACxB,MAAM,EAgBhB;IAJC,cAAkB;IAClB,8BAAgC;IAChC,aAA+B;IAC/B,4CAA0B;IAG5B;;;OAGG;IACH,qBAYC;IAED;;;;;OAKG;IACH,qBAJW,MAAM,GACJ,MAAM,CAYlB;IAED;;;;;OAKG;IACH,6BAJW,MAAM,GACJ,MAAM,CAiBlB;IAED;;;;OAIG;IACH,qBAHW,MAAM,SACN,MAAM,QAkBhB;IAED;;;;OAIG;IACH,UAFa,UAAU,CAYtB;IAED;;;OAGG;IACH,yBAQC;IAED;;;OAGG;IACH,wBAKC;IAED;;;OAGG;IACH,0BAYC;IAED;;;OAGG;IACH,+BAMC;IAED;;;OAGG;IACH,oCAmBC;IAED;;;OAGG;IACH,iCAUC;IAED;;;OAGG;IACH,uBAUC;IAED;;;OAGG;IACH,4BAsBC;IAED;;;OAGG;IACH,oBAIC;IAgxBD;;;;;OAKG;IACH,WAFa,UAAU,CAsFtB;IAED;;;OAGG;IACH,UAFa,MAAM,CA4GlB;CACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "midiwire",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.0",
|
|
4
4
|
"description": "Declarative JavaScript library for browser-based MIDI control",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/midiwire.umd.js",
|
|
@@ -53,16 +53,16 @@
|
|
|
53
53
|
"access": "public"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@biomejs/biome": "2.3.
|
|
56
|
+
"@biomejs/biome": "2.3.15",
|
|
57
57
|
"@jsdoc/cli": "0.3.12",
|
|
58
|
-
"@vitest/coverage-v8": "4.0.
|
|
59
|
-
"@vitest/ui": "4.0.
|
|
58
|
+
"@vitest/coverage-v8": "4.0.18",
|
|
59
|
+
"@vitest/ui": "4.0.18",
|
|
60
60
|
"jsdoc": "4.0.5",
|
|
61
|
-
"jsdom": "
|
|
61
|
+
"jsdom": "28.0.0",
|
|
62
62
|
"typescript": "5.9.3",
|
|
63
63
|
"vite": "7.3.1",
|
|
64
64
|
"vite-plugin-dts": "4.5.4",
|
|
65
|
-
"vitest": "4.0.
|
|
65
|
+
"vitest": "4.0.18"
|
|
66
66
|
},
|
|
67
67
|
"peerDependencies": {
|
|
68
68
|
"typescript": ">=5.0.0"
|
|
@@ -269,6 +269,8 @@ export class DX7Voice {
|
|
|
269
269
|
static DEFAULT_FREQ_COARSE = 1
|
|
270
270
|
static DEFAULT_ALGORITHM = 0
|
|
271
271
|
static DEFAULT_FEEDBACK = 0
|
|
272
|
+
static DEFAULT_OSC_SYNC = 1
|
|
273
|
+
static DEFAULT_LFO_KEY_SYNC = 1
|
|
272
274
|
|
|
273
275
|
// MIDI notes
|
|
274
276
|
static MIDI_OCTAVE_OFFSET = -2 // For displaying MIDI notes (MIDI 0 = C-2)
|
|
@@ -772,12 +774,12 @@ export class DX7Voice {
|
|
|
772
774
|
// Global params
|
|
773
775
|
unpacked[DX7Voice.UNPACKED_ALGORITHM] = DX7Voice.DEFAULT_ALGORITHM
|
|
774
776
|
unpacked[DX7Voice.UNPACKED_FEEDBACK] = DX7Voice.DEFAULT_FEEDBACK
|
|
775
|
-
unpacked[DX7Voice.UNPACKED_OSC_SYNC] =
|
|
777
|
+
unpacked[DX7Voice.UNPACKED_OSC_SYNC] = DX7Voice.DEFAULT_OSC_SYNC
|
|
776
778
|
unpacked[DX7Voice.UNPACKED_LFO_SPEED] = DX7Voice.DEFAULT_LFO_SPEED
|
|
777
779
|
unpacked[DX7Voice.UNPACKED_LFO_DELAY] = 0
|
|
778
780
|
unpacked[DX7Voice.UNPACKED_LFO_PM_DEPTH] = 0
|
|
779
781
|
unpacked[DX7Voice.UNPACKED_LFO_AM_DEPTH] = 0
|
|
780
|
-
unpacked[DX7Voice.UNPACKED_LFO_KEY_SYNC] =
|
|
782
|
+
unpacked[DX7Voice.UNPACKED_LFO_KEY_SYNC] = DX7Voice.DEFAULT_LFO_KEY_SYNC
|
|
781
783
|
unpacked[DX7Voice.UNPACKED_LFO_WAVE] = 0
|
|
782
784
|
unpacked[DX7Voice.UNPACKED_LFO_PM_SENS] = DX7Voice.DEFAULT_LFO_PM_SENS
|
|
783
785
|
unpacked[DX7Voice.UNPACKED_AMP_MOD_SENS] = 0
|