Skip to content

Open source CapCut alternative.

TypeScript

fix(site): a quiet clip still draws a waveform

David Lefe259461a7cd
diff --git a/site/src/cut/components/AudioPanel.tsx b/site/src/cut/components/AudioPanel.tsx
index 0a9069e7..6c9e0528 100644
--- a/site/src/cut/components/AudioPanel.tsx
+++ b/site/src/cut/components/AudioPanel.tsx
@@ -48,6 +48,7 @@ import { stockAssetInDoc } from "@/cut/lib/genvideo/docWriter";
import { creditsUrl, signInUrl, useSignedIn } from "@/cut/lib/generate";
import { genPulseOverlay, useGenNotify } from "@/cut/lib/genNotify";
import { enrichAsset } from "@/cut/lib/media";
+import { waveGain } from "@/cut/lib/waveform";
import { usePreviewAudio } from "@/cut/lib/previewAudio";
import { useEditor } from "@/cut/lib/store";
import { playheadAt } from "@/cut/lib/playhead";
@@ -560,6 +561,7 @@ function VoiceGenerator({ projectId }: { projectId: string }) {
* bigger by the lightbox and the chat's audio cards (`className` restyles). */
export function PeakStrip({ peaks, className }: { peaks: number[]; className?: string }) {
const BARS = 48;
+ const gain = waveGain(peaks);
const step = Math.max(1, Math.floor(peaks.length / BARS));
const bars: number[] = [];
for (let i = 0; i < BARS; i++) {
@@ -577,7 +579,7 @@ export function PeakStrip({ peaks, className }: { peaks: number[]; className?: s
aria-hidden
>
{bars.map((p, i) => {
- const h = Math.max(1.5, p * 16);
+ const h = Math.max(1.5, Math.min(1, p * gain) * 16);
return <rect key={i} x={i * 2} y={(16 - h) / 2} width={1.2} height={h} rx={0.6} fill="currentColor" />;
})}
</svg>
@@ -618,6 +620,7 @@ export function AudioPillSurface({
* than PeakStrip so they read at card size. */
function CardBars({ peaks, className }: { peaks: number[]; className?: string }) {
const BARS = 30;
+ const gain = waveGain(peaks);
const step = Math.max(1, Math.floor(peaks.length / BARS));
const bars: number[] = [];
for (let i = 0; i < BARS; i++) {
@@ -633,7 +636,7 @@ function CardBars({ peaks, className }: { peaks: number[]; className?: string })
<div
key={i}
className="min-w-0 flex-1 rounded-full bg-[#8fcba9]"
- style={{ height: `${Math.round(Math.max(0.15, p) * 100)}%` }}
+ style={{ height: `${Math.round(Math.max(0.15, Math.min(1, p * gain)) * 100)}%` }}
/>
))}
</div>
@@ -713,11 +716,12 @@ function buildAudioDragGhost(name: string, width: number, peaks?: number[]): HTM
ctx.fillStyle = "rgba(255,255,255,0.85)";
const bars = Math.max(1, Math.floor(width / 3));
const n = peaks?.length ?? 0;
+ const gain = n ? waveGain(peaks!) : 1;
for (let i = 0; i < bars; i++) {
// Use the asset's peaks when we have them; otherwise a gentle stand-in so
// the pill still reads as audio.
- const p = n ? (peaks![Math.min(n - 1, Math.floor((i / bars) * n))] ?? 0) : 0.32 + 0.26 * Math.abs(Math.sin(i / 2));
- const h = Math.max(1.5, p * (wave - 2));
+ const p = n ? (peaks![Math.min(n - 1, Math.floor((i / bars) * n))] ?? 0) * gain : 0.32 + 0.26 * Math.abs(Math.sin(i / 2));
+ const h = Math.max(1.5, Math.min(1, p) * (wave - 2));
ctx.fillRect(i * 3, (wave - h) / 2, 2, h);
}
}
diff --git a/site/src/cut/components/Timeline.tsx b/site/src/cut/components/Timeline.tsx
index ab995b60..bfb3bff1 100644
--- a/site/src/cut/components/Timeline.tsx
+++ b/site/src/cut/components/Timeline.tsx
@@ -44,6 +44,7 @@ import { isDragActive, startDrag, subscribeDragActive } from "@/cut/lib/drag";
import { CLIP_GAP, laneDragFor, laneDragParts, startLaneMove, startLaneTrim, type LaneDrag, type LaneKind } from "@/cut/lib/laneTracks";
import { downloadMedia, ensurePeaks, importImage, importStockMusic, importStockVideo, peekEdgeFrame, requestEdgeFrame, revealMedia, stripFailedFor, subscribeStripStatus } from "@/cut/lib/media";
import { planFilmstrip, type FilmTile } from "@/cut/lib/filmstrip";
+import { waveGain } from "@/cut/lib/waveform";
import { track0Clips, laneGapAt, sameLane, type LaneRef, clipLen, clipSpeed, getClipSpans, overlayLaneOrder, overlayLayers, projectDuration, resolveTransitions, rippleInsert, useEditor } from "@/cut/lib/store";
import type { VideoTrackPlacement } from "@/cut/lib/store";
import { playheadAt, setSkim, skimAt, subscribePlayhead, usePlayhead, useSkim } from "@/cut/lib/playhead";
@@ -4358,6 +4359,9 @@ function WaveformCanvas({
}) {
const canvasRef = useRef<HTMLCanvasElement>(null);
const peaks = asset.peaks;
+ // The strip is drawn against the clip's own loudest moment, so a quietly
+ // recorded clip shows its shape instead of a flat line (lib/waveform.ts).
+ const gain = useMemo(() => waveGain(peaks ?? []), [peaks]);
useEffect(() => {
const canvas = canvasRef.current;
if (!canvas || !peaks?.length) return;
@@ -4379,10 +4383,10 @@ function WaveformCanvas({
const b = Math.max(a + 1, Math.ceil(start + (i + 1) * perBar));
let p = 0;
for (let j = a; j < b && j < n; j++) if (peaks[j] > p) p = peaks[j];
- const bh = Math.max(1.5, p * (h - 2));
+ const bh = Math.max(1.5, Math.min(1, p * gain) * (h - 2));
ctx.fillRect(i * 3, (h - bh) / 2, 2, bh);
}
- }, [asset.duration, peaks, from, to, w, h]);
+ }, [asset.duration, peaks, gain, from, to, w, h]);
if (!peaks?.length) return null;
return <canvas ref={canvasRef} className={cn("pointer-events-none absolute w-full", className)} />;
}
diff --git a/site/src/cut/lib/waveform.ts b/site/src/cut/lib/waveform.ts
new file mode 100644
index 00000000..b9ce6b8f
--- /dev/null
+++ b/site/src/cut/lib/waveform.ts
@@ -0,0 +1,23 @@
+"use client";
+
+/**
+ * How loud a waveform is drawn.
+ *
+ * Peaks are measured amplitude, and speech into a phone held at arm's length
+ * lands around a fifth of full scale. Drawn at true amplitude that is a line
+ * one or two pixels tall inside a strip — the clip reads as having no sound at
+ * all. A strip's job is the shape of the sound across the clip, so it is drawn
+ * against the clip's own loudest moment.
+ *
+ * The lift is capped so a strip of room tone stays a strip of room tone: below
+ * the cap the bars rise with the level rather than filling the strip.
+ */
+const MAX_GAIN = 8;
+
+/** Multiplier to draw `peaks` with. 1 for an empty or already-full read. */
+export function waveGain(peaks: readonly number[]): number {
+ let max = 0;
+ for (const p of peaks) if (p > max) max = p;
+ if (!(max > 0)) return 1;
+ return Math.min(MAX_GAIN, 1 / max);
+}