@waveform-playlist/playout 12.5.1 → 12.5.3

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.mjs CHANGED
@@ -19,21 +19,12 @@ import {
19
19
  getTransport,
20
20
  getContext
21
21
  } from "tone";
22
- import { gainToDb } from "@waveform-playlist/core";
22
+ import { gainToDb, applyFadeIn, applyFadeOut } from "@waveform-playlist/core";
23
23
 
24
24
  // src/fades.ts
25
- import {
26
- linearCurve,
27
- exponentialCurve,
28
- sCurveCurve,
29
- logarithmicCurve,
30
- generateCurve,
31
- applyFadeIn,
32
- applyFadeOut
33
- } from "@waveform-playlist/core";
34
25
  var hasWarned = false;
35
26
  function getUnderlyingAudioParam(signal) {
36
- const param = signal._param;
27
+ const param = signal?._param;
37
28
  if (!param && !hasWarned) {
38
29
  hasWarned = true;
39
30
  console.warn(
@@ -116,8 +107,7 @@ var ToneTrack = class {
116
107
  source.start(audioContextTime, offset, duration);
117
108
  } catch (err) {
118
109
  console.warn(
119
- `[waveform-playlist] Failed to start source on track "${this.id}" (time=${audioContextTime}, offset=${offset}, duration=${duration}):`,
120
- err
110
+ `[waveform-playlist] Failed to start source on track "${this.id}" (time=${audioContextTime}, offset=${offset}, duration=${duration}): ` + String(err)
121
111
  );
122
112
  source.disconnect();
123
113
  return;
@@ -276,6 +266,10 @@ var ToneTrack = class {
276
266
  clipInfo.offset + Math.max(0, elapsed),
277
267
  clipInfo.duration - Math.max(0, elapsed)
278
268
  );
269
+ this.scheduleFades(scheduled, audioContextTime, clipInfo.offset + Math.max(0, elapsed));
270
+ } else if (absClipStart >= transportOffset) {
271
+ const delay = absClipStart - transportOffset;
272
+ this.scheduleFades(scheduled, audioContextTime + delay, clipInfo.offset);
279
273
  }
280
274
  }
281
275
  }
@@ -294,7 +288,9 @@ var ToneTrack = class {
294
288
  try {
295
289
  source.stop();
296
290
  } catch (err) {
297
- console.warn(`[waveform-playlist] Error stopping source on track "${this.id}":`, err);
291
+ console.warn(
292
+ `[waveform-playlist] Error stopping source on track "${this.id}": ` + String(err)
293
+ );
298
294
  }
299
295
  });
300
296
  this.activeSources.clear();
@@ -423,10 +419,16 @@ var ToneTrack = class {
423
419
  '[waveform-playlist] Track "' + this.track.id + '" was built with a TrackEffectsFunction closure \u2014 transport effects hooks and closure effects are mutually exclusive.'
424
420
  );
425
421
  }
426
- if (this._effectsChainNode) {
427
- this.muteGain.disconnect(this._effectsChainNode);
428
- } else {
429
- this.muteGain.disconnect(this._destination);
422
+ try {
423
+ if (this._effectsChainNode) {
424
+ this.muteGain.disconnect(this._effectsChainNode);
425
+ } else {
426
+ this.muteGain.disconnect(this._destination);
427
+ }
428
+ } catch (err) {
429
+ console.warn(
430
+ '[waveform-playlist] connectEffects disconnect on track "' + this.track.id + '": ' + String(err)
431
+ );
430
432
  }
431
433
  this.muteGain.connect(node);
432
434
  this._effectsChainNode = node;
@@ -450,7 +452,9 @@ var ToneTrack = class {
450
452
  try {
451
453
  this.effectsCleanup();
452
454
  } catch (err) {
453
- console.warn(`[waveform-playlist] Error during track "${this.id}" effects cleanup:`, err);
455
+ console.warn(
456
+ `[waveform-playlist] Error during track "${this.id}" effects cleanup: ` + String(err)
457
+ );
454
458
  }
455
459
  }
456
460
  this.stopAllSources();
@@ -459,42 +463,49 @@ var ToneTrack = class {
459
463
  transport.clear(scheduled.scheduleId);
460
464
  } catch (err) {
461
465
  console.warn(
462
- `[waveform-playlist] Error clearing schedule ${index} on track "${this.id}":`,
463
- err
466
+ `[waveform-playlist] Error clearing schedule ${index} on track "${this.id}": ` + String(err)
464
467
  );
465
468
  }
466
469
  try {
467
470
  scheduled.fadeGainNode.disconnect();
468
471
  } catch (err) {
469
472
  console.warn(
470
- `[waveform-playlist] Error disconnecting fadeGain ${index} on track "${this.id}":`,
471
- err
473
+ `[waveform-playlist] Error disconnecting fadeGain ${index} on track "${this.id}": ` + String(err)
472
474
  );
473
475
  }
474
476
  });
