audio-mixer-engine 0.4.0 → 0.5.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/README.md CHANGED
@@ -38,8 +38,6 @@ npm install audio-mixer-engine
38
38
  ```javascript
39
39
  import {
40
40
  SpessaSynthAudioEngine,
41
- MidiParser,
42
- MidiPlayer,
43
41
  PlaybackManager
44
42
  } from 'audio-mixer-engine';
45
43
 
@@ -50,38 +48,40 @@ const ac = new AudioContext();
50
48
  const audioEngine = new SpessaSynthAudioEngine(ac);
51
49
  await audioEngine.initialize('/path/to/soundfont.sf2');
52
50
 
53
- // Parse the midi data
54
- const parser = new MidiParser();
55
- const midiData = await parser.parse(midiArrayBuffer);
51
+ // Create PlaybackManager with optional configuration
52
+ const manager = new PlaybackManager(audioEngine, {
53
+ metronome: { enabled: true, volume: 0.7 },
54
+ leadIn: { enabled: true, bars: 1 },
55
+ startup: { delayMs: 25 }
56
+ });
56
57
 
57
- // Initialise the MIDI player and the playback manager
58
- const player = new MidiPlayer(audioEngine, midiData);
59
- const manager = new PlaybackManager(player);
58
+ // Load MIDI data - this parses the file and creates the player automatically
59
+ await manager.load(midiArrayBuffer);
60
60
 
61
61
  // Set up event listeners
62
62
  manager.on('timeupdate', ({currentTime}) => {
63
63
  console.log(`Playing: ${currentTime.toFixed(2)}s`);
64
64
  });
65
65
 
66
- manager.on('barChanged', ({bar, beat, time}) => {
66
+ manager.on('beatChanged', ({bar, beat, time}) => {
67
67
  console.log(`Bar ${bar}, Beat ${beat} at ${time.toFixed(2)}s`);
68
68
  });
69
69
 
70
- // Setup routing
71
- // metronome
72
- const metGain = ac.createGain()
70
+ // Setup audio routing - REQUIRED for audio output
71
+ // Metronome
72
+ const metGain = ac.createGain();
73
73
  metGain.gain.value = 0.5;
74
- manager.getMetronomeOutput().connect(metGain).connect(ac.destination)
75
- // audio part channels
76
- const partGains = Object.keys(midiData.parts).map(partName=>{
77
- const gain = ac.createGain()
74
+ manager.getMetronomeOutput().connect(metGain).connect(ac.destination);
75
+
76
+ // Part outputs using iterator pattern
77
+ for (const [partName, outputNode] of manager.getPartOutputs()) {
78
+ const gain = ac.createGain();
78
79
  gain.gain.value = 0.75;
79
- manager.getPartoutput(partName).connect(gain).connact(ac.destination)
80
- return gain
81
- })
80
+ outputNode.connect(gain).connect(ac.destination);
81
+ }
82
82
 
83
83
  // Start playback with lead-in metronome ticks
84
- manager.play({useLeadIn: true});
84
+ await manager.play({ leadIn: true, metronome: true });
85
85
  ```
86
86
 
87
87
  ## Mixer Integration Pattern
