@waveform-playlist/playout 12.5.0 → 12.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -50,17 +50,16 @@ module.exports = __toCommonJS(index_exports);
50
50
 
51
51
  // src/TonePlayout.ts
52
52
  var import_tone4 = require("tone");
53
- var import_core5 = require("@waveform-playlist/core");
53
+ var import_core4 = require("@waveform-playlist/core");
54
54
 
55
55
  // src/ToneTrack.ts
56
56
  var import_tone = require("tone");
57
- var import_core2 = require("@waveform-playlist/core");
57
+ var import_core = require("@waveform-playlist/core");
58
58
 
59
59
  // src/fades.ts
60
- var import_core = require("@waveform-playlist/core");
61
60
  var hasWarned = false;
62
61
  function getUnderlyingAudioParam(signal) {
63
- const param = signal._param;
62
+ const param = signal?._param;
64
63
  if (!param && !hasWarned) {
65
64
  hasWarned = true;
66
65
  console.warn(
@@ -83,7 +82,7 @@ var ToneTrack = class {
83
82
  // clips at/after this offset.
84
83
  this._scheduleGuardOffset = 0;
85
84
  this.track = options.track;
86
- this.volumeNode = new import_tone.Volume((0, import_core2.gainToDb)(options.track.gain));
85
+ this.volumeNode = new import_tone.Volume((0, import_core.gainToDb)(options.track.gain));
87
86
  this.panNode = new import_tone.Panner({
88
87
  pan: options.track.stereoPan,
89
88
  channelCount: options.channelCount ?? 1
@@ -143,8 +142,7 @@ var ToneTrack = class {
143
142
  source.start(audioContextTime, offset, duration);
144
143
  } catch (err) {
145
144
  console.warn(
146
- `[waveform-playlist] Failed to start source on track "${this.id}" (time=${audioContextTime}, offset=${offset}, duration=${duration}):`,
147
- err
145
+ `[waveform-playlist] Failed to start source on track "${this.id}" (time=${audioContextTime}, offset=${offset}, duration=${duration}): ` + String(err)
148
146
  );
149
147
  source.disconnect();
150
148
  return;
@@ -303,6 +301,10 @@ var ToneTrack = class {
303
301
  clipInfo.offset + Math.max(0, elapsed),
304
302
  clipInfo.duration - Math.max(0, elapsed)
305
303
  );
304
+ this.scheduleFades(scheduled, audioContextTime, clipInfo.offset + Math.max(0, elapsed));
305
+ } else if (absClipStart >= transportOffset) {
306
+ const delay = absClipStart - transportOffset;
307
+ this.scheduleFades(scheduled, audioContextTime + delay, clipInfo.offset);
306
308
  }
307
309
  }
308
310
  }
@@ -321,7 +323,9 @@ var ToneTrack = class {
321
323
  try {
322
324
  source.stop();
323
325
  } catch (err) {
324
- console.warn(`[waveform-playlist] Error stopping source on track "${this.id}":`, err);
326
+ console.warn(
327
+ `[waveform-playlist] Error stopping source on track "${this.id}": ` + String(err)
328
+ );
325
329
  }
326
330
  });
327
331
  this.activeSources.clear();
@@ -422,7 +426,7 @@ var ToneTrack = class {
422
426
  }
423
427
  setVolume(gain) {
424
428
  this.track.gain = gain;
425
- this.volumeNode.volume.value = (0, import_core2.gainToDb)(gain);
429
+ this.volumeNode.volume.value = (0, import_core.gainToDb)(gain);
426
430
  }
427
431
  setPan(pan) {
428
432
  this.track.stereoPan = pan;
@@ -450,10 +454,16 @@ var ToneTrack = class {
450
454
  '[waveform-playlist] Track "' + this.track.id + '" was built with a TrackEffectsFunction closure \u2014 transport effects hooks and closure effects are mutually exclusive.'
451
455
  );
452
456
  }
453
- if (this._effectsChainNode) {
454
- this.muteGain.disconnect(this._effectsChainNode);
455
- } else {
456
- this.muteGain.disconnect(this._destination);
457
+ try {
458
+ if (this._effectsChainNode) {
459
+ this.muteGain.disconnect(this._effectsChainNode);
460
+ } else {
461
+ this.muteGain.disconnect(this._destination);
462
+ }
463
+ } catch (err) {
464
+ console.warn(
465
+ '[waveform-playlist] connectEffects disconnect on track "' + this.track.id + '": ' + String(err)
466
+ );
457
467
  }
458
468
  this.muteGain.connect(node);
459
469
  this._effectsChainNode = node;
@@ -477,7 +487,9 @@ var ToneTrack = class {
477
487
  try {
478
488
  this.effectsCleanup();
479
489
  } catch (err) {
480
- console.warn(`[waveform-playlist] Error during track "${this.id}" effects cleanup:`, err);
490
+ console.warn(
491
+ `[waveform-playlist] Error during track "${this.id}" effects cleanup: ` + String(err)
492
+ );
481
493
  }
482
494
  }
483
495
  this.stopAllSources();
@@ -486,42 +498,49 @@ var ToneTrack = class {
486
498
  transport.clear(scheduled.scheduleId);
487
499
  } catch (err) {
488
500
  console.warn(
489
- `[waveform-playlist] Error clearing schedule ${index} on track "${this.id}":`,
490
- err
501
+ `[waveform-playlist] Error clearing schedule ${index} on track "${this.id}": ` + String(err)
491
502
  );
492
503
  }
493
504
  try {
494
505
  scheduled.fadeGainNode.disconnect();
495
506
  } catch (err) {
496
507
  console.warn(
497
- `[waveform-playlist] Error disconnecting fadeGain ${index} on track "${this.id}":`,
498
- err
508
+ `[waveform-playlist] Error disconnecting fadeGain ${index} on track "${this.id}": ` + String(err)
499
509
  );
500
510
  }
501
511
  });
502
512
  try {
503
513
  this.volumeNode.dispose();
504
514
  } catch (err) {
505
- console.warn(`[waveform-playlist] Error disposing volumeNode on track "${this.id}":`, err);
515
+ console.warn(
516
+ `[waveform-playlist] Error disposing volumeNode on track "${this.id}": ` + String(err)
517
+ );
506
518
  }
507
519
  try {
508
520
  this.panNode.dispose();
509
521
  } catch (err) {
510
- console.warn(`[waveform-playlist] Error disposing panNode on track "${this.id}":`, err);
522
+ console.warn(
523
+ `[waveform-playlist] Error disposing panNode on track "${this.id}": ` + String(err)
524
+ );
511
525
  }
512
526
  try {
513
527
  this.muteGain.dispose();
514
528
  } catch (err) {
515
- console.warn(`[waveform-playlist] Error disposing muteGain on track "${this.id}":`, err);
529
+ console.warn(
530
+ `[waveform-playlist] Error disposing muteGain on track "${this.id}": ` + String(err)
531
+ );
516
532
  }
517
533
  }
518
534
  get id() {
519
535
  return this.track.id;
520
536
  }
521
537
  get duration() {
522
- if (this.scheduledClips.length === 0) return 0;
523
- const lastClip = this.scheduledClips[this.scheduledClips.length - 1];
524
- return lastClip.clipInfo.startTime + lastClip.clipInfo.duration;
538
+ let max = 0;
539
+ for (const { clipInfo } of this.scheduledClips) {
540
+ const end = clipInfo.startTime + clipInfo.duration;
541
+ if (end > max) max = end;
542
+ }
543
+ return max;
525
544
  }
526
545
  get buffer() {
527
546
  return this.scheduledClips[0]?.clipInfo.buffer;
@@ -536,7 +555,7 @@ var ToneTrack = class {
536
555
 
537
556
  // src/MidiToneTrack.ts
538
557
  var import_tone2 = require("tone");
539
- var import_core3 = require("@waveform-playlist/core");
558
+ var import_core2 = require("@waveform-playlist/core");
540
559
  function getDrumCategory(midiNote) {
541
560
  if (midiNote === 35 || midiNote === 36) return "kick";
542
561
  if (midiNote >= 37 && midiNote <= 40) return "snare";
@@ -547,7 +566,7 @@ function getDrumCategory(midiNote) {
547
566
  var MidiToneTrack = class {
548
567
  constructor(options) {
549
568
  this.track = options.track;
550
- this.volumeNode = new import_tone2.Volume((0, import_core3.gainToDb)(options.track.gain));
569
+ this.volumeNode = new import_tone2.Volume((0, import_core2.gainToDb)(options.track.gain));
551
570
  this.panNode = new import_tone2.Panner(options.track.stereoPan);
552
571
  this.muteGain = new import_tone2.Gain(options.track.muted ? 0 : 1);
553
572
  this.volumeNode.chain(this.panNode, this.muteGain);
@@ -650,8 +669,7 @@ var MidiToneTrack = class {
650
669
  this.snareSynth.triggerAttackRelease(duration, time, velocity);
651
670
  } catch (err) {
652
671
  console.warn(
653
- "[waveform-playlist] Snare overlap \u2014 previous hit still decaying, skipped:",
654
- err
672
+ "[waveform-playlist] Snare overlap \u2014 previous hit still decaying, skipped: " + String(err)
655
673
  );
656
674
  }
657
675
  break;
@@ -712,8 +730,7 @@ var MidiToneTrack = class {
712
730
  );
713
731
  } catch (err) {
714
732
  console.warn(
715
- `[waveform-playlist] Failed to start mid-clip MIDI note "${note.name}" on track "${this.id}":`,
716
- err
733
+ `[waveform-playlist] Failed to start mid-clip MIDI note "${note.name}" on track "${this.id}": ` + String(err)
717
734
  );
718
735
  }
719
736
  }
@@ -726,14 +743,19 @@ var MidiToneTrack = class {
726
743
  */
727
744
  stopAllSources() {
728
745
  const now3 = (0, import_tone2.getContext)().rawContext.currentTime;
729
- try {
730
- this.synth.releaseAll(now3);
731
- this.kickSynth.releaseAll(now3);
732
- this.cymbalSynth.releaseAll(now3);
733
- this.tomSynth.releaseAll(now3);
734
- } catch (err) {
735
- console.warn(`[waveform-playlist] Error releasing synth on track "${this.id}":`, err);
736
- }
746
+ const release = (label, fn) => {
747
+ try {
748
+ fn();
749
+ } catch (err) {
750
+ console.warn(
751
+ `[waveform-playlist] Error releasing ${label} synth on track "${this.id}": ` + String(err)
752
+ );
753
+ }
754
+ };
755
+ release("melodic", () => this.synth.releaseAll(now3));
756
+ release("kick", () => this.kickSynth.releaseAll(now3));
757
+ release("cymbal", () => this.cymbalSynth.releaseAll(now3));
758
+ release("tom", () => this.tomSynth.releaseAll(now3));
737
759
  }
738
760
  /**
739
761
  * No-op for MIDI — MIDI uses note velocity, not gain fades.
@@ -747,7 +769,7 @@ var MidiToneTrack = class {
747
769
  }
748
770
  setVolume(gain) {
749
771
  this.track.gain = gain;
750
- this.volumeNode.volume.value = (0, import_core3.gainToDb)(gain);
772
+ this.volumeNode.volume.value = (0, import_core2.gainToDb)(gain);
751
773
  }
752
774
  setPan(pan) {
753
775
  this.track.stereoPan = pan;
@@ -769,8 +791,7 @@ var MidiToneTrack = class {
769
791
  this.effectsCleanup();
770
792
  } catch (err) {
771
793
  console.warn(
772
- `[waveform-playlist] Error during MIDI track "${this.id}" effects cleanup:`,
773
- err
794
+ `[waveform-playlist] Error during MIDI track "${this.id}" effects cleanup: ` + String(err)
774
795
  );
775
796
  }
776
797
  }
@@ -780,8 +801,7 @@ var MidiToneTrack = class {
780
801
  part.dispose();
781
802
  } catch (err) {
782
803
  console.warn(
783
- `[waveform-playlist] Error disposing Part ${index} on MIDI track "${this.id}":`,
784
- err
804
+ `[waveform-playlist] Error disposing Part ${index} on MIDI track "${this.id}": ` + String(err)
785
805
  );
786
806
  }
787
807
  });
@@ -796,35 +816,43 @@ var MidiToneTrack = class {
796
816
  try {
797
817
  s?.dispose();
798
818
  } catch (err) {
799
- console.warn(`[waveform-playlist] Error disposing synth on MIDI track "${this.id}":`, err);
819
+ console.warn(
820
+ `[waveform-playlist] Error disposing synth on MIDI track "${this.id}": ` + String(err)
821
+ );
800
822
  }
801
823
  }
802
824
  try {
803
825
  this.volumeNode.dispose();
804
826
  } catch (err) {
805
827
  console.warn(
806
- `[waveform-playlist] Error disposing volumeNode on MIDI track "${this.id}":`,
807
- err
828
+ `[waveform-playlist] Error disposing volumeNode on MIDI track "${this.id}": ` + String(err)
808
829
  );
809
830
  }
810
831
  try {
811
832
  this.panNode.dispose();
812
833
  } catch (err) {
813
- console.warn(`[waveform-playlist] Error disposing panNode on MIDI track "${this.id}":`, err);
834
+ console.warn(
835
+ `[waveform-playlist] Error disposing panNode on MIDI track "${this.id}": ` + String(err)
836
+ );
814
837
  }
815
838
  try {
816
839
  this.muteGain.dispose();
817
840
  } catch (err) {
818
- console.warn(`[waveform-playlist] Error disposing muteGain on MIDI track "${this.id}":`, err);
841
+ console.warn(
842
+ `[waveform-playlist] Error disposing muteGain on MIDI track "${this.id}": ` + String(err)
843
+ );
819
844
  }
820
845
  }
821
846
  get id() {
822
847
  return this.track.id;
823
848
  }
824
849
  get duration() {
825
- if (this.scheduledClips.length === 0) return 0;
826
- const lastClip = this.scheduledClips[this.scheduledClips.length - 1];
827
- return lastClip.clipInfo.startTime + lastClip.clipInfo.duration;
850
+ let max = 0;
851
+ for (const { clipInfo } of this.scheduledClips) {
852
+ const end = clipInfo.startTime + clipInfo.duration;
853
+ if (end > max) max = end;
854
+ }
855
+ return max;
828
856
  }
829
857
  get muted() {
830
858
  return this.track.muted;
@@ -836,15 +864,19 @@ var MidiToneTrack = class {
836
864
 
837
865
  // src/SoundFontToneTrack.ts
838
866
  var import_tone3 = require("tone");
839
- var import_core4 = require("@waveform-playlist/core");
840
- var _SoundFontToneTrack = class _SoundFontToneTrack {
867
+ var import_core3 = require("@waveform-playlist/core");
868
+ var SoundFontToneTrack = class {
841
869
  constructor(options) {
870
+ /** Rate-limit missing sample warnings — one per track instance (a page-
871
+ * lifetime static would silence diagnostics for unrelated later tracks
872
+ * and soundfont swaps). */
873
+ this._missingSampleWarned = false;
842
874
  this.activeSources = /* @__PURE__ */ new Set();
843
875
  this.track = options.track;
844
876
  this.soundFontCache = options.soundFontCache;
845
877
  this.programNumber = options.programNumber ?? 0;
846
878
  this.bankNumber = options.isPercussion ? 128 : 0;
847
- this.volumeNode = new import_tone3.Volume((0, import_core4.gainToDb)(options.track.gain));
879
+ this.volumeNode = new import_tone3.Volume((0, import_core3.gainToDb)(options.track.gain));
848
880
  this.panNode = new import_tone3.Panner(options.track.stereoPan);
849
881
  this.muteGain = new import_tone3.Gain(options.track.muted ? 0 : 1);
850
882
  this.volumeNode.chain(this.panNode, this.muteGain);
@@ -881,7 +913,13 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
881
913
  });
882
914
  const part = new import_tone3.Part((time, event) => {
883
915
  if (event.duration > 0) {
884
- this.triggerNote(event.midi, event.duration, time, event.velocity, event.channel);
916
+ try {
917
+ this.triggerNote(event.midi, event.duration, time, event.velocity, event.channel);
918
+ } catch (err) {
919
+ console.warn(
920
+ `[waveform-playlist] Failed to trigger SoundFont note on track "${this.id}": ` + String(err)
921
+ );
922
+ }
885
923
  }
886
924
  }, partEvents);
887
925
  part.start(0);
@@ -898,11 +936,11 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
898
936
  const preset = channel === 9 ? 0 : this.programNumber;
899
937
  const sfSample = this.soundFontCache.getAudioBuffer(midiNote, bank, preset);
900
938
  if (!sfSample) {
901
- if (!_SoundFontToneTrack._missingSampleWarned) {
939
+ if (!this._missingSampleWarned) {
902
940
  console.warn(
903
- `[waveform-playlist] SoundFont sample not found for MIDI note ${midiNote} (bank ${bank}, preset ${preset}). Subsequent missing samples will be silent.`
941
+ `[waveform-playlist] SoundFont sample not found for MIDI note ${midiNote} (bank ${bank}, preset ${preset}) on track "${this.id}". Subsequent missing samples on this track will be silent.`
904
942
  );
905
- _SoundFontToneTrack._missingSampleWarned = true;
943
+ this._missingSampleWarned = true;
906
944
  }
907
945
  return;
908
946
  }
@@ -938,7 +976,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
938
976
  try {
939
977
  gainNode.disconnect();
940
978
  } catch (err) {
941
- console.warn("[waveform-playlist] GainNode already disconnected:", err);
979
+ console.warn("[waveform-playlist] GainNode already disconnected: " + String(err));
942
980
  }
943
981
  };
944
982
  source.start(time);
@@ -973,8 +1011,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
973
1011
  );
974
1012
  } catch (err) {
975
1013
  console.warn(
976
- `[waveform-playlist] Failed to start mid-clip SoundFont note on track "${this.id}":`,
977
- err
1014
+ `[waveform-playlist] Failed to start mid-clip SoundFont note on track "${this.id}": ` + String(err)
978
1015
  );
979
1016
  }
980
1017
  }
@@ -990,7 +1027,9 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
990
1027
  try {
991
1028
  source.stop();
992
1029
  } catch (err) {
993
- console.warn("[waveform-playlist] Error stopping AudioBufferSourceNode:", err);
1030
+ console.warn(
1031
+ "[waveform-playlist] Error stopping AudioBufferSourceNode: " + String(err)
1032
+ );
994
1033
  }
995
1034
  }
