@waveform-playlist/playout 12.5.4 → 13.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -55,25 +55,10 @@ var import_core4 = require("@waveform-playlist/core");
55
55
  // src/ToneTrack.ts
56
56
  var import_tone = require("tone");
57
57
  var import_core = require("@waveform-playlist/core");
58
-
59
- // src/fades.ts
60
- var hasWarned = false;
61
- function getUnderlyingAudioParam(signal) {
62
- const param = signal?._param;
63
- if (!param && !hasWarned) {
64
- hasWarned = true;
65
- console.warn(
66
- "[waveform-playlist] Unable to access Tone.js internal _param. This likely means the Tone.js version is incompatible. Mute scheduling may not work correctly."
67
- );
68
- }
69
- return param;
70
- }
71
-
72
- // src/ToneTrack.ts
73
58
  var ToneTrack = class {
74
59
  constructor(options) {
75
60
  this.activeSources = /* @__PURE__ */ new Set();
76
- this._effectsChainNode = null;
61
+ this._effectsChain = null;
77
62
  // Guard against ghost tick schedule callbacks. After stop/start cycles with
78
63
  // loops, stale Clock._lastUpdate causes ticks from the previous cycle to fire
79
64
  // Transport.schedule() callbacks at past positions (e.g., time 0 clips fire
@@ -82,22 +67,25 @@ var ToneTrack = class {
82
67
  // clips at/after this offset.
83
68
  this._scheduleGuardOffset = 0;
84
69
  this.track = options.track;
70
+ this.inputGain = new import_tone.Gain({ gain: 1 });
85
71
  this.volumeNode = new import_tone.Volume((0, import_core.gainToDb)(options.track.gain));
86
- this.panNode = new import_tone.Panner({
87
- pan: options.track.stereoPan,
88
- channelCount: options.channelCount ?? 1
89
- });
72
+ this.panStage = (0, import_core.createPanStage)(
73
+ (0, import_tone.getContext)().rawContext,
74
+ options.track.stereoPan,
75
+ options.channelCount ?? 1
76
+ );
90
77
  this.muteGain = new import_tone.Gain(options.track.muted ? 0 : 1);
91
- this.volumeNode.chain(this.panNode, this.muteGain);
92
- this._destination = options.destination || (0, import_tone.getDestination)();
78
+ this.volumeNode.connect(this.panStage.input);
79
+ (0, import_tone.connect)(this.panStage.output, this.muteGain);
80
+ this.muteGain.connect(options.destination || (0, import_tone.getDestination)());
93
81
  this._hasClosureEffects = Boolean(options.effects);
94
82
  if (options.effects) {
95
- const cleanup = options.effects(this.muteGain, this._destination, false);
83
+ const cleanup = options.effects(this.inputGain, this.volumeNode, false);
96
84
  if (cleanup) {
97
85
  this.effectsCleanup = cleanup;
98
86
  }
99
87
  } else {
100
- this.muteGain.connect(this._destination);
88
+ this.inputGain.connect(this.volumeNode);
101
89
  }
102
90
  const clipInfos = options.clips || (options.buffer ? [
103
91
  {
@@ -112,11 +100,11 @@ var ToneTrack = class {
112
100
  ] : []);
113
101
  const transport = (0, import_tone.getTransport)();
114
102
  const rawContext = (0, import_tone.getContext)().rawContext;
115
- const volumeNativeInput = this.volumeNode.input.input;
103
+ const trackNativeInput = this.inputGain.input;
116
104
  this.scheduledClips = clipInfos.map((clipInfo) => {
117
105
  const fadeGainNode = rawContext.createGain();
118
106
  fadeGainNode.gain.value = clipInfo.gain;
119
- fadeGainNode.connect(volumeNativeInput);
107
+ fadeGainNode.connect(trackNativeInput);
120
108
  const absTransportTime = this.track.startTime + clipInfo.startTime;
121
109
  const scheduleId = transport.schedule((audioContextTime) => {
122
110
  if (absTransportTime < this._scheduleGuardOffset) {
@@ -193,10 +181,9 @@ var ToneTrack = class {
193
181
  addClip(clipInfo) {
194
182
  const transport = (0, import_tone.getTransport)();
195
183
  const rawContext = (0, import_tone.getContext)().rawContext;
196
- const volumeNativeInput = this.volumeNode.input.input;
197
184
  const fadeGainNode = rawContext.createGain();
198
185
  fadeGainNode.gain.value = clipInfo.gain;
199
- fadeGainNode.connect(volumeNativeInput);
186
+ fadeGainNode.connect(this.inputGain.input);
200
187
  const absTransportTime = this.track.startTime + clipInfo.startTime;
201
188
  const scheduleId = transport.schedule((audioContextTime) => {
202
189
  if (absTransportTime < this._scheduleGuardOffset) return;
@@ -230,6 +217,15 @@ var ToneTrack = class {
230
217
  }
231
218
  this.scheduledClips.splice(index, 1);
232
219
  }
220
+ /**
221
+ * Select the pan law from the track's source material: 1 = mono law,
222
+ * 2+ = stereo law. Call when the track's clips change — a stereo clip
223
+ * joining a mono track moves it to the stereo law (same rule as the
224
+ * native transport and the offline export).
225
+ */
226
+ setSourceChannelCount(sourceChannelCount) {
227
+ this.panStage.setSourceChannelCount(sourceChannelCount);
228
+ }
233
229
  /**
234
230
  * Replace clips on this track. Diffs old vs new by buffer + timing —
235
231
  * unchanged clips keep their active sources playing (no audible interruption).
@@ -430,56 +426,71 @@ var ToneTrack = class {
430
426
  }
431
427
  setPan(pan) {
432
428
  this.track.stereoPan = pan;
433
- this.panNode.pan.value = pan;
429
+ this.panStage.setPan(pan);
434
430
  }
435
431
  setMute(muted) {
436
432
  this.track.muted = muted;
437
433
  const value = muted ? 0 : 1;
438
- const audioParam = getUnderlyingAudioParam(this.muteGain.gain);
439
- audioParam?.setValueAtTime(value, 0);
434
+ this.muteGain.input.gain.setValueAtTime(value, 0);
440
435
  this.muteGain.gain.value = value;
441
436
  }
442
437
  setSolo(soloed) {
443
438
  this.track.soloed = soloed;
444
439
  }
440
+ /** Native node behind the Volume stage — where an external chain returns. */
441
+ get _faderNativeInput() {
442
+ return this.volumeNode.input.input;
443
+ }
445
444
  /**
446
- * Insert an external effects chain: reroute muteGain → node instead of
447
- * muteGain → destination. The caller wires the chain's output onward
448
- * (dawcore's EffectsManager connects it to the master bus input).
449
- * Mutually exclusive with the TrackEffectsFunction closure model.
445
+ * Insert an external effects chain PRE-FADER (#660):
446
+ * `inputGain → input … output → volume → pan → mute`. The track owns both
447
+ * edges; the caller only builds what lies between the two nodes (pass the
448
+ * same node twice for a single-node effect). Replaces any chain connected
449
+ * earlier. Mutually exclusive with the TrackEffectsFunction closure model.
450
450
  */
451
- connectEffects(node) {
451
+ connectEffects(input, output) {
452
452
  if (this._hasClosureEffects) {
453
453
  throw new Error(
454
454
  '[waveform-playlist] Track "' + this.track.id + '" was built with a TrackEffectsFunction closure \u2014 transport effects hooks and closure effects are mutually exclusive.'
455
455
  );
456
456
  }
457
+ this._releaseEffectsChain();
457
458
  try {
458
- if (this._effectsChainNode) {
459
- this.muteGain.disconnect(this._effectsChainNode);
460
- } else {
461
- this.muteGain.disconnect(this._destination);
462
- }
459
+ this.inputGain.disconnect();
463
460
  } catch (err) {
464
461
  console.warn(
465
462
  '[waveform-playlist] connectEffects disconnect on track "' + this.track.id + '": ' + String(err)
466
463
  );
467
464
  }
468
- this.muteGain.connect(node);
469
- this._effectsChainNode = node;
465
+ this.inputGain.connect(input);
466
+ output.connect(this._faderNativeInput);
467
+ this._effectsChain = { input, output };
470
468
  }
471
- /** Restore the direct muteGain → destination connection. Safe when nothing is connected. */
469
+ /** Restore the direct inputGain → volume connection. Safe when nothing is connected. */
472
470
  disconnectEffects() {
473
- if (!this._effectsChainNode) return;
471
+ if (!this._effectsChain) return;
472
+ this._releaseEffectsChain();
474
473
  try {
475
- this.muteGain.disconnect(this._effectsChainNode);
474
+ this.inputGain.disconnect();
476
475
  } catch (err) {
477
476
  console.warn(
478
477
  "[waveform-playlist] disconnectEffects: " + (err instanceof Error ? err.message : String(err))
479
478
  );
480
479
  }
481
- this.muteGain.connect(this._destination);
482
- this._effectsChainNode = null;
480
+ this.inputGain.connect(this.volumeNode);
481
+ }
482
+ /** Targeted disconnect of the chain's return edge (output → volume); a
483
+ * consumer may have disposed its chain first, so failures only warn. */
484
+ _releaseEffectsChain() {
485
+ if (!this._effectsChain) return;
486
+ try {
487
+ this._effectsChain.output.disconnect(this._faderNativeInput);
488
+ } catch (err) {
489
+ console.warn(
490
+ '[waveform-playlist] effects chain was already disconnected from track "' + this.track.id + '": ' + String(err)
491
+ );
492
+ }
493
+ this._effectsChain = null;
483
494
  }
484
495
  dispose() {
485
496
  const transport = (0, import_tone.getTransport)();
@@ -509,6 +520,14 @@ var ToneTrack = class {
509
520
  );
510
521
  }
511
522
  });
523
+ this._releaseEffectsChain();
524
+ try {
525
+ this.inputGain.dispose();
526
+ } catch (err) {
527
+ console.warn(
528
+ `[waveform-playlist] Error disposing inputGain on track "${this.id}": ` + String(err)
529
+ );
530
+ }
512
531
  try {
513
532
  this.volumeNode.dispose();
514
533
  } catch (err) {
@@ -517,7 +536,7 @@ var ToneTrack = class {
517
536
  );
518
537
  }
519
538
  try {
520
- this.panNode.dispose();
539
+ this.panStage.dispose();
521
540
  } catch (err) {
522
541
  console.warn(
523
542
  `[waveform-playlist] Error disposing panNode on track "${this.id}": ` + String(err)
@@ -566,12 +585,18 @@ function getDrumCategory(midiNote) {
566
585
  var MidiToneTrack = class {
567
586
  constructor(options) {
568
587
  this.track = options.track;
588
+ this.inputGain = new import_tone2.Gain({ gain: 1 });
569
589
  this.volumeNode = new import_tone2.Volume((0, import_core2.gainToDb)(options.track.gain));
570
- this.panNode = new import_tone2.Panner(options.track.stereoPan);
590
+ this.panStage = (0, import_core2.createPanStage)(
591
+ (0, import_tone2.getContext)().rawContext,
592
+ options.track.stereoPan,
593
+ 1
594
+ );
571
595
  this.muteGain = new import_tone2.Gain(options.track.muted ? 0 : 1);
572
- this.volumeNode.chain(this.panNode, this.muteGain);
596
+ this.volumeNode.connect(this.panStage.input);
597
+ (0, import_tone2.connect)(this.panStage.output, this.muteGain);
573
598
  this.synth = new import_tone2.PolySynth(import_tone2.Synth, options.synthOptions);
574
- this.synth.connect(this.volumeNode);
599
+ this.synth.connect(this.inputGain);
575
600
  this.kickSynth = new import_tone2.PolySynth(import_tone2.MembraneSynth, {
576
601
  voice: import_tone2.MembraneSynth,
577
602
  options: {
@@ -602,18 +627,19 @@ var MidiToneTrack = class {
602
627
  envelope: { attack: 1e-3, decay: 0.3, sustain: 0, release: 0.1 }
603
628
  }
604
629
  });
605
- this.kickSynth.connect(this.volumeNode);
606
- this.snareSynth.connect(this.volumeNode);
607
- this.cymbalSynth.connect(this.volumeNode);
608
- this.tomSynth.connect(this.volumeNode);
630
+ this.kickSynth.connect(this.inputGain);
631
+ this.snareSynth.connect(this.inputGain);
632
+ this.cymbalSynth.connect(this.inputGain);
633
+ this.tomSynth.connect(this.inputGain);
609
634
  const destination = options.destination || (0, import_tone2.getDestination)();
635
+ this.muteGain.connect(destination);
610
636
  if (options.effects) {
611
- const cleanup = options.effects(this.muteGain, destination, false);
637
+ const cleanup = options.effects(this.inputGain, this.volumeNode, false);
612
638
  if (cleanup) {
613
639
  this.effectsCleanup = cleanup;
614
640
  }
615
641
  } else {
616
- this.muteGain.connect(destination);
642
+ this.inputGain.connect(this.volumeNode);
617
643
  }
618
644
  this.scheduledClips = options.clips.map((clipInfo) => {
619
645
  const visibleNotes = clipInfo.notes.filter((note) => {
@@ -773,13 +799,12 @@ var MidiToneTrack = class {
773
799
  }
774
800
  setPan(pan) {
775
801
  this.track.stereoPan = pan;
776
- this.panNode.pan.value = pan;
802
+ this.panStage.setPan(pan);
777
803
  }
778
804
  setMute(muted) {
779
805
  this.track.muted = muted;
780
806
  const value = muted ? 0 : 1;
781
- const audioParam = getUnderlyingAudioParam(this.muteGain.gain);
782
- audioParam?.setValueAtTime(value, 0);
807
+ this.muteGain.input.gain.setValueAtTime(value, 0);
783
808
  this.muteGain.gain.value = value;
784
809
  }
785
810
  setSolo(soloed) {
@@ -821,6 +846,13 @@ var MidiToneTrack = class {
821
846
  );
822
847
  }
823
848
  }
849
+ try {
850
+ this.inputGain.dispose();
851
+ } catch (err) {
852
+ console.warn(
853
+ `[waveform-playlist] Error disposing inputGain on MIDI track "${this.id}": ` + String(err)
854
+ );
855
+ }
824
856
  try {
825
857
  this.volumeNode.dispose();
826
858
  } catch (err) {
@@ -829,7 +861,7 @@ var MidiToneTrack = class {
829
861
  );
830
862
  }
831
863
  try {
832
- this.panNode.dispose();
864
+ this.panStage.dispose();
833
865
  } catch (err) {
834
866
  console.warn(
835
867
  `[waveform-playlist] Error disposing panNode on MIDI track "${this.id}": ` + String(err)
@@ -889,18 +921,25 @@ var SoundFontToneTrack = class {
889
921
  this.soundFontCache = options.soundFontCache;
890
922
  this.programNumber = options.programNumber ?? 0;
891
923
  this.bankNumber = options.isPercussion ? 128 : 0;
924
+ this.inputGain = new import_tone3.Gain({ gain: 1 });
892
925
  this.volumeNode = new import_tone3.Volume((0, import_core3.gainToDb)(options.track.gain));
893
- this.panNode = new import_tone3.Panner(options.track.stereoPan);
926
+ this.panStage = (0, import_core3.createPanStage)(
927
+ (0, import_tone3.getContext)().rawContext,
928
+ options.track.stereoPan,
929
+ 1
930
+ );
894
931
  this.muteGain = new import_tone3.Gain(options.track.muted ? 0 : 1);
895
- this.volumeNode.chain(this.panNode, this.muteGain);
932
+ this.volumeNode.connect(this.panStage.input);
933
+ (0, import_tone3.connect)(this.panStage.output, this.muteGain);
896
934
  const destination = options.destination || (0, import_tone3.getDestination)();
935
+ this.muteGain.connect(destination);
897
936
  if (options.effects) {
898
- const cleanup = options.effects(this.muteGain, destination, false);
937
+ const cleanup = options.effects(this.inputGain, this.volumeNode, false);
899
938
  if (cleanup) {
900
939
  this.effectsCleanup = cleanup;
901
940
  }
902
941
  } else {
903
- this.muteGain.connect(destination);
942
+ this.inputGain.connect(this.volumeNode);
904
943
  }
905
944
  this.scheduledClips = options.clips.map((clipInfo) => {
906
945
  const visibleNotes = clipInfo.notes.filter((note) => {
@@ -1001,7 +1040,7 @@ var SoundFontToneTrack = class {
1001
1040
  }
1002
1041
  gainNode.gain.linearRampToValueAtTime(0, noteOff + releaseVolEnv);
1003
1042
  source.connect(gainNode);
1004
- gainNode.connect(this.volumeNode.input.input);
1043
+ gainNode.connect(this.inputGain.input);
1005
1044
  this.activeSources.add(source);
1006
1045
  source.onended = () => {
1007
1046
  this.activeSources.delete(source);
@@ -1078,13 +1117,12 @@ var SoundFontToneTrack = class {
1078
1117
  }
1079
1118
  setPan(pan) {
1080
1119
  this.track.stereoPan = pan;
1081
- this.panNode.pan.value = pan;
1120
+ this.panStage.setPan(pan);
1082
1121
  }
1083
1122
  setMute(muted) {
1084
1123
  this.track.muted = muted;
1085
1124
  const value = muted ? 0 : 1;
1086
- const audioParam = getUnderlyingAudioParam(this.muteGain.gain);
1087
- audioParam?.setValueAtTime(value, 0);
1125
+ this.muteGain.input.gain.setValueAtTime(value, 0);
1088
1126
  this.muteGain.gain.value = value;
1089
1127
  }
1090
1128
  setSolo(soloed) {
@@ -1110,6 +1148,13 @@ var SoundFontToneTrack = class {
1110
1148
  );
1111
1149
  }
1112
1150
  });
1151
+ try {
1152
+ this.inputGain.dispose();
1153
+ } catch (err) {
1154
+ console.warn(
1155
+ `[waveform-playlist] Error disposing inputGain on SoundFont track "${this.id}": ` + String(err)
1156
+ );
1157
+ }
1113
1158
  try {
1114
1159
  this.volumeNode.dispose();
1115
1160
  } catch (err) {
@@ -1118,7 +1163,7 @@ var SoundFontToneTrack = class {
1118
1163
  );
1119
1164
  }
1120
1165
  try {
1121
- this.panNode.dispose();
1166
+ this.panStage.dispose();
1122
1167
  } catch (err) {
1123
1168
  console.warn(
1124
1169
  `[waveform-playlist] Error disposing panNode on SoundFont track "${this.id}": ` + String(err)
@@ -1263,11 +1308,17 @@ var TonePlayout = class {
1263
1308
  /**
1264
1309
  * Replace clips on a track, preserving the track's audio graph.
1265
1310
  * Only works for ToneTrack (audio clips), not MidiToneTrack.
1311
+ * `sourceChannelCount` (max channel count across the new clips) re-selects
1312
+ * the track's pan law; omit to leave it unchanged.
1266
1313
  */
1267
- replaceTrackClips(trackId, newClips, newStartTime) {
1314
+ replaceTrackClips(trackId, newClips, newStartTime, sourceChannelCount) {
1268
1315
  const track = this.tracks.get(trackId);
1269
1316
  if (!track || !("replaceClips" in track)) return false;
1270
- track.replaceClips(newClips, newStartTime);
1317
+ const toneTrack = track;
1318
+ toneTrack.replaceClips(newClips, newStartTime);
1319
+ if (sourceChannelCount !== void 0) {
1320
+ toneTrack.setSourceChannelCount(sourceChannelCount);
1321
+ }
1271
1322
  return true;
1272
1323
  }
1273
1324
  /**
@@ -1400,21 +1451,24 @@ var TonePlayout = class {
1400
1451
  get masterBusInputNode() {
1401
1452
  return this.masterVolume.input.input;
1402
1453
  }
1403
- /** Insert an external chain on a track: muteGain → node (caller wires node onward). */
1404
- connectTrackOutput(trackId, node) {
1454
+ /**
1455
+ * Insert an external chain on a track, PRE-FADER (#660): clips → input …
1456
+ * output → volume → pan → mute. The track wires both edges.
1457
+ */
1458
+ connectTrackEffects(trackId, input, output) {
1405
1459
  const track = this.tracks.get(trackId);
1406
1460
  if (!track) {
1407
- throw new Error('[waveform-playlist] connectTrackOutput: unknown track "' + trackId + '"');
1461
+ throw new Error('[waveform-playlist] connectTrackEffects: unknown track "' + trackId + '"');
1408
1462
  }
1409
1463
  if (!(track instanceof ToneTrack)) {
1410
1464
  throw new Error(
1411
- '[waveform-playlist] connectTrackOutput: per-track effects chains are not supported for MIDI tracks on the Tone adapter (track "' + trackId + '")'
1465
+ '[waveform-playlist] connectTrackEffects: per-track effects chains are not supported for MIDI tracks on the Tone adapter (track "' + trackId + '")'
1412
1466
  );
1413
1467
  }
1414
- track.connectEffects(node);
1468
+ track.connectEffects(input, output);
1415
1469
  }
1416
1470
  /** Restore a track's direct connection. No-op for unknown or MIDI tracks. */
1417
- disconnectTrackOutput(trackId) {
1471
+ disconnectTrackEffects(trackId) {
1418
1472
  const track = this.tracks.get(trackId);
1419
1473
  if (track instanceof ToneTrack) {
1420
1474
  track.disconnectEffects();
@@ -1783,7 +1837,11 @@ function configureGlobalContext(options) {
1783
1837
  "[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."
1784
1838
  );
1785
1839
  }
1786
- globalToneContext = options.latencyHint !== void 0 ? new import_tone5.Context({ latencyHint: options.latencyHint }) : new import_tone5.Context();
1840
+ const contextOptions = {
1841
+ ...options.latencyHint !== void 0 ? { latencyHint: options.latencyHint } : {},
1842
+ ...options.sampleRate !== void 0 ? { sampleRate: options.sampleRate } : {}
1843
+ };
1844
+ globalToneContext = Object.keys(contextOptions).length > 0 ? new import_tone5.Context(contextOptions) : new import_tone5.Context();
1787
1845
  (0, import_tone5.setContext)(globalToneContext);
1788
1846
  const actualRate = globalToneContext.rawContext.sampleRate;
1789
1847
  if (options.sampleRate !== void 0 && options.sampleRate !== actualRate) {
@@ -1860,6 +1918,19 @@ function hasMediaStreamSource(stream) {
1860
1918
  return streamSources.has(stream);
1861
1919
  }
1862
1920
 
1921
+ // src/fades.ts
1922
+ var hasWarned = false;
1923
+ function getUnderlyingAudioParam(signal) {
1924
+ const param = signal?._param;
1925
+ if (!param && !hasWarned) {
1926
+ hasWarned = true;
1927
+ console.warn(
1928
+ "[waveform-playlist] Unable to access Tone.js internal _param. This likely means the Tone.js version is incompatible. Mute scheduling may not work correctly."
1929
+ );
1930
+ }
1931
+ return param;
1932
+ }
1933
+
1863
1934
  // src/TonePlayoutAdapter.ts
1864
1935
  var import_core5 = require("@waveform-playlist/core");
1865
1936
  var import_tone6 = require("tone");
@@ -1888,12 +1959,11 @@ function createToneAdapter(options) {
1888
1959
  let _soundFontCache = options?.soundFontCache;
1889
1960
  const _currentTracks = /* @__PURE__ */ new Map();
1890
1961
  const _midiTrackBuild = /* @__PURE__ */ new Map();
1891
- function midiPlayoutTrackId(track) {
1892
- const hasAudio = track.clips.some((c) => c.audioBuffer && !c.midiNotes);
1893
- return hasAudio ? `${track.id}:midi` : track.id;
1962
+ function playableAudioClips(track) {
1963
+ return track.clips.filter((c) => c.audioBuffer && !c.midiNotes);
1894
1964
  }
1895
1965
  function addAudioTrackToPlayout(p, track) {
1896
- const audioClips = track.clips.filter((c) => c.audioBuffer && !c.midiNotes);
1966
+ const audioClips = playableAudioClips(track);
1897
1967
  if (audioClips.length === 0) return;
1898
1968
  const startTime = Math.min(...audioClips.map(import_core5.clipStartTime));
1899
1969
  const endTime = Math.max(...audioClips.map(import_core5.clipEndTime));
@@ -1919,7 +1989,7 @@ function createToneAdapter(options) {
1919
1989
  p.addTrack({
1920
1990
  clips: clipInfos,
1921
1991
  track: trackObj,
1922
- effects: track.effects,
1992
+ effects: options?.trackEffects?.(track.id),
1923
1993
  channelCount: (0, import_core5.trackChannelCount)(track)
1924
1994
  });
1925
1995
  }
@@ -1928,7 +1998,7 @@ function createToneAdapter(options) {
1928
1998
  if (midiClips.length === 0) return;
1929
1999
  const startTime = Math.min(...midiClips.map(import_core5.clipStartTime));
1930
2000
  const endTime = Math.max(...midiClips.map(import_core5.clipEndTime));
1931
- const trackId = midiPlayoutTrackId(track);
2001
+ const trackId = track.id;
1932
2002
  const trackObj = {
1933
2003
  id: trackId,
1934
2004
  name: track.name,
@@ -1939,6 +2009,7 @@ function createToneAdapter(options) {
1939
2009
  startTime,
1940
2010
  endTime
1941
2011
  };
2012
+ const effects = options?.trackEffects?.(track.id);
1942
2013
  const midiClipInfos = midiClips.map((clip) => ({
1943
2014
  notes: clip.midiNotes,
1944
2015
  startTime: (0, import_core5.clipStartTime)(clip) - startTime,
@@ -1956,7 +2027,7 @@ function createToneAdapter(options) {
1956
2027
  soundFontCache: _soundFontCache,
1957
2028
  programNumber,
1958
2029
  isPercussion,
1959
- effects: track.effects
2030
+ effects
1960
2031
  });
1961
2032
  _midiTrackBuild.set(trackId, _soundFontCache);
1962
2033
  } else {
@@ -1968,14 +2039,17 @@ function createToneAdapter(options) {
1968
2039
  p.addMidiTrack({
1969
2040
  clips: midiClipInfos,
1970
2041
  track: trackObj,
1971
- effects: track.effects
2042
+ effects
1972
2043
  });
1973
2044
  _midiTrackBuild.set(trackId, null);
1974
2045
  }
1975
2046
  }
1976
2047
  function addTrackToPlayout(p, track) {
1977
- addAudioTrackToPlayout(p, track);
1978
- addMidiTrackToPlayout(p, track);
2048
+ if ((0, import_core5.trackKind)(track) === "midi") {
2049
+ addMidiTrackToPlayout(p, track);
2050
+ } else {
2051
+ addAudioTrackToPlayout(p, track);
2052
+ }
1979
2053
  }
1980
2054
  function buildPlayout(tracks) {
1981
2055
  if (playout) {
@@ -2023,8 +2097,8 @@ function createToneAdapter(options) {
2023
2097
  return playout;
2024
2098
  }
2025
2099
  const effectsTransport = {
2026
- connectTrackOutput: (trackId, node) => requireEffectsPlayout().connectTrackOutput(trackId, node),
2027
- disconnectTrackOutput: (trackId) => requireEffectsPlayout().disconnectTrackOutput(trackId),
2100
+ connectTrackEffects: (trackId, input, output) => requireEffectsPlayout().connectTrackEffects(trackId, input, output),
2101
+ disconnectTrackEffects: (trackId) => requireEffectsPlayout().disconnectTrackEffects(trackId),
2028
2102
  connectMasterOutput: (node) => requireEffectsPlayout().connectMasterOutput(node),
2029
2103
  disconnectMasterOutput: () => requireEffectsPlayout().disconnectMasterOutput(),
2030
2104
  get masterOutputNode() {
@@ -2063,7 +2137,6 @@ function createToneAdapter(options) {
2063
2137
  for (const track of tracks) {
2064
2138
  if (oldTrackIds.has(track.id)) {
2065
2139
  playout.removeTrack(track.id);
2066
- playout.removeTrack(track.id + ":midi");
2067
2140
  }
2068
2141
  addTrackToPlayout(playout, track);
2069
2142
  }
@@ -2071,14 +2144,13 @@ function createToneAdapter(options) {
2071
2144
  if (_isPlaying) {
2072
2145
  for (const track of tracks) {
2073
2146
  playout.resumeTrackMidPlayback(track.id);
2074
- playout.resumeTrackMidPlayback(track.id + ":midi");
2075
2147
  }
2076
2148
  }
2077
2149
  },
2078
2150
  updateTrack(trackId, track) {
2079
2151
  _currentTracks.set(trackId, track);
2080
2152
  if (!playout) return;
2081
- const audioClips = track.clips.filter((c) => c.audioBuffer && !c.midiNotes);
2153
+ const audioClips = (0, import_core5.trackKind)(track) === "midi" ? [] : playableAudioClips(track);
2082
2154
  if (audioClips.length > 0) {
2083
2155
  const startTime = Math.min(...audioClips.map(import_core5.clipStartTime));
2084
2156
  const clipInfos = audioClips.map((clip) => ({
@@ -2090,33 +2162,23 @@ function createToneAdapter(options) {
2090
2162
  fadeOut: clip.fadeOut,
2091
2163
  gain: clip.gain
2092
2164
  }));
2093
- const audioUpdated = playout.replaceTrackClips(trackId, clipInfos, startTime);
2094
- const midiClips = track.clips.filter((c) => c.midiNotes && c.midiNotes.length > 0);
2095
- const midiTrackId = trackId + ":midi";
2096
- if (midiClips.length > 0) {
2097
- playout.removeTrack(midiTrackId);
2098
- addMidiTrackToPlayout(playout, track);
2099
- if (_isPlaying) {
2100
- playout.resumeTrackMidPlayback(midiTrackId);
2101
- }
2102
- } else {
2103
- playout.removeTrack(midiTrackId);
2104
- _midiTrackBuild.delete(midiTrackId);
2105
- }
2165
+ const audioUpdated = playout.replaceTrackClips(
2166
+ trackId,
2167
+ clipInfos,
2168
+ startTime,
2169
+ (0, import_core5.trackChannelCount)(track)
2170
+ );
2106
2171
  if (audioUpdated) {
2107
2172
  playout.applyInitialSoloState();
2108
2173
  return;
2109
2174
  }
2110
2175
  }
2111
2176
  playout.removeTrack(trackId);
2112
- playout.removeTrack(trackId + ":midi");
2113
2177
  _midiTrackBuild.delete(trackId);
2114
- _midiTrackBuild.delete(trackId + ":midi");
2115
2178
  addTrackToPlayout(playout, track);
2116
2179
  playout.applyInitialSoloState();
2117
2180
  if (_isPlaying) {
2118
2181
  playout.resumeTrackMidPlayback(trackId);
2119
- playout.resumeTrackMidPlayback(trackId + ":midi");
2120
2182
  }
2121
2183
  },
2122
2184
  addTrack(track) {
@@ -2133,10 +2195,8 @@ function createToneAdapter(options) {
2133
2195
  removeTrack(trackId) {
2134
2196
  _currentTracks.delete(trackId);
2135
2197
  _midiTrackBuild.delete(trackId);
2136
- _midiTrackBuild.delete(trackId + ":midi");
2137
2198
  if (!playout) return;
2138
2199
  playout.removeTrack(trackId);
2139
- playout.removeTrack(trackId + ":midi");
2140
2200
  playout.applyInitialSoloState();
2141
2201
  },
2142
2202
  play(startTime, endTime) {
@@ -2173,34 +2233,25 @@ function createToneAdapter(options) {
2173
2233
  setMasterVolume(volume) {
2174
2234
  playout?.setMasterGain(volume);
2175
2235
  },
2176
- // Runtime controls must reach BOTH playout halves of a mixed audio+MIDI
2177
- // track (trackId + trackId:midi) — otherwise muting a mixed track leaves
2178
- // its MIDI playing and soloing it silences its own MIDI half. The :midi
2179
- // calls no-op for audio-only and MIDI-only tracks (unknown playout id),
2180
- // same blind-forwarding pattern as removeTrack.
2181
2236
  setTrackVolume(trackId, volume) {
2182
2237
  const existing = _currentTracks.get(trackId);
2183
2238
  if (existing) _currentTracks.set(trackId, { ...existing, volume });
2184
2239
  playout?.getTrack(trackId)?.setVolume(volume);
2185
- playout?.getTrack(trackId + ":midi")?.setVolume(volume);
2186
2240
  },
2187
2241
  setTrackMute(trackId, muted) {
2188
2242
  const existing = _currentTracks.get(trackId);
2189
2243
  if (existing) _currentTracks.set(trackId, { ...existing, muted });
2190
2244
  playout?.setMute(trackId, muted);
2191
- playout?.setMute(trackId + ":midi", muted);
2192
2245
  },
2193
2246
  setTrackSolo(trackId, soloed) {
2194
2247
  const existing = _currentTracks.get(trackId);
2195
2248
  if (existing) _currentTracks.set(trackId, { ...existing, soloed });
2196
2249
  playout?.setSolo(trackId, soloed);
2197
- playout?.setSolo(trackId + ":midi", soloed);
2198
2250
  },
2199
2251
  setTrackPan(trackId, pan) {
2200
2252
  const existing = _currentTracks.get(trackId);
2201
2253
  if (existing) _currentTracks.set(trackId, { ...existing, pan });
2202
2254
  playout?.getTrack(trackId)?.setPan(pan);
2203
- playout?.getTrack(trackId + ":midi")?.setPan(pan);
2204
2255
  },
2205
2256
  setLoop(enabled, start2, end) {
2206
2257
  _loopEnabled = enabled;
@@ -2235,7 +2286,13 @@ function createToneAdapter(options) {
2235
2286
  ticksToSeconds(tick) {
2236
2287
  return tick * 60 / (_bpm * _ppqn);
2237
2288
  },
2289
+ /** Integer tick (engine `PlayoutAdapter.secondsToTicks` contract): the
2290
+ * exact position rounded once here, as NativePlayoutAdapter does. */
2238
2291
  secondsToTicks(seconds) {
2292
+ return Math.round(seconds * _bpm * _ppqn / 60);
2293
+ },
2294
+ /** Fractional tick position (engine `PlayoutAdapter.secondsToTicksExact`). */
2295
+ secondsToTicksExact(seconds) {
2239
2296
  return seconds * _bpm * _ppqn / 60;
2240
2297
  },
2241
2298
  // --- Cross-context worklet support ---
@@ -2272,9 +2329,10 @@ function createToneAdapter(options) {
2272
2329
  const effective = cache?.isLoaded ? cache : null;
2273
2330
  let changed = false;
2274
2331
  for (const track of _currentTracks.values()) {
2275
- const hasMidi = track.clips.some((c) => c.midiNotes && c.midiNotes.length > 0);
2276
- if (!hasMidi) continue;
2277
- const midiTrackId = midiPlayoutTrackId(track);
2332
+ if ((0, import_core5.trackKind)(track) !== "midi") continue;
2333
+ const hasNotes = track.clips.some((c) => c.midiNotes && c.midiNotes.length > 0);
2334
+ if (!hasNotes) continue;
2335
+ const midiTrackId = track.id;
2278
2336
  if (_midiTrackBuild.get(midiTrackId) === effective) continue;
2279
2337
  try {
2280
2338
  playout.removeTrack(midiTrackId);