Open source CapCut alternative.
TypeScript
feat(site): a sound effect tile plays what it does
David Lecca7ee71bee5
diff --git a/docs/guides/cut/README.md b/docs/guides/cut/README.mdindex 0f40fa70..fc771487 100644--- a/docs/guides/cut/README.md+++ b/docs/guides/cut/README.md@@ -36,7 +36,7 @@ The client probes the engine's health endpoint (dedicated port first, dev server**Motion is data.** Every animation in the editor — an entrance, an exit, a loop, and the moves an element makes while it holds — is one format: a pose keyframed over a normalized window with cubic-bezier easing, plus a range selector that says how the motion is handed from one character to the next. That is the After Effects text-animator model, the one Lottie serializes, and picking it means a preset and a hand-keyed track are the same object at two levels of convenience: the named entrances, loops, and moves are slots on the element, evaluated internally, and anything no name covers is keyed directly on the element's own pose track — the track stays whatever the user keys by hand. The library is a JSON asset in the effects kit rather than code, and the menus, tool enums, prompts, and the frame sampler's crop budget are all read from it, so adding an animation is adding an entry. One evaluator samples it, which is what keeps the preview, the export, and the headless worker showing the same motion. A slot may also carry a preset of its own rather than naming one from the library, in the same shape a library entry has — the evaluator cannot tell the two apart, so an animation composed inside a project plays wherever a shipped one does.-**An effect treats what runs under it.** An effect is a stretch of the timeline rather than a property of a clip, so it can cover part of a shot, a whole one, or a run of them, and it is trimmed and moved like any other element. Some of them treat the picture — a grade, a grain, a shake, a push in — and some treat the sound: everything audible under the window, clip audio, upper tracks and soundtrack, mixed and then filtered as one. The sound treatments are described as data — a dry level, delay taps, filter bands, a tremolo, a bit crush — and three renderers read that one description: the Web Audio graph the preview plays, the same graph folded offline for an in-tab export, and an ffmpeg chain for the engine and the cloud worker. That is what keeps the export sounding like the preview; the ffmpeg side cuts the window out of the mix, runs the chain over it, and pads the result back to the length it went in at, so a chain that rings past its window cannot walk the rest of the sound late. The window's edges cross over rather than switch, in both mixes, so an effect coming in cannot click.+**An effect treats what runs under it.** An effect is a stretch of the timeline rather than a property of a clip, so it can cover part of a shot, a whole one, or a run of them, and it is trimmed and moved like any other element. Some of them treat the picture — a grade, a grain, a shake, a push in — and some treat the sound: everything audible under the window, clip audio, upper tracks and soundtrack, mixed and then filtered as one. The sound treatments are described as data — a dry level, delay taps, filter bands, a tremolo, a bit crush — and three renderers read that one description: the Web Audio graph the preview plays, the same graph folded offline for an in-tab export, and an ffmpeg chain for the engine and the cloud worker. That is what keeps the export sounding like the preview; the ffmpeg side cuts the window out of the mix, runs the chain over it, and pads the result back to the length it went in at, so a chain that rings past its window cannot walk the rest of the sound late. The window's edges cross over rather than switch, in both mixes, so an effect coming in cannot click. A picture effect can be shown on its tile; a sound treatment has to be heard, so the tile plays — a line of recorded speech run through the same chain the mix would build. The line is one of the voice personas already shipped with the site, fetched once and kept decoded for the session, so the shelf demonstrates itself in a project with no footage in it yet.**A handover is on the picture or on the sound.** A transition is a blend at the cut rather than an overlap: the clips still abut, the layout never moves, and the incoming clip starts exactly at the cut. Most styles blend the picture across the outgoing clip's last seconds. One hands over on the sound instead — the picture cuts, the outgoing clip's sound fades out into the cut and the incoming clip's fades in out of it. The two windows sit on separate fields, so everything that draws the picture reads a plain cut and only the mix ramps: the preview, the in-tab export, and the ffmpeg graph the engine and the worker build all follow from that one split. A sound handover needs a second clip's sound to cross into, so a bar of it parked on a clip's open head or tail plays nothing. It is picked where the other work on the sound is picked — the Effects tab's Sound family — while the styles that blend the picture are picked from the Transitions tab.diff --git a/site/src/cut/components/EffectsPanel.tsx b/site/src/cut/components/EffectsPanel.tsxindex 678442a0..4d452a91 100644--- a/site/src/cut/components/EffectsPanel.tsx+++ b/site/src/cut/components/EffectsPanel.tsx@@ -3,6 +3,7 @@import { useEffect, useRef, useState } from "react";import {ALL_EFFECT_IDS,+ AUDIO_EFFECT_IDS,audioFxBars,EFFECT_LABELS,effectPreviewState,@@ -13,12 +14,15 @@ import {streakGradient,type EffectId,} from "@donkeycut/effects-kit";+import { Pause, Play } from "lucide-react";+import { PHASE_STEP } from "@/cut/components/AnimationTiles";import { PICKED_RING, pickGridNav, useAssetPick } from "@/cut/lib/assetPick";+import { useFxAudition } from "@/cut/lib/audioFxAudition";import { clearElementDrag, setElementDragData, setObjectDragImage } from "@/cut/lib/assetDrag";import { SubTabs } from "@/cut/components/SubTabs";import { usePlayheadFrame } from "@/cut/components/usePlayheadFrame";import { useEditor } from "@/cut/lib/store";-import { isEffectOverlay, type EffectOverlay } from "@/cut/lib/types";+import { isEffectOverlay, TRANSITION_STYLE_LABELS, type EffectOverlay } from "@/cut/lib/types";import { useLocalPref } from "@/cut/lib/uiState";import { cn } from "@/lib/utils";import { ScrollArea } from "@/components/ui/scroll-area";@@ -29,14 +33,18 @@ import "./grain.css";* toggle splits the grid into three families: Moving, the effects that play* over their stretch of timeline (zoom, grain, VHS, glitch, light leak, flash,* shake), Filters, the still treatments and graded looks (blur, vignette,- * vintage, noir, halation…), and Sound, the treatments over the audio under- * the window (echo, reverb, muffle, telephone…). Drag one onto the timeline+ * vintage, noir, halation…), and Sound, everything done to the audio — the+ * treatments over the window (echo, reverb, muffle, telephone…) and the+ * handover that dissolves the sound across a cut. Drag one onto the timeline* to place it; a click only picks the tile, the same as every other panel.** The picture effects treat the footage and everything laid over it; the audio* ones treat everything audible under them — clip sound, upper tracks and the- * soundtrack, mixed. A cut's own crossfade at a join stays where it belongs,- * on the transition bar.+ * soundtrack, mixed. The sound tiles are cards with a play control, because a+ * treatment on the sound is picked by ear: pressing one plays a spoken line+ * through it, and the dissolve plays two voices crossing at a cut. The sound+ * dissolve is a transition, so it lands on a cut rather than over a stretch,+ * and the styles that blend the picture stay in the Transitions tab.** Select an effect on the timeline and the tab follows it: its family opens,* its tile is the marked one, and a click on another tile changes that@@ -46,6 +54,10 @@ import "./grain.css";type EffectGroup = "moving" | "filters" | "audio";+/** The transition that hands over on the sound alone, picked from this shelf+ * rather than the Transitions tab: what it does, it does to the sound. */+const SOUND_DISSOLVE = "audiocross" as const;+const GROUPS = [{ id: "moving", label: "Moving" },{ id: "filters", label: "Filters" },@@ -74,6 +86,9 @@ export function EffectsPanel() {if (liveEffect) setGroup(groupOf(liveEffect));}, [liveEffect, setGroup]);const frame = usePlayheadFrame();+ // An audition belongs to the panel that started it; leaving the tab, or the+ // panel, silences it.+ useEffect(() => () => useFxAudition.getState().stop(), [group]);return (<>{/* PanelHead's height, so the side panel's floating close button lands@@ -87,9 +102,21 @@ export function EffectsPanel() {cut off there. */}<ScrollArea className="min-h-0 flex-1" contentClassName="px-3.5 pt-1 pb-4"><div className="grid grid-cols-2 gap-2" onKeyDown={pickGridNav}>- {ALL_EFFECT_IDS.filter((id) => groupOf(id) === group).map((id) => (- <EffectTile key={id} id={id} live={live} frame={frame} />- ))}+ {group === "audio" ? (+ <>+ {AUDIO_EFFECT_IDS.map((id, i) => (+ <SoundTile key={id} id={id} live={live} index={i} />+ ))}+ {/* The handover on the sound sits with the treatments on the+ sound. It is a transition — it drags to a cut, where the+ picture keeps cutting and the sound crosses over. */}+ <SoundDissolveTile index={AUDIO_EFFECT_IDS.length} />+ </>+ ) : (+ ALL_EFFECT_IDS.filter((id) => groupOf(id) === group).map((id) => (+ <EffectTile key={id} id={id} live={live} frame={frame} />+ ))+ )}</div></ScrollArea></>@@ -126,7 +153,7 @@ function EffectTile({// Selecting an effect far down the list brings its tile into view.if (isLive) ref.current?.scrollIntoView({ block: "nearest" });}, [isLive]);- return (+ const tile = (<buttonref={ref}type="button"@@ -151,23 +178,157 @@ function EffectTile({// ring is the focus indicator; the browser outline stays off.className="flex scroll-m-2 flex-col gap-1.5 text-[11px] font-medium text-muted-foreground outline-none transition-colors hover:text-foreground">- {isAudioEffect(id) ? (- <AudioSwatch- id={id}- t={t}- className={cn("w-full rounded-lg border border-border", marked && PICKED_RING)}- />- ) : (- <EffectSwatch- id={id}- t={t}- frame={frame}- className={cn("w-full rounded-lg border border-border", marked && PICKED_RING)}- />- )}+ <EffectSwatch+ id={id}+ t={t}+ frame={frame}+ className={cn("w-full rounded-lg border border-border", marked && PICKED_RING)}+ /><span className="leading-none">{EFFECT_LABELS[id]}</span></button>);+ return tile;+}++/**+ * One treatment on the sound, as the card the stock audio shelves use: the+ * figure across the middle, the play control on it, the name on its own strip+ * underneath. A sound tile is picked by ear, so it carries the same play+ * affordance a music sample does; the card itself picks and drags like every+ * other tile.+ */+function SoundTile({+ id,+ live,+ index,+}: {+ id: EffectId;+ live: EffectOverlay | null;+ index: number;+}) {+ const { picked, pick } = useAssetPick(`effect:${id}`);+ const t = useSoundClock(index);+ const ref = useRef<HTMLDivElement>(null);+ const isLive = live?.effect === id;+ const marked = live ? isLive : picked;+ useEffect(() => {+ if (isLive) ref.current?.scrollIntoView({ block: "nearest" });+ }, [isLive]);+ const choose = () => {+ if (!live) return pick();+ if (!isLive) useEditor.getState().updateOverlay(live.id, { effect: id });+ };+ return (+ <SoundCard+ cardRef={ref}+ pickId={`effect:${id}`}+ label={EFFECT_LABELS[id]}+ marked={marked}+ onChoose={choose}+ onDragStart={(e) => {+ setElementDragData(e, { kind: "effect", effect: id });+ setObjectDragImage(e);+ }}+ figure={<BarStrip bars={audioFxBars(id, t, STRIP_BARS, 0.9)} />}+ audition={id}+ />+ );+}++/**+ * The handover on the sound: the picture cuts and the sound crosses it.+ *+ * It is a transition rather than an effect element — it drags onto a cut, and+ * the timeline marks the joint it would take — but it belongs on this shelf,+ * because what it does is done to the sound.+ */+function SoundDissolveTile({ index }: { index: number }) {+ const { picked, pick } = useAssetPick(`transition:${SOUND_DISSOLVE}`);+ const t = useSoundClock(index);+ return (+ <SoundCard+ pickId={`transition:${SOUND_DISSOLVE}`}+ label={TRANSITION_STYLE_LABELS[SOUND_DISSOLVE]}+ marked={picked}+ onChoose={pick}+ onDragStart={(e) => {+ setElementDragData(e, { kind: "transition", style: SOUND_DISSOLVE });+ setObjectDragImage(e);+ }}+ figure={<CrossStrip t={t} />}+ audition={SOUND_DISSOLVE}+ />+ );+}++/** The card the sound shelf is built from: figure, play control, name. */+function SoundCard({+ cardRef,+ pickId,+ label,+ marked,+ onChoose,+ onDragStart,+ figure,+ audition,+}: {+ cardRef?: React.RefObject<HTMLDivElement | null>;+ pickId: string;+ label: string;+ marked: boolean;+ onChoose: () => void;+ onDragStart: (e: React.DragEvent) => void;+ figure: React.ReactNode;+ audition: string;+}) {+ const playing = useFxAudition((s) => s.effect === audition);+ return (+ // The play control sits over the card as a sibling rather than inside it:+ // the card is what a drag carries, and the ghost of it is the boxed+ // waveform with its name — pressing play is not part of what is dragged.+ <span className="relative block">+ <div+ ref={cardRef}+ data-pick-id={pickId}+ data-drag-object+ role="button"+ tabIndex={0}+ aria-pressed={marked}+ draggable+ onDragStart={onDragStart}+ onDragEnd={clearElementDrag}+ onClick={onChoose}+ onKeyDown={(e) => {+ if (e.key === "Enter" || e.key === " ") {+ e.preventDefault();+ onChoose();+ }+ }}+ className={cn(+ "group flex scroll-m-2 cursor-grab flex-col overflow-hidden rounded-xl border border-border bg-muted/40 outline-none",+ marked && PICKED_RING+ )}+ >+ <div className="relative h-14">+ <span className="absolute inset-x-2.5 top-1/2 block -translate-y-1/2">{figure}</span>+ </div>+ <div data-drag-omit className="border-t border-border px-2.5 py-1.5">+ <span className="block truncate text-[11.5px] font-medium" title={label}>+ {label}+ </span>+ </div>+ </div>+ <button+ type="button"+ title={playing ? "Stop" : "Play"}+ aria-label={`${playing ? "Stop" : "Play"} ${label}`}+ onClick={() => useFxAudition.getState().toggle(audition)}+ className="absolute top-1.5 left-1.5 grid size-6 place-items-center rounded-full bg-background text-foreground shadow-sm ring-1 ring-border transition-transform hover:scale-105"+ >+ {playing ? <Pause className="size-3" /> : <Play className="size-3 translate-x-px" />}+ </button>+ </span>+ );}/** Seconds into the effect the preview stands at: still at rest, running while@@ -282,43 +443,91 @@ export function SwatchScene({** The figure comes off the same recipe the mix is built from — a delay tap* shows as its repeat, a low pass rounds the bars off, a wobble swings them —- * so a tile can only show what the effect actually does.+ * so a tile can only show what the effect actually does. The geometry is the+ * waveform strip the audio shelves draw, so a treatment and a piece of music+ * read as the same kind of thing.*/-export function AudioSwatch({- id,- t = SWATCH_T,+const STRIP_BARS = 40;++function BarStrip({+ bars,+ alphaAt,className,}: {- id: EffectId;- t?: number;+ bars: number[];+ /** Per-bar opacity, for a figure that crosses rather than one that plays. */+ alphaAt?: (i: number, n: number) => number;className?: string;}) {- const bars = audioFxBars(id, t, undefined, 0.9);- const w = 100 / (bars.length * 1.7);return (- <span- data-drag-object- className={cn(- "relative block aspect-square overflow-hidden rounded-md bg-neutral-900",- className- )}+ <svg+ viewBox={`0 0 ${bars.length * 2} 16`}+ preserveAspectRatio="none"+ aria-hidden+ className={cn("block h-7 w-full text-muted-foreground/70", className)}>- <svg viewBox="0 0 100 100" aria-hidden className="absolute inset-0 size-full">- {bars.map((v, i) => {- const h = Math.max(w, v * 76);- return (- <rect- key={i}- x={((i + 0.5) / bars.length) * 100 - w / 2}- y={50 - h / 2}- width={w}- height={h}- rx={w / 2}- className="fill-white/85"- />- );- })}- </svg>+ {bars.map((v, i) => {+ const h = Math.max(1.5, Math.min(1, v) * 16);+ return (+ <rect+ key={i}+ x={i * 2}+ y={(16 - h) / 2}+ width={1.2}+ height={h}+ rx={0.6}+ fill="currentColor"+ fillOpacity={alphaAt ? alphaAt(i, bars.length) : undefined}+ />+ );+ })}+ </svg>+ );+}++/**+ * The clock one sound tile's figure runs on.+ *+ * The figures roll rather than loop, so nothing on the shelf has to snap back+ * to a start — and each tile is handed its own place in the roll and its own+ * slightly different rate, a golden-ratio step by its position in the grid, so+ * the shelf never moves in formation and never falls back into it.+ */+function useSoundClock(index: number): number {+ const now = useSwatchClock(true, Infinity);+ const phase = ((index * PHASE_STEP) % 1) * 4;+ const rate = 0.8 + ((index * PHASE_STEP * 3) % 1) * 0.45;+ return (now + phase) * rate;+}++/** The sound dissolve's figure: one sound going as the other arrives, with the+ * cut standing where the picture changes.+ *+ * Each side is scaled by its own ramp rather than only faded, because that is+ * what the effect does to it — the outgoing sound is smaller as it reaches the+ * cut, the incoming one grows out of it — and a level is what reads at tile+ * size. */+function CrossStrip({ t }: { t: number }) {+ // The two ramps meet at zero on the cut, which is where the sound really is+ // at the middle of a dissolve — so the figure pinches there.+ const ramp = (i: number, n: number) => Math.max(0, Math.min(1, i / (n - 1) - 0.5) * 2);+ const scaled = (bars: number[], at: (i: number, n: number) => number) =>+ bars.map((v, i) => v * at(i, bars.length));+ const outgoing = audioFxBars(null, t, STRIP_BARS);+ const incoming = audioFxBars(null, t + 1.7, STRIP_BARS);+ return (+ <span className="relative block h-7">+ <BarStrip+ bars={scaled(outgoing, (i, n) => 1 - Math.min(1, (i / (n - 1)) * 2))}+ alphaAt={(i, n) => 0.35 + 0.65 * (1 - Math.min(1, (i / (n - 1)) * 2))}+ className="absolute inset-0"+ />+ <BarStrip+ bars={scaled(incoming, ramp)}+ alphaAt={(i, n) => 0.35 + 0.65 * ramp(i, n)}+ className="absolute inset-0"+ />+ <span className="absolute inset-y-0 left-1/2 w-px -translate-x-1/2 bg-foreground/40" /></span>);}diff --git a/site/src/cut/lib/audioFxAudition.ts b/site/src/cut/lib/audioFxAudition.tsnew file mode 100644index 00000000..c4c14382--- /dev/null+++ b/site/src/cut/lib/audioFxAudition.ts@@ -0,0 +1,196 @@+"use client";++/**+ * Hearing an audio effect before placing it.+ *+ * A picker tile for a picture effect shows what it does; a tile for a sound+ * treatment has to play it. This is the one player that does that: a line of+ * recorded speech, run through the same chain the mix would build, out of a+ * context of its own. The line is one of the voice personas already shipped+ * with the site, so the tiles play the same way in a project with no footage+ * in it yet — a picker is used before there is anything to use it on.+ *+ * The sound dissolve auditions as what it is: a man's line handing over to a+ * woman's across a cut, with the ramps the render would apply.+ *+ * A clip is fetched once and kept decoded for the session, so the first press+ * loads it and every press after plays it straight away. One audition at a+ * time, and starting one silences the shared preview player the other panels+ * use.+ */++import { audioFxRecipe, buildAudioFx, type AudioFxNodes } from "@donkeycut/effects-kit";+import { create } from "zustand";+import { registerPreviewSilencer, usePreviewAudio } from "./previewAudio";+import { voiceSampleUrl } from "./voices";++/** The id of the sound handover, auditioned as the handover itself. */+export const SOUND_DISSOLVE_AUDITION = "audiocross";++/**+ * The two lines the shelf plays.+ *+ * Both personas speak the same two sentences — the voice picker's sample text+ * — so the halves are used apart: a treatment runs on the second sentence, one+ * unbroken phrase with the end of the clip left on it for a delay to ring into,+ * and the handover plays the man's first sentence into the woman's second one,+ * which is two different lines meeting at a cut.+ */+const LINES = {+ /** Charlie, informative — an even read that filters and delays show on. */+ man: voiceSampleUrl("Charon"),+ /** Kate, firm — audibly another speaker, which is the point at a cut. */+ woman: voiceSampleUrl("Kore"),+};++/** Where the sentences sit in those clips, from the silence between them. */+const FIRST_SENTENCE = { at: 0.2, len: 1.55 };+const SECOND_SENTENCE = { at: 2.0, len: 2.5 };+/** How much of the handover's two sides cross over. */+const CROSS_WINDOW = 0.6;++interface AuditionState {+ /** The effect being played right now, or null. */+ effect: string | null;+ /** Play this effect's sample, or stop it when it is the one playing. */+ toggle: (effect: string, amount?: number) => void;+ stop: () => void;+}++let ctx: AudioContext | null = null;+let voices: AudioBufferSourceNode[] = [];+let chain: AudioFxNodes | null = null;+/** Guards a load that finished after its audition was cancelled. */+let token = 0;++const context = (): AudioContext => (ctx ??= new AudioContext());++/** The clips, fetched and decoded at most once each. A failed load drops out+ * of the cache so the next press tries again. */+const decoded = new Map<string, Promise<AudioBuffer>>();++function clip(target: AudioContext, url: string): Promise<AudioBuffer> {+ const hit = decoded.get(url);+ if (hit) return hit;+ const load = fetch(url)+ .then((r) => r.arrayBuffer())+ .then((bytes) => target.decodeAudioData(bytes));+ decoded.set(url, load);+ void load.catch(() => decoded.delete(url));+ return load;+}++function teardown(): void {+ for (const node of voices) {+ try {+ node.stop();+ } catch {+ // Already finished; stopping a spent node throws and means nothing.+ }+ node.disconnect();+ }+ voices = [];+ if (chain) {+ for (const node of chain.sources) {+ try {+ node.stop();+ } catch {+ // Already stopped.+ }+ }+ chain.input.disconnect();+ chain.output.disconnect();+ chain = null;+ }+}++export const useFxAudition = create<AuditionState>((set, get) => ({+ effect: null,+ toggle: (effect, amount) => {+ if (get().effect === effect) {+ get().stop();+ return;+ }+ teardown();+ // Two things playing at once teaches nothing about either.+ usePreviewAudio.getState().stop();+ const target = context();+ void target.resume();+ const mine = ++token;+ set({ effect });+ const done = () => {+ if (token === mine) {+ teardown();+ set({ effect: null });+ }+ };+ /** One side of an audition: a stretch of a clip, played at `when`. */+ const play = (+ buffer: AudioBuffer,+ part: { at: number; len: number },+ when: number,+ into: AudioNode,+ fade?: "in" | "out"+ ) => {+ const node = target.createBufferSource();+ node.buffer = buffer;+ const gain = target.createGain();+ if (fade === "in") {+ gain.gain.setValueAtTime(0, when);+ gain.gain.linearRampToValueAtTime(1, when + CROSS_WINDOW);+ } else if (fade === "out") {+ gain.gain.setValueAtTime(1, when + Math.max(0, part.len - CROSS_WINDOW));+ gain.gain.linearRampToValueAtTime(0, when + part.len);+ }+ node.connect(gain);+ gain.connect(into);+ node.start(when, part.at, part.len);+ voices.push(node);+ return node;+ };++ if (effect === SOUND_DISSOLVE_AUDITION) {+ void Promise.all([clip(target, LINES.man), clip(target, LINES.woman)])+ .then(([man, woman]) => {+ if (token !== mine) return;+ const at = target.currentTime + 0.05;+ // What the render does: the outgoing line ramps down into the cut,+ // the incoming one ramps up out of it, and the picture cuts where+ // the two meet.+ play(man, FIRST_SENTENCE, at, target.destination, "out");+ const second = play(+ woman,+ SECOND_SENTENCE,+ at + FIRST_SENTENCE.len,+ target.destination,+ "in"+ );+ second.onended = done;+ })+ .catch(done);+ return;+ }+ void clip(target, LINES.man)+ .then((buffer) => {+ if (token !== mine) return;+ const recipe = audioFxRecipe(effect, amount);+ let into: AudioNode = target.destination;+ if (recipe) {+ chain = buildAudioFx(target, recipe, target.currentTime);+ chain.output.connect(target.destination);+ into = chain.input;+ }+ const voice = play(buffer, SECOND_SENTENCE, target.currentTime + 0.05, into);+ voice.onended = done;+ })+ .catch(done);+ },+ stop: () => {+ token++;+ teardown();+ set({ effect: null });+ },+}));++// The other direction: a preview started anywhere else silences the audition.+registerPreviewSilencer(() => useFxAudition.getState().stop());diff --git a/site/src/cut/lib/previewAudio.ts b/site/src/cut/lib/previewAudio.tsindex 3e347c4a..be2da18c 100644--- a/site/src/cut/lib/previewAudio.ts+++ b/site/src/cut/lib/previewAudio.ts@@ -19,9 +19,22 @@ interface PreviewAudioState {let el: HTMLAudioElement | null = null;+/** Other players that have to go quiet when a preview starts. The effect+ * auditions run on their own audio graph rather than this element, and two+ * things playing at once teaches nothing about either. */+const silencers = new Set<() => void>();++export function registerPreviewSilencer(stop: () => void): () => void {+ silencers.add(stop);+ return () => {+ silencers.delete(stop);+ };+}+export const usePreviewAudio = create<PreviewAudioState>((set, get) => ({url: null,toggle: (url) => {+ for (const stop of silencers) stop();const audio = (el ??= new Audio());if (get().url === url) {audio.pause();diff --git a/site/src/cut/lib/store.ts b/site/src/cut/lib/store.tsindex 9e1553f5..955444be 100644--- a/site/src/cut/lib/store.ts+++ b/site/src/cut/lib/store.ts@@ -2,6 +2,7 @@import {groupRemap,+ isAudioEffect,KEY_EPSILON,keyAt,lineLikeShape,@@ -2300,14 +2301,20 @@ export const useEditor = create<EditorState>((baseSet, get, api) => {},addEffect: (effect, aim) => {- // An effect grades a stretch of the picture, and a clip is the stretch- // people mean: dropped anywhere over one, it opens covering that clip.- // Off the end of the cut it falls back to the standard length.+ // A picture effect grades a stretch of the picture, and a clip is the+ // stretch people mean: dropped anywhere over one, it opens covering that+ // clip. Off the end of the cut it falls back to the standard length.+ //+ // A treatment on the sound is aimed at a moment — a line, a word, the+ // bar the music turns on — so it opens at the standard length where it+ // was dropped and is stretched from there.const s0 = get();const t = aim?.at ?? playheadAt();- const span = getClipSpans(s0.clips, s0.assets).find(- (sp) => t >= sp.start - 1e-6 && t < sp.start + sp.len- );+ const span = isAudioEffect(effect)+ ? undefined+ : getClipSpans(s0.clips, s0.assets).find(+ (sp) => t >= sp.start - 1e-6 && t < sp.start + sp.len+ );const fit = span ? { at: span.start, len: span.len } : { at: aim?.at };addElement("effect",