996
1035
  this.activeSources.clear();
@@ -1003,7 +1042,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1003
1042
  }
1004
1043
  setVolume(gain) {
1005
1044
  this.track.gain = gain;
1006
- this.volumeNode.volume.value = (0, import_core4.gainToDb)(gain);
1045
+ this.volumeNode.volume.value = (0, import_core3.gainToDb)(gain);
1007
1046
  }
1008
1047
  setPan(pan) {
1009
1048
  this.track.stereoPan = pan;
@@ -1025,8 +1064,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1025
1064
  this.effectsCleanup();
1026
1065
  } catch (err) {
1027
1066
  console.warn(
1028
- `[waveform-playlist] Error during SoundFont track "${this.id}" effects cleanup:`,
1029
- err
1067
+ `[waveform-playlist] Error during SoundFont track "${this.id}" effects cleanup: ` + String(err)
1030
1068
  );
1031
1069
  }
1032
1070
  }
@@ -1036,8 +1074,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1036
1074
  part.dispose();
1037
1075
  } catch (err) {
1038
1076
  console.warn(
1039
- `[waveform-playlist] Error disposing Part ${index} on SoundFont track "${this.id}":`,
1040
- err
1077
+ `[waveform-playlist] Error disposing Part ${index} on SoundFont track "${this.id}": ` + String(err)
1041
1078
  );
1042
1079
  }
