Skip to content

Open source CapCut alternative.

TypeScript

feat(site): both mixes carry the audio effect elements

David Le8c4fd9237804
diff --git a/site/src/cut/hooks/usePlayback.ts b/site/src/cut/hooks/usePlayback.ts
index 0434f4d8..77c45cb4 100644
--- a/site/src/cut/hooks/usePlayback.ts
+++ b/site/src/cut/hooks/usePlayback.ts
@@ -15,6 +15,7 @@ import { SubjectMaskCompositor } from "@/cut/lib/behindPass";
import { FrameCompositor, MISSING_FRAME, PENDING_FRAME, type Frame } from "@/cut/lib/composite";
import { duckGainAt, overlayPlan, trackZeroPlan } from "@/cut/lib/framePlan";
import { type ClipFrameSource, FrameSourcePool, mappingKey, walkCostMs } from "@/cut/lib/frameSource";
+import { liveAudioFxAt } from "@/cut/lib/audioEffects";
import { PreviewMixer, type Voice } from "@/cut/lib/previewMixer";
import {
flushMeter,
@@ -546,6 +547,16 @@ class Engine {
if (playing) {
this.mixer.setMasterGain(fadeGain);
+ // The audio effect elements over this moment, each at the level its
+ // window puts it at; the mixer carries them over the whole mix.
+ this.mixer.setEffects(
+ liveAudioFxAt(s.overlays, t).map(({ span, wet }) => ({
+ id: span.id,
+ effect: span.effect,
+ amount: span.amount,
+ wet,
+ }))
+ );
this.mixer.update(t, this.voicesAt(t, spans, master));
// A scoped effect preview auto-pauses at its stop mark; the end of the
// cut stops playback outright.
diff --git a/site/src/cut/lib/audioEffects.test.ts b/site/src/cut/lib/audioEffects.test.ts
new file mode 100644
index 00000000..a31e66fa
--- /dev/null
+++ b/site/src/cut/lib/audioEffects.test.ts
@@ -0,0 +1,89 @@
+import { describe, expect, test } from "bun:test";
+import { audioFxSpans, audioFxWetAt, hasAudioEffects, liveAudioFxAt } from "./audioEffects";
+import type { Overlay } from "./types";
+
+const fx = (over: Partial<Overlay> & { effect: string }): Overlay =>
+ ({
+ id: over.effect,
+ kind: "effect",
+ x: 0.5,
+ y: 0.5,
+ start: 0,
+ end: 2,
+ ...over,
+ }) as Overlay;
+
+describe("audioFxSpans", () => {
+ test("picks the audio effects out of the element list, in timeline order", () => {
+ const spans = audioFxSpans([
+ fx({ effect: "echo", start: 4, end: 6 }),
+ fx({ effect: "vignette", start: 0, end: 2 }),
+ fx({ effect: "muffle", start: 1, end: 3 }),
+ { id: "t", kind: "text", text: "hi", start: 0, end: 2, x: 0.5, y: 0.5 } as Overlay,
+ ]);
+ expect(spans.map((s) => s.effect)).toEqual(["muffle", "echo"]);
+ });
+
+ test("a hidden effect treats nothing", () => {
+ expect(audioFxSpans([fx({ effect: "echo", hidden: true })])).toEqual([]);
+ });
+
+ test("clips to the cut's length and drops what falls outside it", () => {
+ const spans = audioFxSpans(
+ [fx({ effect: "echo", start: 1, end: 9 }), fx({ effect: "crush", start: 6, end: 8 })],
+ 5
+ );
+ expect(spans).toHaveLength(1);
+ expect(spans[0].end).toBe(5);
+ });
+
+ test("hasAudioEffects answers for the doc, picture effects aside", () => {
+ expect(hasAudioEffects([fx({ effect: "grain" })])).toBe(false);
+ expect(hasAudioEffects([fx({ effect: "grain" }), fx({ effect: "reverb" })])).toBe(true);
+ });
+});
+
+describe("audioFxWetAt", () => {
+ const span = { id: "a", effect: "echo" as const, start: 2, end: 6 };
+
+ test("full through the window, silent outside it", () => {
+ expect(audioFxWetAt(span, 4)).toBe(1);
+ expect(audioFxWetAt(span, 0)).toBe(0);
+ expect(audioFxWetAt(span, 9)).toBe(0);
+ });
+
+ test("crosses over at the edges rather than switching", () => {
+ expect(audioFxWetAt(span, 2)).toBeCloseTo(0.5, 5);
+ expect(audioFxWetAt(span, 6)).toBeCloseTo(0.5, 5);
+ expect(audioFxWetAt(span, 1.99)).toBeGreaterThan(0);
+ expect(audioFxWetAt(span, 1.99)).toBeLessThan(0.5);
+ });
+
+ test("a window shorter than the ramp still reaches full and comes back", () => {
+ const brief = { id: "b", effect: "echo" as const, start: 1, end: 1.02 };
+ expect(audioFxWetAt(brief, 1.01)).toBeGreaterThan(0.5);
+ expect(audioFxWetAt(brief, 0.9)).toBe(0);
+ expect(audioFxWetAt(brief, 1.2)).toBe(0);
+ });
+});
+
+describe("liveAudioFxAt", () => {
+ const doc = [
+ fx({ effect: "echo", start: 0, end: 2 }),
+ fx({ effect: "muffle", start: 10, end: 12 }),
+ ];
+
+ test("carries the effects near the playhead, so a chain joins while silent", () => {
+ const live = liveAudioFxAt(doc, 2.3);
+ expect(live.map((l) => l.span.effect)).toEqual(["echo"]);
+ expect(live[0].wet).toBe(0);
+ });
+
+ test("leaves the far ones out entirely", () => {
+ expect(liveAudioFxAt(doc, 5)).toEqual([]);
+ });
+
+ test("reports the level the window puts each one at", () => {
+ expect(liveAudioFxAt(doc, 1)[0].wet).toBe(1);
+ });
+});
diff --git a/site/src/cut/lib/audioEffects.ts b/site/src/cut/lib/audioEffects.ts
new file mode 100644
index 00000000..53419258
--- /dev/null
+++ b/site/src/cut/lib/audioEffects.ts
@@ -0,0 +1,69 @@
+/**
+ * The audio effects on a timeline, and how loud each one runs at a moment.
+ *
+ * An audio effect element treats every sound under its stretch of timeline.
+ * Inside the window the treated signal is what is heard, outside it the mix is
+ * untouched, and the edges cross over in a few milliseconds so switching the
+ * effect on cannot click. Both mixes read this: the live preview asks per
+ * frame, the offline fold asks for the whole list and schedules the same
+ * ramps.
+ */
+
+import { AUDIO_FX_RAMP, isAudioEffect, type AudioEffectId } from "@donkeycut/effects-kit";
+import { isEffectOverlay, type Overlay } from "./types";
+
+/** One effect element's window, with the knobs its recipe reads. */
+export interface AudioFxSpan {
+ id: string;
+ effect: AudioEffectId;
+ amount?: number;
+ start: number;
+ end: number;
+}
+
+/** The audio effect elements of a doc, in timeline order, clipped to
+ * `duration` when one is given. Hidden elements are left out, the same as
+ * everywhere else. */
+export function audioFxSpans(overlays: Overlay[], duration?: number): AudioFxSpan[] {
+ const spans: AudioFxSpan[] = [];
+ for (const o of overlays) {
+ if (!isEffectOverlay(o) || o.hidden || !isAudioEffect(o.effect)) continue;
+ const end = duration === undefined ? o.end : Math.min(o.end, duration);
+ if (!(end > o.start)) continue;
+ spans.push({ id: o.id, effect: o.effect, amount: o.amount, start: Math.max(0, o.start), end });
+ }
+ return spans.sort((a, b) => a.start - b.start);
+}
+
+/** Whether a doc carries any audio effect at all — asked before the per-frame
+ * work of finding out which ones are live. */
+export const hasAudioEffects = (overlays: Overlay[]) =>
+ overlays.some((o) => isEffectOverlay(o) && isAudioEffect(o.effect));
+
+/** How much of the treated signal is heard at `t`: 1 through the window, 0
+ * outside it, ramping across the edges. */
+export function audioFxWetAt(span: AudioFxSpan, t: number): number {
+ const ramp = Math.min(AUDIO_FX_RAMP, Math.max(0.001, (span.end - span.start) / 2));
+ if (t <= span.start - ramp || t >= span.end + ramp) return 0;
+ const inP = (t - (span.start - ramp)) / (2 * ramp);
+ const outP = (span.end + ramp - t) / (2 * ramp);
+ return Math.max(0, Math.min(1, Math.min(inP, outP)));
+}
+
+/**
+ * The effects the preview keeps built at `t`, each with the wet level it runs
+ * at right now.
+ *
+ * The list holds every effect whose window is near the playhead, rather than
+ * only the ones sounding: a chain joins and leaves the graph while its wet
+ * level is zero, so the rebuild it costs is silent.
+ */
+export function liveAudioFxAt(
+ overlays: Overlay[],
+ t: number
+): { span: AudioFxSpan; wet: number }[] {
+ const NEAR = 0.5;
+ return audioFxSpans(overlays)
+ .filter((s) => t > s.start - NEAR && t < s.end + NEAR)
+ .map((span) => ({ span, wet: audioFxWetAt(span, t) }));
+}
diff --git a/site/src/cut/lib/audioMix.ts b/site/src/cut/lib/audioMix.ts
index f5738dde..a9a04832 100644
--- a/site/src/cut/lib/audioMix.ts
+++ b/site/src/cut/lib/audioMix.ts
@@ -17,6 +17,7 @@
* words that the exported audio says at another time.
*/
+import { AUDIO_FX_RAMP, audioFxRecipe, buildAudioFx } from "@donkeycut/effects-kit";
import { decodeAudioSpan } from "./mediaRead";
import { timeStretch } from "./timeStretch";
@@ -50,11 +51,22 @@ export interface MixItem {
duck?: number;
}
+/** One audio effect element: the treatment it names, and the stretch of
+ * timeline it treats. */
+export interface MixEffect {
+ effect: string;
+ amount?: number;
+ start: number;
+ end: number;
+}
+
export interface MixSpec {
duration: number;
clips: MixClip[];
/** Soundtrack, voiceovers, and upper-track clip audio. */
items: MixItem[];
+ /** Audio effect elements over the finished mix, in timeline order. */
+ effects?: MixEffect[];
/** Whole-project audio fades, seconds. */
fadeIn?: number;
fadeOut?: number;
@@ -187,6 +199,11 @@ export async function renderMix(spec: MixSpec, opts: MixOptions): Promise<AudioB
const ducked = ctx.createGain();
scheduleDuck(ducked, duckWindows(spec.items));
+ // Every sound meets on the bus, which is what the effect elements treat —
+ // the ffmpeg graph applies them to the mixed stream in the same place.
+ const bus = ctx.createGain();
+ ducked.connect(bus);
+
// The project fade is the last thing on the way out, matching the export's
// fade on the final mix.
const master = ctx.createGain();
@@ -201,7 +218,38 @@ export async function renderMix(spec: MixSpec, opts: MixOptions): Promise<AudioB
master.gain.setValueAtTime(1, spec.duration - fadeOut);
master.gain.linearRampToValueAtTime(0, spec.duration);
}
- ducked.connect(master);
+ // The effects run in series off the bus, each crossed in over its own
+ // window: inside it the treated signal is heard, outside it the untouched
+ // one, with the edges ramping so the switch cannot click.
+ let treated: AudioNode = bus;
+ for (const fx of spec.effects ?? []) {
+ const recipe = audioFxRecipe(fx.effect, fx.amount);
+ const from = Math.max(0, fx.start);
+ const to = Math.min(fx.end, spec.duration);
+ if (!recipe || !(to > from)) continue;
+ const nodes = buildAudioFx(ctx, recipe, from);
+ const wet = ctx.createGain();
+ const dry = ctx.createGain();
+ const out = ctx.createGain();
+ const ramp = Math.min(AUDIO_FX_RAMP, Math.max(0.001, (to - from) / 2));
+ const cross = (gain: GainNode, inside: number) => {
+ const outside = 1 - inside;
+ gain.gain.setValueAtTime(outside, 0);
+ gain.gain.setValueAtTime(outside, Math.max(0, from - ramp));
+ gain.gain.linearRampToValueAtTime(inside, from + ramp);
+ gain.gain.setValueAtTime(inside, Math.max(from + ramp, to - ramp));
+ gain.gain.linearRampToValueAtTime(outside, to + ramp);
+ };
+ cross(wet, 1);
+ cross(dry, 0);
+ treated.connect(nodes.input);
+ nodes.output.connect(wet);
+ wet.connect(out);
+ treated.connect(dry);
+ dry.connect(out);
+ treated = out;
+ }
+ treated.connect(master);
master.connect(ctx.destination);
/**
@@ -273,8 +321,9 @@ export async function renderMix(spec: MixSpec, opts: MixOptions): Promise<AudioB
const buf = a.muted ? undefined : buffers.get(spanKey(a));
if (!buf) continue;
const dur = itemDur(a);
- // A voiceover that ducks everything else is not itself ducked.
- const into = a.duck !== undefined && a.duck < 1 ? master : ducked;
+ // A voiceover that ducks everything else is not itself ducked; it still
+ // lands on the bus, so the effect elements treat it with the rest.
+ const into = a.duck !== undefined && a.duck < 1 ? bus : ducked;
play(fitted(spanKey(a), buf, speedOf(a.speed)), a.start, dur, into, (gain) => {
const level = Math.max(0, a.volume);
gain.gain.value = level;
diff --git a/site/src/cut/lib/audioMixFx.test.ts b/site/src/cut/lib/audioMixFx.test.ts
new file mode 100644
index 00000000..9651a7be
--- /dev/null
+++ b/site/src/cut/lib/audioMixFx.test.ts
@@ -0,0 +1,93 @@
+import { describe, expect, mock, test } from "bun:test";
+import { AudioBuffer, OfflineAudioContext } from "node-web-audio-api";
+
+// The fold runs on Web Audio, which Node has through the same package the
+// headless runtime installs, and it reads its sources through `mediaRead` —
+// stubbed here with a synthetic tone so the test is about the mix alone.
+const RATE = 44100;
+const SECONDS = 6;
+
+const globals = globalThis as Record<string, unknown>;
+globals.AudioBuffer ??= AudioBuffer;
+globals.OfflineAudioContext ??= OfflineAudioContext;
+
+const tone = () => {
+ const buf = new AudioBuffer({
+ length: SECONDS * RATE,
+ numberOfChannels: 1,
+ sampleRate: RATE,
+ });
+ const ch = buf.getChannelData(0);
+ // A click every quarter second: a delay tap and a filter both show on it.
+ for (let i = 0; i < ch.length; i++) {
+ const phase = (i % (RATE / 4)) / (RATE / 4);
+ ch[i] = phase < 0.02 ? Math.sin(2 * Math.PI * 900 * (i / RATE)) * (1 - phase / 0.02) : 0;
+ }
+ return buf;
+};
+
+mock.module("./mediaRead", () => ({ decodeAudioSpan: async () => tone() }));
+
+const { renderMix } = await import("./audioMix");
+
+const rms = (data: Float32Array, from: number, to: number) => {
+ let sum = 0;
+ const a = Math.floor(from * RATE);
+ const b = Math.floor(to * RATE);
+ for (let i = a; i < b; i++) sum += data[i] * data[i];
+ return Math.sqrt(sum / Math.max(1, b - a));
+};
+
+const fold = async (effects: { effect: string; amount?: number; start: number; end: number }[]) => {
+ const buffer = await renderMix(
+ {
+ duration: SECONDS,
+ clips: [{ file: "a.mp4", in: 0, out: SECONDS, muted: false }],
+ items: [],
+ effects,
+ },
+ { sampleRate: RATE, channels: 1, resolve: (f) => f }
+ );
+ expect(buffer).not.toBeNull();
+ return buffer!.getChannelData(0);
+};
+
+describe("audio effects in the offline fold", () => {
+ test("treats its own window and leaves the rest of the mix alone", async () => {
+ const plain = await fold([]);
+ const treated = await fold([{ effect: "echo", amount: 0.9, start: 2, end: 4 }]);
+ // Outside the window the samples are the untreated mix, to the sample.
+ expect(rms(treated, 0, 1.9)).toBeCloseTo(rms(plain, 0, 1.9), 5);
+ expect(rms(treated, 4.2, 6)).toBeCloseTo(rms(plain, 4.2, 6), 5);
+ // Inside it the echoes are audible.
+ expect(rms(treated, 2.2, 3.8)).toBeGreaterThan(rms(plain, 2.2, 3.8) * 1.2);
+ });
+
+ test("a muffle takes the top off inside its window only", async () => {
+ const plain = await fold([]);
+ const treated = await fold([{ effect: "muffle", amount: 1, start: 1, end: 3 }]);
+ expect(rms(treated, 4, 6)).toBeCloseTo(rms(plain, 4, 6), 5);
+ // The clicks are 900 Hz transients and the muffle closes to 800 Hz, so
+ // what is left inside the window is a different signal.
+ const inside = rms(treated, 1.2, 2.8);
+ expect(inside).toBeGreaterThan(0);
+ expect(Math.abs(inside - rms(plain, 1.2, 2.8))).toBeGreaterThan(0.001);
+ });
+
+ test("two effects stack in series over their own stretches", async () => {
+ const plain = await fold([]);
+ const treated = await fold([
+ { effect: "echo", amount: 0.9, start: 1, end: 2.5 },
+ { effect: "crush", amount: 1, start: 3.5, end: 5 },
+ ]);
+ expect(rms(treated, 2.8, 3.4)).toBeCloseTo(rms(plain, 2.8, 3.4), 5);
+ expect(rms(treated, 1.2, 2.3)).toBeGreaterThan(rms(plain, 1.2, 2.3) * 1.2);
+ expect(Math.abs(rms(treated, 3.7, 4.8) - rms(plain, 3.7, 4.8))).toBeGreaterThan(0.001);
+ });
+
+ test("an effect the fold does not know leaves the mix as it was", async () => {
+ const plain = await fold([]);
+ const treated = await fold([{ effect: "grain", start: 1, end: 3 }]);
+ expect(rms(treated, 0, SECONDS)).toBeCloseTo(rms(plain, 0, SECONDS), 5);
+ });
+});
diff --git a/site/src/cut/lib/exportClient.ts b/site/src/cut/lib/exportClient.ts
index b5fbe16b..d343e61b 100644
--- a/site/src/cut/lib/exportClient.ts
+++ b/site/src/cut/lib/exportClient.ts
@@ -736,7 +736,8 @@ async function buildExportPayload(
lane?: number;
}[] = [];
// Effect elements never rasterize: they ship as time-gated filter recipes
- // the server builds into the graph (the ids ride, never filter text).
+ // the server builds into the graph (the ids ride, never filter text). The
+ // audio ones ride the same list and treat the mix rather than the picture.
const effects = doc.overlays
.filter((o) => o.kind === "effect" && !o.hidden && o.start < duration)
.map((o) => ({
diff --git a/site/src/cut/lib/exportRender.ts b/site/src/cut/lib/exportRender.ts
index 294a3949..1ae7380b 100644
--- a/site/src/cut/lib/exportRender.ts
+++ b/site/src/cut/lib/exportRender.ts
@@ -27,13 +27,14 @@ import {
StreamTarget,
type VideoCodec,
} from "mediabunny";
+import { audioFxSpans } from "./audioEffects";
import { renderMix, type MixClip, type MixItem, type MixSpec } from "./audioMix";
import { FrameCompositor, MISSING_FRAME, type Frame } from "./composite";
import { overlayPlan, trackZeroPlan } from "./framePlan";
import { frameSink, openMedia, videoTrackOf } from "./mediaRead";
import { getClipSpans, overlayLayers, projectDuration, spanSequence } from "./store";
import { captionStyle, cueOverlay, cueWordWindows, laneCues, laneHidden, subtitleLaneCount, trackPos } from "./subtitles";
-import { applyEffectToCanvas, evalOverlayFrame, grainTile, isMaskAnimated, isOverlayAnimated, maskFrameAt, planAnimatedLayers, type LottieHandle, type OverlayAnim, type PaintPhase } from "@donkeycut/effects-kit";
+import { applyEffectToCanvas, evalOverlayFrame, grainTile, isAudioEffect, isMaskAnimated, isOverlayAnimated, maskFrameAt, planAnimatedLayers, type LottieHandle, type OverlayAnim, type PaintPhase } from "@donkeycut/effects-kit";
import { hasSubjectOverlays, SubjectMaskCompositor } from "./behindPass";
import { createRasterCanvas, type RasterSurface } from "./raster";
import { renderElementPng } from "./textRender";
@@ -232,6 +233,7 @@ export class ClipReader {
function liveEffectsAt(overlays: Overlay[], t: number): EffectOverlay[] {
return overlays
.filter(isEffectOverlay)
+ .filter((o) => !isAudioEffect(o.effect))
.filter((o) => !o.hidden && t >= o.start && t < o.end)
.sort((a, b) => laneOf(b) - laneOf(a));
}
@@ -527,6 +529,8 @@ export function mixSpecFor(doc: ExportDoc, resolve: (asset: MediaAsset) => strin
duration,
clips,
items,
+ // Audio effect elements treat the finished mix over their own windows.
+ effects: audioFxSpans(doc.overlays, duration),
fadeIn: projectFadeSeconds(doc.fadeIn, duration),
fadeOut: projectFadeSeconds(doc.fadeOut, duration),
};
diff --git a/site/src/cut/lib/previewMixer.ts b/site/src/cut/lib/previewMixer.ts
index 1a679b08..8f3efaca 100644
--- a/site/src/cut/lib/previewMixer.ts
+++ b/site/src/cut/lib/previewMixer.ts
@@ -18,11 +18,16 @@
* which is what keeps its pitch and matches what the export writes.
*
* Gains are the only thing touched per frame, and only their value: the clip's
- * own volume, its fades, the duck under a live voiceover, and the whole-project
- * fade. The ramps themselves are the frame plan's, so the mix follows the same
- * description the picture does.
+ * own volume, its fades, the duck under a live voiceover, the whole-project
+ * fade, and how much of each audio effect is heard. The ramps themselves are
+ * the frame plan's, so the mix follows the same description the picture does.
+ *
+ * The audio effect elements sit between the voices and that final fade, each
+ * treating what the one before it handed on — the order the export's filter
+ * chain runs them in.
*/
+import { audioFxRecipe, buildAudioFx, type AudioFxNodes } from "@donkeycut/effects-kit";
import { decodeAudioSpan } from "./mediaRead";
import { timeStretch } from "./timeStretch";
@@ -77,6 +82,26 @@ interface LiveVoice {
dead: boolean;
}
+/** One audio effect element the graph is carrying: its recipe's chain, and
+ * the pair of gains that cross the mix between treated and untreated. */
+interface LiveFx {
+ nodes: AudioFxNodes;
+ wet: GainNode;
+ dry: GainNode;
+ /** Where the treated and untreated sides meet — the next stage's input. */
+ out: GainNode;
+}
+
+/** What the mixer is told about one effect element on the timeline: which
+ * effect at what strength, and how much of it is heard right now. */
+export interface FxVoice {
+ id: string;
+ effect: string;
+ amount?: number;
+ /** 0 = the untreated mix, 1 = the treated one. */
+ wet: number;
+}
+
/**
* The preview's clock and mixer.
*
@@ -87,6 +112,12 @@ interface LiveVoice {
export class PreviewMixer {
private ctx: AudioContext | null = null;
private master: GainNode | null = null;
+ /** Where every voice lands, and what the effect chains treat. */
+ private bus: GainNode | null = null;
+ private fx = new Map<string, LiveFx>();
+ /** What the built chains stand for, so a frame that changes nothing about
+ * them touches gains alone. */
+ private fxKey = "";
private voices = new Map<string, LiveVoice>();
/** Timeline time, context time and wall time of the same instant. Everything
* scheduled is placed against this. */
@@ -103,6 +134,10 @@ export class PreviewMixer {
this.ctx = new AudioContext();
this.master = this.ctx.createGain();
this.master.connect(this.ctx.destination);
+ // Voices meet on the bus, the effect chains treat what is on it, and the
+ // project fade rides the sum — the order the export's ffmpeg graph has.
+ this.bus = this.ctx.createGain();
+ this.bus.connect(this.master);
}
return this.ctx;
}
@@ -246,6 +281,78 @@ export class PreviewMixer {
if (this.master) this.master.gain.value = Math.max(0, Math.min(1, g));
}
+ /**
+ * Carry these audio effects over the bus, in the order given.
+ *
+ * The chains run in series — the second effect treats what the first one
+ * handed on, the way the export's filter chain does — and each one is
+ * crossed into the mix by its own `wet`. The graph is rebuilt only when the
+ * list itself changes; a caller that passes the effects near the playhead
+ * therefore rebuilds while the entering or leaving chain is silent, and
+ * every frame in between only moves gains.
+ */
+ setEffects(fx: FxVoice[]): void {
+ const ctx = this.ctx;
+ const bus = this.bus;
+ const master = this.master;
+ if (!ctx || !bus || !master) return;
+ const key = fx.map((f) => `${f.id}:${f.effect}:${f.amount ?? ""}`).join("|");
+ if (key !== this.fxKey) {
+ this.fxKey = key;
+ this.teardownFx();
+ // The bus's old outlet goes first, so the stages built below are the
+ // only path from it to the fade.
+ bus.disconnect();
+ let node: AudioNode = bus;
+ for (const f of fx) {
+ const recipe = audioFxRecipe(f.effect, f.amount);
+ if (!recipe) continue;
+ const nodes = buildAudioFx(ctx, recipe, ctx.currentTime);
+ // One stage: what came in, and the treated copy of it, meeting at a
+ // sum that the next stage reads.
+ const wet = ctx.createGain();
+ const dry = ctx.createGain();
+ const out = ctx.createGain();
+ wet.gain.value = 0;
+ dry.gain.value = 1;
+ node.connect(nodes.input);
+ nodes.output.connect(wet);
+ wet.connect(out);
+ node.connect(dry);
+ dry.connect(out);
+ node = out;
+ this.fx.set(f.id, { nodes, wet, dry, out });
+ }
+ node.connect(master);
+ }
+ for (const f of fx) {
+ const live = this.fx.get(f.id);
+ if (!live) continue;
+ const w = Math.max(0, Math.min(1, f.wet));
+ live.wet.gain.value = w;
+ live.dry.gain.value = 1 - w;
+ }
+ }
+
+ /** Drop every effect chain, stopping the modulators they started. */
+ private teardownFx(): void {
+ for (const live of this.fx.values()) {
+ for (const src of live.nodes.sources) {
+ try {
+ src.stop();
+ } catch {
+ // Already stopped; a spent node throws and means nothing.
+ }
+ }
+ live.nodes.input.disconnect();
+ live.nodes.output.disconnect();
+ live.wet.disconnect();
+ live.dry.disconnect();
+ live.out.disconnect();
+ }
+ this.fx.clear();
+ }
+
/**
* Bring the mix in line with what is audible at timeline time `t`.
*
@@ -299,18 +406,21 @@ export class PreviewMixer {
dispose(): void {
this.stop();
+ this.teardownFx();
+ this.fxKey = "";
this.decoded.clear();
void this.ctx?.close();
this.ctx = null;
this.master = null;
+ this.bus = null;
}
private open(v: Voice, t: number): LiveVoice | null {
const ctx = this.ctx;
- if (!ctx || !this.master) return null;
+ if (!ctx || !this.bus) return null;
const gain = ctx.createGain();
gain.gain.value = 0;
- gain.connect(this.master);
+ gain.connect(this.bus);
const live: LiveVoice = {
url: v.url,
speed: v.speed,
diff --git a/site/src/types/bun-test.d.ts b/site/src/types/bun-test.d.ts
index 0c43b4ae..73c63246 100644
--- a/site/src/types/bun-test.d.ts
+++ b/site/src/types/bun-test.d.ts
@@ -22,10 +22,15 @@ declare module "bun:test" {
toBeLessThanOrEqual(expected: number): void;
toContain(expected: string): void;
toBeUndefined(): void;
+ toBeNull(): void;
toHaveLength(expected: number): void;
toBeTruthy(): void;
toBeFalsy(): void;
not: Matchers;
}
export function expect(value: unknown): Matchers;
+ /** Module mocking, for the tests that stand a dependency in. */
+ export const mock: {
+ module(specifier: string, factory: () => unknown): void;
+ };
}