475
477
  try {
476
478
  this.volumeNode.dispose();
477
479
  } catch (err) {
478
- console.warn(`[waveform-playlist] Error disposing volumeNode on track "${this.id}":`, err);
480
+ console.warn(
481
+ `[waveform-playlist] Error disposing volumeNode on track "${this.id}": ` + String(err)
482
+ );
479
483
  }
480
484
  try {
481
485
  this.panNode.dispose();
482
486
  } catch (err) {
483
- console.warn(`[waveform-playlist] Error disposing panNode on track "${this.id}":`, err);
487
+ console.warn(
488
+ `[waveform-playlist] Error disposing panNode on track "${this.id}": ` + String(err)
489
+ );
484
490
  }
485
491
  try {
486
492
  this.muteGain.dispose();
487
493
  } catch (err) {
488
- console.warn(`[waveform-playlist] Error disposing muteGain on track "${this.id}":`, err);
494
+ console.warn(
495
+ `[waveform-playlist] Error disposing muteGain on track "${this.id}": ` + String(err)
496
+ );
489
497
  }
490
498
  }
491
499
  get id() {
492
500
  return this.track.id;
493
501
  }
494
502
  get duration() {
495
- if (this.scheduledClips.length === 0) return 0;
496
- const lastClip = this.scheduledClips[this.scheduledClips.length - 1];
497
- return lastClip.clipInfo.startTime + lastClip.clipInfo.duration;
503
+ let max = 0;
504
+ for (const { clipInfo } of this.scheduledClips) {
505
+ const end = clipInfo.startTime + clipInfo.duration;
506
+ if (end > max) max = end;
507
+ }
508
+ return max;
498
509
  }
499
510
  get buffer() {
500
511
  return this.scheduledClips[0]?.clipInfo.buffer;
@@ -635,8 +646,7 @@ var MidiToneTrack = class {
635
646
  this.snareSynth.triggerAttackRelease(duration, time, velocity);
636
647
  } catch (err) {
637
648
  console.warn(
638
- "[waveform-playlist] Snare overlap \u2014 previous hit still decaying, skipped:",
639
- err
649
+ "[waveform-playlist] Snare overlap \u2014 previous hit still decaying, skipped: " + String(err)
640
650
  );
641
651
  }
642
652
  break;
@@ -697,8 +707,7 @@ var MidiToneTrack = class {
697
707
  );
698
708
  } catch (err) {
699
709
  console.warn(
700
- `[waveform-playlist] Failed to start mid-clip MIDI note "${note.name}" on track "${this.id}":`,
701
- err
710
+ `[waveform-playlist] Failed to start mid-clip MIDI note "${note.name}" on track "${this.id}": ` + String(err)
702
711
  );
703
712
  }
704
713
  }
