@waveform-playlist/playout 12.4.0 → 12.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # @waveform-playlist/playout
2
+
3
+ Tone.js-based `PlayoutAdapter` for [`@waveform-playlist/engine`](https://www.npmjs.com/package/@waveform-playlist/engine) — drives playback, recording, and effects on a shared global Tone.js audio context. Used by `@waveform-playlist/browser` (the React surface). For native Web Audio (no Tone.js), see `@dawcore/transport`.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @waveform-playlist/playout
9
+ ```
10
+
11
+ Requires `tone` (`^15.0.0`) as a peer dependency — this package doesn't bundle Tone.js, so the version in your app is the one that runs. `@waveform-playlist/engine` is an optional peer; most consumers pull in both automatically via `@waveform-playlist/browser` rather than installing this package directly.
12
+
13
+ ## Usage
14
+
15
+ ```typescript
16
+ import { createToneAdapter } from '@waveform-playlist/playout';
17
+ import { PlaylistEngine } from '@waveform-playlist/engine';
18
+
19
+ const adapter = createToneAdapter();
20
+ const engine = new PlaylistEngine({ adapter });
21
+
22
+ engine.setTracks(tracks); // ClipTrack[]
23
+ engine.play(0);
24
+ ```
25
+
26
+ `createToneAdapter()` eagerly creates the shared global Tone.js context before building its audio graph, so `adapter.audioContext` is guaranteed to be the same context the playout's nodes are on.
27
+
28
+ ## Audio context is a standardized-audio-context ponyfill
29
+
30
+ `getGlobalContext()` / `getGlobalAudioContext()` return Tone.js's global context, whose `rawContext` is **always** a [`standardized-audio-context`](https://github.com/chrisguttandin/standardized-audio-context) ponyfill — **not** a native `BaseAudioContext` / `AudioContext`. This is deliberate: the ponyfill normalizes cross-browser differences (e.g. Firefox `AudioWorkletNode` / `AudioParam`).
31
+
32
+ Consequences for integration:
33
+
34
+ - `getGlobalAudioContext() instanceof window.AudioContext` is `false`.
35
+ - The **native** `AudioWorkletNode` constructor rejects it — `new AudioWorkletNode(getGlobalAudioContext(), 'my-processor')` throws `TypeError: parameter 1 is not of type 'BaseAudioContext'` (same in Chrome and Firefox).
36
+
37
+ Third-party worklet code should not hardcode the native constructor. Inject the `AudioWorkletNode` constructor and the module loader (the dependency-injection pattern from [`chrisguttandin/limiter-audio-worklet`](https://github.com/chrisguttandin/limiter-audio-worklet) — paired with [`limiter-audio-worklet-processor`](https://github.com/chrisguttandin/limiter-audio-worklet-processor)), or import `AudioWorkletNode` from `standardized-audio-context`, so the code works on a native **or** ponyfilled context. `@waveform-playlist/worklets` follows this exact pattern:
38
+
39
+ ```ts
40
+ // caller supplies addModule for whichever context type is in use
41
+ await addRecordingWorkletModule((url) => ctx.audioWorklet.addModule(url));
42
+ ```
43
+
44
+ If you genuinely need a native `AudioContext`, own it yourself via the web-components adapter surface (`@dawcore/*`), which uses native Web Audio rather than Tone.
45
+
46
+ ## License
47
+
48
+ MIT
package/dist/index.d.mts CHANGED
@@ -36,6 +36,9 @@ declare class ToneTrack {
36
36
  private muteGain;
37
37
  private track;
38
38
  private effectsCleanup?;
39
+ private _destination;
40
+ private _hasClosureEffects;
41
+ private _effectsChainNode;
39
42
  private _scheduleGuardOffset;
40
43
  constructor(options: ToneTrackOptions);
41
44
  /**
@@ -103,6 +106,15 @@ declare class ToneTrack {
103
106
  setPan(pan: number): void;
104
107
  setMute(muted: boolean): void;
105
108
  setSolo(soloed: boolean): void;
109
+ /**
110
+ * Insert an external effects chain: reroute muteGain → node instead of
111
+ * muteGain → destination. The caller wires the chain's output onward
112
+ * (dawcore's EffectsManager connects it to the master bus input).
113
+ * Mutually exclusive with the TrackEffectsFunction closure model.
114
+ */
115
+ connectEffects(node: AudioNode): void;
116
+ /** Restore the direct muteGain → destination connection. Safe when nothing is connected. */
117
+ disconnectEffects(): void;
106
118
  dispose(): void;
107
119
  get id(): string;
108
120
  get duration(): number;
@@ -446,6 +458,7 @@ declare class TonePlayout {
446
458
  private _loopEnabled;
447
459
  private _loopStart;
448
460
  private _loopEnd;
461
+ private _masterChainNode;
449
462
  constructor(options?: TonePlayoutOptions);
450
463
  private clearCompletionEvent;
451
464
  init(): Promise<void>;
@@ -479,6 +492,23 @@ declare class TonePlayout {
479
492
  * Connect analyzers/effects/recorders here — parallel or serial.
480
493
  * The tap's native GainNode is on the same standardized-audio-context as adapter.audioContext. */
481
494
  get masterOutputNode(): GainNode;
495
+ /**
496
+ * Native GainNode behind masterVolume.input — the master-bus junction that
497
+ * per-track effect chains reconnect into (pre master volume). Distinct from
498
+ * masterOutputNode (the post-volume tap for analyzers).
499
+ */
500
+ get masterBusInputNode(): GainNode;
501
+ /** Insert an external chain on a track: muteGain → node (caller wires node onward). */
502
+ connectTrackOutput(trackId: string, node: AudioNode): void;
503
+ /** Restore a track's direct connection. No-op for unknown or MIDI tracks. */
504
+ disconnectTrackOutput(trackId: string): void;
505
+ /**
506
+ * Insert an external master chain after the tap: masterVolume → [closure
507
+ * effects] → tap → node (caller wires node.output → ctx.destination).
508
+ */
509
+ connectMasterOutput(node: AudioNode): void;
510
+ /** Restore tap → destination. Safe when no master chain is connected. */
511
+ disconnectMasterOutput(): void;
482
512
  setSolo(trackId: string, soloed: boolean): void;
483
513
  private updateSoloMuting;
484
514
  setMute(trackId: string, muted: boolean): void;
@@ -497,27 +527,48 @@ declare class TonePlayout {
497
527
  * Provides a single AudioContext shared across the entire application.
498
528
  * This context is used by Tone.js for playback and by all recording/monitoring hooks.
499
529
  *
500
- * Uses Tone.js's Context class which wraps standardized-audio-context for
501
- * cross-browser compatibility (fixes Firefox AudioListener issues).
530
+ * Supports both native AudioContext (WAM 2.0 plugin hosting, requires
531
+ * AudioListener AudioParams — Firefox fallback to standardized-audio-context)
532
+ * and standardized-audio-context wrapper for cross-browser compatibility.
502
533
  */
503
534
 
504
535
  interface AudioContextOptions {
536
+ /**
537
+ * Create the global context around a NATIVE AudioContext instead of
538
+ * standardized-audio-context. Required for WAM 2.0 plugin hosting — WAM
539
+ * worklets subclass the native AudioWorkletNode and cannot join a
540
+ * standardized-audio-context graph. Falls back to the default context
541
+ * (with a console warning) on browsers missing AudioListener AudioParams
542
+ * (Firefox), where Tone.js Listener initialization would throw.
543
+ */
544
+ nativeAudioContext?: boolean;
505
545
  /** Desired sample rate. Creates a standardized-audio-context AudioContext
506
546
  * at this rate, bypassing Tone.js 15.1.22's limitation. Cross-browser safe. */
507
547
  sampleRate?: number;
508
548
  /** Latency hint passed to the AudioContext constructor. */
509
549
  latencyHint?: AudioContextLatencyCategory | number;
510
550
  }
551
+ /**
552
+ * Whether this browser can run Tone.js on a raw native AudioContext.
553
+ * Firefox lacks the AudioListener AudioParams (positionX/…/upZ) that Tone's
554
+ * Listener wraps eagerly at context initialization (Tone.js #681) —
555
+ * standardized-audio-context polyfills them, native contexts cannot.
556
+ */
557
+ declare function supportsNativeContextMode(): boolean;
558
+ /**
559
+ * True when the global context wraps a native AudioContext (WAM-capable).
560
+ */
561
+ declare function isNativeGlobalContext(): boolean;
511
562
  /**
512
563
  * Configure the global AudioContext with sample rate and latency hints.
513
- * Creates a standardized-audio-context AudioContext (cross-browser, fixes
514
- * Firefox AudioListener bug) and wraps it in Tone.js Context.
564
+ * Supports both native AudioContext (for WAM 2.0 hosting) and standardized-audio-context.
515
565
  *
516
566
  * Should be called BEFORE getGlobalContext(). If the context already exists
517
567
  * (e.g., from resumeGlobalAudioContext), warns and returns the existing rate.
518
568
  *
519
569
  * ```ts
520
570
  * configureGlobalContext({ sampleRate: 48000, latencyHint: 0 })
571
+ * configureGlobalContext({ nativeAudioContext: true, sampleRate: 48000 })
521
572
  * ```
522
573
  */
523
574
  declare function configureGlobalContext(options: AudioContextOptions): number;
@@ -620,6 +671,19 @@ interface ToneAdapterOptions {
620
671
  /** Pulses per quarter note. Defaults to 192 (Tone.js native). */
621
672
  ppqn?: number;
622
673
  }
674
+ /**
675
+ * Effects wiring hooks consumed by dawcore's EffectsManager (structural match
676
+ * for its EffectsTransportLike). All hooks require native-context mode —
677
+ * effect chains carry native AudioNodes (incl. WAM worklets) that cannot join
678
+ * a standardized-audio-context graph.
679
+ */
680
+ interface ToneEffectsTransport {
681
+ connectTrackOutput(trackId: string, node: AudioNode): void;
682
+ disconnectTrackOutput(trackId: string): void;
683
+ connectMasterOutput(node: AudioNode): void;
684
+ disconnectMasterOutput(): void;
685
+ readonly masterOutputNode: AudioNode;
686
+ }
623
687
  interface ToneAdapter extends PlayoutAdapter {
624
688
  /**
625
689
  * Provide or swap the SoundFont after creation. Rebuilds only the MIDI
@@ -627,6 +691,8 @@ interface ToneAdapter extends PlayoutAdapter {
627
691
  * Pass undefined to revert MIDI tracks to PolySynth synthesis.
628
692
  */
629
693
  setSoundFontCache(cache: SoundFontCache | undefined): void;
694
+ /** Effects wiring hooks (dawcore EffectsManager). Requires native-context mode. */
695
+ readonly transport: ToneEffectsTransport;
630
696
  }
631
697
  /**
632
698
  * Capability check for Tone-specific adapter features. Structural, not
@@ -636,4 +702,4 @@ interface ToneAdapter extends PlayoutAdapter {
636
702
  declare function isToneAdapter(adapter: PlayoutAdapter | null | undefined): adapter is ToneAdapter;
637
703
  declare function createToneAdapter(options?: ToneAdapterOptions): ToneAdapter;
638
704
 
639
- export { type AudioContextOptions, type EffectsFunction, type LoopAndEnvelopeParams, type MidiClipInfo, MidiToneTrack, type MidiToneTrackOptions, type PlayableTrack, type PlaybackRateParams, SoundFontCache, type SoundFontSample, SoundFontToneTrack, type SoundFontToneTrackOptions, type ToneAdapter, type ToneAdapterOptions, TonePlayout, type TonePlayoutOptions, ToneTrack, type ToneTrackOptions, type TrackEffectsFunction, calculatePlaybackRate, closeGlobalAudioContext, configureGlobalContext, createToneAdapter, extractLoopAndEnvelope, getGeneratorValue, getGlobalAudioContext, getGlobalAudioContextState, getGlobalContext, getGlobalToneContext, getMediaStreamSource, getUnderlyingAudioParam, hasMediaStreamSource, int16ToFloat32, isToneAdapter, releaseMediaStreamSource, resumeGlobalAudioContext, timecentsToSeconds };
705
+ export { type AudioContextOptions, type EffectsFunction, type LoopAndEnvelopeParams, type MidiClipInfo, MidiToneTrack, type MidiToneTrackOptions, type PlayableTrack, type PlaybackRateParams, SoundFontCache, type SoundFontSample, SoundFontToneTrack, type SoundFontToneTrackOptions, type ToneAdapter, type ToneAdapterOptions, type ToneEffectsTransport, TonePlayout, type TonePlayoutOptions, ToneTrack, type ToneTrackOptions, type TrackEffectsFunction, calculatePlaybackRate, closeGlobalAudioContext, configureGlobalContext, createToneAdapter, extractLoopAndEnvelope, getGeneratorValue, getGlobalAudioContext, getGlobalAudioContextState, getGlobalContext, getGlobalToneContext, getMediaStreamSource, getUnderlyingAudioParam, hasMediaStreamSource, int16ToFloat32, isNativeGlobalContext, isToneAdapter, releaseMediaStreamSource, resumeGlobalAudioContext, supportsNativeContextMode, timecentsToSeconds };
package/dist/index.d.ts CHANGED
@@ -36,6 +36,9 @@ declare class ToneTrack {
36
36
  private muteGain;
37
37
  private track;
38
38
  private effectsCleanup?;
39
+ private _destination;
40
+ private _hasClosureEffects;
41
+ private _effectsChainNode;
39
42
  private _scheduleGuardOffset;
40
43
  constructor(options: ToneTrackOptions);
41
44
  /**
@@ -103,6 +106,15 @@ declare class ToneTrack {
103
106
  setPan(pan: number): void;
104
107
  setMute(muted: boolean): void;
105
108
  setSolo(soloed: boolean): void;
109
+ /**
110
+ * Insert an external effects chain: reroute muteGain → node instead of
111
+ * muteGain → destination. The caller wires the chain's output onward
112
+ * (dawcore's EffectsManager connects it to the master bus input).
113
+ * Mutually exclusive with the TrackEffectsFunction closure model.
114
+ */
115
+ connectEffects(node: AudioNode): void;
116
+ /** Restore the direct muteGain → destination connection. Safe when nothing is connected. */
117
+ disconnectEffects(): void;
106
118
  dispose(): void;
107
119
  get id(): string;
108
120
  get duration(): number;
@@ -446,6 +458,7 @@ declare class TonePlayout {
446
458
  private _loopEnabled;
447
459
  private _loopStart;
448
460
  private _loopEnd;
461
+ private _masterChainNode;
449
462
  constructor(options?: TonePlayoutOptions);
450
463
  private clearCompletionEvent;
451
464
  init(): Promise<void>;
@@ -479,6 +492,23 @@ declare class TonePlayout {
479
492
  * Connect analyzers/effects/recorders here — parallel or serial.
480
493
  * The tap's native GainNode is on the same standardized-audio-context as adapter.audioContext. */
481
494
  get masterOutputNode(): GainNode;
495
+ /**
496
+ * Native GainNode behind masterVolume.input — the master-bus junction that
497
+ * per-track effect chains reconnect into (pre master volume). Distinct from
498
+ * masterOutputNode (the post-volume tap for analyzers).
499
+ */
500
+ get masterBusInputNode(): GainNode;
501
+ /** Insert an external chain on a track: muteGain → node (caller wires node onward). */
502
+ connectTrackOutput(trackId: string, node: AudioNode): void;
503
+ /** Restore a track's direct connection. No-op for unknown or MIDI tracks. */
504
+ disconnectTrackOutput(trackId: string): void;
505
+ /**
506
+ * Insert an external master chain after the tap: masterVolume → [closure
507
+ * effects] → tap → node (caller wires node.output → ctx.destination).
508
+ */
509
+ connectMasterOutput(node: AudioNode): void;
510
+ /** Restore tap → destination. Safe when no master chain is connected. */
511
+ disconnectMasterOutput(): void;
482
512
  setSolo(trackId: string, soloed: boolean): void;
483
513
  private updateSoloMuting;
484
514
  setMute(trackId: string, muted: boolean): void;
@@ -497,27 +527,48 @@ declare class TonePlayout {
497
527
  * Provides a single AudioContext shared across the entire application.
498
528
  * This context is used by Tone.js for playback and by all recording/monitoring hooks.
499
529
  *
500
- * Uses Tone.js's Context class which wraps standardized-audio-context for
501
- * cross-browser compatibility (fixes Firefox AudioListener issues).
530
+ * Supports both native AudioContext (WAM 2.0 plugin hosting, requires
531
+ * AudioListener AudioParams — Firefox fallback to standardized-audio-context)
532
+ * and standardized-audio-context wrapper for cross-browser compatibility.
502
533
  */
503
534
 
504
535
  interface AudioContextOptions {
536
+ /**
537
+ * Create the global context around a NATIVE AudioContext instead of
538
+ * standardized-audio-context. Required for WAM 2.0 plugin hosting — WAM
539
+ * worklets subclass the native AudioWorkletNode and cannot join a
540
+ * standardized-audio-context graph. Falls back to the default context
541
+ * (with a console warning) on browsers missing AudioListener AudioParams
542
+ * (Firefox), where Tone.js Listener initialization would throw.
543
+ */
544
+ nativeAudioContext?: boolean;
505
545
  /** Desired sample rate. Creates a standardized-audio-context AudioContext
506
546
  * at this rate, bypassing Tone.js 15.1.22's limitation. Cross-browser safe. */
507
547
  sampleRate?: number;
508
548
  /** Latency hint passed to the AudioContext constructor. */
509
549
  latencyHint?: AudioContextLatencyCategory | number;
510
550
  }
551
+ /**
552
+ * Whether this browser can run Tone.js on a raw native AudioContext.
553
+ * Firefox lacks the AudioListener AudioParams (positionX/…/upZ) that Tone's
554
+ * Listener wraps eagerly at context initialization (Tone.js #681) —
555
+ * standardized-audio-context polyfills them, native contexts cannot.
556
+ */
557
+ declare function supportsNativeContextMode(): boolean;
558
+ /**
559
+ * True when the global context wraps a native AudioContext (WAM-capable).
560
+ */
561
+ declare function isNativeGlobalContext(): boolean;
511
562
  /**
512
563
  * Configure the global AudioContext with sample rate and latency hints.
513
- * Creates a standardized-audio-context AudioContext (cross-browser, fixes
514
- * Firefox AudioListener bug) and wraps it in Tone.js Context.
564
+ * Supports both native AudioContext (for WAM 2.0 hosting) and standardized-audio-context.
515
565
  *
516
566
  * Should be called BEFORE getGlobalContext(). If the context already exists
517
567
  * (e.g., from resumeGlobalAudioContext), warns and returns the existing rate.
518
568
  *
519
569
  * ```ts
520
570
  * configureGlobalContext({ sampleRate: 48000, latencyHint: 0 })
571
+ * configureGlobalContext({ nativeAudioContext: true, sampleRate: 48000 })
521
572
  * ```
522
573
  */
523
574
  declare function configureGlobalContext(options: AudioContextOptions): number;
@@ -620,6 +671,19 @@ interface ToneAdapterOptions {
620
671
  /** Pulses per quarter note. Defaults to 192 (Tone.js native). */
621
672
  ppqn?: number;
622
673
  }
674
+ /**
675
+ * Effects wiring hooks consumed by dawcore's EffectsManager (structural match
676
+ * for its EffectsTransportLike). All hooks require native-context mode —
677
+ * effect chains carry native AudioNodes (incl. WAM worklets) that cannot join
678
+ * a standardized-audio-context graph.
679
+ */
680
+ interface ToneEffectsTransport {
681
+ connectTrackOutput(trackId: string, node: AudioNode): void;
682
+ disconnectTrackOutput(trackId: string): void;
683
+ connectMasterOutput(node: AudioNode): void;
684
+ disconnectMasterOutput(): void;
685
+ readonly masterOutputNode: AudioNode;
686
+ }
623
687
  interface ToneAdapter extends PlayoutAdapter {
624
688
  /**
625
689
  * Provide or swap the SoundFont after creation. Rebuilds only the MIDI
@@ -627,6 +691,8 @@ interface ToneAdapter extends PlayoutAdapter {
627
691
  * Pass undefined to revert MIDI tracks to PolySynth synthesis.
628
692
  */
629
693
  setSoundFontCache(cache: SoundFontCache | undefined): void;
694
+ /** Effects wiring hooks (dawcore EffectsManager). Requires native-context mode. */
695
+ readonly transport: ToneEffectsTransport;
630
696
  }
631
697
  /**
632
698
  * Capability check for Tone-specific adapter features. Structural, not
@@ -636,4 +702,4 @@ interface ToneAdapter extends PlayoutAdapter {
636
702
  declare function isToneAdapter(adapter: PlayoutAdapter | null | undefined): adapter is ToneAdapter;
637
703
  declare function createToneAdapter(options?: ToneAdapterOptions): ToneAdapter;
638
704
 
639
- export { type AudioContextOptions, type EffectsFunction, type LoopAndEnvelopeParams, type MidiClipInfo, MidiToneTrack, type MidiToneTrackOptions, type PlayableTrack, type PlaybackRateParams, SoundFontCache, type SoundFontSample, SoundFontToneTrack, type SoundFontToneTrackOptions, type ToneAdapter, type ToneAdapterOptions, TonePlayout, type TonePlayoutOptions, ToneTrack, type ToneTrackOptions, type TrackEffectsFunction, calculatePlaybackRate, closeGlobalAudioContext, configureGlobalContext, createToneAdapter, extractLoopAndEnvelope, getGeneratorValue, getGlobalAudioContext, getGlobalAudioContextState, getGlobalContext, getGlobalToneContext, getMediaStreamSource, getUnderlyingAudioParam, hasMediaStreamSource, int16ToFloat32, isToneAdapter, releaseMediaStreamSource, resumeGlobalAudioContext, timecentsToSeconds };
705
+ export { type AudioContextOptions, type EffectsFunction, type LoopAndEnvelopeParams, type MidiClipInfo, MidiToneTrack, type MidiToneTrackOptions, type PlayableTrack, type PlaybackRateParams, SoundFontCache, type SoundFontSample, SoundFontToneTrack, type SoundFontToneTrackOptions, type ToneAdapter, type ToneAdapterOptions, type ToneEffectsTransport, TonePlayout, type TonePlayoutOptions, ToneTrack, type ToneTrackOptions, type TrackEffectsFunction, calculatePlaybackRate, closeGlobalAudioContext, configureGlobalContext, createToneAdapter, extractLoopAndEnvelope, getGeneratorValue, getGlobalAudioContext, getGlobalAudioContextState, getGlobalContext, getGlobalToneContext, getMediaStreamSource, getUnderlyingAudioParam, hasMediaStreamSource, int16ToFloat32, isNativeGlobalContext, isToneAdapter, releaseMediaStreamSource, resumeGlobalAudioContext, supportsNativeContextMode, timecentsToSeconds };
package/dist/index.js CHANGED
@@ -39,9 +39,11 @@ __export(index_exports, {
39
39
  getUnderlyingAudioParam: () => getUnderlyingAudioParam,
40
40
  hasMediaStreamSource: () => hasMediaStreamSource,
41
41
  int16ToFloat32: () => int16ToFloat32,
42
+ isNativeGlobalContext: () => isNativeGlobalContext,
42
43
  isToneAdapter: () => isToneAdapter,
43
44
  releaseMediaStreamSource: () => releaseMediaStreamSource,
44
45
  resumeGlobalAudioContext: () => resumeGlobalAudioContext,
46
+ supportsNativeContextMode: () => supportsNativeContextMode,
45
47
  timecentsToSeconds: () => timecentsToSeconds
46
48
  });
47
49
  module.exports = __toCommonJS(index_exports);
@@ -72,6 +74,7 @@ function getUnderlyingAudioParam(signal) {
72
74
  var ToneTrack = class {
73
75
  constructor(options) {
74
76
  this.activeSources = /* @__PURE__ */ new Set();
77
+ this._effectsChainNode = null;
75
78
  // Guard against ghost tick schedule callbacks. After stop/start cycles with
76
79
  // loops, stale Clock._lastUpdate causes ticks from the previous cycle to fire
77
80
  // Transport.schedule() callbacks at past positions (e.g., time 0 clips fire
@@ -87,14 +90,15 @@ var ToneTrack = class {
87
90
  });
88
91
  this.muteGain = new import_tone.Gain(options.track.muted ? 0 : 1);
89
92
  this.volumeNode.chain(this.panNode, this.muteGain);
90
- const destination = options.destination || (0, import_tone.getDestination)();
93
+ this._destination = options.destination || (0, import_tone.getDestination)();
94
+ this._hasClosureEffects = Boolean(options.effects);
91
95
  if (options.effects) {
92
- const cleanup = options.effects(this.muteGain, destination, false);
96
+ const cleanup = options.effects(this.muteGain, this._destination, false);
93
97
  if (cleanup) {
94
98
  this.effectsCleanup = cleanup;
95
99
  }
96
100
  } else {
97
- this.muteGain.connect(destination);
101
+ this.muteGain.connect(this._destination);
98
102
  }
99
103
  const clipInfos = options.clips || (options.buffer ? [
100
104
  {
@@ -434,6 +438,39 @@ var ToneTrack = class {
434
438
  setSolo(soloed) {
435
439
  this.track.soloed = soloed;
436
440
  }
441
+ /**
442
+ * Insert an external effects chain: reroute muteGain → node instead of
443
+ * muteGain → destination. The caller wires the chain's output onward
444
+ * (dawcore's EffectsManager connects it to the master bus input).
445
+ * Mutually exclusive with the TrackEffectsFunction closure model.
446
+ */
447
+ connectEffects(node) {
448
+ if (this._hasClosureEffects) {
449
+ throw new Error(
450
+ '[waveform-playlist] Track "' + this.track.id + '" was built with a TrackEffectsFunction closure \u2014 transport effects hooks and closure effects are mutually exclusive.'
451
+ );
452
+ }
453
+ if (this._effectsChainNode) {
454
+ this.muteGain.disconnect(this._effectsChainNode);
455
+ } else {
456
+ this.muteGain.disconnect(this._destination);
457
+ }
458
+ this.muteGain.connect(node);
459
+ this._effectsChainNode = node;
460
+ }
461
+ /** Restore the direct muteGain → destination connection. Safe when nothing is connected. */
462
+ disconnectEffects() {
463
+ if (!this._effectsChainNode) return;
464
+ try {
465
+ this.muteGain.disconnect(this._effectsChainNode);
466
+ } catch (err) {
467
+ console.warn(
468
+ "[waveform-playlist] disconnectEffects: " + (err instanceof Error ? err.message : String(err))
469
+ );
470
+ }
471
+ this.muteGain.connect(this._destination);
472
+ this._effectsChainNode = null;
473
+ }
437
474
  dispose() {
438
475
  const transport = (0, import_tone.getTransport)();
439
476
  if (this.effectsCleanup) {
@@ -1060,6 +1097,7 @@ var TonePlayout = class {
1060
1097
  this._loopEnabled = false;
1061
1098
  this._loopStart = 0;
1062
1099
  this._loopEnd = 0;
1100
+ this._masterChainNode = null;
1063
1101
  this.masterVolume = new import_tone4.Volume((0, import_core5.gainToDb)(options.masterGain ?? 1));
1064
1102
  this._masterTap = new import_tone4.Gain(1);
1065
1103
  if (options.effects) {
@@ -1280,6 +1318,60 @@ var TonePlayout = class {
1280
1318
  get masterOutputNode() {
1281
1319
  return this._masterTap.input;
1282
1320
  }
1321
+ /**
1322
+ * Native GainNode behind masterVolume.input — the master-bus junction that
1323
+ * per-track effect chains reconnect into (pre master volume). Distinct from
1324
+ * masterOutputNode (the post-volume tap for analyzers).
1325
+ */
1326
+ get masterBusInputNode() {
1327
+ return this.masterVolume.input.input;
1328
+ }
1329
+ /** Insert an external chain on a track: muteGain → node (caller wires node onward). */
1330
+ connectTrackOutput(trackId, node) {
1331
+ const track = this.tracks.get(trackId);
1332
+ if (!track) {
1333
+ throw new Error('[waveform-playlist] connectTrackOutput: unknown track "' + trackId + '"');
1334
+ }
1335
+ if (!(track instanceof ToneTrack)) {
1336
+ throw new Error(
1337
+ '[waveform-playlist] connectTrackOutput: per-track effects chains are not supported for MIDI tracks on the Tone adapter (track "' + trackId + '")'
1338
+ );
1339
+ }
1340
+ track.connectEffects(node);
1341
+ }
1342
+ /** Restore a track's direct connection. No-op for unknown or MIDI tracks. */
1343
+ disconnectTrackOutput(trackId) {
1344
+ const track = this.tracks.get(trackId);
1345
+ if (track instanceof ToneTrack) {
1346
+ track.disconnectEffects();
1347
+ }
1348
+ }
1349
+ /**
1350
+ * Insert an external master chain after the tap: masterVolume → [closure
1351
+ * effects] → tap → node (caller wires node.output → ctx.destination).
1352
+ */
1353
+ connectMasterOutput(node) {
1354
+ if (this._masterChainNode) {
1355
+ this._masterTap.disconnect(this._masterChainNode);
1356
+ } else {
1357
+ this._masterTap.disconnect((0, import_tone4.getDestination)());
1358
+ }
1359
+ this._masterTap.connect(node);
1360
+ this._masterChainNode = node;
1361
+ }
1362
+ /** Restore tap → destination. Safe when no master chain is connected. */
1363
+ disconnectMasterOutput() {
1364
+ if (!this._masterChainNode) return;
1365
+ try {
1366
+ this._masterTap.disconnect(this._masterChainNode);
1367
+ } catch (err) {
1368
+ console.warn(
1369
+ "[waveform-playlist] disconnectMasterOutput: " + (err instanceof Error ? err.message : String(err))
1370
+ );
1371
+ }
1372
+ this._masterTap.connect((0, import_tone4.getDestination)());
1373
+ this._masterChainNode = null;
1374
+ }
1283
1375
  setSolo(trackId, soloed) {
1284
1376
  const track = this.tracks.get(trackId);
1285
1377
  if (track) {
@@ -1565,9 +1657,21 @@ var SoundFontCache = class _SoundFontCache {
1565
1657
  // src/audioContext.ts
1566
1658
  var import_tone5 = require("tone");
1567
1659
  var globalToneContext = null;
1660
+ var _nativeMode = false;
1661
+ function supportsNativeContextMode() {
1662
+ return typeof AudioContext !== "undefined" && typeof AudioListener !== "undefined" && "positionX" in AudioListener.prototype;
1663
+ }
1664
+ function isNativeGlobalContext() {
1665
+ return _nativeMode && globalToneContext !== null;
1666
+ }
1568
1667
  function configureGlobalContext(options) {
1569
1668
  if (globalToneContext) {
1570
1669
  const existingRate = globalToneContext.rawContext.sampleRate;
1670
+ if (options.nativeAudioContext && !_nativeMode) {
1671
+ console.warn(
1672
+ "[playout] configureGlobalContext: context already created \u2014 nativeAudioContext ignored. Call configureGlobalContext before any audio operations."
1673
+ );
1674
+ }
1571
1675
  if (options.sampleRate !== void 0 && options.sampleRate !== existingRate) {
1572
1676
  console.warn(
1573
1677
  "[playout] configureGlobalContext: context already created at " + existingRate + " Hz (requested " + options.sampleRate + " Hz). Call configureGlobalContext before any audio operations for sample rate control."
@@ -1575,6 +1679,23 @@ function configureGlobalContext(options) {
1575
1679
  }
1576
1680
  return existingRate;
1577
1681
  }
1682
+ if (options.nativeAudioContext) {
1683
+ if (supportsNativeContextMode()) {
1684
+ const nativeCtx = new AudioContext({
1685
+ ...options.sampleRate !== void 0 ? { sampleRate: options.sampleRate } : {},
1686
+ ...options.latencyHint !== void 0 ? { latencyHint: options.latencyHint } : {}
1687
+ });
1688
+ globalToneContext = new import_tone5.Context(
1689
+ nativeCtx
1690
+ );
1691
+ (0, import_tone5.setContext)(globalToneContext);
1692
+ _nativeMode = true;
1693
+ return nativeCtx.sampleRate;
1694
+ }
1695
+ console.warn(
1696
+ "[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
+ );
1698
+ }
1578
1699
  globalToneContext = new import_tone5.Context();
1579
1700
  (0, import_tone5.setContext)(globalToneContext);
1580
1701
  const actualRate = globalToneContext.rawContext.sampleRate;
@@ -1611,6 +1732,7 @@ async function closeGlobalAudioContext() {
1611
1732
  if (globalToneContext && globalToneContext.rawContext.state !== "closed") {
1612
1733
  await globalToneContext.close();
1613
1734
  globalToneContext = null;
1735
+ _nativeMode = false;
1614
1736
  }
1615
1737
  }
1616
1738
 
@@ -1796,6 +1918,26 @@ function createToneAdapter(options) {
1796
1918
  }
1797
1919
  });
1798
1920
  }
1921
+ function requireEffectsPlayout() {
1922
+ if (!isNativeGlobalContext()) {
1923
+ throw new Error(
1924
+ "[waveform-playlist] Effects chains on the Tone adapter require a native AudioContext. Call configureGlobalContext({ nativeAudioContext: true }) from @waveform-playlist/playout before any audio initialization."
1925
+ );
1926
+ }
1927
+ if (!playout) {
1928
+ throw new Error("[waveform-playlist] adapter.transport accessed after dispose.");
1929
+ }
1930
+ return playout;
1931
+ }
1932
+ const effectsTransport = {
1933
+ connectTrackOutput: (trackId, node) => requireEffectsPlayout().connectTrackOutput(trackId, node),
1934
+ disconnectTrackOutput: (trackId) => requireEffectsPlayout().disconnectTrackOutput(trackId),
1935
+ connectMasterOutput: (node) => requireEffectsPlayout().connectMasterOutput(node),
1936
+ disconnectMasterOutput: () => requireEffectsPlayout().disconnectMasterOutput(),
1937
+ get masterOutputNode() {
1938
+ return requireEffectsPlayout().masterBusInputNode;
1939
+ }
1940
+ };
1799
1941
  return {
1800
1942
  async init() {
1801
1943
  if (_pendingInit) {
@@ -2008,6 +2150,9 @@ function createToneAdapter(options) {
2008
2150
  }
2009
2151
  return playout.masterOutputNode;
2010
2152
  },
2153
+ get transport() {
2154
+ return effectsTransport;
2155
+ },
2011
2156
  setSoundFontCache(cache) {
2012
2157
  _soundFontCache = cache;
2013
2158
  if (cache && !cache.isLoaded) {
@@ -2074,9 +2219,11 @@ function createToneAdapter(options) {
2074
2219
  getUnderlyingAudioParam,
2075
2220
  hasMediaStreamSource,
2076
2221
  int16ToFloat32,
2222
+ isNativeGlobalContext,
2077
2223
  isToneAdapter,
2078
2224
  releaseMediaStreamSource,
2079
2225
  resumeGlobalAudioContext,
2226
+ supportsNativeContextMode,
2080
2227
  timecentsToSeconds
2081
2228
  });
2082
2229
  //# sourceMappingURL=index.js.map