Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/audio-graph-publish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Publish the live audio graph (context + masterGain) on
// window.slopsmith.stems.audioGraph so the host shell can observe/re-route
// the stem mix — e.g. the desktop renderer-bus feeder taps masterNode when
// the output device is exclusive-style (feedBack Phase 2). Deliberately
// deployment-agnostic: this plugin only says "here is my master"; what the
// host does with it is the host's business.
import { S } from './state.js';

export function publishAudioGraph() {
if (!window.slopsmith || !window.slopsmith.stems
|| typeof window.slopsmith.stems !== 'object') return;
if (!S.audioCtx || !S.masterGain) return;
window.slopsmith.stems.audioGraph = { context: S.audioCtx, masterNode: S.masterGain };
}

// Retract only if the published graph is still ours: the AudioContext is
// reused across songs, so identity is the masterGain node, not the context.
// A newer graph published between our build and teardown must not be
// clobbered. Call BEFORE S.masterGain is nulled.
export function retractAudioGraph() {
const g = window.slopsmith?.stems?.audioGraph;
if (g && S.masterGain && g.masterNode === S.masterGain) {
delete window.slopsmith.stems.audioGraph;
}
}
3 changes: 3 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { computeMixGains } from './mix-gains.js';
import { clampVolume, coerceBool, hashString, isGuitarStemId } from './util.js';
import { ensureCtx, ensureWorklet, updateLatencyOffset } from './audio-ctx.js';
import { makeStemGainHandle } from './mix.js';
import { publishAudioGraph, retractAudioGraph } from './audio-graph-publish.js';
import {
setupStreaming, resetStreamState, streamingSupported, isWavResponse, configureStreaming,
} from './streaming.js';
Expand Down Expand Up @@ -259,6 +260,7 @@ import {
S.analyserNode = null;
}
if (S.masterGain) {
retractAudioGraph();
try { S.masterGain.disconnect(); } catch (_) {}
S.masterGain = null;
}
Expand Down Expand Up @@ -564,6 +566,7 @@ import {
S.analyserNode = S.audioCtx.createAnalyser();
S.analyserNode.fftSize = 256;
S.masterGain.connect(S.analyserNode);
publishAudioGraph();

// In worklet mode the single 'stem-mixer' node replaces every per-stem
// AudioBufferSourceNode + GainNode and feeds S.masterGain directly. It has
Expand Down
2 changes: 2 additions & 0 deletions src/streaming.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import { S, ST, transport } from './state.js';
import { ensureCtxAtRate, updateLatencyOffset } from './audio-ctx.js';
import { makeStemGainHandle } from './mix.js';
import { publishAudioGraph } from './audio-graph-publish.js';
import { computeMixGains } from './mix-gains.js';
import { clampVolume } from './util.js';
import { parseWavHeader, pcm16ToFloat32 } from './wav-pcm.js';
Expand Down Expand Up @@ -400,6 +401,7 @@ export async function setupStreaming(stems, probeResp, fullUrl, gen) {
S.analyserNode = S.audioCtx.createAnalyser();
S.analyserNode.fftSize = 256;
S.masterGain.connect(S.analyserNode);
publishAudioGraph();
S.workletPostReady = false;
try {
S.workletNode = new AudioWorkletNode(S.audioCtx, 'stem-mixer', {
Expand Down
Loading