@@ -711,14 +720,19 @@ var MidiToneTrack = class {
711
720
  */
712
721
  stopAllSources() {
713
722
  const now3 = getContext2().rawContext.currentTime;
714
- try {
715
- this.synth.releaseAll(now3);
716
- this.kickSynth.releaseAll(now3);
717
- this.cymbalSynth.releaseAll(now3);
718
- this.tomSynth.releaseAll(now3);
719
- } catch (err) {
720
- console.warn(`[waveform-playlist] Error releasing synth on track "${this.id}":`, err);
721
- }
723
+ const release = (label, fn) => {
724
+ try {
725
+ fn();
726
+ } catch (err) {
727
+ console.warn(
728
+ `[waveform-playlist] Error releasing ${label} synth on track "${this.id}": ` + String(err)
729
+ );
730
+ }
731
+ };
732
+ release("melodic", () => this.synth.releaseAll(now3));
733
+ release("kick", () => this.kickSynth.releaseAll(now3));
734
+ release("cymbal", () => this.cymbalSynth.releaseAll(now3));
735
+ release("tom", () => this.tomSynth.releaseAll(now3));
722
736
  }
723
737
  /**
724
738
  * No-op for MIDI — MIDI uses note velocity, not gain fades.
@@ -754,8 +768,7 @@ var MidiToneTrack = class {
754
768
  this.effectsCleanup();
755
769
  } catch (err) {
756
770
  console.warn(
757
- `[waveform-playlist] Error during MIDI track "${this.id}" effects cleanup:`,
758
- err
771
+ `[waveform-playlist] Error during MIDI track "${this.id}" effects cleanup: ` + String(err)
759
772
  );
760
773
  }
761
774
  }
@@ -765,8 +778,7 @@ var MidiToneTrack = class {
765
778
  part.dispose();
766
779
  } catch (err) {
767
780
  console.warn(
768
- `[waveform-playlist] Error disposing Part ${index} on MIDI track "${this.id}":`,
769
- err
781
+ `[waveform-playlist] Error disposing Part ${index} on MIDI track "${this.id}": ` + String(err)
770
782
  );
771
783
  }
772
784
  });
@@ -781,35 +793,43 @@ var MidiToneTrack = class {
781
793
  try {
782
794
  s?.dispose();
783
795
  } catch (err) {
784
- console.warn(`[waveform-playlist] Error disposing synth on MIDI track "${this.id}":`, err);
796
+ console.warn(
797
+ `[waveform-playlist] Error disposing synth on MIDI track "${this.id}": ` + String(err)
798
+ );
785
799
  }
786
800
  }
787
801
  try {
788
802
  this.volumeNode.dispose();
789
803
  } catch (err) {
790
804
  console.warn(
791
- `[waveform-playlist] Error disposing volumeNode on MIDI track "${this.id}":`,
792
- err
805
+ `[waveform-playlist] Error disposing volumeNode on MIDI track "${this.id}": ` + String(err)
793
806
  );
794
807
  }
795
808
  try {
796
809
  this.panNode.dispose();
797
810
  } catch (err) {
798
- console.warn(`[waveform-playlist] Error disposing panNode on MIDI track "${this.id}":`, err);
811
+ console.warn(
812
+ `[waveform-playlist] Error disposing panNode on MIDI track "${this.id}": ` + String(err)
813
+ );
799
814
  }
800
815
  try {
801
816
  this.muteGain.dispose();
802
817
  } catch (err) {
803
- console.warn(`[waveform-playlist] Error disposing muteGain on MIDI track "${this.id}":`, err);
818
+ console.warn(
819
+ `[waveform-playlist] Error disposing muteGain on MIDI track "${this.id}": ` + String(err)
820
+ );
804
821
  }
805
822
  }
806
823
  get id() {
807
824
  return this.track.id;
808
825
  }
809
826
  get duration() {
810
- if (this.scheduledClips.length === 0) return 0;
811
- const lastClip = this.scheduledClips[this.scheduledClips.length - 1];
812
- return lastClip.clipInfo.startTime + lastClip.clipInfo.duration;
827
+ let max = 0;
828
+ for (const { clipInfo } of this.scheduledClips) {
829
+ const end = clipInfo.startTime + clipInfo.duration;
830
+ if (end > max) max = end;
831
+ }
832
+ return max;
813
833
  }
814
834
  get muted() {
815
835
  return this.track.muted;
@@ -822,8 +842,25 @@ var MidiToneTrack = class {
822
842
  // src/SoundFontToneTrack.ts
823
843
  import { Volume as Volume3, Gain as Gain3, Panner as Panner3, Part as Part2, getDestination as getDestination3, getContext as getContext3 } from "tone";
824
844
  import { gainToDb as gainToDb3 } from "@waveform-playlist/core";
825
- var _SoundFontToneTrack = class _SoundFontToneTrack {
845
+ function envelopeValueAt(tRel, attack, hold, decay, peakGain, sustainGain) {
846
+ if (tRel <= 0) return 0;
847
+ if (tRel < attack) return peakGain * (tRel / attack);
848
+ const rampStart = hold > 1e-3 ? attack + hold : attack;
849
+ const rampEnd = attack + hold + decay;
850
+ if (tRel <= rampStart || rampEnd <= rampStart) {
851
+ return tRel >= rampEnd ? sustainGain : peakGain;
852
+ }
853
+ if (tRel < rampEnd) {
854
+ return peakGain + (sustainGain - peakGain) * ((tRel - rampStart) / (rampEnd - rampStart));
855
+ }
856
+ return sustainGain;
857
+ }
858
+ var SoundFontToneTrack = class {
826
859
  constructor(options) {
860
+ /** Rate-limit missing sample warnings — one per track instance (a page-
861
+ * lifetime static would silence diagnostics for unrelated later tracks
862
+ * and soundfont swaps). */
863
+ this._missingSampleWarned = false;
827
864
  this.activeSources = /* @__PURE__ */ new Set();
828
865
  this.track = options.track;
829
866
  this.soundFontCache = options.soundFontCache;
@@ -866,7 +903,13 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
866
903
  });
867
904
  const part = new Part2((time, event) => {
868
905
  if (event.duration > 0) {
869
- this.triggerNote(event.midi, event.duration, time, event.velocity, event.channel);
906
+ try {
907
+ this.triggerNote(event.midi, event.duration, time, event.velocity, event.channel);
908
+ } catch (err) {
909
+ console.warn(
910
+ `[waveform-playlist] Failed to trigger SoundFont note on track "${this.id}": ` + String(err)
911
+ );
912
+ }
870
913
  }
871
914
  }, partEvents);
872
915
  part.start(0);
@@ -883,11 +926,11 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
883
926
  const preset = channel === 9 ? 0 : this.programNumber;
884
927
  const sfSample = this.soundFontCache.getAudioBuffer(midiNote, bank, preset);