@@ -214,14 +214,23 @@ player.on('barChanged', ({bar, beat, repeat, time}) => { /* */
214
214
  ### PlaybackManager Methods
215
215
 
216
216
  ```javascript
217
- // Advanced playback with metronome, lead-in, and startup timing
218
- const playbackManager = new PlaybackManager(midiPlayer, {
219
- metronome: { enabled: true, volume: 0.7 },
217
+ // Create PlaybackManager with audio engine and optional configuration
218
+ const playbackManager = new PlaybackManager(audioEngine, {
219
+ metronome: { enabled: true, volume: 0.7, tickInstrument: 115, accentInstrument: 116 },
220
220
  leadIn: { enabled: true, bars: 2 },
221
221
  startup: { delayMs: 25 }
222
222
  });
223
223
 
224
- // Transport controls (wraps MidiPlayer)
224
+ // Load MIDI data - parses and creates player automatically
225
+ await playbackManager.load(midiArrayBuffer);
226
+ // Or with metadata and custom instrument mapping
227
+ await playbackManager.load(midiArrayBuffer, structureMetadata, instrumentMap);
228
+
229
+ // Reset and load new MIDI file
230
+ playbackManager.reset();
231
+ await playbackManager.load(newMidiArrayBuffer);
232
+
233
+ // Transport controls
225
234
  await playbackManager.play({ leadIn: true, metronome: true });
226
235
  playbackManager.pause();
227
236
  playbackManager.resume();
@@ -242,10 +251,17 @@ playbackManager.setStartupDelay(50); // 50ms delay
242
251
  const startupSettings = playbackManager.getStartupSettings();
243
252
 
244
253
  // State and timing
245
- const state = playbackManager.getState(); // 'stopped', 'lead-in', 'playing', 'paused'
254
+ const state = playbackManager.getState(); // 'reset', 'ready', 'stopped', 'lead-in', 'playing', 'paused'
246
255
  const isPlaying = playbackManager.isPlaying();
247
256
  const currentTime = playbackManager.getCurrentTime();
248
257
 
258
+ // Audio routing access
259
+ const metronomeOutput = playbackManager.getMetronomeOutput();
260
+ for (const [partName, outputNode] of playbackManager.getPartOutputs()) {
261
+ // Set up external routing for each part
262
+ }
263
+ const partNames = playbackManager.getPartNames(); // Array of part names
264
+
249
265
  // Enhanced event handling
250
266
  playbackManager.on('leadInStarted', ({ bars, totalBeats, startupDelayMs }) => {});
251
267
  playbackManager.on('leadInEnded', () => {});
@@ -307,46 +323,59 @@ sopranoOutput.connect(levelAnalyzer);
307
323
  ## Complete Mixer Example
308
324
 
309
325
  ```javascript
310
- import { SpessaSynthAudioEngine } from 'audio-mixer-engine';
326
+ import { SpessaSynthAudioEngine, PlaybackManager } from 'audio-mixer-engine';
311
327
 
312
328
  class AudioMixer {
313
329
  constructor(audioContext) {
314
330
  this.audioContext = audioContext;
315
331
  this.audioEngine = new SpessaSynthAudioEngine(audioContext);
316
- this.parts = new Map(); // partId -> { channel, gain, analyzer, muted, soloed }
332
+ this.playbackManager = null;
333
+ this.parts = new Map(); // partId -> { gain, analyzer, muted, soloed }
317
334
  this.masterGain = audioContext.createGain();
318
335
  this.masterGain.connect(audioContext.destination);
319
336
  }
320
337
 
321
338
  async initialize(soundfontPath) {
322
339
  await this.audioEngine.initialize(soundfontPath);
340
+
341
+ // Create PlaybackManager with configuration
342
+ this.playbackManager = new PlaybackManager(this.audioEngine, {
343
+ metronome: { enabled: false, volume: 0.7 },
344
+ leadIn: { enabled: false, bars: 1 },
345
+ startup: { delayMs: 25 }
346
+ });
323
347
  }
324
348
 
325
- addPart(partId, instrument = 'piano') {
326
- // Create channel for this part
327
- const channel = this.audioEngine.createChannel(partId, { instrument });
328
-
349
+ async loadMidiFile(midiArrayBuffer, metadata = null) {
350
+ // Load MIDI data into PlaybackManager
351
+ await this.playbackManager.load(midiArrayBuffer, metadata);
352
+
353
+ // Set up mixer routing for all parts
354
+ for (const [partName, outputNode] of this.playbackManager.getPartOutputs()) {
355
+ this.addPartToMixer(partName, outputNode);
356
+ }
357
+ }
358
+
359
+ addPartToMixer(partId, partOutput) {
329
360
  // Set up mixer chain for this part
330
361
  const gain = this.audioContext.createGain();
331
362
  const analyzer = this.audioContext.createAnalyser();
332
363
  analyzer.fftSize = 256;
333
-
364
+
334
365
  // Audio routing: Part → Gain → Analyzer → Master → Destination
335
- const partOutput = channel.getOutputNode();
336
366
  partOutput.connect(gain);
337
367
  gain.connect(analyzer);
338
368
  analyzer.connect(this.masterGain);
339
-
369
+
340
370
  // Track part in mixer
341
371
  this.parts.set(partId, {
342
- channel,
343
372
  gain,
344
373
  analyzer,
345
374
  muted: false,
346
375
  soloed: false
347
376
  });
348
-
349
- return { channel, gain, analyzer };
377
+
378
+ return { gain, analyzer };
350
379
  }
351
380
 
352
381
  // Mixer controls
@@ -367,14 +396,14 @@ class AudioMixer {
367
396
  // Toggle solo state
368
397
  const part = this.parts.get(partId);
369
398
  if (!part) return;
370
-
399
+
371
400
  part.soloed = !part.soloed;
372
-
401
+
373
402
  // Apply solo logic: if any parts are soloed, mute non-soloed parts
374
- const hasAnysoloed = Array.from(this.parts.values()).some(p => p.soloed);
375
-
403
+ const hasAnySoloed = Array.from(this.parts.values()).some(p => p.soloed);
404
+
376
405
  this.parts.forEach((partData, id) => {
377
- if (hasAnysoloed) {
406
+ if (hasAnySoloed) {
378
407
  partData.gain.gain.value = partData.soloed ? 1 : 0;
379
408
  } else {
380
409
  partData.gain.gain.value = partData.muted ? 0 : 1;
@@ -386,11 +415,11 @@ class AudioMixer {
386
415
  getPartLevel(partId) {
387
416
  const part = this.parts.get(partId);
388
417
  if (!part) return 0;
389
-
418
+
390
419
  const bufferLength = part.analyzer.frequencyBinCount;
391
420
  const dataArray = new Uint8Array(bufferLength);
392
421
  part.analyzer.getByteFrequencyData(dataArray);
393
-
422
+
394
423
  // Calculate RMS level
395
424
  let sum = 0;
396
425
  for (let i = 0; i < bufferLength; i++) {
@@ -399,19 +428,17 @@ class AudioMixer {
399
428
  return Math.sqrt(sum / bufferLength) / 255; // Normalized 0-1
400
429
  }
401
430
 
402
- // Play notes on specific parts
403
- playNote(partId, pitch, velocity = 100) {
404
- const part = this.parts.get(partId);
405
- if (part) {
406
- part.channel.noteOn(pitch, velocity);
407
- }
431
+ // Playback controls (delegated to PlaybackManager)
432
+ async play(options = {}) {
433
+ await this.playbackManager.play(options);
408
434
  }
409
-
410
- stopNote(partId, pitch) {
411
- const part = this.parts.get(partId);
412
- if (part) {
413
- part.channel.noteOff(pitch);
414
- }
435
+
436
+ pause() {
437
+ this.playbackManager.pause();
438
+ }
439
+
440
+ stop() {
441
+ this.playbackManager.stop();
415
442
  }
416
443
  }
417
444
 
@@ -419,17 +446,18 @@ class AudioMixer {
419
446
  const mixer = new AudioMixer(audioContext);
420
447
  await mixer.initialize('soundfont.sf2');
421
448
 
422
- // Add parts to mixer
423
- mixer.addPart('soprano', 'choir_aahs');
424
- mixer.addPart('piano', 'piano');
449
+ // Load MIDI file
450
+ await mixer.loadMidiFile(midiArrayBuffer);
425
451
 
426
452
  // Mixer controls
427
453
  mixer.setPartVolume('soprano', 0.7);
428
454
  mixer.mutePart('piano');
429
455
  mixer.soloPart('soprano');
430
456
 
431
- // Play and monitor
432
- mixer.playNote('soprano', 60, 100); // C4 at velocity 100
457
+ // Playback
458
+ await mixer.play({ leadIn: true, metronome: true });
459
+
460
+ // Monitor levels
433
461
  const level = mixer.getPartLevel('soprano'); // Get real-time level
434
462
  ```
435
463
 
@@ -506,10 +534,14 @@ class PartRecorder {
506
534
  ### PlaybackManager with Metronome and Lead-in
507
535
 
508
536
  ```javascript
509
- import { PlaybackManager } from 'audio-mixer-engine';
537
+ import { SpessaSynthAudioEngine, PlaybackManager } from 'audio-mixer-engine';
510
538
 
511
- // Wrap MidiPlayer with PlaybackManager for advanced features
512
- const playbackManager = new PlaybackManager(player, {
539
+ // Initialize audio engine
540
+ const audioEngine = new SpessaSynthAudioEngine(audioContext);
541
+ await audioEngine.initialize('/path/to/soundfont.sf2');
542
+
543
+ // Create PlaybackManager with advanced features
544
+ const playbackManager = new PlaybackManager(audioEngine, {
513
545
  metronome: {
514
546
  enabled: true,
515
547
  tickInstrument: 115, // Woodblock for regular beats
@@ -525,6 +557,9 @@ const playbackManager = new PlaybackManager(player, {
525
557
  }
526
558
  });
527
559
 
560
+ // Load MIDI data
561
+ await playbackManager.load(midiArrayBuffer);
562
+
528
563
  // Enhanced event handling
529
564
  playbackManager.on('leadInStarted', ({ bars, totalBeats, startupDelayMs }) => {
530
565
  console.log(`Lead-in: ${bars} bars (${totalBeats} beats), ${startupDelayMs}ms startup delay`);
@@ -553,7 +588,7 @@ The PlaybackManager includes configurable startup delays to ensure perfect timin
553
588
 
554
589
  ```javascript
555
590
  // Configure startup delay during construction
556
- const playbackManager = new PlaybackManager(player, {
591
+ const playbackManager = new PlaybackManager(audioEngine, {
557
592
  startup: {
558
593
  delayMs: 25 // Default: 25ms delay before audio starts
559
594
  }
@@ -1 +1 @@
1
- "use strict";var v=Object.create;var y=Object.defineProperty;var w=Object.getOwnPropertyDescriptor;var M=Object.getOwnPropertyNames;var E=Object.getPrototypeOf,S=Object.prototype.hasOwnProperty;var B=(c,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of M(t))!S.call(c,s)&&s!==e&&y(c,s,{get:()=>t[s],enumerable:!(i=w(t,s))||i.enumerable});return c};var P=(c,t,e)=>(e=c!=null?v(E(c)):{},B(t||!c||!c.__esModule?y(e,"default",{value:c,enumerable:!0}):e,c));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const O="data:audio/mpeg;base64,//uUxAAAAAAAAAAAAAAAAAAAAAAAWGluZwAAAA8AAAAMAAAPMAA4ODg4ODg4OFRUVFRUVFRUcHBwcHBwcHCFhYWFhYWFhYWampqampqamq+vr6+vr6+vwMDAwMDAwMDA0tLS0tLS0tLj4+Pj4+Pj4/Hx8f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S+Vv8y3rfRYKi4jDSMS4i9KTEFNRTMuMTAwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqpMQU1FMy4xMDCqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//tkxPIADij5M+wwzYmAm6X9hJmwqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//s0xPaAClDTI6OxLwCwkeLoEJkoqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//sUxNoDwAAB/gAAACAAADSAAAAEqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";class p{constructor(t,e={}){if(new.target===p)throw new Error("AudioEngine is abstract and cannot be instantiated directly");this.audioContext=t,this.options=e,this.isInitialized=!1,this.channels=new WeakMap,this.activeChannels=new Set}async initialize(t){throw new Error("initialize() must be implemented by subclass")}createChannel(t,e={}){throw new Error("createChannel() must be implemented by subclass")}allSoundsOff(){throw new Error("allSoundsOff() must be implemented by subclass")}async playMetronomeTick(t,e,i){try{await this._ensureMetronomeBuffersLoaded();const s=e?this.accentTickBuffer:this.regularTickBuffer;if(!s){console.warn("Metronome buffer not available");return}const n=this.audioContext.createBufferSource();n.buffer=s;const a=this.audioContext.createGain();a.gain.value=i,n.connect(a);const r=this.getMetronomeOutput();r?a.connect(r):a.connect(this.audioContext.destination);const q=Math.max(t,this.audioContext.currentTime);n.start(q)}catch(s){console.warn("Buffer metronome playback failed:",s)}}getMetronomeOutput(){return this._metronomeOutput||(this._metronomeOutput=this.audioContext.createGain(),this._metronomeOutput.gain.value=1,this._metronomeOutput.connect(this.audioContext.destination)),this._metronomeOutput}async _ensureMetronomeBuffersLoaded(){if(!(this.regularTickBuffer&&this.accentTickBuffer)){try{if(typeof fetch<"u"){const[t,e]=await Promise.all([fetch(O),fetch(N)]),[i,s]=await Promise.all([t.arrayBuffer(),e.arrayBuffer()]),[n,a]=await Promise.all([this.audioContext.decodeAudioData(i),this.audioContext.decodeAudioData(s)]);this.regularTickBuffer=n,this.accentTickBuffer=a;return}}catch(t){console.warn("Failed to load metronome sounds:",t)}this.regularTickBuffer=this.audioContext.createBuffer(2,1024,this.audioContext.sampleRate),this.accentTickBuffer=this.audioContext.createBuffer(2,1024,this.audioContext.sampleRate)}}getActiveChannels(){return Array.from(this.activeChannels)}destroy(){this.allSoundsOff(),this._metronomeOutput&&(this._metronomeOutput.disconnect(),this._metronomeOutput=null),this.regularTickBuffer=null,this.accentTickBuffer=null,this.activeChannels.clear(),this.isInitialized=!1}_validateInitialized(){if(!this.isInitialized)throw new Error("AudioEngine not initialized. Call initialize() first.")}_registerChannel(t){this.activeChannels.add(t)}_unregisterChannel(t){this.activeChannels.delete(t),this.channels.delete(t)}}class g{constructor(t,e,i={}){if(new.target===g)throw new Error("ChannelHandle is abstract and cannot be instantiated directly");this.engine=t,this.partId=e,this.options={initialVolume:1,...i},this.isDestroyed=!1,this.noteRefCounts=new Map,this.scheduledEvents=new Map,this.activeNotes=new Set}getOutputNode(){throw new Error("getOutputNode() must be implemented by subclass")}noteOn(t,e){this._validateActive();const i=this.noteRefCounts.get(t)||0;this.noteRefCounts.set(t,i+1),this._actualNoteOn(t,e),i===0&&this.activeNotes.add(t)}noteOff(t){this._validateActive();const e=this.noteRefCounts.get(t)||0;if(e<=0)return;const i=e-1;this.noteRefCounts.set(t,i),i===0&&(this._actualNoteOff(t),this.activeNotes.delete(t),this.noteRefCounts.delete(t))}playNote(t,e,i,s){this._validateActive();const n=this.engine.audioContext.currentTime,a=`${this.partId}_${t}_${e}_${Date.now()}`;let r=t,q=s;if(t<n){const d=n-t;r=n,q=Math.max(0,s-d)}if(q<=0)return a;const o=Math.max(0,(r-n)*1e3),h=setTimeout(()=>{this.noteOn(e,i),this.scheduledEvents.delete(`${a}_on`)},o),l=o+q*1e3,A=setTimeout(()=>{this.noteOff(e),this.scheduledEvents.delete(`${a}_off`)},l);return this.scheduledEvents.set(`${a}_on`,h),this.scheduledEvents.set(`${a}_off`,A),a}allNotesOff(){this._validateActive(),this.scheduledEvents.forEach(t=>{clearTimeout(t)}),this.scheduledEvents.clear(),this.activeNotes.forEach(t=>{this._actualNoteOff(t)}),this.noteRefCounts.clear(),this.activeNotes.clear()}_actualNoteOn(t,e){throw new Error("_actualNoteOn() must be implemented by subclass")}_actualNoteOff(t){throw new Error("_actualNoteOff() must be implemented by subclass")}async setInstrument(t){throw new Error("setInstrument() must be implemented by subclass")}getInstrument(){throw new Error("getInstrument() must be implemented by subclass")}setVolume(t){throw new Error("setVolume() must be implemented by subclass")}getVolume(){throw new Error("getVolume() must be implemented by subclass")}getPartId(){return this.partId}isActive(){return!this.isDestroyed&&this.engine.isInitialized&&this.engine.activeChannels.has(this)}destroy(){if(!this.isDestroyed){this.allNotesOff();const t=this.getOutputNode();t&&t.disconnect(),this.noteRefCounts.clear(),this.scheduledEvents.clear(),this.activeNotes.clear(),this.engine._unregisterChannel(this),this.isDestroyed=!0}}_validateActive(){if(this.isDestroyed)throw new Error("Channel has been destroyed");if(!this.engine.isInitialized)throw new Error("AudioEngine is not initialized")}}const T={piano:0,bright_piano:1,electric_grand:2,honky_tonk:3,electric_piano_1:4,electric_piano_2:5,harpsichord:6,clavinet:7,celesta:8,glockenspiel:9,music_box:10,vibraphone:11,marimba:12,xylophone:13,tubular_bells:14,dulcimer:15,drawbar_organ:16,percussive_organ:17,rock_organ:18,church_organ:19,reed_organ:20,accordion:21,harmonica:22,tango_accordion:23,organ:19,nylon_guitar:24,steel_guitar:25,electric_guitar_jazz:26,electric_guitar_clean:27,electric_guitar_muted:28,overdriven_guitar:29,distortion_guitar:30,guitar_harmonics:31,guitar:24,acoustic_bass:32,electric_bass_finger:33,electric_bass_pick:34,fretless_bass:35,slap_bass_1:36,slap_bass_2:37,synth_bass_1:38,synth_bass_2:39,bass:32,violin:40,viola:41,cello:42,contrabass:43,tremolo_strings:44,pizzicato_strings:45,orchestral_harp:46,timpani:47,strings:48,strings_ensemble:48,slow_strings:49,synth_strings_1:50,synth_strings_2:51,choir_aahs:52,voice_oohs:53,synth_voice:54,orchestra_hit:55,trumpet:56,trombone:57,tuba:58,muted_trumpet:59,french_horn:60,brass_section:61,synth_brass_1:62,synth_brass_2:63,soprano_sax:64,alto_sax:65,tenor_sax:66,baritone_sax:67,oboe:68,english_horn:69,bassoon:70,clarinet:71,saxophone:64,piccolo:72,flute:73,recorder:74,pan_flute:75,blown_bottle:76,shakuhachi:77,whistle:78,ocarina:79,lead_1_square:80,lead_2_sawtooth:81,lead_3_calliope:82,lead_4_chiff:83,lead_5_charang:84,lead_6_voice:85,lead_7_fifths:86,lead_8_bass:87,pad_1_new_age:88,pad_2_warm:89,pad_3_polysynth:90,pad_4_choir:91,pad_5_bowed:92,pad_6_metallic:93,pad_7_halo:94,pad_8_sweep:95,fx_1_rain:96,fx_2_soundtrack:97,fx_3_crystal:98,fx_4_atmosphere:99,fx_5_brightness:100,fx_6_goblins:101,fx_7_echoes:102,fx_8_sci_fi:103,sitar:104,banjo:105,shamisen:106,koto:107,kalimba:108,bag_pipe:109,fiddle:110,shanai:111,tinkle_bell:112,agogo:113,steel_drums:114,woodblock:115,taiko_drum:116,melodic_tom:117,synth_drum:118,reverse_cymbal:119,guitar_fret_noise:120,breath_noise:121,seashore:122,bird_tweet:123,telephone_ring:124,helicopter:125,applause:126,gunshot:127},D=Object.entries(T).reduce((c,[t,e])=>(c[e]=t,c),{});class I{static getInstrumentProgram(t){if(typeof t=="number")return t;const e=T[t.toLowerCase()];return e!==void 0?e:0}static getProgramName(t){return D[t]||`Program ${t}`}static generateNoteId(t,e,i){return`${t}_${e}_${Math.round(i)}`}}class _ extends g{constructor(t,e,i,s={}){super(t,e,s),this.midiChannel=i,this.currentVolume=s.initialVolume||1,this.currentInstrument=s.instrument||"piano",this.outputGain=null,this._setupOutputNode(),this.setVolume(this.currentVolume),s.instrument&&this.setInstrument(s.instrument)}getOutputNode(){return this.outputGain}_actualNoteOn(t,e){const i=this.engine._getSynthesizer();if(i&&i.noteOn){const s=Math.round(e*this.currentVolume);i.noteOn(this.midiChannel,t,s)}}_actualNoteOff(t){const e=this.engine._getSynthesizer();e&&e.noteOff&&e.noteOff(this.midiChannel,t)}async setInstrument(t){this._validateActive();const e=I.getInstrumentProgram(t);this.currentInstrument=t;const i=this.engine._getSynthesizer();i&&i.programChange?i.programChange(this.midiChannel,e):console.warn("Cannot set instrument: synthesizer not available or no programChange method")}getInstrument(){return this.currentInstrument}setVolume(t){this._validateActive(),t=Math.max(0,Math.min(1,t)),this.currentVolume=t;const e=Math.round(t*127),i=this.engine._getSynthesizer();i&&i.controllerChange&&i.controllerChange(this.midiChannel,7,e)}getVolume(){return this.currentVolume}getMidiChannel(){return this.midiChannel}getActiveNoteCount(){return this.activeNotes.size}playNote(t,e,i,s){this._validateActive();const n=`${this.partId}_${t}_${e}_${Date.now()}`,a=this.engine._getSynthesizer();if(a&&a.post){const r=Math.round(i*this.currentVolume);a.post({channelNumber:this.midiChannel,type:"midiMessage",data:{messageData:[144|this.midiChannel,e,r],channelOffset:0,force:!1,options:{time:t}}}),a.post({channelNumber:this.midiChannel,type:"midiMessage",data:{messageData:[128|this.midiChannel,e,0],channelOffset:0,force:!1,options:{time:t+s}}})}else return super.playNote(t,e,i,s);return n}allNotesOff(){this._validateActive();const t=this.engine._getSynthesizer();t&&t.post?t.post({channelNumber:this.midiChannel,type:"midiMessage",data:{messageData:[176|this.midiChannel,123,0],channelOffset:0,force:!1,options:{time:this.engine.audioContext.currentTime}}}):super.allNotesOff()}destroy(){if(!this.isDestroyed){const t=this.engine._getIndividualOutput(this.midiChannel);this.outputGain&&this.outputGain!==t&&this.outputGain.disconnect(),this.outputGain=null}super.destroy()}_setupOutputNode(){const t=this.engine._getIndividualOutput(this.midiChannel);t?this.outputGain=t:(console.warn(`No individual output available for MIDI channel ${this.midiChannel}, using fallback`),this.outputGain=this.engine.audioContext.createGain(),this.outputGain.gain.value=this.currentVolume)}}class z extends p{constructor(t,e={}){super(t,e),this.synthesizer=null,this.soundfont=null,this.channelCounter=0,this.partToMidiChannel=new Map,this.midiChannelToPart=new Map,this.individualOutputs=[]}async initialize(t){const{WorkletSynthesizer:e}=await import("spessasynth_lib");let i;if(typeof t=="string")i=await this._loadSoundfontFromPath(t);else if(t instanceof ArrayBuffer)i=t;else throw new Error("Invalid soundfont data type. Expected string path or ArrayBuffer.");await this._loadAudioWorkletSafely(),this._setupIndividualOutputs(),this.dummyTarget=this.audioContext.createGain(),await new Promise(s=>setTimeout(s,50)),this.synthesizer=new e(this.audioContext),await this.synthesizer.soundBankManager.addSoundBank(i,"main"),await this.synthesizer.isReady,this._connectIndividualOutputs(),this._initializeMetronomeChannel(),this.isInitialized=!0}createChannel(t,e={}){if(this._validateInitialized(),this.partToMidiChannel.has(t))throw new Error(`Channel for part '${t}' already exists`);const i=this.channelCounter;if(i>=15)throw new Error("Maximum number of musical part channels (15) exceeded. Channel 15 is reserved for metronome.");this.channelCounter++,this.partToMidiChannel.set(t,i),this.midiChannelToPart.set(i,t);const s=new _(this,t,i,e);return this._registerChannel(s),e.instrument&&s.setInstrument(e.instrument),s}allSoundsOff(){this.synthesizer&&this.midiChannelToPart.forEach((t,e)=>{this.synthesizer.controllerChange&&this.synthesizer.controllerChange(e,120,0)})}clearAllChannels(){this.allSoundsOff(),this.partToMidiChannel.clear(),this.midiChannelToPart.clear(),this.channelCounter=0}destroy(){this.allSoundsOff(),this.synthesizer&&typeof this.synthesizer.disconnect=="function"&&this.synthesizer.disconnect(),this.individualOutputs.forEach(t=>{t&&t.disconnect&&t.disconnect()}),this.individualOutputs=[],this.dummyTarget&&(this.dummyTarget.disconnect(),this.dummyTarget=null),this.partToMidiChannel.clear(),this.midiChannelToPart.clear(),this.channelCounter=0,super.destroy(),this.synthesizer=null,this.soundfont=null}getMidiChannelForPart(t){return this.partToMidiChannel.has(t)?this.partToMidiChannel.get(t):null}_getSynthesizer(){return this.synthesizer}_getIndividualOutput(t){return t>=0&&t<this.individualOutputs.length?this.individualOutputs[t]:null}getMetronomeChannel(){const t=this._getIndividualOutput(15);return console.log("Metronome channel 15 output:",t?"Available":"NULL",`(total outputs: ${this.individualOutputs.length})`),t}_setupIndividualOutputs(){this.individualOutputs=[];for(let t=0;t<16;t++){const e=this.audioContext.createGain();e.gain.value=1,this.individualOutputs.push(e)}}_connectIndividualOutputs(){try{this.synthesizer&&this.synthesizer.connectIndividualOutputs?this.synthesizer.connectIndividualOutputs(this.individualOutputs):(console.warn("Synthesizer does not support individual outputs, using master output only"),this.synthesizer&&this.synthesizer.connect&&this.audioContext.destination&&this.synthesizer.connect(this.audioContext.destination))}catch(t){console.warn("Failed to connect individual outputs:",t.message),console.warn("Falling back to master output routing")}}async _loadSoundfontFromPath(t){const e=await fetch(t);if(!e.ok)throw new Error(`Failed to load soundfont: ${e.status} ${e.statusText}`);return await e.arrayBuffer()}async _loadAudioWorkletSafely(){const t="/node_modules/spessasynth_lib/dist/spessasynth_processor.min.js";for(let i=1;i<=5;i++)try{await this.audioContext.audioWorklet.addModule(t);return}catch(s){if(console.warn(`AudioWorklet loading failed (attempt ${i}/5):`,s.message),i===5)throw new Error(`AudioWorklet failed after 5 attempts: ${s.message}`);const n=i*500;await new Promise(a=>setTimeout(a,n))}}_initializeMetronomeChannel(){try{const t=this._getSynthesizer();if(!t){console.warn("Cannot initialize metronome channel: synthesizer not available");return}const e=15;t.programChange&&(t.programChange(e,115),console.log("Metronome channel 15 initialized with woodblock instrument (115)")),t.controllerChange&&t.controllerChange(e,7,127)}catch(t){console.warn("Failed to initialize metronome channel:",t)}}async playMetronomeTick(t,e,i){try{const s=this.getMetronomeChannel(),n=this._getSynthesizer();if(!s||!n)return super.playMetronomeTick(t,e,i);const a=15,r=e?86:60,o=Math.round(Math.min(127,Math.max(0,i*(e?127:100)))),h=this.audioContext.currentTime,l=Math.max(t,h),A=l-h;n.post?(n.post({channelNumber:a,type:"midiMessage",data:{messageData:[144|a,r,o],channelOffset:0,force:!1,options:{time:l}}}),n.post({channelNumber:a,type:"midiMessage",data:{messageData:[128|a,r,0],channelOffset:0,force:!1,options:{time:l+.1}}})):A<=.01?(n.noteOn&&n.noteOn(a,r,o),setTimeout(()=>{n.noteOff&&n.noteOff(a,r)},100)):setTimeout(()=>{n.noteOn&&n.noteOn(a,r,o),setTimeout(()=>{n.noteOff&&n.noteOff(a,r)},100)},A*1e3)}catch(s){return console.warn("MIDI metronome failed, falling back to buffers:",s),super.playMetronomeTick(t,e,i)}}getMetronomeOutput(){return!this.individualOutputs||this.individualOutputs.length<16?null:this.individualOutputs[15]}}class U{constructor(){this.partNames=["soprano","alto","tenor","bass","treble","mezzo","baritone","s","a","t","b","satb"],this.parsedData={parts:{},barStructure:[],metadata:{}}}async parse(t){try{const e=await this._parseMidiBuffer(t);return this._extractMetadata(e),this._extractBarStructure(e),this._extractParts(e),this.parsedData}catch(e){throw console.error("Error parsing MIDI file:",e),e}}async _parseMidiBuffer(t){const e=new Uint8Array(t);if(!(e[0]===77&&e[1]===84&&e[2]===104&&e[3]===100))throw new Error("Not a valid MIDI file");const i=this._bytesToNumber(e.slice(4,8)),s=this._bytesToNumber(e.slice(8,10)),n=this._bytesToNumber(e.slice(10,12)),a=this._bytesToNumber(e.slice(12,14)),r=a&32768?null:a,q={format:s,ticksPerBeat:r,tracks:[],duration:0};let o=8+i;for(let h=0;h<n;h++)if(e[o]===77&&e[o+1]===84&&e[o+2]===114&&e[o+3]===107){const l=this._bytesToNumber(e.slice(o+4,o+8)),A=e.slice(o+8,o+8+l),d=this._parseTrack(A);q.tracks.push(d),o+=8+l}else throw new Error(`Invalid track header at position ${o}`);return q}_parseTrack(t){const e={notes:[],name:null,lyrics:[],events:[],duration:0};let i=0,s=0,n=null;for(;i<t.length;){let a=0,r=0;do r=t[i++],a=a<<7|r&127;while(r&128);s+=a,r=t[i++];let q=r;if((r&128)===0){if(n===null)throw new Error("Running status byte encountered before status byte");q=n,i--}else n=q;if(q===255){const o=t[i++],h=this._readVariableLengthValue(t,i);i+=h.bytesRead;const l=t.slice(i,i+h.value);switch(i+=h.value,o){case 3:e.name=this._bytesToString(l);break;case 1:e.events.push({type:"text",text:this._bytesToString(l),tick:s});break;case 5:e.lyrics.push({text:this._bytesToString(l),tick:s});break;case 81:const A=this._bytesToNumber(l),d=Math.round(6e7/A);e.events.push({type:"tempo",bpm:d,tick:s});break;case 88:e.events.push({type:"timeSignature",numerator:l[0],denominator:Math.pow(2,l[1]),tick:s});break;case 47:e.duration=s;break}}else if((q&240)===144){const o=q&15,h=t[i++],l=t[i++];l>0?e.notes.push({type:"noteOn",noteNumber:h,velocity:l,tick:s,channel:o}):e.notes.push({type:"noteOff",noteNumber:h,tick:s,channel:o})}else if((q&240)===128){const o=q&15,h=t[i++];t[i++],e.notes.push({type:"noteOff",noteNumber:h,tick:s,channel:o})}else if(q===240||q===247){const o=this._readVariableLengthValue(t,i);i+=o.bytesRead+o.value}else if((q&240)===176){const o=q&15,h=t[i++],l=t[i++];e.events.push({type:"controller",controllerNumber:h,value:l,channel:o,tick:s})}else if((q&240)===192){const o=q&15,h=t[i++];e.events.push({type:"programChange",programNumber:h,channel:o,tick:s})}else if((q&240)===208){const o=q&15,h=t[i++];e.events.push({type:"channelAftertouch",pressure:h,channel:o,tick:s})}else if((q&240)===224){const o=q&15,h=t[i++],A=(t[i++]<<7|h)-8192;e.events.push({type:"pitchBend",value:A,channel:o,tick:s})}else if((q&240)===160){const o=q&15,h=t[i++],l=t[i++];e.events.push({type:"noteAftertouch",noteNumber:h,pressure:l,channel:o,tick:s})}else console.warn(`Unknown event type: ${q.toString(16)} at position ${i-1}`),i++}return e}_extractMetadata(t){const e={title:null,composer:null,partNames:[],format:t.format,ticksPerBeat:t.ticksPerBeat};t.tracks.forEach((i,s)=>{if(i.name&&!e.title&&(e.title=i.name),i.events.filter(n=>n.type==="text").forEach(n=>{const a=n.text.toLowerCase();(a.includes("compos")||a.includes("by"))&&!e.composer&&(e.composer=n.text)}),i.name){const n=i.name.toLowerCase();for(const a of this.partNames)if(n.includes(a)){e.partNames.push({index:s,name:i.name});break}}}),this.parsedData.metadata=e}_extractBarStructure(t){const e=t.ticksPerBeat||480,i=[];t.tracks.forEach(h=>{h.events.forEach(l=>{(l.type==="timeSignature"||l.type==="tempo")&&i.push(l)})}),i.sort((h,l)=>h.tick-l.tick);let s=0;t.tracks.forEach(h=>{h.notes&&h.notes.forEach(l=>{l.type==="noteOff"&&l.tick>s&&(s=l.tick)})}),s===0&&(s=e*8);const n=[],a=i.filter(h=>h.type==="timeSignature").sort((h,l)=>h.tick-l.tick);let r={numerator:4,denominator:4},q=0,o=0;for(;q<s;){for(;o<a.length&&a[o].tick<=q;)r=a[o],o++;let h;h=q+e*4*r.numerator/r.denominator,i.filter(u=>u.type==="tempo"&&u.tick>=q&&u.tick<h);const l=r.numerator,A=[],d=e*(4/r.denominator);for(let u=0;u<l;u++){const m=q+u*d,f=this._ticksToTime(m,t);A.push(f)}n.push({sig:[r.numerator,r.denominator],beats:A}),q=h}this.parsedData.barStructure=n}_extractParts(t){const e={},i=t.ticksPerBeat;t.tracks.forEach((s,n)=>{if(!s.notes.length)return;let a=null;if(s.name){const u=s.name.toLowerCase();for(const m of this.partNames)if(m.length===1){if(u===m){a=m;break}}else if(u.includes(m)){a=m;break}}a||(a=s.name||`Track ${n+1}`),a==="s"&&(a="soprano"),a==="a"&&(a="alto"),a==="t"&&(a="tenor"),a==="b"&&(a="bass");let r=a,q=2;for(;e[r];)r=`${a} ${q}`,q++;a=r;const o=[],h={};s.notes.forEach(u=>{if(u.type==="noteOn")h[u.noteNumber]={tick:u.tick,velocity:u.velocity};else if(u.type==="noteOff"&&h[u.noteNumber]){const m=h[u.noteNumber],f=u.tick-m.tick;o.push({pitch:u.noteNumber,name:this._midiNoteToName(u.noteNumber),startTick:m.tick,endTick:u.tick,duration:f,startTime:this._ticksToTime(m.tick,t),endTime:this._ticksToTime(u.tick,t),velocity:m.velocity}),delete h[u.noteNumber]}});const l=s.lyrics.map(u=>({text:u.text,tick:u.tick,time:u.tick/i}));o.sort((u,m)=>u.startTick-m.startTick);const A=s.events.filter(u=>u.type==="programChange").map(u=>({programNumber:u.programNumber,tick:u.tick,time:this._ticksToTime(u.tick,t)})).sort((u,m)=>u.tick-m.tick),d=A.length>0?A[0].programNumber:0;e[a]={notes:o,lyrics:l,trackIndex:n,programChanges:A,defaultInstrument:d}}),this.parsedData.parts=e}_midiNoteToName(t){const e=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],i=Math.floor(t/12)-1;return`${e[t%12]}${i}`}_bytesToNumber(t){let e=0;for(let i=0;i<t.length;i++)e=e<<8|t[i];return e}_bytesToString(t){return new TextDecoder().decode(t)}_readVariableLengthValue(t,e){let i=0,s,n=0;do s=t[e+n++],i=i<<7|s&127;while(s&128);return{value:i,bytesRead:n}}_ticksToTime(t,e){const i=e.ticksPerBeat||480,s=[];e.tracks.forEach(q=>{q.events.forEach(o=>{o.type==="tempo"&&s.push(o)})}),s.sort((q,o)=>q.tick-o.tick);let n=0,a=0,r=120;for(const q of s){if(q.tick>t)break;if(q.tick>a){const h=(q.tick-a)/i*(60/r);n+=h,a=q.tick}r=q.bpm}if(t>a){const o=(t-a)/i*(60/r);n+=o}return n}}class k{constructor(){this.barOrder=[],this.beatTable=[]}mapBeats(t,e){try{return this.barOrder=this.generateBarOrder(e.sections,e.order),this.beatTable=this.generateBeatTable(this.barOrder,t.barStructure),this.beatTable}catch(i){throw console.error("Error mapping beats:",i),i}}generateBarOrder(t,e){const i=[],s={};for(const n of e){const a=t[n.section];if(!a)throw new Error(`Invalid section index: ${n.section}`);const r=n.section;s[r]||(s[r]=0),s[r]++;const q=s[r],o=n.from!==void 0?n.from:this._getSectionStartBar(t,n.section),h=n.to!==void 0?n.to:a.to,l=n.as||1;for(let A=o;A<=h;A++)this._shouldPlayBar(a,A,l)&&i.push({barNumber:A,repeat:q,sectionIndex:n.section,voltaTime:l})}return i}generateBeatTable(t,e){const i=[],s={};let n=0,a=0;const r=[...e];for(;a<t.length&&n<r.length;){const q=t[a],o=q.barNumber;if(s[o]===void 0){const d=r[n];if(!d||!d.sig)throw new Error(`Invalid MIDI bar structure at index ${n}`);s[o]=d.sig[0]}const h=s[o];let l=r[n],A=l.sig[0];for(;A<h&&n+1<r.length;){const d=r[n+1],u=[l.sig[0]+d.sig[0],l.sig[1]],m=[...l.beats||[],...d.beats||[]];l={sig:u,beats:m},r[n]=l,r.splice(n+1,1),A=u[0]}if(A>h){const d=h,u=A-h,m=l.beats?l.beats.slice(0,d):[],f=l.beats?l.beats.slice(d):[],b={sig:[d,l.sig[1]],beats:m},x={sig:[u,l.sig[1]],beats:f};r[n]=b,r.splice(n+1,0,x),l=b}this._generateBeatsForBar(i,q,l,h),n++,a++}if(a<t.length)throw new Error(`Ran out of MIDI bars before completing score. Score bar ${a}/${t.length}, MIDI bar ${n}/${r.length}`);return i}_generateBeatsForBar(t,e,i,s){const{beats:n}=i;if(!n||!Array.isArray(n))throw new Error(`Invalid MIDI bar: missing beats array. Got: ${JSON.stringify(i)}`);const a=n.slice(0,s);for(let r=1;r<=s;r++){const o={time:a[r-1],repeat:e.repeat,bar:e.barNumber,beat:r,timeSig:s};t.push(o)}}_getSectionStartBar(t,e){return t[e].pickup!==void 0?0:e===0?1:t[e-1].to+1}_shouldPlayBar(t,e,i){if(!t.voltas)return!0;const s=t.voltas.indexOf(e);return s===-1?!0:s+1===i}}function C(c){return{all:c=c||new Map,on:function(t,e){var i=c.get(t);i?i.push(e):c.set(t,[e])},off:function(t,e){var i=c.get(t);i&&(e?i.splice(i.indexOf(e)>>>0,1):c.set(t,[]))},emit:function(t,e){var i=c.get(t);i&&i.slice().map(function(s){s(e)}),(i=c.get("*"))&&i.slice().map(function(s){s(t,e)})}}}class L{constructor(t,e,i=null,s=null){if(!t||!t.isInitialized)throw new Error("Initialized AudioEngine is required");if(!e)throw new Error("Parsed MIDI data is required");this.audioEngine=t,this.instrumentMap=i||{},this.parsedData=e,this._isPlaying=!1,this._currentTime=0,this._totalDuration=0,this.playbackSpeed=1,this.partChannels=new Map,this.partOutputs=new Map,this.playbackStartTime=0,this.lookAheadTime=.05,this.scheduleInterval=null,this.partNotePointers=new Map,this.partProgramPointers=new Map,this.eventBus=C(),this.beatMapper=new k,this.beats=[],this._setupPartChannels(),this._calculateTotalDuration(),this._resetNotePointers(),this._resetProgramPointers();const n=s||this._createDefaultStructureMetadata();this.beats=this.beatMapper.mapBeats(e,n)}play(){this._isPlaying||(this._isPlaying=!0,this.playbackStartTime=this.audioEngine.audioContext.currentTime-this._currentTime/this.playbackSpeed,this._resetNotePointers(),this._resetProgramPointers(),this._schedulePlayback(),this._startTimeUpdateLoop())}playAt(t){this._isPlaying||(this._isPlaying=!0,this.playbackStartTime=t-this._currentTime/this.playbackSpeed,this._resetNotePointers(),this._resetProgramPointers(),this._schedulePlayback(),this._startTimeUpdateLoop())}pause(){this._isPlaying&&(this._isPlaying=!1,this._stopScheduling(),this._stopTimeUpdateLoop())}stop(){this._isPlaying=!1,this._currentTime=0,this._stopScheduling(),this._stopTimeUpdateLoop(),this._resetNotePointers(),this._resetProgramPointers()}skipToTime(t){t=Math.max(0,Math.min(t,this._totalDuration));const e=this._isPlaying;e&&this.pause(),this._currentTime=t,this._resetNotePointers(),this._resetProgramPointers(),e&&this.play()}setPlaybackSpeed(t,e=!0){if(t<=0)throw new Error("Playback speed must be greater than 0");const i=e&&this._isPlaying;i&&this.pause();const s=(this.audioEngine.audioContext.currentTime-this.playbackStartTime)*this.playbackSpeed;this.playbackSpeed=t,i?this.play():this.playbackStartTime=this.audioEngine.audioContext.currentTime-s/this.playbackSpeed}setBar(t,e=0){const i=this.getTimeFromBar(t,e);i!==null&&(this.skipToTime(i),this._emitEvent("barChanged",{bar:t,beat:1,repeat:e,time:i}))}getTimeFromBar(t,e=0){e||(e=1);const i=this.beats.find(s=>s.bar===t&&s.beat===1&&s.repeat===e);return i?i.time:null}getBeatFromTime(t){if(!this.beats.length)return null;let e=null;for(let i=this.beats.length-1;i>=0;i--)if(this.beats[i].time<=t){e=this.beats[i];break}return e}allSoundsOff(){this.audioEngine.allSoundsOff()}getPartOutput(t){return this.partOutputs.get(t)||null}getPartChannel(t){return this.partChannels.get(t)||null}getCurrentTime(){if(this._isPlaying){const t=(this.audioEngine.audioContext.currentTime-this.playbackStartTime)*this.playbackSpeed;this._currentTime=Math.min(t,this._totalDuration)}return this._currentTime}getTotalDuration(){return this._totalDuration}isPlaying(){return this._isPlaying}on(t,e){this.eventBus.on(t,e)}off(t,e){this.eventBus.off(t,e)}_setupPartChannels(){Object.keys(this.parsedData.parts).forEach(t=>{const e=this.parsedData.parts[t],i=this.instrumentMap[t]||{},s=i.instrument!==void 0?i.instrument:e.defaultInstrument!==void 0?e.defaultInstrument:0;try{const n=this.audioEngine.createChannel(t,{instrument:s,initialVolume:i.volume||1});this.partChannels.set(t,n);const a=this.audioEngine.audioContext.createGain();a.gain.value=1;const r=n.getOutputNode();r&&r.connect(a),this.partOutputs.set(t,a)}catch(n){console.error(`Failed to create channel for part '${t}':`,n),this._emitEvent("error",n)}})}_calculateTotalDuration(){let t=0;Object.values(this.parsedData.parts).forEach(e=>{e.notes.forEach(i=>{i.endTime>t&&(t=i.endTime)})}),this._totalDuration=t}_schedulePlayback(){this._stopScheduling(),this._startScheduleLoop()}_startScheduleLoop(){this.scheduleInterval||(this.scheduleInterval=setInterval(()=>{if(!this._isPlaying)return;const e=(this.audioEngine.audioContext.currentTime-this.playbackStartTime)*this.playbackSpeed,i=e+this.lookAheadTime;for(const[s,n]of this.partChannels){const a=this.parsedData.parts[s];if(a){if(a.programChanges&&a.programChanges.length>0){let r=this.partProgramPointers.get(s)||0;const q=a.programChanges;for(;r<q.length&&q[r].time<e;)r++;for(;r<q.length&&q[r].time<=i;){const o=q[r];n.setInstrument(o.programNumber),r++}this.partProgramPointers.set(s,r)}if(a.notes){let r=this.partNotePointers.get(s)||0;const q=a.notes;for(;r<q.length&&q[r].endTime<e;)r++;for(;r<q.length&&q[r].startTime<=i;){const o=q[r];if(o.endTime-o.startTime>=.01){const h=this.playbackStartTime+o.startTime/this.playbackSpeed,l=(o.endTime-o.startTime)/this.playbackSpeed;n.playNote(h,o.pitch,o.velocity,l)}r++}this.partNotePointers.set(s,r)}}}},50))}_resetNotePointers(){const t=this._currentTime;for(const[e]of this.partChannels){const i=this.parsedData.parts[e];if(!i||!i.notes)continue;let s=0;for(;s<i.notes.length&&i.notes[s].endTime<t;)s++;this.partNotePointers.set(e,s)}}_resetProgramPointers(){const t=this._currentTime;for(const[e,i]of this.partChannels){const s=this.parsedData.parts[e];if(!s||!s.programChanges){this.partProgramPointers.set(e,0);continue}let n=0,a=s.defaultInstrument;for(;n<s.programChanges.length&&s.programChanges[n].time<=t;)a=s.programChanges[n].programNumber,n++;i.setInstrument(a),this.partProgramPointers.set(e,n)}}_stopScheduling(){this.scheduleInterval&&(clearInterval(this.scheduleInterval),this.scheduleInterval=null),this.partChannels.forEach(t=>{t.isActive()&&t.allNotesOff()})}_startTimeUpdateLoop(){this.timeUpdateInterval=setInterval(()=>{const t=this.getCurrentTime();this._emitEvent("timeupdate",{currentTime:t});const e=this.getBeatFromTime(t);e&&this._emitEvent("beatChanged",e),t>=this._totalDuration+.05&&(this.stop(),this._emitEvent("ended",{finalTime:t}))},100)}_stopTimeUpdateLoop(){this.timeUpdateInterval&&(clearInterval(this.timeUpdateInterval),this.timeUpdateInterval=null)}_emitEvent(t,e){(this.eventBus.all.get(t)||[]).forEach(s=>{try{s(e)}catch(n){console.error(`Error in ${t} event listener:`,n)}})}_createDefaultStructureMetadata(){return{sections:[{from:1,to:this.parsedData.barStructure.length}],order:[{section:0}]}}destroy(){this.stop(),this.partChannels.forEach(t=>{t.destroy()}),this.partChannels.clear(),this.partOutputs.forEach(t=>{t.disconnect()}),this.partOutputs.clear(),this.partNotePointers.clear(),this.partProgramPointers.clear(),this.eventBus.all.clear()}}class F{constructor(t,e={}){if(!t)throw new Error("MidiPlayer is required");this.midiPlayer=t,this.audioEngine=t.audioEngine,this.eventBus=C(),this.metronomeConfig={enabled:!1,tickInstrument:115,accentInstrument:116,volume:.7,...e.metronome},this.leadInConfig={enabled:!1,bars:1,...e.leadIn},this.startupConfig={delayMs:25,...e.startup},this.state="stopped",this.frozenTime=0,this.leadInData=null,this.leadInStartTime=null,this.leadInProgress=null,this.leadInInterval=null,this.timeUpdateInterval=null,this.metronomeScheduleInterval=null,this.nextBeatIndex=0,this._setupEventDelegation(),this._validateConfig()}async play(t={}){if(!(this.state==="playing"||this.state==="lead-in"))try{const e=t.leadIn!==void 0?t.leadIn:this.leadInConfig.enabled,i=t.metronome!==void 0?t.metronome:this.metronomeConfig.enabled;this.frozenTime===0&&(this.frozenTime=this.midiPlayer.getCurrentTime()),e?await this._startLeadIn(i):await this._startMidiPlayback(i)}catch(e){throw this.state="stopped",this._emitEvent("error",e),e}}pause(){if(this.state==="stopped"||this.state==="paused")return;const t=this.state;this.state="paused",t==="lead-in"?this._pauseLeadIn():t==="playing"&&(this.midiPlayer.pause(),this._stopMetronome()),this._stopTimeUpdateLoop(),this._emitEvent("playbackPaused",{})}resume(){this.state==="paused"&&(this.leadInData&&this.leadInProgress!==null&&this.leadInProgress<1?this._resumeLeadIn():(this.state="playing",this._resetMetronomeBeatTracking(),this.midiPlayer.play(),this._startMetronomeIfEnabled(),this._startTimeUpdateLoop(),this._emitEvent("playbackStarted",{})))}stop(){if(this.state==="stopped")return;const t=this.state==="playing";this.state="stopped",this.frozenTime=0,this.leadInData=null,this.leadInProgress=null,this.leadInStartTime=null,this._stopLeadIn(),this._stopMetronome(),this._stopTimeUpdateLoop(),this._resetMetronomeBeatTracking(),t&&this.midiPlayer.stop(),this._emitEvent("playbackStopped",{})}skipToTime(t){this.frozenTime=t,this.state!=="lead-in"&&(this.midiPlayer.skipToTime(t),this.state==="playing"&&this.metronomeConfig.enabled&&this._resetMetronomeBeatTracking())}setBar(t,e=0){if(this.state==="lead-in"){const i=this.midiPlayer.getTimeFromBar(t,e);i!==null&&(this.frozenTime=i);return}if(this.midiPlayer.setBar(t,e),this.state==="stopped"){const i=this.midiPlayer.getTimeFromBar(t,e);i!==null&&(this.frozenTime=i)}this.state==="playing"&&this.metronomeConfig.enabled&&this._resetMetronomeBeatTracking()}getCurrentTime(){return this.state==="lead-in"||this.state==="paused"&&this.leadInProgress!==null?this.frozenTime:this.midiPlayer.getCurrentTime()}getLeadInProgress(){return this.leadInProgress}setMetronomeEnabled(t){const e=this.metronomeConfig.enabled;this.metronomeConfig.enabled=t,this.state==="playing"&&(t&&!e?this._startMetronome():!t&&e&&this._stopMetronome()),this._emitEvent("metronomeEnabledChanged",{enabled:t})}setMetronomeSettings(t){if(t.volume!==void 0&&(t.volume<0||t.volume>1))throw new Error("Metronome volume must be between 0.0 and 1.0");if(Object.assign(this.metronomeConfig,t),t.volume!==void 0){const e=this.audioEngine.getMetronomeOutput();e&&e.gain&&(e.gain.value=t.volume)}this._emitEvent("metronomeSettingsChanged",{...t})}setLeadInEnabled(t){this.leadInConfig.enabled=t,this._emitEvent("leadInSettingsChanged",{enabled:t,bars:this.leadInConfig.bars})}setLeadInBars(t){if(t<1)throw new Error("Lead-in bars must be at least 1");this.leadInConfig.bars=t,this._emitEvent("leadInSettingsChanged",{enabled:this.leadInConfig.enabled,bars:t})}setPlaybackSpeed(t,e=!0){this.midiPlayer.setPlaybackSpeed(t,e)}getMetronomeSettings(){return{...this.metronomeConfig}}isMetronomeEnabled(){return this.metronomeConfig.enabled}getLeadInSettings(){return{enabled:this.leadInConfig.enabled,bars:this.leadInConfig.bars}}getStartupSettings(){return{...this.startupConfig}}setStartupDelay(t){if(typeof t!="number"||t<0||t>1e3)throw new Error("Startup delay must be a number between 0 and 1000 milliseconds");this.startupConfig.delayMs=t,this._emitEvent("startupSettingsChanged",{delayMs:t})}getState(){return this.state}isInLeadIn(){return this.state==="lead-in"}isPlaying(){return this.state==="playing"||this.state==="lead-in"}getMetronomeOutput(){return this.audioEngine.getMetronomeOutput()}getPartOutput(t){return this.midiPlayer.getPartOutput(t)}allSoundsOff(){this.midiPlayer.allSoundsOff()}getTotalDuration(){return this.midiPlayer.getTotalDuration()}on(t,e){this.eventBus.on(t,e)}off(t,e){this.eventBus.off(t,e)}_setupEventDelegation(){this.midiPlayer.on("timeupdate",t=>{this.state==="playing"&&this._emitEvent("timeupdate",{...t,effectiveTime:t.currentTime,leadInProgress:null})}),this.midiPlayer.on("beatChanged",t=>{this.state==="playing"&&this._emitEvent("beatChanged",{...t,isLeadIn:!1})}),this.midiPlayer.on("barChanged",t=>{this.state==="playing"&&this._emitEvent("barChanged",t)}),this.midiPlayer.on("ended",t=>{this.state==="playing"&&(this.state="stopped",this._stopMetronome(),this._stopTimeUpdateLoop(),this._emitEvent("playbackEnded",t))}),this.midiPlayer.on("error",t=>{this._emitEvent("error",t)})}_validateConfig(){if(this.metronomeConfig.volume<0||this.metronomeConfig.volume>1)throw new Error("Metronome volume must be between 0.0 and 1.0");if(this.leadInConfig.bars<1)throw new Error("Lead-in bars must be at least 1")}async _startLeadIn(t){this.state="lead-in";const e=this.startupConfig.delayMs/1e3;this.leadInStartTime=this.audioEngine.audioContext.currentTime+e,this.leadInProgress=0,this.leadInData=this._calculateLeadInBeats(),this._emitEvent("leadInStarted",{totalBeats:this.leadInData.totalBeats,duration:this.leadInData.duration,bars:this.leadInConfig.bars,startupDelayMs:this.startupConfig.delayMs}),setTimeout(()=>{this.state==="lead-in"&&this._startLeadInScheduling(t)},this.startupConfig.delayMs),this._startTimeUpdateLoop()}_calculateLeadInBeats(){const t=this.midiPlayer.beats,e=this.frozenTime,i=this.leadInConfig.bars;let s=t.length-1,n=.5;for(;t[s].time>e;)s--;const a=t[s],r=t[s+1];r?n=r.time-a.time:s>0&&(n=a.time-t[s-1].time);const q=this.midiPlayer.playbackSpeed||1,o=n/q,h=a.timeSig===1,l=h&&r?r.timeSig:a.timeSig,A=h?l-1:a.beat>1?a.beat-1:0,d=i*l+A;return{totalBeats:d,duration:d*o,beatSequence:this._generateBeatSequence(d,o,l),beatsPerBar:l,startBeat:a}}_generateBeatSequence(t,e,i){const s=[];for(let n=0;n<t;n++){const a=n%i+1;s.push({beat:a,isAccent:a===1,time:n*e,absoluteTime:this.leadInStartTime+n*e})}return s}_startLeadInScheduling(t){this.leadInBeatIndex=0,this.leadInScheduledBeats=new Set;const e=this.leadInData.beatSequence,i=10;this.leadInStartTime=this.audioEngine.audioContext.currentTime,this.leadInInterval=setInterval(()=>{const s=this.audioEngine.audioContext.currentTime-this.leadInStartTime;for(this.leadInProgress=Math.min(1,s/this.leadInData.duration);this.leadInBeatIndex<e.length;){const n=e[this.leadInBeatIndex],a=n.time-s;if(a>.05)break;if(!this.leadInScheduledBeats.has(this.leadInBeatIndex)&&a>=-.05&&a<=.05){const r=a<=0?this.audioEngine.audioContext.currentTime+.01:this.audioEngine.audioContext.currentTime+a;this._scheduleTickAtTime(r,n.isAccent),this._emitEvent("beatChanged",{bar:Math.floor(this.leadInBeatIndex/this.leadInData.beatsPerBar)+1,beat:n.beat,repeat:1,time:this.frozenTime,isLeadIn:!0}),this.leadInScheduledBeats.add(this.leadInBeatIndex)}this.leadInBeatIndex++}this.leadInProgress>=1&&this._completeLeadIn(t)},i)}_completeLeadIn(t){const e=this.leadInStartTime+this.leadInData.duration;this._stopLeadIn(),this._emitEvent("leadInEnded",{}),this.leadInData=null,this.leadInProgress=null,this.leadInStartTime=null,this._startMidiPlaybackAt(e,t,!0)}async _startMidiPlaybackAt(t,e,i=!0){this.state="playing",i&&this._emitEvent("playbackStarted",{startupDelayMs:0,scheduledStartTime:t}),this.midiPlayer.playAt(t),e&&this._startMetronomeAt(t),this.timeUpdateInterval||this._startTimeUpdateLoop()}async _startMidiPlayback(t,e=!0,i=!1){this.state="playing",this._resetMetronomeBeatTracking(),e&&this._emitEvent("playbackStarted",{startupDelayMs:i?0:this.startupConfig.delayMs});const s=()=>{if(this.state==="playing")try{this.midiPlayer.play(),t&&this._startMetronome()}catch(n){this.state="stopped",this._emitEvent("error",n)}};i?s():setTimeout(s,this.startupConfig.delayMs),this.timeUpdateInterval||this._startTimeUpdateLoop()}_startMetronome(){!this.metronomeConfig.enabled||this.state!=="playing"||(this._scheduleMetronomeTicks(),this.metronomeScheduleInterval=setInterval(()=>{this._scheduleMetronomeTicks()},50))}_startMetronomeAt(t){this.metronomeConfig.enabled&&(this.metronomeScheduledStartTime=t,this._resetMetronomeBeatTracking(),this._scheduleMetronomeTicksAt(t),this.metronomeScheduleInterval=setInterval(()=>{this._scheduleMetronomeTicks()},50))}_startMetronomeIfEnabled(){this.metronomeConfig.enabled&&this._startMetronome()}_scheduleMetronomeTicksAt(t){if(!this.metronomeConfig.enabled)return;const e=this.midiPlayer.beats;if(!e||e.length===0)return;const i=this.midiPlayer.getCurrentTime(),s=.1;for(let n=this.nextBeatIndex;n<e.length;n++){const a=e[n],r=t+(a.time-i)/this.midiPlayer.playbackSpeed;if(r>this.audioEngine.audioContext.currentTime+s)break;if(r>=this.audioEngine.audioContext.currentTime-.01){const q=Math.max(r,this.audioEngine.audioContext.currentTime+.001),o=a.isDownbeat||a.beat===1;this._scheduleTickAtTime(q,o),this.nextBeatIndex=n+1}}}_scheduleMetronomeTicks(){if(!this.metronomeConfig.enabled)return;const t=this.midiPlayer.getCurrentTime(),e=this.midiPlayer.beats;if(!e||e.length===0)return;for(;this.nextBeatIndex<e.length&&e[this.nextBeatIndex].time<t-.025;)this.nextBeatIndex++;const s=t+.15;for(;this.nextBeatIndex<e.length;){const n=e[this.nextBeatIndex],a=n.time-t;if(n.time>s)break;if(a>=-.025&&a<=.15){const r=this.audioEngine.audioContext.currentTime+.005,q=this.audioEngine.audioContext.currentTime+Math.max(a,.005),o=Math.max(r,q),h=n.beat===1;this._scheduleTickAtTime(o,h)}this.nextBeatIndex++}}async _scheduleTickAtTime(t,e){try{await this.audioEngine.playMetronomeTick(t,e,this.metronomeConfig.volume)}catch(i){console.warn("Failed to schedule metronome tick:",i)}}_resetMetronomeBeatTracking(){const t=this.getCurrentTime(),e=this.midiPlayer.beats;if(!e||e.length===0){this.nextBeatIndex=0;return}for(this.nextBeatIndex=0;this.nextBeatIndex<e.length&&e[this.nextBeatIndex].time<t-.01;)this.nextBeatIndex++}_stopMetronome(){this.metronomeScheduleInterval&&(clearInterval(this.metronomeScheduleInterval),this.metronomeScheduleInterval=null)}_stopLeadIn(){this.leadInInterval&&(clearInterval(this.leadInInterval),this.leadInInterval=null)}_pauseLeadIn(){this._stopLeadIn()}_resumeLeadIn(){if(!this.leadInData||this.leadInProgress===null)return;this.state="lead-in";const t=this.audioEngine.audioContext.currentTime,e=this.leadInProgress*this.leadInData.duration;this.leadInStartTime=t-e,this._startLeadInScheduling(this.metronomeConfig.enabled),this._startTimeUpdateLoop()}_startTimeUpdateLoop(){this.timeUpdateInterval||(this.timeUpdateInterval=setInterval(()=>{this.state==="lead-in"?this._emitEvent("timeupdate",{currentTime:this.frozenTime,effectiveTime:this.frozenTime,leadInProgress:this.leadInProgress||0}):this.state},100))}_stopTimeUpdateLoop(){this.timeUpdateInterval&&(clearInterval(this.timeUpdateInterval),this.timeUpdateInterval=null)}_emitEvent(t,e){(this.eventBus.all.get(t)||[]).forEach(s=>{try{s(e)}catch(n){console.error(`Error in ${t} event listener:`,n)}})}destroy(){this.stop(),this._stopLeadIn(),this._stopMetronome(),this._stopTimeUpdateLoop(),this.eventBus.all.clear()}}exports.AudioEngine=p;exports.AudioEngineUtils=I;exports.BeatMapper=k;exports.ChannelHandle=g;exports.MidiParser=U;exports.MidiPlayer=L;exports.PlaybackManager=F;exports.SpessaSynthAudioEngine=z;exports.SpessaSynthChannelHandle=_;
1
+ "use strict";var M=Object.create;var T=Object.defineProperty;var P=Object.getOwnPropertyDescriptor;var E=Object.getOwnPropertyNames;var S=Object.getPrototypeOf,B=Object.prototype.hasOwnProperty;var O=(c,t,e,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of E(t))!B.call(c,s)&&s!==e&&T(c,s,{get:()=>t[s],enumerable:!(i=P(t,s))||i.enumerable});return c};var D=(c,t,e)=>(e=c!=null?M(S(c)):{},O(t||!c||!c.__esModule?T(e,"default",{value:c,enumerable:!0}):e,c));Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const N="data:audio/mpeg;base64,//uUxAAAAAAAAAAAAAAAAAAAAAAAWGluZwAAAA8AAAAMAAAPMAA4ODg4ODg4OFRUVFRUVFRUcHBwcHBwcHCFhYWFhYWFhYWampqampqamq+vr6+vr6+vwMDAwMDAwMDA0tLS0tLS0tLj4+Pj4+Pj4/Hx8fHx8fHx8fj4+Pj4+Pj4//////////8AAABQTEFNRTMuMTAwBLkAAAAAAAAAABUgJAUlgQAB4AAADzAG7EJ1AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//s0xPaAClDTI6OxLwCwkeLoEJkoqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq//sUxNoDwAAB/gAAACAAADSAAAAEqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq";class g{constructor(t,e={}){if(new.target===g)throw new Error("AudioEngine is abstract and cannot be instantiated directly");this.audioContext=t,this.options=e,this.isInitialized=!1,this.channels=new WeakMap,this.activeChannels=new Set}async initialize(t){throw new Error("initialize() must be implemented by subclass")}createChannel(t,e={}){throw new Error("createChannel() must be implemented by subclass")}allSoundsOff(){throw new Error("allSoundsOff() must be implemented by subclass")}async playMetronomeTick(t,e,i){try{await this._ensureMetronomeBuffersLoaded();const s=e?this.accentTickBuffer:this.regularTickBuffer;if(!s){console.warn("Metronome buffer not available");return}const n=this.audioContext.createBufferSource();n.buffer=s;const a=this.audioContext.createGain();a.gain.value=i,n.connect(a);const r=this.getMetronomeOutput();r?a.connect(r):a.connect(this.audioContext.destination);const q=Math.max(t,this.audioContext.currentTime);n.start(q)}catch(s){console.warn("Buffer metronome playback failed:",s)}}getMetronomeOutput(){return this._metronomeOutput||(this._metronomeOutput=this.audioContext.createGain(),this._metronomeOutput.gain.value=1,this._metronomeOutput.connect(this.audioContext.destination)),this._metronomeOutput}async _ensureMetronomeBuffersLoaded(){if(!(this.regularTickBuffer&&this.accentTickBuffer)){try{if(typeof fetch<"u"){const[t,e]=await Promise.all([fetch(N),fetch(z)]),[i,s]=await Promise.all([t.arrayBuffer(),e.arrayBuffer()]),[n,a]=await Promise.all([this.audioContext.decodeAudioData(i),this.audioContext.decodeAudioData(s)]);this.regularTickBuffer=n,this.accentTickBuffer=a;return}}catch(t){console.warn("Failed to load metronome sounds:",t)}this.regularTickBuffer=this.audioContext.createBuffer(2,1024,this.audioContext.sampleRate),this.accentTickBuffer=this.audioContext.createBuffer(2,1024,this.audioContext.sampleRate)}}getActiveChannels(){return Array.from(this.activeChannels)}destroy(){this.allSoundsOff(),this._metronomeOutput&&(this._metronomeOutput.disconnect(),this._metronomeOutput=null),this.regularTickBuffer=null,this.accentTickBuffer=null,this.activeChannels.clear(),this.isInitialized=!1}_validateInitialized(){if(!this.isInitialized)throw new Error("AudioEngine not initialized. Call initialize() first.")}_registerChannel(t){this.activeChannels.add(t)}_unregisterChannel(t){this.activeChannels.delete(t),this.channels.delete(t)}}class y{constructor(t,e,i={}){if(new.target===y)throw new Error("ChannelHandle is abstract and cannot be instantiated directly");this.engine=t,this.partId=e,this.options={initialVolume:1,...i},this.isDestroyed=!1,this.noteRefCounts=new Map,this.scheduledEvents=new Map,this.activeNotes=new Set}getOutputNode(){throw new Error("getOutputNode() must be implemented by subclass")}noteOn(t,e){this._validateActive();const i=this.noteRefCounts.get(t)||0;this.noteRefCounts.set(t,i+1),this._actualNoteOn(t,e),i===0&&this.activeNotes.add(t)}noteOff(t){this._validateActive();const e=this.noteRefCounts.get(t)||0;if(e<=0)return;const i=e-1;this.noteRefCounts.set(t,i),i===0&&(this._actualNoteOff(t),this.activeNotes.delete(t),this.noteRefCounts.delete(t))}playNote(t,e,i,s){this._validateActive();const n=this.engine.audioContext.currentTime,a=`${this.partId}_${t}_${e}_${Date.now()}`;let r=t,q=s;if(t<n){const A=n-t;r=n,q=Math.max(0,s-A)}if(q<=0)return a;const o=Math.max(0,(r-n)*1e3),l=setTimeout(()=>{this.noteOn(e,i),this.scheduledEvents.delete(`${a}_on`)},o),h=o+q*1e3,d=setTimeout(()=>{this.noteOff(e),this.scheduledEvents.delete(`${a}_off`)},h);return this.scheduledEvents.set(`${a}_on`,l),this.scheduledEvents.set(`${a}_off`,d),a}allNotesOff(){this._validateActive(),this.scheduledEvents.forEach(t=>{clearTimeout(t)}),this.scheduledEvents.clear(),this.activeNotes.forEach(t=>{this._actualNoteOff(t)}),this.noteRefCounts.clear(),this.activeNotes.clear()}_actualNoteOn(t,e){throw new Error("_actualNoteOn() must be implemented by subclass")}_actualNoteOff(t){throw new Error("_actualNoteOff() must be implemented by subclass")}async setInstrument(t){throw new Error("setInstrument() must be implemented by subclass")}getInstrument(){throw new Error("getInstrument() must be implemented by subclass")}setVolume(t){throw new Error("setVolume() must be implemented by subclass")}getVolume(){throw new Error("getVolume() must be implemented by subclass")}getPartId(){return this.partId}isActive(){return!this.isDestroyed&&this.engine.isInitialized&&this.engine.activeChannels.has(this)}destroy(){if(!this.isDestroyed){this.allNotesOff();const t=this.getOutputNode();t&&t.disconnect(),this.noteRefCounts.clear(),this.scheduledEvents.clear(),this.activeNotes.clear(),this.engine._unregisterChannel(this),this.isDestroyed=!0}}_validateActive(){if(this.isDestroyed)throw new Error("Channel has been destroyed");if(!this.engine.isInitialized)throw new Error("AudioEngine is not initialized")}}const I={piano:0,bright_piano:1,electric_grand:2,honky_tonk:3,electric_piano_1:4,electric_piano_2:5,harpsichord:6,clavinet:7,celesta:8,glockenspiel:9,music_box:10,vibraphone:11,marimba:12,xylophone:13,tubular_bells:14,dulcimer:15,drawbar_organ:16,percussive_organ:17,rock_organ:18,church_organ:19,reed_organ:20,accordion:21,harmonica:22,tango_accordion:23,organ:19,nylon_guitar:24,steel_guitar:25,electric_guitar_jazz:26,electric_guitar_clean:27,electric_guitar_muted:28,overdriven_guitar:29,distortion_guitar:30,guitar_harmonics:31,guitar:24,acoustic_bass:32,electric_bass_finger:33,electric_bass_pick:34,fretless_bass:35,slap_bass_1:36,slap_bass_2:37,synth_bass_1:38,synth_bass_2:39,bass:32,violin:40,viola:41,cello:42,contrabass:43,tremolo_strings:44,pizzicato_strings:45,orchestral_harp:46,timpani:47,strings:48,strings_ensemble:48,slow_strings:49,synth_strings_1:50,synth_strings_2:51,choir_aahs:52,voice_oohs:53,synth_voice:54,orchestra_hit:55,trumpet:56,trombone:57,tuba:58,muted_trumpet:59,french_horn:60,brass_section:61,synth_brass_1:62,synth_brass_2:63,soprano_sax:64,alto_sax:65,tenor_sax:66,baritone_sax:67,oboe:68,english_horn:69,bassoon:70,clarinet:71,saxophone:64,piccolo:72,flute:73,recorder:74,pan_flute:75,blown_bottle:76,shakuhachi:77,whistle:78,ocarina:79,lead_1_square:80,lead_2_sawtooth:81,lead_3_calliope:82,lead_4_chiff:83,lead_5_charang:84,lead_6_voice:85,lead_7_fifths:86,lead_8_bass:87,pad_1_new_age:88,pad_2_warm:89,pad_3_polysynth:90,pad_4_choir:91,pad_5_bowed:92,pad_6_metallic:93,pad_7_halo:94,pad_8_sweep:95,fx_1_rain:96,fx_2_soundtrack:97,fx_3_crystal:98,fx_4_atmosphere:99,fx_5_brightness:100,fx_6_goblins:101,fx_7_echoes:102,fx_8_sci_fi:103,sitar:104,banjo:105,shamisen:106,koto:107,kalimba:108,bag_pipe:109,fiddle:110,shanai:111,tinkle_bell:112,agogo:113,steel_drums:114,woodblock:115,taiko_drum:116,melodic_tom:117,synth_drum:118,reverse_cymbal:119,guitar_fret_noise:120,breath_noise:121,seashore:122,bird_tweet:123,telephone_ring:124,helicopter:125,applause:126,gunshot:127},L=Object.entries(I).reduce((c,[t,e])=>(c[e]=t,c),{});class _{static getInstrumentProgram(t){if(typeof t=="number")return t;const e=I[t.toLowerCase()];return e!==void 0?e:0}static getProgramName(t){return L[t]||`Program ${t}`}static generateNoteId(t,e,i){return`${t}_${e}_${Math.round(i)}`}}class k extends y{constructor(t,e,i,s={}){super(t,e,s),this.midiChannel=i,this.currentVolume=s.initialVolume||1,this.currentInstrument=s.instrument||"piano",this.outputGain=null,this._setupOutputNode(),this.setVolume(this.currentVolume),s.instrument&&this.setInstrument(s.instrument)}getOutputNode(){return this.outputGain}_actualNoteOn(t,e){const i=this.engine._getSynthesizer();if(i&&i.noteOn){const s=Math.round(e*this.currentVolume);i.noteOn(this.midiChannel,t,s)}}_actualNoteOff(t){const e=this.engine._getSynthesizer();e&&e.noteOff&&e.noteOff(this.midiChannel,t)}async setInstrument(t){this._validateActive();const e=_.getInstrumentProgram(t);this.currentInstrument=t;const i=this.engine._getSynthesizer();i&&i.programChange?i.programChange(this.midiChannel,e):console.warn("Cannot set instrument: synthesizer not available or no programChange method")}getInstrument(){return this.currentInstrument}setVolume(t){this._validateActive(),t=Math.max(0,Math.min(1,t)),this.currentVolume=t;const e=Math.round(t*127),i=this.engine._getSynthesizer();i&&i.controllerChange&&i.controllerChange(this.midiChannel,7,e)}getVolume(){return this.currentVolume}getMidiChannel(){return this.midiChannel}getActiveNoteCount(){return this.activeNotes.size}playNote(t,e,i,s){this._validateActive();const n=`${this.partId}_${t}_${e}_${Date.now()}`,a=this.engine._getSynthesizer();if(a&&a.post){const r=Math.round(i*this.currentVolume);a.post({channelNumber:this.midiChannel,type:"midiMessage",data:{messageData:[144|this.midiChannel,e,r],channelOffset:0,force:!1,options:{time:t}}}),a.post({channelNumber:this.midiChannel,type:"midiMessage",data:{messageData:[128|this.midiChannel,e,0],channelOffset:0,force:!1,options:{time:t+s}}})}else return super.playNote(t,e,i,s);return n}allNotesOff(){this._validateActive();const t=this.engine._getSynthesizer();t&&t.post?t.post({channelNumber:this.midiChannel,type:"midiMessage",data:{messageData:[176|this.midiChannel,123,0],channelOffset:0,force:!1,options:{time:this.engine.audioContext.currentTime}}}):super.allNotesOff()}destroy(){if(!this.isDestroyed){const t=this.engine._getIndividualOutput(this.midiChannel);this.outputGain&&this.outputGain!==t&&this.outputGain.disconnect(),this.outputGain=null}super.destroy()}_setupOutputNode(){const t=this.engine._getIndividualOutput(this.midiChannel);t?this.outputGain=t:(console.warn(`No individual output available for MIDI channel ${this.midiChannel}, using fallback`),this.outputGain=this.engine.audioContext.createGain(),this.outputGain.gain.value=this.currentVolume)}}class U extends g{constructor(t,e={}){super(t,e),this.synthesizer=null,this.soundfont=null,this.channelCounter=0,this.partToMidiChannel=new Map,this.midiChannelToPart=new Map,this.individualOutputs=[]}async initialize(t){const{WorkletSynthesizer:e}=await import("spessasynth_lib");let i;if(typeof t=="string")i=await this._loadSoundfontFromPath(t);else if(t instanceof ArrayBuffer)i=t;else throw new Error("Invalid soundfont data type. Expected string path or ArrayBuffer.");await this._loadAudioWorkletSafely(),this._setupIndividualOutputs(),this.dummyTarget=this.audioContext.createGain(),await new Promise(s=>setTimeout(s,50)),this.synthesizer=new e(this.audioContext),await this.synthesizer.soundBankManager.addSoundBank(i,"main"),await this.synthesizer.isReady,this._connectIndividualOutputs(),this._initializeMetronomeChannel(),this.isInitialized=!0}createChannel(t,e={}){if(this._validateInitialized(),this.partToMidiChannel.has(t))throw new Error(`Channel for part '${t}' already exists`);const i=this.channelCounter;if(i>=15)throw new Error("Maximum number of musical part channels (15) exceeded. Channel 15 is reserved for metronome.");this.channelCounter++,this.partToMidiChannel.set(t,i),this.midiChannelToPart.set(i,t);const s=new k(this,t,i,e);return this._registerChannel(s),e.instrument&&s.setInstrument(e.instrument),s}allSoundsOff(){this.synthesizer&&this.midiChannelToPart.forEach((t,e)=>{this.synthesizer.controllerChange&&this.synthesizer.controllerChange(e,120,0)})}clearAllChannels(){this.allSoundsOff(),this.partToMidiChannel.clear(),this.midiChannelToPart.clear(),this.channelCounter=0}destroy(){this.allSoundsOff(),this.synthesizer&&typeof this.synthesizer.disconnect=="function"&&this.synthesizer.disconnect(),this.individualOutputs.forEach(t=>{t&&t.disconnect&&t.disconnect()}),this.individualOutputs=[],this.dummyTarget&&(this.dummyTarget.disconnect(),this.dummyTarget=null),this.partToMidiChannel.clear(),this.midiChannelToPart.clear(),this.channelCounter=0,super.destroy(),this.synthesizer=null,this.soundfont=null}getMidiChannelForPart(t){return this.partToMidiChannel.has(t)?this.partToMidiChannel.get(t):null}_getSynthesizer(){return this.synthesizer}_getIndividualOutput(t){return t>=0&&t<this.individualOutputs.length?this.individualOutputs[t]:null}getMetronomeChannel(){const t=this._getIndividualOutput(15);return console.log("Metronome channel 15 output:",t?"Available":"NULL",`(total outputs: ${this.individualOutputs.length})`),t}_setupIndividualOutputs(){this.individualOutputs=[];for(let t=0;t<16;t++){const e=this.audioContext.createGain();e.gain.value=1,this.individualOutputs.push(e)}}_connectIndividualOutputs(){try{this.synthesizer&&this.synthesizer.connectIndividualOutputs?this.synthesizer.connectIndividualOutputs(this.individualOutputs):(console.warn("Synthesizer does not support individual outputs, using master output only"),this.synthesizer&&this.synthesizer.connect&&this.audioContext.destination&&this.synthesizer.connect(this.audioContext.destination))}catch(t){console.warn("Failed to connect individual outputs:",t.message),console.warn("Falling back to master output routing")}}async _loadSoundfontFromPath(t){const e=await fetch(t);if(!e.ok)throw new Error(`Failed to load soundfont: ${e.status} ${e.statusText}`);return await e.arrayBuffer()}async _loadAudioWorkletSafely(){const t="/node_modules/spessasynth_lib/dist/spessasynth_processor.min.js";for(let i=1;i<=5;i++)try{await this.audioContext.audioWorklet.addModule(t);return}catch(s){if(console.warn(`AudioWorklet loading failed (attempt ${i}/5):`,s.message),i===5)throw new Error(`AudioWorklet failed after 5 attempts: ${s.message}`);const n=i*500;await new Promise(a=>setTimeout(a,n))}}_initializeMetronomeChannel(){try{const t=this._getSynthesizer();if(!t){console.warn("Cannot initialize metronome channel: synthesizer not available");return}const e=15;t.programChange&&(t.programChange(e,115),console.log("Metronome channel 15 initialized with woodblock instrument (115)")),t.controllerChange&&t.controllerChange(e,7,127)}catch(t){console.warn("Failed to initialize metronome channel:",t)}}async playMetronomeTick(t,e,i){try{const s=this.getMetronomeChannel(),n=this._getSynthesizer();if(!s||!n)return super.playMetronomeTick(t,e,i);const a=15,r=e?86:60,o=Math.round(Math.min(127,Math.max(0,i*(e?127:100)))),l=this.audioContext.currentTime,h=Math.max(t,l),d=h-l;n.post?(n.post({channelNumber:a,type:"midiMessage",data:{messageData:[144|a,r,o],channelOffset:0,force:!1,options:{time:h}}}),n.post({channelNumber:a,type:"midiMessage",data:{messageData:[128|a,r,0],channelOffset:0,force:!1,options:{time:h+.1}}})):d<=.01?(n.noteOn&&n.noteOn(a,r,o),setTimeout(()=>{n.noteOff&&n.noteOff(a,r)},100)):setTimeout(()=>{n.noteOn&&n.noteOn(a,r,o),setTimeout(()=>{n.noteOff&&n.noteOff(a,r)},100)},d*1e3)}catch(s){return console.warn("MIDI metronome failed, falling back to buffers:",s),super.playMetronomeTick(t,e,i)}}getMetronomeOutput(){return!this.individualOutputs||this.individualOutputs.length<16?null:this.individualOutputs[15]}}class C{constructor(){this.partNames=["soprano","alto","tenor","bass","treble","mezzo","baritone","s","a","t","b","satb"],this.parsedData={parts:{},barStructure:[],metadata:{}}}async parse(t){try{const e=await this._parseMidiBuffer(t);return this._extractMetadata(e),this._extractBarStructure(e),this._extractParts(e),this.parsedData}catch(e){throw console.error("Error parsing MIDI file:",e),e}}async _parseMidiBuffer(t){const e=new Uint8Array(t);if(!(e[0]===77&&e[1]===84&&e[2]===104&&e[3]===100))throw new Error("Not a valid MIDI file");const i=this._bytesToNumber(e.slice(4,8)),s=this._bytesToNumber(e.slice(8,10)),n=this._bytesToNumber(e.slice(10,12)),a=this._bytesToNumber(e.slice(12,14)),r=a&32768?null:a,q={format:s,ticksPerBeat:r,tracks:[],duration:0};let o=8+i;for(let l=0;l<n;l++)if(e[o]===77&&e[o+1]===84&&e[o+2]===114&&e[o+3]===107){const h=this._bytesToNumber(e.slice(o+4,o+8)),d=e.slice(o+8,o+8+h),A=this._parseTrack(d);q.tracks.push(A),o+=8+h}else throw new Error(`Invalid track header at position ${o}`);return q}_parseTrack(t){const e={notes:[],name:null,lyrics:[],events:[],duration:0};let i=0,s=0,n=null;for(;i<t.length;){let a=0,r=0;do r=t[i++],a=a<<7|r&127;while(r&128);s+=a,r=t[i++];let q=r;if((r&128)===0){if(n===null)throw new Error("Running status byte encountered before status byte");q=n,i--}else n=q;if(q===255){const o=t[i++],l=this._readVariableLengthValue(t,i);i+=l.bytesRead;const h=t.slice(i,i+l.value);switch(i+=l.value,o){case 3:e.name=this._bytesToString(h);break;case 1:e.events.push({type:"text",text:this._bytesToString(h),tick:s});break;case 5:e.lyrics.push({text:this._bytesToString(h),tick:s});break;case 81:const d=this._bytesToNumber(h),A=Math.round(6e7/d);e.events.push({type:"tempo",bpm:A,tick:s});break;case 88:e.events.push({type:"timeSignature",numerator:h[0],denominator:Math.pow(2,h[1]),tick:s});break;case 47:e.duration=s;break}}else if((q&240)===144){const o=q&15,l=t[i++],h=t[i++];h>0?e.notes.push({type:"noteOn",noteNumber:l,velocity:h,tick:s,channel:o}):e.notes.push({type:"noteOff",noteNumber:l,tick:s,channel:o})}else if((q&240)===128){const o=q&15,l=t[i++];t[i++],e.notes.push({type:"noteOff",noteNumber:l,tick:s,channel:o})}else if(q===240||q===247){const o=this._readVariableLengthValue(t,i);i+=o.bytesRead+o.value}else if((q&240)===176){const o=q&15,l=t[i++],h=t[i++];e.events.push({type:"controller",controllerNumber:l,value:h,channel:o,tick:s})}else if((q&240)===192){const o=q&15,l=t[i++];e.events.push({type:"programChange",programNumber:l,channel:o,tick:s})}else if((q&240)===208){const o=q&15,l=t[i++];e.events.push({type:"channelAftertouch",pressure:l,channel:o,tick:s})}else if((q&240)===224){const o=q&15,l=t[i++],d=(t[i++]<<7|l)-8192;e.events.push({type:"pitchBend",value:d,channel:o,tick:s})}else if((q&240)===160){const o=q&15,l=t[i++],h=t[i++];e.events.push({type:"noteAftertouch",noteNumber:l,pressure:h,channel:o,tick:s})}else console.warn(`Unknown event type: ${q.toString(16)} at position ${i-1}`),i++}return e}_extractMetadata(t){const e={title:null,composer:null,partNames:[],format:t.format,ticksPerBeat:t.ticksPerBeat};t.tracks.forEach((i,s)=>{if(i.name&&!e.title&&(e.title=i.name),i.events.filter(n=>n.type==="text").forEach(n=>{const a=n.text.toLowerCase();(a.includes("compos")||a.includes("by"))&&!e.composer&&(e.composer=n.text)}),i.name){const n=i.name.toLowerCase();for(const a of this.partNames)if(n.includes(a)){e.partNames.push({index:s,name:i.name});break}}}),this.parsedData.metadata=e}_extractBarStructure(t){const e=t.ticksPerBeat||480,i=[];t.tracks.forEach(l=>{l.events.forEach(h=>{(h.type==="timeSignature"||h.type==="tempo")&&i.push(h)})}),i.sort((l,h)=>l.tick-h.tick);let s=0;t.tracks.forEach(l=>{l.notes&&l.notes.forEach(h=>{h.type==="noteOff"&&h.tick>s&&(s=h.tick)})}),s===0&&(s=e*8);const n=[],a=i.filter(l=>l.type==="timeSignature").sort((l,h)=>l.tick-h.tick);let r={numerator:4,denominator:4},q=0,o=0;for(;q<s;){for(;o<a.length&&a[o].tick<=q;)r=a[o],o++;let l;l=q+e*4*r.numerator/r.denominator,i.filter(u=>u.type==="tempo"&&u.tick>=q&&u.tick<l);const h=r.numerator,d=[],A=e*(4/r.denominator);for(let u=0;u<h;u++){const m=q+u*A,f=this._ticksToTime(m,t);d.push(f)}n.push({sig:[r.numerator,r.denominator],beats:d}),q=l}this.parsedData.barStructure=n}_extractParts(t){const e={},i=t.ticksPerBeat;t.tracks.forEach((s,n)=>{if(!s.notes.length)return;let a=null;if(s.name){const u=s.name.toLowerCase();for(const m of this.partNames)if(m.length===1){if(u===m){a=m;break}}else if(u.includes(m)){a=m;break}}a||(a=s.name||`Track ${n+1}`),a==="s"&&(a="soprano"),a==="a"&&(a="alto"),a==="t"&&(a="tenor"),a==="b"&&(a="bass");let r=a,q=2;for(;e[r];)r=`${a} ${q}`,q++;a=r;const o=[],l={};s.notes.forEach(u=>{if(u.type==="noteOn")l[u.noteNumber]={tick:u.tick,velocity:u.velocity};else if(u.type==="noteOff"&&l[u.noteNumber]){const m=l[u.noteNumber],f=u.tick-m.tick;o.push({pitch:u.noteNumber,name:this._midiNoteToName(u.noteNumber),startTick:m.tick,endTick:u.tick,duration:f,startTime:this._ticksToTime(m.tick,t),endTime:this._ticksToTime(u.tick,t),velocity:m.velocity}),delete l[u.noteNumber]}});const h=s.lyrics.map(u=>({text:u.text,tick:u.tick,time:u.tick/i}));o.sort((u,m)=>u.startTick-m.startTick);const d=s.events.filter(u=>u.type==="programChange").map(u=>({programNumber:u.programNumber,tick:u.tick,time:this._ticksToTime(u.tick,t)})).sort((u,m)=>u.tick-m.tick),A=d.length>0?d[0].programNumber:0;e[a]={notes:o,lyrics:h,trackIndex:n,programChanges:d,defaultInstrument:A}}),this.parsedData.parts=e}_midiNoteToName(t){const e=["C","C#","D","D#","E","F","F#","G","G#","A","A#","B"],i=Math.floor(t/12)-1;return`${e[t%12]}${i}`}_bytesToNumber(t){let e=0;for(let i=0;i<t.length;i++)e=e<<8|t[i];return e}_bytesToString(t){return new TextDecoder().decode(t)}_readVariableLengthValue(t,e){let i=0,s,n=0;do s=t[e+n++],i=i<<7|s&127;while(s&128);return{value:i,bytesRead:n}}_ticksToTime(t,e){const i=e.ticksPerBeat||480,s=[];e.tracks.forEach(q=>{q.events.forEach(o=>{o.type==="tempo"&&s.push(o)})}),s.sort((q,o)=>q.tick-o.tick);let n=0,a=0,r=120;for(const q of s){if(q.tick>t)break;if(q.tick>a){const l=(q.tick-a)/i*(60/r);n+=l,a=q.tick}r=q.bpm}if(t>a){const o=(t-a)/i*(60/r);n+=o}return n}}class x{constructor(){this.barOrder=[],this.beatTable=[]}mapBeats(t,e){try{return this.barOrder=this.generateBarOrder(e.sections,e.order),this.beatTable=this.generateBeatTable(this.barOrder,t.barStructure),this.beatTable}catch(i){throw console.error("Error mapping beats:",i),i}}generateBarOrder(t,e){const i=[],s={};for(const n of e){const a=t[n.section];if(!a)throw new Error(`Invalid section index: ${n.section}`);const r=n.section;s[r]||(s[r]=0),s[r]++;const q=s[r],o=n.from!==void 0?n.from:this._getSectionStartBar(t,n.section),l=n.to!==void 0?n.to:a.to,h=n.as||1;for(let d=o;d<=l;d++)this._shouldPlayBar(a,d,h)&&i.push({barNumber:d,repeat:q,sectionIndex:n.section,voltaTime:h})}return i}generateBeatTable(t,e){const i=[],s={};let n=0,a=0;const r=[...e];for(;a<t.length&&n<r.length;){const q=t[a],o=q.barNumber;if(s[o]===void 0){const A=r[n];if(!A||!A.sig)throw new Error(`Invalid MIDI bar structure at index ${n}`);s[o]=A.sig[0]}const l=s[o];let h=r[n],d=h.sig[0];for(;d<l&&n+1<r.length;){const A=r[n+1],u=[h.sig[0]+A.sig[0],h.sig[1]],m=[...h.beats||[],...A.beats||[]];h={sig:u,beats:m},r[n]=h,r.splice(n+1,1),d=u[0]}if(d>l){const A=l,u=d-l,m=h.beats?h.beats.slice(0,A):[],f=h.beats?h.beats.slice(A):[],b={sig:[A,h.sig[1]],beats:m},v={sig:[u,h.sig[1]],beats:f};r[n]=b,r.splice(n+1,0,v),h=b}this._generateBeatsForBar(i,q,h,l),n++,a++}if(a<t.length)throw new Error(`Ran out of MIDI bars before completing score. Score bar ${a}/${t.length}, MIDI bar ${n}/${r.length}`);return i}_generateBeatsForBar(t,e,i,s){const{beats:n}=i;if(!n||!Array.isArray(n))throw new Error(`Invalid MIDI bar: missing beats array. Got: ${JSON.stringify(i)}`);const a=n.slice(0,s);for(let r=1;r<=s;r++){const o={time:a[r-1],repeat:e.repeat,bar:e.barNumber,beat:r,timeSig:s};t.push(o)}}_getSectionStartBar(t,e){return t[e].pickup!==void 0?0:e===0?1:t[e-1].to+1}_shouldPlayBar(t,e,i){if(!t.voltas)return!0;const s=t.voltas.indexOf(e);return s===-1?!0:s+1===i}}function w(c){return{all:c=c||new Map,on:function(t,e){var i=c.get(t);i?i.push(e):c.set(t,[e])},off:function(t,e){var i=c.get(t);i&&(e?i.splice(i.indexOf(e)>>>0,1):c.set(t,[]))},emit:function(t,e){var i=c.get(t);i&&i.slice().map(function(s){s(e)}),(i=c.get("*"))&&i.slice().map(function(s){s(t,e)})}}}class p{constructor(t,e,i=null,s=null){if(!t||!t.isInitialized)throw new Error("Initialized AudioEngine is required");if(!e)throw new Error("Parsed MIDI data is required");this.audioEngine=t,this.instrumentMap=i||{},this.parsedData=e,this._isPlaying=!1,this._currentTime=0,this._totalDuration=0,this.playbackSpeed=1,this.partChannels=new Map,this.partOutputs=new Map,this.playbackStartTime=0,this.lookAheadTime=.05,this.scheduleInterval=null,this.partNotePointers=new Map,this.partProgramPointers=new Map,this.eventBus=w(),this.beatMapper=new x,this.beats=[],this._setupPartChannels(),this._calculateTotalDuration(),this._resetNotePointers(),this._resetProgramPointers();const n=s||this._createDefaultStructureMetadata();this.beats=this.beatMapper.mapBeats(e,n)}play(){this._isPlaying||(this._isPlaying=!0,this.playbackStartTime=this.audioEngine.audioContext.currentTime-this._currentTime/this.playbackSpeed,this._resetNotePointers(),this._resetProgramPointers(),this._schedulePlayback(),this._startTimeUpdateLoop())}playAt(t){this._isPlaying||(this._isPlaying=!0,this.playbackStartTime=t-this._currentTime/this.playbackSpeed,this._resetNotePointers(),this._resetProgramPointers(),this._schedulePlayback(),this._startTimeUpdateLoop())}pause(){this._isPlaying&&(this._isPlaying=!1,this._stopScheduling(),this._stopTimeUpdateLoop())}stop(){this._isPlaying=!1,this._currentTime=0,this._stopScheduling(),this._stopTimeUpdateLoop(),this._resetNotePointers(),this._resetProgramPointers()}skipToTime(t){t=Math.max(0,Math.min(t,this._totalDuration));const e=this._isPlaying;e&&this.pause(),this._currentTime=t,this._resetNotePointers(),this._resetProgramPointers(),e&&this.play()}setPlaybackSpeed(t,e=!0){if(t<=0)throw new Error("Playback speed must be greater than 0");const i=e&&this._isPlaying;i&&this.pause();const s=(this.audioEngine.audioContext.currentTime-this.playbackStartTime)*this.playbackSpeed;this.playbackSpeed=t,i?this.play():this.playbackStartTime=this.audioEngine.audioContext.currentTime-s/this.playbackSpeed}setBar(t,e=0){const i=this.getTimeFromBar(t,e);i!==null&&(this.skipToTime(i),this._emitEvent("barChanged",{bar:t,beat:1,repeat:e,time:i}))}getTimeFromBar(t,e=0){e||(e=1);const i=this.beats.find(s=>s.bar===t&&s.beat===1&&s.repeat===e);return i?i.time:null}getBeatFromTime(t){if(!this.beats.length)return null;let e=null;for(let i=this.beats.length-1;i>=0;i--)if(this.beats[i].time<=t){e=this.beats[i];break}return e}allSoundsOff(){this.audioEngine.allSoundsOff()}getPartOutput(t){return this.partOutputs.get(t)||null}getPartChannel(t){return this.partChannels.get(t)||null}getCurrentTime(){if(this._isPlaying){const t=(this.audioEngine.audioContext.currentTime-this.playbackStartTime)*this.playbackSpeed;this._currentTime=Math.min(t,this._totalDuration)}return this._currentTime}getTotalDuration(){return this._totalDuration}isPlaying(){return this._isPlaying}on(t,e){this.eventBus.on(t,e)}off(t,e){this.eventBus.off(t,e)}_setupPartChannels(){Object.keys(this.parsedData.parts).forEach(t=>{const e=this.parsedData.parts[t],i=this.instrumentMap[t]||{},s=i.instrument!==void 0?i.instrument:e.defaultInstrument!==void 0?e.defaultInstrument:0;try{const n=this.audioEngine.createChannel(t,{instrument:s,initialVolume:i.volume||1});this.partChannels.set(t,n);const a=this.audioEngine.audioContext.createGain();a.gain.value=1;const r=n.getOutputNode();r&&r.connect(a),this.partOutputs.set(t,a)}catch(n){console.error(`Failed to create channel for part '${t}':`,n),this._emitEvent("error",n)}})}_calculateTotalDuration(){let t=0;Object.values(this.parsedData.parts).forEach(e=>{e.notes.forEach(i=>{i.endTime>t&&(t=i.endTime)})}),this._totalDuration=t}_schedulePlayback(){this._stopScheduling(),this._startScheduleLoop()}_startScheduleLoop(){this.scheduleInterval||(this.scheduleInterval=setInterval(()=>{if(!this._isPlaying)return;const e=(this.audioEngine.audioContext.currentTime-this.playbackStartTime)*this.playbackSpeed,i=e+this.lookAheadTime;for(const[s,n]of this.partChannels){const a=this.parsedData.parts[s];if(a){if(a.programChanges&&a.programChanges.length>0){let r=this.partProgramPointers.get(s)||0;const q=a.programChanges;for(;r<q.length&&q[r].time<e;)r++;for(;r<q.length&&q[r].time<=i;){const o=q[r];n.setInstrument(o.programNumber),r++}this.partProgramPointers.set(s,r)}if(a.notes){let r=this.partNotePointers.get(s)||0;const q=a.notes;for(;r<q.length&&q[r].endTime<e;)r++;for(;r<q.length&&q[r].startTime<=i;){const o=q[r];if(o.endTime-o.startTime>=.01){const l=this.playbackStartTime+o.startTime/this.playbackSpeed,h=(o.endTime-o.startTime)/this.playbackSpeed;n.playNote(l,o.pitch,o.velocity,h)}r++}this.partNotePointers.set(s,r)}}}},50))}_resetNotePointers(){const t=this._currentTime;for(const[e]of this.partChannels){const i=this.parsedData.parts[e];if(!i||!i.notes)continue;let s=0;for(;s<i.notes.length&&i.notes[s].endTime<t;)s++;this.partNotePointers.set(e,s)}}_resetProgramPointers(){const t=this._currentTime;for(const[e,i]of this.partChannels){const s=this.parsedData.parts[e];if(!s||!s.programChanges){this.partProgramPointers.set(e,0);continue}let n=0,a=s.defaultInstrument;for(;n<s.programChanges.length&&s.programChanges[n].time<=t;)a=s.programChanges[n].programNumber,n++;i.setInstrument(a),this.partProgramPointers.set(e,n)}}_stopScheduling(){this.scheduleInterval&&(clearInterval(this.scheduleInterval),this.scheduleInterval=null),this.partChannels.forEach(t=>{t.isActive()&&t.allNotesOff()})}_startTimeUpdateLoop(){this.timeUpdateInterval=setInterval(()=>{const t=this.getCurrentTime();this._emitEvent("timeupdate",{currentTime:t});const e=this.getBeatFromTime(t);e&&this._emitEvent("beatChanged",e),t>=this._totalDuration+.05&&(this.stop(),this._emitEvent("ended",{finalTime:t}))},100)}_stopTimeUpdateLoop(){this.timeUpdateInterval&&(clearInterval(this.timeUpdateInterval),this.timeUpdateInterval=null)}_emitEvent(t,e){(this.eventBus.all.get(t)||[]).forEach(s=>{try{s(e)}catch(n){console.error(`Error in ${t} event listener:`,n)}})}_createDefaultStructureMetadata(){return{sections:[{from:1,to:this.parsedData.barStructure.length}],order:[{section:0}]}}destroy(){this.stop(),this.partChannels.forEach(t=>{t.destroy()}),this.partChannels.clear(),this.partOutputs.forEach(t=>{t.disconnect()}),this.partOutputs.clear(),this.partNotePointers.clear(),this.partProgramPointers.clear(),this.eventBus.all.clear()}}class F{constructor(t,e={}){if(!t)throw new Error("AudioEngine is required");this.audioEngine=t,this.midiPlayer=null,this.eventBus=w(),this._parser=new C,this._partOutputsMap=new Map,this.metronomeConfig={enabled:!1,tickInstrument:115,accentInstrument:116,volume:.7,...e.metronome},this.leadInConfig={enabled:!1,bars:1,...e.leadIn},this.startupConfig={delayMs:25,...e.startup},this.state="reset",this.frozenTime=0,this.leadInData=null,this.leadInStartTime=null,this.leadInProgress=null,this.leadInInterval=null,this.timeUpdateInterval=null,this.metronomeScheduleInterval=null,this.nextBeatIndex=0,this._validateConfig()}async load(t,e=null,i=null){this.reset();let s;if(t instanceof p)s=t;else if(t instanceof ArrayBuffer){const n=await this._parser.parse(t);let a=null;typeof e=="string"?a=JSON.parse(e):e&&typeof e=="object"&&(a=e);const r=i||this._createDefaultInstrumentMap(n.parts);s=new p(this.audioEngine,n,r,a)}else if(t&&typeof t=="object"&&t.parts){let n=null;typeof e=="string"?n=JSON.parse(e):e&&typeof e=="object"&&(n=e);const a=i||this._createDefaultInstrumentMap(t.parts);s=new p(this.audioEngine,t,a,n)}else throw new Error("Invalid input type. Expected MidiPlayer, parsed MIDI data, or ArrayBuffer");this._setMidiPlayer(s)}reset(){this.stop(),this.midiPlayer&&(this.midiPlayer.destroy(),this.midiPlayer=null),this.audioEngine.clearAllChannels(),this._partOutputsMap.clear(),this.frozenTime=0,this.leadInData=null,this.leadInProgress=null,this.leadInStartTime=null,this.nextBeatIndex=0,this._stopLeadIn(),this._stopMetronome(),this._stopTimeUpdateLoop(),this.state="reset"}getPartOutputs(){return this._partOutputsMap.entries()}getPartNames(){return Array.from(this._partOutputsMap.keys())}async play(t={}){if(!this.midiPlayer)throw new Error("No MIDI data loaded. Call load() first.");if(!(this.state==="playing"||this.state==="lead-in"))try{const e=t.leadIn!==void 0?t.leadIn:this.leadInConfig.enabled,i=t.metronome!==void 0?t.metronome:this.metronomeConfig.enabled;this.frozenTime===0&&(this.frozenTime=this.midiPlayer.getCurrentTime()),e?await this._startLeadIn(i):await this._startMidiPlayback(i)}catch(e){throw this.state="stopped",this._emitEvent("error",e),e}}pause(){if(!this.midiPlayer||this.state==="stopped"||this.state==="paused")return;const t=this.state;this.state="paused",t==="lead-in"?this._pauseLeadIn():t==="playing"&&(this.midiPlayer.pause(),this._stopMetronome()),this._stopTimeUpdateLoop(),this._emitEvent("playbackPaused",{})}resume(){!this.midiPlayer||this.state!=="paused"||(this.leadInData&&this.leadInProgress!==null&&this.leadInProgress<1?this._resumeLeadIn():(this.state="playing",this._resetMetronomeBeatTracking(),this.midiPlayer.play(),this._startMetronomeIfEnabled(),this._startTimeUpdateLoop(),this._emitEvent("playbackStarted",{})))}stop(){if(this.state==="stopped")return;const t=this.state==="playing";this.state="stopped",this.frozenTime=0,this.leadInData=null,this.leadInProgress=null,this.leadInStartTime=null,this._stopLeadIn(),this._stopMetronome(),this._stopTimeUpdateLoop(),this._resetMetronomeBeatTracking(),t&&this.midiPlayer.stop(),this._emitEvent("playbackStopped",{})}skipToTime(t){if(!this.midiPlayer)throw new Error("No MIDI data loaded. Call load() first.");this.frozenTime=t,this.state!=="lead-in"&&(this.midiPlayer.skipToTime(t),this.state==="playing"&&this.metronomeConfig.enabled&&this._resetMetronomeBeatTracking())}setBar(t,e=0){if(!this.midiPlayer)throw new Error("No MIDI data loaded. Call load() first.");if(this.state==="lead-in"){const i=this.midiPlayer.getTimeFromBar(t,e);i!==null&&(this.frozenTime=i);return}if(this.midiPlayer.setBar(t,e),this.state==="stopped"){const i=this.midiPlayer.getTimeFromBar(t,e);i!==null&&(this.frozenTime=i)}this.state==="playing"&&this.metronomeConfig.enabled&&this._resetMetronomeBeatTracking()}getCurrentTime(){return this.state==="lead-in"||this.state==="paused"&&this.leadInProgress!==null?this.frozenTime:this.midiPlayer?this.midiPlayer.getCurrentTime():0}getLeadInProgress(){return this.leadInProgress}setMetronomeEnabled(t){const e=this.metronomeConfig.enabled;this.metronomeConfig.enabled=t,this.state==="playing"&&(t&&!e?this._startMetronome():!t&&e&&this._stopMetronome()),this._emitEvent("metronomeEnabledChanged",{enabled:t})}setMetronomeSettings(t){if(t.volume!==void 0&&(t.volume<0||t.volume>1))throw new Error("Metronome volume must be between 0.0 and 1.0");if(Object.assign(this.metronomeConfig,t),t.volume!==void 0){const e=this.audioEngine.getMetronomeOutput();e&&e.gain&&(e.gain.value=t.volume)}this._emitEvent("metronomeSettingsChanged",{...t})}setLeadInEnabled(t){this.leadInConfig.enabled=t,this._emitEvent("leadInSettingsChanged",{enabled:t,bars:this.leadInConfig.bars})}setLeadInBars(t){if(t<1)throw new Error("Lead-in bars must be at least 1");this.leadInConfig.bars=t,this._emitEvent("leadInSettingsChanged",{enabled:this.leadInConfig.enabled,bars:t})}setPlaybackSpeed(t,e=!0){if(!this.midiPlayer)throw new Error("No MIDI data loaded. Call load() first.");this.midiPlayer.setPlaybackSpeed(t,e)}getMetronomeSettings(){return{...this.metronomeConfig}}isMetronomeEnabled(){return this.metronomeConfig.enabled}getLeadInSettings(){return{enabled:this.leadInConfig.enabled,bars:this.leadInConfig.bars}}getStartupSettings(){return{...this.startupConfig}}setStartupDelay(t){if(typeof t!="number"||t<0||t>1e3)throw new Error("Startup delay must be a number between 0 and 1000 milliseconds");this.startupConfig.delayMs=t,this._emitEvent("startupSettingsChanged",{delayMs:t})}getState(){return this.state}isInLeadIn(){return this.state==="lead-in"}isPlaying(){return this.state==="playing"||this.state==="lead-in"}getMetronomeOutput(){return this.audioEngine.getMetronomeOutput()}getPartOutput(t){return this.midiPlayer?this.midiPlayer.getPartOutput(t):null}allSoundsOff(){this.midiPlayer&&this.midiPlayer.allSoundsOff()}getTotalDuration(){return this.midiPlayer?this.midiPlayer.getTotalDuration():0}on(t,e){this.eventBus.on(t,e)}off(t,e){this.eventBus.off(t,e)}_setupEventDelegation(){this.midiPlayer.on("timeupdate",t=>{this.state==="playing"&&this._emitEvent("timeupdate",{...t,effectiveTime:t.currentTime,leadInProgress:null})}),this.midiPlayer.on("beatChanged",t=>{this.state==="playing"&&this._emitEvent("beatChanged",{...t,isLeadIn:!1})}),this.midiPlayer.on("barChanged",t=>{this.state==="playing"&&this._emitEvent("barChanged",t)}),this.midiPlayer.on("ended",t=>{this.state==="playing"&&(this.state="stopped",this._stopMetronome(),this._stopTimeUpdateLoop(),this._emitEvent("playbackEnded",t))}),this.midiPlayer.on("error",t=>{this._emitEvent("error",t)})}_validateConfig(){if(this.metronomeConfig.volume<0||this.metronomeConfig.volume>1)throw new Error("Metronome volume must be between 0.0 and 1.0");if(this.leadInConfig.bars<1)throw new Error("Lead-in bars must be at least 1")}async _startLeadIn(t){this.state="lead-in";const e=this.startupConfig.delayMs/1e3;this.leadInStartTime=this.audioEngine.audioContext.currentTime+e,this.leadInProgress=0,this.leadInData=this._calculateLeadInBeats(),this._emitEvent("leadInStarted",{totalBeats:this.leadInData.totalBeats,duration:this.leadInData.duration,bars:this.leadInConfig.bars,startupDelayMs:this.startupConfig.delayMs}),setTimeout(()=>{this.state==="lead-in"&&this._startLeadInScheduling(t)},this.startupConfig.delayMs),this._startTimeUpdateLoop()}_calculateLeadInBeats(){if(!this.midiPlayer)return{totalBeats:4,duration:2,beatSequence:[],beatsPerBar:4,startBeat:{bar:1,beat:1,timeSig:4}};const t=this.midiPlayer.beats,e=this.frozenTime,i=this.leadInConfig.bars;let s=t.length-1,n=.5;for(;t[s].time>e;)s--;const a=t[s],r=t[s+1];r?n=r.time-a.time:s>0&&(n=a.time-t[s-1].time);const q=this.midiPlayer&&this.midiPlayer.playbackSpeed||1,o=n/q,l=a.timeSig===1,h=l&&r?r.timeSig:a.timeSig,d=l?h-1:a.beat>1?a.beat-1:0,A=i*h+d;return{totalBeats:A,duration:A*o,beatSequence:this._generateBeatSequence(A,o,h),beatsPerBar:h,startBeat:a}}_generateBeatSequence(t,e,i){const s=[];for(let n=0;n<t;n++){const a=n%i+1;s.push({beat:a,isAccent:a===1,time:n*e,absoluteTime:this.leadInStartTime+n*e})}return s}_startLeadInScheduling(t){this.leadInBeatIndex=0,this.leadInScheduledBeats=new Set;const e=this.leadInData.beatSequence,i=10;this.leadInStartTime=this.audioEngine.audioContext.currentTime,this.leadInInterval=setInterval(()=>{const s=this.audioEngine.audioContext.currentTime-this.leadInStartTime;for(this.leadInProgress=Math.min(1,s/this.leadInData.duration);this.leadInBeatIndex<e.length;){const n=e[this.leadInBeatIndex],a=n.time-s;if(a>.05)break;if(!this.leadInScheduledBeats.has(this.leadInBeatIndex)&&a>=-.05&&a<=.05){const r=a<=0?this.audioEngine.audioContext.currentTime+.01:this.audioEngine.audioContext.currentTime+a;this._scheduleTickAtTime(r,n.isAccent),this._emitEvent("beatChanged",{bar:Math.floor(this.leadInBeatIndex/this.leadInData.beatsPerBar)+1,beat:n.beat,repeat:1,time:this.frozenTime,isLeadIn:!0}),this.leadInScheduledBeats.add(this.leadInBeatIndex)}this.leadInBeatIndex++}this.leadInProgress>=1&&this._completeLeadIn(t)},i)}_completeLeadIn(t){const e=this.leadInStartTime+this.leadInData.duration;this._stopLeadIn(),this._emitEvent("leadInEnded",{}),this.leadInData=null,this.leadInProgress=null,this.leadInStartTime=null,this._startMidiPlaybackAt(e,t,!0)}async _startMidiPlaybackAt(t,e,i=!0){this.state="playing",i&&this._emitEvent("playbackStarted",{startupDelayMs:0,scheduledStartTime:t}),this.midiPlayer.playAt(t),e&&this._startMetronomeAt(t),this.timeUpdateInterval||this._startTimeUpdateLoop()}async _startMidiPlayback(t,e=!0,i=!1){this.state="playing",this._resetMetronomeBeatTracking(),e&&this._emitEvent("playbackStarted",{startupDelayMs:i?0:this.startupConfig.delayMs});const s=()=>{if(this.state==="playing")try{this.midiPlayer.play(),t&&this._startMetronome()}catch(n){this.state="stopped",this._emitEvent("error",n)}};i?s():setTimeout(s,this.startupConfig.delayMs),this.timeUpdateInterval||this._startTimeUpdateLoop()}_startMetronome(){!this.metronomeConfig.enabled||this.state!=="playing"||(this._scheduleMetronomeTicks(),this.metronomeScheduleInterval=setInterval(()=>{this._scheduleMetronomeTicks()},50))}_startMetronomeAt(t){this.metronomeConfig.enabled&&(this.metronomeScheduledStartTime=t,this._resetMetronomeBeatTracking(),this._scheduleMetronomeTicksAt(t),this.metronomeScheduleInterval=setInterval(()=>{this._scheduleMetronomeTicks()},50))}_startMetronomeIfEnabled(){this.metronomeConfig.enabled&&this._startMetronome()}_scheduleMetronomeTicksAt(t){if(!this.metronomeConfig.enabled||!this.midiPlayer)return;const e=this.midiPlayer.beats;if(!e||e.length===0)return;const i=this.midiPlayer.getCurrentTime(),s=.1;for(let n=this.nextBeatIndex;n<e.length;n++){const a=e[n],r=this.midiPlayer.playbackSpeed||1,q=t+(a.time-i)/r;if(q>this.audioEngine.audioContext.currentTime+s)break;if(q>=this.audioEngine.audioContext.currentTime-.01){const o=Math.max(q,this.audioEngine.audioContext.currentTime+.001),l=a.isDownbeat||a.beat===1;this._scheduleTickAtTime(o,l),this.nextBeatIndex=n+1}}}_scheduleMetronomeTicks(){if(!this.metronomeConfig.enabled||!this.midiPlayer)return;const t=this.midiPlayer.getCurrentTime(),e=this.midiPlayer.beats;if(!e||e.length===0)return;for(;this.nextBeatIndex<e.length&&e[this.nextBeatIndex].time<t-.025;)this.nextBeatIndex++;const s=t+.15;for(;this.nextBeatIndex<e.length;){const n=e[this.nextBeatIndex],a=n.time-t;if(n.time>s)break;if(a>=-.025&&a<=.15){const r=this.audioEngine.audioContext.currentTime+.005,q=this.audioEngine.audioContext.currentTime+Math.max(a,.005),o=Math.max(r,q),l=n.beat===1;this._scheduleTickAtTime(o,l)}this.nextBeatIndex++}}async _scheduleTickAtTime(t,e){try{await this.audioEngine.playMetronomeTick(t,e,this.metronomeConfig.volume)}catch(i){console.warn("Failed to schedule metronome tick:",i)}}_resetMetronomeBeatTracking(){if(!this.midiPlayer){this.nextBeatIndex=0;return}const t=this.getCurrentTime(),e=this.midiPlayer.beats;if(!e||e.length===0){this.nextBeatIndex=0;return}for(this.nextBeatIndex=0;this.nextBeatIndex<e.length&&e[this.nextBeatIndex].time<t-.01;)this.nextBeatIndex++}_stopMetronome(){this.metronomeScheduleInterval&&(clearInterval(this.metronomeScheduleInterval),this.metronomeScheduleInterval=null)}_stopLeadIn(){this.leadInInterval&&(clearInterval(this.leadInInterval),this.leadInInterval=null)}_pauseLeadIn(){this._stopLeadIn()}_resumeLeadIn(){if(!this.leadInData||this.leadInProgress===null)return;this.state="lead-in";const t=this.audioEngine.audioContext.currentTime,e=this.leadInProgress*this.leadInData.duration;this.leadInStartTime=t-e,this._startLeadInScheduling(this.metronomeConfig.enabled),this._startTimeUpdateLoop()}_startTimeUpdateLoop(){this.timeUpdateInterval||(this.timeUpdateInterval=setInterval(()=>{this.state==="lead-in"?this._emitEvent("timeupdate",{currentTime:this.frozenTime,effectiveTime:this.frozenTime,leadInProgress:this.leadInProgress||0}):this.state},100))}_stopTimeUpdateLoop(){this.timeUpdateInterval&&(clearInterval(this.timeUpdateInterval),this.timeUpdateInterval=null)}_emitEvent(t,e){(this.eventBus.all.get(t)||[]).forEach(s=>{try{s(e)}catch(n){console.error(`Error in ${t} event listener:`,n)}})}destroy(){this.stop(),this._stopLeadIn(),this._stopMetronome(),this._stopTimeUpdateLoop(),this.eventBus.all.clear()}_setMidiPlayer(t){this.midiPlayer=t,this.audioEngine=t.audioEngine,this._partOutputsMap.clear(),Object.keys(this.midiPlayer.parsedData.parts).forEach(e=>{const i=this.midiPlayer.getPartOutput(e);i&&this._partOutputsMap.set(e,i)}),this._setupEventDelegation(),this.state="ready"}_createDefaultInstrumentMap(t){const e={},i={soprano:{instrument:"choir_aahs",volume:1},alto:{instrument:"choir_aahs",volume:1},tenor:{instrument:"choir_aahs",volume:1},bass:{instrument:"choir_aahs",volume:1},piano:{instrument:"piano",volume:.8},organ:{instrument:"church_organ",volume:.7},strings:{instrument:"string_ensemble_1",volume:.6},flute:{instrument:"flute",volume:.8},trumpet:{instrument:"trumpet",volume:.7}};return Object.keys(t).forEach(s=>{const n=s.toLowerCase(),a=i[n]||i.piano;e[s]={instrument:a.instrument,volume:a.volume}}),e}}exports.AudioEngine=g;exports.AudioEngineUtils=_;exports.BeatMapper=x;exports.ChannelHandle=y;exports.MidiParser=C;exports.MidiPlayer=p;exports.PlaybackManager=F;exports.SpessaSynthAudioEngine=U;exports.SpessaSynthChannelHandle=k;