1043
1080
  });
@@ -1045,24 +1082,21 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1045
1082
  this.volumeNode.dispose();
1046
1083
  } catch (err) {
1047
1084
  console.warn(
1048
- `[waveform-playlist] Error disposing volumeNode on SoundFont track "${this.id}":`,
1049
- err
1085
+ `[waveform-playlist] Error disposing volumeNode on SoundFont track "${this.id}": ` + String(err)
1050
1086
  );
1051
1087
  }
1052
1088
  try {
1053
1089
  this.panNode.dispose();
1054
1090
  } catch (err) {
1055
1091
  console.warn(
1056
- `[waveform-playlist] Error disposing panNode on SoundFont track "${this.id}":`,
1057
- err
1092
+ `[waveform-playlist] Error disposing panNode on SoundFont track "${this.id}": ` + String(err)
1058
1093
  );
1059
1094
  }
1060
1095
  try {
1061
1096
  this.muteGain.dispose();
1062
1097
  } catch (err) {
1063
1098
  console.warn(
1064
- `[waveform-playlist] Error disposing muteGain on SoundFont track "${this.id}":`,
1065
- err
1099
+ `[waveform-playlist] Error disposing muteGain on SoundFont track "${this.id}": ` + String(err)
1066
1100
  );
1067
1101
  }