885
928
  if (!sfSample) {
886
- if (!_SoundFontToneTrack._missingSampleWarned) {
929
+ if (!this._missingSampleWarned) {
887
930
  console.warn(
888
- `[waveform-playlist] SoundFont sample not found for MIDI note ${midiNote} (bank ${bank}, preset ${preset}). Subsequent missing samples will be silent.`
931
+ `[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.`
889
932
  );
890
- _SoundFontToneTrack._missingSampleWarned = true;
933
+ this._missingSampleWarned = true;
891
934
  }
892
935
  return;
893
936
  }
@@ -906,15 +949,34 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
906
949
  const gainNode = rawContext.createGain();
907
950
  const { attackVolEnv, holdVolEnv, decayVolEnv, sustainVolEnv, releaseVolEnv } = sfSample;
908
951
  const sustainGain = peakGain * sustainVolEnv;
952
+ const noteOff = time + effectiveDuration;
953
+ const attackEnd = time + attackVolEnv;
954
+ const decayStart = time + attackVolEnv + holdVolEnv;
955
+ const decayEnd = decayStart + decayVolEnv;
956
+ const heldAtNoteOff = envelopeValueAt(
957
+ effectiveDuration,
958
+ attackVolEnv,
959
+ holdVolEnv,
960
+ decayVolEnv,
961
+ peakGain,
962
+ sustainGain
963
+ );
909
964
  gainNode.gain.setValueAtTime(0, time);
910
- gainNode.gain.linearRampToValueAtTime(peakGain, time + attackVolEnv);
911
- if (holdVolEnv > 1e-3) {
912
- gainNode.gain.setValueAtTime(peakGain, time + attackVolEnv + holdVolEnv);
965
+ if (noteOff < attackEnd) {
966
+ gainNode.gain.linearRampToValueAtTime(heldAtNoteOff, noteOff);
967
+ } else {
968
+ gainNode.gain.linearRampToValueAtTime(peakGain, attackEnd);
969
+ if (holdVolEnv > 1e-3 && decayStart < noteOff) {
970
+ gainNode.gain.setValueAtTime(peakGain, decayStart);
971
+ }
972
+ if (noteOff < decayEnd) {
973
+ gainNode.gain.linearRampToValueAtTime(heldAtNoteOff, noteOff);
974
+ } else {
975
+ gainNode.gain.linearRampToValueAtTime(sustainGain, decayEnd);
976
+ gainNode.gain.setValueAtTime(sustainGain, noteOff);
977
+ }
913
978
  }
914
- const decayStart = time + attackVolEnv + holdVolEnv;
915
- gainNode.gain.linearRampToValueAtTime(sustainGain, decayStart + decayVolEnv);
916
- gainNode.gain.setValueAtTime(sustainGain, time + effectiveDuration);
917
- gainNode.gain.linearRampToValueAtTime(0, time + effectiveDuration + releaseVolEnv);
979
+ gainNode.gain.linearRampToValueAtTime(0, noteOff + releaseVolEnv);
918
980
  source.connect(gainNode);
919
981
  gainNode.connect(this.volumeNode.input.input);
920
982
  this.activeSources.add(source);
@@ -923,7 +985,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
923
985
  try {
924
986
  gainNode.disconnect();
925
987
  } catch (err) {
926
- console.warn("[waveform-playlist] GainNode already disconnected:", err);
988
+ console.warn("[waveform-playlist] GainNode already disconnected: " + String(err));
927
989
  }
928
990
  };
929
991
  source.start(time);
@@ -958,8 +1020,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
958
1020
  );
959
1021
  } catch (err) {
960
1022
  console.warn(
961
- `[waveform-playlist] Failed to start mid-clip SoundFont note on track "${this.id}":`,
962
- err
1023
+ `[waveform-playlist] Failed to start mid-clip SoundFont note on track "${this.id}": ` + String(err)
963
1024
  );
964
1025
  }
965
1026
  }
@@ -975,7 +1036,9 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
975
1036
  try {
976
1037
  source.stop();
977
1038
  } catch (err) {
978
- console.warn("[waveform-playlist] Error stopping AudioBufferSourceNode:", err);
1039
+ console.warn(
1040
+ "[waveform-playlist] Error stopping AudioBufferSourceNode: " + String(err)
1041
+ );
979
1042
  }
980
1043
  }
981
1044
  this.activeSources.clear();
@@ -1010,8 +1073,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1010
1073
  this.effectsCleanup();
1011
1074
  } catch (err) {
1012
1075
  console.warn(
1013
- `[waveform-playlist] Error during SoundFont track "${this.id}" effects cleanup:`,
1014
- err
1076
+ `[waveform-playlist] Error during SoundFont track "${this.id}" effects cleanup: ` + String(err)
1015
1077
  );
1016
1078
  }
1017
1079
  }
@@ -1021,8 +1083,7 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1021
1083
  part.dispose();
