From c1973c4342fccc87f1aa087f3cc0ecf934cd5427 Mon Sep 17 00:00:00 2001 From: OmikronApex Date: Thu, 9 Jul 2026 20:47:50 +0200 Subject: [PATCH] feat: publish live audio graph (context + masterGain) on window.slopsmith.stems MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Lets the host shell observe/re-route the stem mix — the desktop renderer-bus feeder taps masterGain when the output device is exclusive-style (feedBack Phase 2). Deployment-agnostic: the plugin only says "here is my master"; retracted in teardown, guarded by node identity since the AudioContext is reused across songs. Co-Authored-By: Claude Fable 5 --- src/audio-graph-publish.js | 25 +++++++++++++++++++++++++ src/main.js | 3 +++ src/streaming.js | 2 ++ 3 files changed, 30 insertions(+) create mode 100644 src/audio-graph-publish.js diff --git a/src/audio-graph-publish.js b/src/audio-graph-publish.js new file mode 100644 index 0000000..bbd11bb --- /dev/null +++ b/src/audio-graph-publish.js @@ -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; + } +} diff --git a/src/main.js b/src/main.js index 256c72b..d0b7faf 100644 --- a/src/main.js +++ b/src/main.js @@ -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'; @@ -259,6 +260,7 @@ import { S.analyserNode = null; } if (S.masterGain) { + retractAudioGraph(); try { S.masterGain.disconnect(); } catch (_) {} S.masterGain = null; } @@ -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 diff --git a/src/streaming.js b/src/streaming.js index 53ee21f..05af1ac 100644 --- a/src/streaming.js +++ b/src/streaming.js @@ -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'; @@ -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', {