1068
1102
  }
@@ -1070,9 +1104,12 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1070
1104
  return this.track.id;
1071
1105
  }
1072
1106
  get duration() {
1073
- if (this.scheduledClips.length === 0) return 0;
1074
- const lastClip = this.scheduledClips[this.scheduledClips.length - 1];
1075
- return lastClip.clipInfo.startTime + lastClip.clipInfo.duration;
1107
+ let max = 0;
1108
+ for (const { clipInfo } of this.scheduledClips) {
1109
+ const end = clipInfo.startTime + clipInfo.duration;
1110
+ if (end > max) max = end;
1111
+ }
1112
+ return max;
1076
1113
  }
1077
1114
  get muted() {
1078
1115
  return this.track.muted;
@@ -1081,9 +1118,6 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1081
1118
  return this.track.startTime;
1082
1119
  }
1083
1120
  };
1084
- /** Rate-limit missing sample warnings — one per class lifetime */
1085
- _SoundFontToneTrack._missingSampleWarned = false;
1086
- var SoundFontToneTrack = _SoundFontToneTrack;
1087
1121
 
1088
1122
  // src/TonePlayout.ts
1089
1123
  var TonePlayout = class {
@@ -1098,7 +1132,7 @@ var TonePlayout = class {
1098
1132
  this._loopStart = 0;
1099
1133
  this._loopEnd = 0;
1100
1134
  this._masterChainNode = null;
1101
- this.masterVolume = new import_tone4.Volume((0, import_core5.gainToDb)(options.masterGain ?? 1));
1135
+ this.masterVolume = new import_tone4.Volume((0, import_core4.gainToDb)(options.masterGain ?? 1));
1102
1136
  this._masterTap = new import_tone4.Gain(1);
1103
1137
  if (options.effects) {
1104
1138
  const cleanup = options.effects(this.masterVolume, this._masterTap, false);
@@ -1121,7 +1155,9 @@ var TonePlayout = class {
1121
1155
  try {
1122
1156
  (0, import_tone4.getTransport)().clear(this._completionEventId);
1123
1157
  } catch (err) {
1124
- console.warn("[waveform-playlist] Error clearing Transport completion event:", err);
1158
+ console.warn(
1159
+ "[waveform-playlist] Error clearing Transport completion event: " + String(err)
1160
+ );
1125
1161
  }
1126
1162
  this._completionEventId = null;
1127
1163
  }
@@ -1216,6 +1252,7 @@ var TonePlayout = class {
1216
1252
  const lookAhead = context.lookAhead ?? 0;
1217
1253
  const audibleOffset = Math.max(0, transport.seconds - lookAhead);
1218
1254
  track.startMidClipSources(audibleOffset, context.currentTime);
1255
+ track.prepareFades(context.currentTime, audibleOffset);
1219
1256
  }
1220
1257
  play(when, offset, duration) {
1221
1258
  if (!this.isInitialized) {
@@ -1235,7 +1272,9 @@ var TonePlayout = class {
1235
1272
  try {
1236
1273
  this.onPlaybackCompleteCallback?.();
1237
1274
  } catch (err) {
1238
- console.warn("[waveform-playlist] Error in playback completion callback:", err);
1275
+ console.warn(
1276
+ "[waveform-playlist] Error in playback completion callback: " + String(err)
1277
+ );
1239
1278
  }
1240
1279
  }, transportOffset + duration);
1241
1280
  }
@@ -1261,8 +1300,7 @@ var TonePlayout = class {
1261
1300
  this.clearCompletionEvent();
1262
1301
  this.tracks.forEach((track) => track.cancelFades());
1263
1302
  console.warn(
1264
- "[waveform-playlist] Transport.start() failed. Audio playback could not begin.",
1265
- err
1303
+ "[waveform-playlist] Transport.start() failed. Audio playback could not begin. " + String(err)
1266
1304
  );
1267
1305
  throw err;
1268
1306
  }
@@ -1272,7 +1310,7 @@ var TonePlayout = class {
1272
1310
  try {
1273
1311
  transport.pause();
1274
1312
  } catch (err) {
1275
- console.warn("[waveform-playlist] Transport.pause() failed:", err);
1313
+ console.warn("[waveform-playlist] Transport.pause() failed: " + String(err));
1276
1314
  }
1277
1315
  this.tracks.forEach((track) => track.stopAllSources());
1278
1316
  this.tracks.forEach((track) => track.cancelFades());
@@ -1283,13 +1321,13 @@ var TonePlayout = class {
1283
1321
  try {
1284
1322
  transport.stop();
1285
1323
  } catch (err) {
1286
- console.warn("[waveform-playlist] Transport.stop() failed:", err);
1324
+ console.warn("[waveform-playlist] Transport.stop() failed: " + String(err));
1287
1325
  }
1288
1326
  if (this._loopHandler) {
1289
1327
  try {
1290
1328
  transport.off("loop", this._loopHandler);
1291
1329
  } catch (err) {
1292
- console.warn("[waveform-playlist] Error removing loop handler:", err);
1330
+ console.warn("[waveform-playlist] Error removing loop handler: " + String(err));
1293
1331
  }
1294
1332
  this._loopHandler = null;
1295
1333
  }
@@ -1297,20 +1335,24 @@ var TonePlayout = class {
1297
1335
  try {
1298
1336
  track.stopAllSources();
1299
1337
  } catch (err) {
1300
- console.warn(`[waveform-playlist] Error stopping sources for track "${track.id}":`, err);
1338
+ console.warn(
1339
+ `[waveform-playlist] Error stopping sources for track "${track.id}": ` + String(err)
1340
+ );
1301
1341
  }
1302
1342
  });
1303
1343
  this.tracks.forEach((track) => {
1304
1344
  try {
1305
1345
  track.cancelFades();
1306
1346
  } catch (err) {
1307
- console.warn(`[waveform-playlist] Error canceling fades for track "${track.id}":`, err);
1347
+ console.warn(
1348
+ `[waveform-playlist] Error canceling fades for track "${track.id}": ` + String(err)
1349
+ );
1308
1350
  }
1309
1351
  });
1310
1352
  this.clearCompletionEvent();
1311
1353
  }
1312
1354
  setMasterGain(gain) {
1313
- this.masterVolume.volume.value = (0, import_core5.gainToDb)(gain);
1355
+ this.masterVolume.volume.value = (0, import_core4.gainToDb)(gain);
1314
1356
  }
1315
1357
  /** The master output tap node. In the signal chain: masterVolume → tap → destination.
1316
1358
  * Connect analyzers/effects/recorders here — parallel or serial.
@@ -1351,10 +1393,14 @@ var TonePlayout = class {
1351
1393
  * effects] → tap → node (caller wires node.output → ctx.destination).
1352
1394
  */
1353
1395
  connectMasterOutput(node) {
1354
- if (this._masterChainNode) {
1355
- this._masterTap.disconnect(this._masterChainNode);
1356
- } else {
1357
- this._masterTap.disconnect((0, import_tone4.getDestination)());
1396
+ try {
1397
+ if (this._masterChainNode) {
1398
+ this._masterTap.disconnect(this._masterChainNode);
1399
+ } else {
1400
+ this._masterTap.disconnect((0, import_tone4.getDestination)());
1401
+ }
1402
+ } catch (err) {
1403
+ console.warn("[waveform-playlist] connectMasterOutput disconnect: " + String(err));
1358
1404
  }
1359
1405
  this._masterTap.connect(node);
1360
1406
  this._masterChainNode = node;
@@ -1404,7 +1450,7 @@ var TonePlayout = class {
1404
1450
  const track = this.tracks.get(trackId);
1405
1451
  if (track) {
1406
1452
  this.manualMuteState.set(trackId, muted);
1407
- track.setMute(muted);
1453
+ this.updateSoloMuting();
1408
1454
  }
1409
1455
  }
1410
1456
  setLoop(enabled, loopStart, loopEnd) {
@@ -1417,7 +1463,7 @@ var TonePlayout = class {
1417
1463
  transport.loopEnd = loopEnd;
1418
1464
  transport.loop = enabled;
1419
1465
  } catch (err) {
1420
- console.warn("[waveform-playlist] Error configuring Transport loop:", err);
1466
+ console.warn("[waveform-playlist] Error configuring Transport loop: " + String(err));
1421
1467
  return;
1422
1468
  }
1423
1469
  if (enabled && !this._loopHandler) {
@@ -1432,8 +1478,7 @@ var TonePlayout = class {
1432
1478
  track.prepareFades(currentTime, this._loopStart);
1433
1479
  } catch (err) {
1434
1480
  console.warn(
1435
- `[waveform-playlist] Error re-scheduling track "${track.id}" on loop:`,
1436
- err
1481
+ `[waveform-playlist] Error re-scheduling track "${track.id}" on loop: ` + String(err)
1437
1482
  );
1438
1483
  }
1439
1484
  });
@@ -1454,13 +1499,17 @@ var TonePlayout = class {
1454
1499
  try {
1455
1500
  this.stop();
1456
1501
  } catch (err) {
1457
- console.warn("[waveform-playlist] Error stopping Transport during dispose:", err);
1502
+ console.warn(
1503
+ "[waveform-playlist] Error stopping Transport during dispose: " + String(err)
1504
+ );
1458
1505
  }
1459
1506
  this.tracks.forEach((track) => {
1460
1507
  try {
1461
1508
  track.dispose();
1462
1509
  } catch (err) {
1463
- console.warn(`[waveform-playlist] Error disposing track "${track.id}":`, err);
1510
+ console.warn(
1511
+ `[waveform-playlist] Error disposing track "${track.id}": ` + String(err)
1512
+ );
1464
1513
  }
1465
1514
  });
1466
1515
  this.tracks.clear();
@@ -1468,7 +1517,9 @@ var TonePlayout = class {
1468
1517
  try {
1469
1518
  this.effectsCleanup();
1470
1519
  } catch (err) {
1471
- console.warn("[waveform-playlist] Error during master effects cleanup:", err);
1520
+ console.warn(
1521
+ "[waveform-playlist] Error during master effects cleanup: " + String(err)
1522
+ );
1472
1523
  }
1473
1524
  }
1474
1525
  try {
@@ -1576,7 +1627,9 @@ var SoundFontCache = class _SoundFontCache {
1576
1627
  async load(url, signal) {
1577
1628
  const response = await fetch(url, { signal });
1578
1629
  if (!response.ok) {
1579
- throw new Error(`Failed to fetch SoundFont ${url}: ${response.statusText}`);
1630
+ throw new Error(
1631
+ `Failed to fetch SoundFont ${url}: ${response.status}${response.statusText ? ` ${response.statusText}` : ""}`
1632
+ );
1580
1633
  }
1581
1634
  const arrayBuffer = await response.arrayBuffer();
1582
1635
  try {
@@ -1586,6 +1639,7 @@ var SoundFontCache = class _SoundFontCache {
1586
1639
  `Failed to parse SoundFont ${url}: ${err instanceof Error ? err.message : String(err)}`
1587
1640
  );
1588
1641
  }
1642
+ this.audioBufferCache.clear();
1589
1643
  }
1590
1644
  /**
1591
1645
  * Load from an already-fetched ArrayBuffer.
@@ -1598,6 +1652,7 @@ var SoundFontCache = class _SoundFontCache {
1598
1652
  `Failed to parse SoundFont from buffer: ${err instanceof Error ? err.message : String(err)}`
1599
1653
  );
1600
1654
  }
1655
+ this.audioBufferCache.clear();
1601
1656
  }
1602
1657
  get isLoaded() {
1603
1658
  return this.sf2 !== null;
@@ -1696,7 +1751,7 @@ function configureGlobalContext(options) {
1696
1751
  "[playout] nativeAudioContext requested but this browser does not implement the AudioListener AudioParams Tone.js needs on a native context (Firefox). Falling back to the standardized-audio-context default \u2014 WAM plugin hosting is unavailable."
1697
1752
  );
1698
1753
  }
1699
- globalToneContext = new import_tone5.Context();
1754
+ globalToneContext = options.latencyHint !== void 0 ? new import_tone5.Context({ latencyHint: options.latencyHint }) : new import_tone5.Context();
1700
1755
  (0, import_tone5.setContext)(globalToneContext);
1701
1756
  const actualRate = globalToneContext.rawContext.sampleRate;
1702
1757
  if (options.sampleRate !== void 0 && options.sampleRate !== actualRate) {
@@ -1729,34 +1784,38 @@ function getGlobalAudioContextState() {
1729
1784
  return globalToneContext?.rawContext.state || "suspended";
1730
1785
  }
1731
1786
  async function closeGlobalAudioContext() {
1732
- if (globalToneContext && globalToneContext.rawContext.state !== "closed") {
1787
+ if (!globalToneContext) return;
1788
+ if (globalToneContext.rawContext.state !== "closed") {
1733
1789
  await globalToneContext.close();
1734
- globalToneContext = null;
1735
- _nativeMode = false;
1736
1790
  }
1791
+ globalToneContext = null;
1792
+ _nativeMode = false;
1737
1793
  }
1738
1794
 
1739
1795
  // src/mediaStreamSourceManager.ts
1740
- var import_tone6 = require("tone");
1741
1796
  var streamSources = /* @__PURE__ */ new Map();
1742
1797
  var streamCleanupHandlers = /* @__PURE__ */ new Map();
1743
1798
  function getMediaStreamSource(stream) {
1744
1799
  if (streamSources.has(stream)) {
1745
1800
  return streamSources.get(stream);
1746
1801
  }
1747
- const context = (0, import_tone6.getContext)();
1802
+ const context = getGlobalContext();
1748
1803
  const source = context.createMediaStreamSource(stream);
1749
1804
  streamSources.set(stream, source);
1805
+ const tracks = stream.getTracks();
1806
+ const onTrackEnded = () => {
1807
+ if (!stream.active) {
1808
+ cleanup();
1809
+ }
1810
+ };
1750
1811
  const cleanup = () => {
1751
1812
  source.disconnect();
1752
1813
  streamSources.delete(stream);
1753
1814
  streamCleanupHandlers.delete(stream);
1754
- stream.removeEventListener("ended", cleanup);
1755
- stream.removeEventListener("inactive", cleanup);
1815
+ tracks.forEach((track) => track.removeEventListener("ended", onTrackEnded));
1756
1816
  };
1757
1817
  streamCleanupHandlers.set(stream, cleanup);
1758
- stream.addEventListener("ended", cleanup);
1759
- stream.addEventListener("inactive", cleanup);
1818
+ tracks.forEach((track) => track.addEventListener("ended", onTrackEnded));
1760
1819
  return source;
1761
1820
  }
1762
1821
  function releaseMediaStreamSource(stream) {
@@ -1770,8 +1829,8 @@ function hasMediaStreamSource(stream) {
1770
1829
  }
1771
1830
 
1772
1831
  // src/TonePlayoutAdapter.ts
1773
- var import_core6 = require("@waveform-playlist/core");
1774
- var import_tone7 = require("tone");
1832
+ var import_core5 = require("@waveform-playlist/core");
1833
+ var import_tone6 = require("tone");
1775
1834
  function isToneAdapter(adapter) {
1776
1835
  return typeof adapter?.setSoundFontCache === "function";
1777
1836
  }
@@ -1802,8 +1861,8 @@ function createToneAdapter(options) {
1802
1861
  function addAudioTrackToPlayout(p, track) {
1803
1862
  const audioClips = track.clips.filter((c) => c.audioBuffer && !c.midiNotes);
1804
1863
  if (audioClips.length === 0) return;
1805
- const startTime = Math.min(...audioClips.map(import_core6.clipStartTime));
1806
- const endTime = Math.max(...audioClips.map(import_core6.clipEndTime));
1864
+ const startTime = Math.min(...audioClips.map(import_core5.clipStartTime));
1865
+ const endTime = Math.max(...audioClips.map(import_core5.clipEndTime));
1807
1866
  const trackObj = {
1808
1867
  id: track.id,
1809
1868
  name: track.name,
@@ -1816,9 +1875,9 @@ function createToneAdapter(options) {
1816
1875
  };
1817
1876
  const clipInfos = audioClips.map((clip) => ({
1818
1877
  buffer: clip.audioBuffer,
1819
- startTime: (0, import_core6.clipStartTime)(clip) - startTime,
1820
- duration: (0, import_core6.clipDurationTime)(clip),
1821
- offset: (0, import_core6.clipOffsetTime)(clip),
1878
+ startTime: (0, import_core5.clipStartTime)(clip) - startTime,
1879
+ duration: (0, import_core5.clipDurationTime)(clip),
1880
+ offset: (0, import_core5.clipOffsetTime)(clip),
1822
1881
  fadeIn: clip.fadeIn,
1823
1882
  fadeOut: clip.fadeOut,
1824
1883
  gain: clip.gain
@@ -1827,14 +1886,14 @@ function createToneAdapter(options) {
1827
1886
  clips: clipInfos,
1828
1887
  track: trackObj,
1829
1888
  effects: track.effects,
1830
- channelCount: (0, import_core6.trackChannelCount)(track)
1889
+ channelCount: (0, import_core5.trackChannelCount)(track)
1831
1890
  });
1832
1891
  }
1833
1892
  function addMidiTrackToPlayout(p, track) {
1834
1893
  const midiClips = track.clips.filter((c) => c.midiNotes && c.midiNotes.length > 0);
1835
1894
  if (midiClips.length === 0) return;
1836
- const startTime = Math.min(...midiClips.map(import_core6.clipStartTime));
1837
- const endTime = Math.max(...midiClips.map(import_core6.clipEndTime));
1895
+ const startTime = Math.min(...midiClips.map(import_core5.clipStartTime));
1896
+ const endTime = Math.max(...midiClips.map(import_core5.clipEndTime));
1838
1897
  const trackId = midiPlayoutTrackId(track);
1839
1898
  const trackObj = {
1840
1899
  id: trackId,
@@ -1848,9 +1907,9 @@ function createToneAdapter(options) {
1848
1907
  };
1849
1908
  const midiClipInfos = midiClips.map((clip) => ({
1850
1909
  notes: clip.midiNotes,
1851
- startTime: (0, import_core6.clipStartTime)(clip) - startTime,
1852
- duration: (0, import_core6.clipDurationTime)(clip),
1853
- offset: (0, import_core6.clipOffsetTime)(clip)
1910
+ startTime: (0, import_core5.clipStartTime)(clip) - startTime,
1911
+ duration: (0, import_core5.clipDurationTime)(clip),
1912
+ offset: (0, import_core5.clipOffsetTime)(clip)
1854
1913
  }));
1855
1914
  if (_soundFontCache?.isLoaded) {
1856
1915
  const firstClip = midiClips[0];
@@ -1889,7 +1948,7 @@ function createToneAdapter(options) {
1889
1948
  try {
1890
1949
  playout.dispose();
1891
1950
  } catch (err) {
1892
- console.warn("[waveform-playlist] Error disposing previous playout:", err);
1951
+ console.warn("[waveform-playlist] Error disposing previous playout: " + String(err));
1893
1952
  }
1894
1953
  playout = null;
1895
1954
  }
@@ -1901,8 +1960,7 @@ function createToneAdapter(options) {
1901
1960
  if (_audioInitialized) {
1902
1961
  _pendingInit = playout.init().catch((err) => {
1903
1962
  console.warn(
1904
- "[waveform-playlist] Failed to initialize playout. Audio playback will require another user gesture.",
1905
- err
1963
+ "[waveform-playlist] Failed to initialize playout. Audio playback will require another user gesture. " + String(err)
1906
1964
  );
1907
1965
  _audioInitialized = false;
1908
1966
  });
@@ -1987,25 +2045,28 @@ function createToneAdapter(options) {
1987
2045
  if (!playout) return;
1988
2046
  const audioClips = track.clips.filter((c) => c.audioBuffer && !c.midiNotes);
1989
2047
  if (audioClips.length > 0) {
1990
- const startTime = Math.min(...audioClips.map(import_core6.clipStartTime));
2048
+ const startTime = Math.min(...audioClips.map(import_core5.clipStartTime));
1991
2049
  const clipInfos = audioClips.map((clip) => ({
1992
2050
  buffer: clip.audioBuffer,
1993
- startTime: (0, import_core6.clipStartTime)(clip) - startTime,
1994
- duration: (0, import_core6.clipDurationTime)(clip),
1995
- offset: (0, import_core6.clipOffsetTime)(clip),
2051
+ startTime: (0, import_core5.clipStartTime)(clip) - startTime,
2052
+ duration: (0, import_core5.clipDurationTime)(clip),
2053
+ offset: (0, import_core5.clipOffsetTime)(clip),
1996
2054
  fadeIn: clip.fadeIn,
1997
2055
  fadeOut: clip.fadeOut,
1998
2056
  gain: clip.gain
1999
2057
  }));
2000
2058
  const audioUpdated = playout.replaceTrackClips(trackId, clipInfos, startTime);
2001
2059
  const midiClips = track.clips.filter((c) => c.midiNotes && c.midiNotes.length > 0);
2060
+ const midiTrackId = trackId + ":midi";
2002
2061
  if (midiClips.length > 0) {
2003
- const midiTrackId = trackId + ":midi";
2004
2062
  playout.removeTrack(midiTrackId);
2005
2063
  addMidiTrackToPlayout(playout, track);
2006
2064
  if (_isPlaying) {
2007
2065
  playout.resumeTrackMidPlayback(midiTrackId);
2008
2066
  }
2067
+ } else {
2068
+ playout.removeTrack(midiTrackId);
2069
+ _midiTrackBuild.delete(midiTrackId);
2009
2070
  }
2010
2071
  if (audioUpdated) {
2011
2072
  playout.applyInitialSoloState();
@@ -2051,7 +2112,7 @@ function createToneAdapter(options) {
2051
2112
  return;
2052
2113
  }
2053
2114
  const duration = endTime !== void 0 ? endTime - startTime : void 0;
2054
- playout.play((0, import_tone7.now)(), startTime, duration);
2115
+ playout.play((0, import_tone6.now)(), startTime, duration);
2055
2116
  _isPlaying = true;
2056
2117
  },
2057
2118
  pause() {
@@ -2074,25 +2135,34 @@ function createToneAdapter(options) {
2074
2135
  setMasterVolume(volume) {
2075
2136
  playout?.setMasterGain(volume);
2076
2137
  },
2138
+ // Runtime controls must reach BOTH playout halves of a mixed audio+MIDI
2139
+ // track (trackId + trackId:midi) — otherwise muting a mixed track leaves
2140
+ // its MIDI playing and soloing it silences its own MIDI half. The :midi
2141
+ // calls no-op for audio-only and MIDI-only tracks (unknown playout id),
2142
+ // same blind-forwarding pattern as removeTrack.
2077
2143
  setTrackVolume(trackId, volume) {
2078
2144
  const existing = _currentTracks.get(trackId);
2079
2145
  if (existing) _currentTracks.set(trackId, { ...existing, volume });
2080
2146
  playout?.getTrack(trackId)?.setVolume(volume);
2147
+ playout?.getTrack(trackId + ":midi")?.setVolume(volume);
2081
2148
  },
2082
2149
  setTrackMute(trackId, muted) {
2083
2150
  const existing = _currentTracks.get(trackId);
2084
2151
  if (existing) _currentTracks.set(trackId, { ...existing, muted });
2085
2152
  playout?.setMute(trackId, muted);
2153
+ playout?.setMute(trackId + ":midi", muted);
2086
2154
  },
2087
2155
  setTrackSolo(trackId, soloed) {
2088
2156
  const existing = _currentTracks.get(trackId);
2089
2157
  if (existing) _currentTracks.set(trackId, { ...existing, soloed });
2090
2158
  playout?.setSolo(trackId, soloed);
2159
+ playout?.setSolo(trackId + ":midi", soloed);
2091
2160
  },
2092
2161
  setTrackPan(trackId, pan) {
2093
2162
  const existing = _currentTracks.get(trackId);
2094
2163
  if (existing) _currentTracks.set(trackId, { ...existing, pan });
2095
2164
  playout?.getTrack(trackId)?.setPan(pan);
2165
+ playout?.getTrack(trackId + ":midi")?.setPan(pan);
2096
2166
  },
2097
2167
  setLoop(enabled, start2, end) {
2098
2168
  _loopEnabled = enabled;