1022
1084
  } catch (err) {
1023
1085
  console.warn(
1024
- `[waveform-playlist] Error disposing Part ${index} on SoundFont track "${this.id}":`,
1025
- err
1086
+ `[waveform-playlist] Error disposing Part ${index} on SoundFont track "${this.id}": ` + String(err)
1026
1087
  );
1027
1088
  }
1028
1089
  });
@@ -1030,24 +1091,21 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1030
1091
  this.volumeNode.dispose();
1031
1092
  } catch (err) {
1032
1093
  console.warn(
1033
- `[waveform-playlist] Error disposing volumeNode on SoundFont track "${this.id}":`,
1034
- err
1094
+ `[waveform-playlist] Error disposing volumeNode on SoundFont track "${this.id}": ` + String(err)
1035
1095
  );
1036
1096
  }
1037
1097
  try {
1038
1098
  this.panNode.dispose();
1039
1099
  } catch (err) {
1040
1100
  console.warn(
1041
- `[waveform-playlist] Error disposing panNode on SoundFont track "${this.id}":`,
1042
- err
1101
+ `[waveform-playlist] Error disposing panNode on SoundFont track "${this.id}": ` + String(err)
1043
1102
  );
1044
1103
  }
1045
1104
  try {
1046
1105
  this.muteGain.dispose();
1047
1106
  } catch (err) {
1048
1107
  console.warn(
1049
- `[waveform-playlist] Error disposing muteGain on SoundFont track "${this.id}":`,
1050
- err
1108
+ `[waveform-playlist] Error disposing muteGain on SoundFont track "${this.id}": ` + String(err)
1051
1109
  );
1052
1110
  }
1053
1111
  }
@@ -1055,9 +1113,12 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1055
1113
  return this.track.id;
1056
1114
  }
1057
1115
  get duration() {
1058
- if (this.scheduledClips.length === 0) return 0;
1059
- const lastClip = this.scheduledClips[this.scheduledClips.length - 1];
1060
- return lastClip.clipInfo.startTime + lastClip.clipInfo.duration;
1116
+ let max = 0;
1117
+ for (const { clipInfo } of this.scheduledClips) {
1118
+ const end = clipInfo.startTime + clipInfo.duration;
1119
+ if (end > max) max = end;
1120
+ }
1121
+ return max;
1061
1122
  }
1062
1123
  get muted() {
1063
1124
  return this.track.muted;
@@ -1066,9 +1127,6 @@ var _SoundFontToneTrack = class _SoundFontToneTrack {
1066
1127
  return this.track.startTime;
1067
1128
  }
1068
1129
  };
1069
- /** Rate-limit missing sample warnings — one per class lifetime */
1070
- _SoundFontToneTrack._missingSampleWarned = false;
1071
- var SoundFontToneTrack = _SoundFontToneTrack;
1072
1130
 
1073
1131
  // src/TonePlayout.ts
1074
1132
  var TonePlayout = class {
@@ -1106,7 +1164,9 @@ var TonePlayout = class {
1106
1164
  try {
1107
1165
  getTransport2().clear(this._completionEventId);
1108
1166
  } catch (err) {
1109
- console.warn("[waveform-playlist] Error clearing Transport completion event:", err);
1167
+ console.warn(
1168
+ "[waveform-playlist] Error clearing Transport completion event: " + String(err)
1169
+ );
1110
1170
  }
1111
1171
  this._completionEventId = null;
1112
1172
  }
@@ -1201,6 +1261,7 @@ var TonePlayout = class {
1201
1261
  const lookAhead = context.lookAhead ?? 0;
1202
1262
  const audibleOffset = Math.max(0, transport.seconds - lookAhead);
1203
1263
  track.startMidClipSources(audibleOffset, context.currentTime);
1264
+ track.prepareFades(context.currentTime, audibleOffset);
1204
1265
  }
1205
1266
  play(when, offset, duration) {
1206
1267
  if (!this.isInitialized) {
@@ -1220,7 +1281,9 @@ var TonePlayout = class {
1220
1281
  try {
1221
1282
  this.onPlaybackCompleteCallback?.();
1222
1283
  } catch (err) {
1223
- console.warn("[waveform-playlist] Error in playback completion callback:", err);
1284
+ console.warn(
1285
+ "[waveform-playlist] Error in playback completion callback: " + String(err)
1286
+ );
1224
1287
  }
1225
1288
  }, transportOffset + duration);
1226
1289
  }
@@ -1246,8 +1309,7 @@ var TonePlayout = class {
1246
1309
  this.clearCompletionEvent();
1247
1310
  this.tracks.forEach((track) => track.cancelFades());
1248
1311
  console.warn(
1249
- "[waveform-playlist] Transport.start() failed. Audio playback could not begin.",
1250
- err
1312
+ "[waveform-playlist] Transport.start() failed. Audio playback could not begin. " + String(err)
1251
1313
  );
1252
1314
  throw err;
1253
1315
  }
@@ -1257,7 +1319,7 @@ var TonePlayout = class {
1257
1319
  try {
1258
1320
  transport.pause();
1259
1321
  } catch (err) {
1260
- console.warn("[waveform-playlist] Transport.pause() failed:", err);
1322
+ console.warn("[waveform-playlist] Transport.pause() failed: " + String(err));
1261
1323
  }
1262
1324
  this.tracks.forEach((track) => track.stopAllSources());
1263
1325
  this.tracks.forEach((track) => track.cancelFades());
@@ -1268,13 +1330,13 @@ var TonePlayout = class {
1268
1330
  try {
1269
1331
  transport.stop();
1270
1332
  } catch (err) {
1271
- console.warn("[waveform-playlist] Transport.stop() failed:", err);
1333
+ console.warn("[waveform-playlist] Transport.stop() failed: " + String(err));
1272
1334
  }
1273
1335
  if (this._loopHandler) {
1274
1336
  try {
1275
1337
  transport.off("loop", this._loopHandler);
1276
1338
  } catch (err) {
1277
- console.warn("[waveform-playlist] Error removing loop handler:", err);
1339
+ console.warn("[waveform-playlist] Error removing loop handler: " + String(err));
1278
1340
  }
1279
1341
  this._loopHandler = null;
1280
1342
  }
@@ -1282,14 +1344,18 @@ var TonePlayout = class {
1282
1344
  try {
1283
1345
  track.stopAllSources();
1284
1346
  } catch (err) {
1285
- console.warn(`[waveform-playlist] Error stopping sources for track "${track.id}":`, err);
1347
+ console.warn(
1348
+ `[waveform-playlist] Error stopping sources for track "${track.id}": ` + String(err)
1349
+ );
1286
1350
  }
1287
1351
  });
1288
1352
  this.tracks.forEach((track) => {
1289
1353
  try {
1290
1354
  track.cancelFades();
1291
1355
  } catch (err) {
1292
- console.warn(`[waveform-playlist] Error canceling fades for track "${track.id}":`, err);
1356
+ console.warn(
1357
+ `[waveform-playlist] Error canceling fades for track "${track.id}": ` + String(err)
1358
+ );
1293
1359
  }
1294
1360
  });
1295
1361
  this.clearCompletionEvent();
@@ -1336,10 +1402,14 @@ var TonePlayout = class {
1336
1402
  * effects] → tap → node (caller wires node.output → ctx.destination).
1337
1403
  */
1338
1404
  connectMasterOutput(node) {
1339
- if (this._masterChainNode) {
1340
- this._masterTap.disconnect(this._masterChainNode);
1341
- } else {
1342
- this._masterTap.disconnect(getDestination4());
1405
+ try {
1406
+ if (this._masterChainNode) {
1407
+ this._masterTap.disconnect(this._masterChainNode);
1408
+ } else {
1409
+ this._masterTap.disconnect(getDestination4());
1410
+ }
1411
+ } catch (err) {
1412
+ console.warn("[waveform-playlist] connectMasterOutput disconnect: " + String(err));
1343
1413
  }
1344
1414
  this._masterTap.connect(node);
1345
1415
  this._masterChainNode = node;
@@ -1389,7 +1459,7 @@ var TonePlayout = class {
1389
1459
  const track = this.tracks.get(trackId);
1390
1460
  if (track) {
1391
1461
  this.manualMuteState.set(trackId, muted);
1392
- track.setMute(muted);
1462
+ this.updateSoloMuting();
1393
1463
  }
1394
1464
  }
1395
1465
  setLoop(enabled, loopStart, loopEnd) {
@@ -1402,7 +1472,7 @@ var TonePlayout = class {
1402
1472
  transport.loopEnd = loopEnd;
1403
1473
  transport.loop = enabled;
1404
1474
  } catch (err) {
1405
- console.warn("[waveform-playlist] Error configuring Transport loop:", err);
1475
+ console.warn("[waveform-playlist] Error configuring Transport loop: " + String(err));
1406
1476
  return;
1407
1477
  }
1408
1478
  if (enabled && !this._loopHandler) {
@@ -1417,8 +1487,7 @@ var TonePlayout = class {
1417
1487
  track.prepareFades(currentTime, this._loopStart);
1418
1488
  } catch (err) {
1419
1489
  console.warn(
1420
- `[waveform-playlist] Error re-scheduling track "${track.id}" on loop:`,
1421
- err
1490
+ `[waveform-playlist] Error re-scheduling track "${track.id}" on loop: ` + String(err)
1422
1491
  );
1423
1492
  }
1424
1493
  });
@@ -1439,13 +1508,17 @@ var TonePlayout = class {
1439
1508
  try {
1440
1509
  this.stop();
1441
1510
  } catch (err) {
1442
- console.warn("[waveform-playlist] Error stopping Transport during dispose:", err);
1511
+ console.warn(
1512
+ "[waveform-playlist] Error stopping Transport during dispose: " + String(err)
1513
+ );
1443
1514
  }
1444
1515
  this.tracks.forEach((track) => {
1445
1516
  try {
1446
1517
  track.dispose();
1447
1518
  } catch (err) {
1448
- console.warn(`[waveform-playlist] Error disposing track "${track.id}":`, err);
1519
+ console.warn(
1520
+ `[waveform-playlist] Error disposing track "${track.id}": ` + String(err)
1521
+ );
1449
1522
  }
1450
1523
  });
1451
1524
  this.tracks.clear();
@@ -1453,7 +1526,9 @@ var TonePlayout = class {
1453
1526
  try {
1454
1527
  this.effectsCleanup();
1455
1528
  } catch (err) {
1456
- console.warn("[waveform-playlist] Error during master effects cleanup:", err);
1529
+ console.warn(
1530
+ "[waveform-playlist] Error during master effects cleanup: " + String(err)
1531
+ );
1457
1532
  }
1458
1533
  }
1459
1534
  try {
@@ -1561,7 +1636,9 @@ var SoundFontCache = class _SoundFontCache {
1561
1636
  async load(url, signal) {
1562
1637
  const response = await fetch(url, { signal });
1563
1638
  if (!response.ok) {
1564
- throw new Error(`Failed to fetch SoundFont ${url}: ${response.statusText}`);
1639
+ throw new Error(
1640
+ `Failed to fetch SoundFont ${url}: ${response.status}${response.statusText ? ` ${response.statusText}` : ""}`
1641
+ );
1565
1642
  }
1566
1643
  const arrayBuffer = await response.arrayBuffer();
1567
1644
  try {
@@ -1571,6 +1648,7 @@ var SoundFontCache = class _SoundFontCache {
1571
1648
  `Failed to parse SoundFont ${url}: ${err instanceof Error ? err.message : String(err)}`
1572
1649
  );
1573
1650
  }
1651
+ this.audioBufferCache.clear();
1574
1652
  }
1575
1653
  /**
1576
1654
  * Load from an already-fetched ArrayBuffer.
@@ -1583,6 +1661,7 @@ var SoundFontCache = class _SoundFontCache {
1583
1661
  `Failed to parse SoundFont from buffer: ${err instanceof Error ? err.message : String(err)}`
1584
1662
  );
1585
1663
  }
1664
+ this.audioBufferCache.clear();
1586
1665
  }
1587
1666
  get isLoaded() {
1588
1667
  return this.sf2 !== null;
@@ -1681,7 +1760,7 @@ function configureGlobalContext(options) {
1681
1760
  "[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."
1682
1761
  );
1683
1762
  }
1684
- globalToneContext = new Context();
1763
+ globalToneContext = options.latencyHint !== void 0 ? new Context({ latencyHint: options.latencyHint }) : new Context();
1685
1764
  setContext(globalToneContext);
1686
1765
  const actualRate = globalToneContext.rawContext.sampleRate;
1687
1766
  if (options.sampleRate !== void 0 && options.sampleRate !== actualRate) {
@@ -1714,34 +1793,38 @@ function getGlobalAudioContextState() {
1714
1793
  return globalToneContext?.rawContext.state || "suspended";
1715
1794
  }
1716
1795
  async function closeGlobalAudioContext() {
1717
- if (globalToneContext && globalToneContext.rawContext.state !== "closed") {
1796
+ if (!globalToneContext) return;
1797
+ if (globalToneContext.rawContext.state !== "closed") {
1718
1798
  await globalToneContext.close();
1719
- globalToneContext = null;
1720
- _nativeMode = false;
1721
1799
  }
1800
+ globalToneContext = null;
1801
+ _nativeMode = false;
1722
1802
  }
1723
1803
 
1724
1804
  // src/mediaStreamSourceManager.ts
1725
- import { getContext as getContext5 } from "tone";
1726
1805
  var streamSources = /* @__PURE__ */ new Map();
1727
1806
  var streamCleanupHandlers = /* @__PURE__ */ new Map();
1728
1807
  function getMediaStreamSource(stream) {
1729
1808
  if (streamSources.has(stream)) {
1730
1809
  return streamSources.get(stream);
1731
1810
  }
1732
- const context = getContext5();
1811
+ const context = getGlobalContext();
1733
1812
  const source = context.createMediaStreamSource(stream);
1734
1813
  streamSources.set(stream, source);
1814
+ const tracks = stream.getTracks();
1815
+ const onTrackEnded = () => {
1816
+ if (!stream.active) {
1817
+ cleanup();
1818
+ }
1819
+ };
1735
1820
  const cleanup = () => {
1736
1821
  source.disconnect();
1737
1822
  streamSources.delete(stream);
1738
1823
  streamCleanupHandlers.delete(stream);
1739
- stream.removeEventListener("ended", cleanup);
1740
- stream.removeEventListener("inactive", cleanup);
1824
+ tracks.forEach((track) => track.removeEventListener("ended", onTrackEnded));
1741
1825
  };
1742
1826
  streamCleanupHandlers.set(stream, cleanup);
1743
- stream.addEventListener("ended", cleanup);
1744
- stream.addEventListener("inactive", cleanup);
1827
+ tracks.forEach((track) => track.addEventListener("ended", onTrackEnded));
1745
1828
  return source;
1746
1829
  }
1747
1830
  function releaseMediaStreamSource(stream) {
@@ -1880,7 +1963,7 @@ function createToneAdapter(options) {
1880
1963
  try {
1881
1964
  playout.dispose();
1882
1965
  } catch (err) {
1883
- console.warn("[waveform-playlist] Error disposing previous playout:", err);
1966
+ console.warn("[waveform-playlist] Error disposing previous playout: " + String(err));
1884
1967
  }
1885
1968
  playout = null;
1886
1969
  }
@@ -1892,8 +1975,7 @@ function createToneAdapter(options) {
1892
1975
  if (_audioInitialized) {
1893
1976
  _pendingInit = playout.init().catch((err) => {
1894
1977
  console.warn(
1895
- "[waveform-playlist] Failed to initialize playout. Audio playback will require another user gesture.",
1896
- err
1978
+ "[waveform-playlist] Failed to initialize playout. Audio playback will require another user gesture. " + String(err)
1897
1979
  );
1898
1980
  _audioInitialized = false;
1899
1981
  });
@@ -1990,13 +2072,16 @@ function createToneAdapter(options) {
1990
2072
  }));
1991
2073
  const audioUpdated = playout.replaceTrackClips(trackId, clipInfos, startTime);
1992
2074
  const midiClips = track.clips.filter((c) => c.midiNotes && c.midiNotes.length > 0);
2075
+ const midiTrackId = trackId + ":midi";
1993
2076
  if (midiClips.length > 0) {
1994
- const midiTrackId = trackId + ":midi";
1995
2077
  playout.removeTrack(midiTrackId);
1996
2078
  addMidiTrackToPlayout(playout, track);
1997
2079
  if (_isPlaying) {
1998
2080
  playout.resumeTrackMidPlayback(midiTrackId);
1999
2081
  }
2082
+ } else {
2083
+ playout.removeTrack(midiTrackId);
2084
+ _midiTrackBuild.delete(midiTrackId);
2000
2085
  }
2001
2086
  if (audioUpdated) {
2002
2087
  playout.applyInitialSoloState();
@@ -2065,25 +2150,34 @@ function createToneAdapter(options) {
2065
2150
  setMasterVolume(volume) {
2066
2151
  playout?.setMasterGain(volume);
2067
2152
  },
2153
+ // Runtime controls must reach BOTH playout halves of a mixed audio+MIDI
2154
+ // track (trackId + trackId:midi) — otherwise muting a mixed track leaves
2155
+ // its MIDI playing and soloing it silences its own MIDI half. The :midi
2156
+ // calls no-op for audio-only and MIDI-only tracks (unknown playout id),
2157
+ // same blind-forwarding pattern as removeTrack.
2068
2158
  setTrackVolume(trackId, volume) {
2069
2159
  const existing = _currentTracks.get(trackId);
2070
2160
  if (existing) _currentTracks.set(trackId, { ...existing, volume });
2071
2161
  playout?.getTrack(trackId)?.setVolume(volume);
2162
+ playout?.getTrack(trackId + ":midi")?.setVolume(volume);
2072
2163
  },
2073
2164
  setTrackMute(trackId, muted) {
2074
2165
  const existing = _currentTracks.get(trackId);
2075
2166
  if (existing) _currentTracks.set(trackId, { ...existing, muted });
2076
2167
  playout?.setMute(trackId, muted);
2168
+ playout?.setMute(trackId + ":midi", muted);
2077
2169
  },
2078
2170
  setTrackSolo(trackId, soloed) {
2079
2171
  const existing = _currentTracks.get(trackId);
2080
2172
  if (existing) _currentTracks.set(trackId, { ...existing, soloed });
2081
2173
  playout?.setSolo(trackId, soloed);
2174
+ playout?.setSolo(trackId + ":midi", soloed);
2082
2175
  },
2083
2176
  setTrackPan(trackId, pan) {
2084
2177
  const existing = _currentTracks.get(trackId);
2085
2178
  if (existing) _currentTracks.set(trackId, { ...existing, pan });
2086
2179
  playout?.getTrack(trackId)?.setPan(pan);
2180
+ playout?.getTrack(trackId + ":midi")?.setPan(pan);
2087
2181
  },
2088
2182
  setLoop(enabled, start2, end) {
2089
2183
  _loopEnabled = enabled;