Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
Refactor waveform rendering in ClipTrack
Richie McIlroydbe8ae18df58
diff --git a/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsx b/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsxindex af712758a..0d1fcc13f 100644--- a/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsx+++ b/apps/desktop/src/routes/editor/Timeline/ClipTrack.tsx@@ -30,6 +30,69 @@ import {useSegmentWidth,} from "./Track";+const CANVAS_HEIGHT = 52;+const WAVEFORM_MIN_DB = -60;+const WAVEFORM_SAMPLE_STEP = 0.1;+const WAVEFORM_CONTROL_STEP = 0.05;+const WAVEFORM_PADDING_SECONDS = 0.3;++function gainToScale(gain?: number) {+ if (!Number.isFinite(gain)) return 1;+ const value = gain as number;+ if (value <= WAVEFORM_MIN_DB) return 0;+ return Math.max(0, 1 + value / -WAVEFORM_MIN_DB);+}++function createWaveformPath(+ segment: { start: number; end: number },+ waveform?: number[],+) {+ if (typeof Path2D === "undefined") return;+ if (!waveform || waveform.length === 0) return;++ const duration = Math.max(segment.end - segment.start, WAVEFORM_SAMPLE_STEP);+ if (!Number.isFinite(duration) || duration <= 0) return;++ const path = new Path2D();+ path.moveTo(0, 1);++ const amplitudeAt = (index: number) => {+ const sample = waveform[index];+ const db =+ typeof sample === "number" && Number.isFinite(sample)+ ? sample+ : WAVEFORM_MIN_DB;+ const clamped = Math.max(db, WAVEFORM_MIN_DB);+ const amplitude = 1 + clamped / -WAVEFORM_MIN_DB;+ return Math.min(Math.max(amplitude, 0), 1);+ };++ const controlStep = Math.min(WAVEFORM_CONTROL_STEP / duration, 0.25);++ for (+ let time = segment.start;+ time <= segment.end + WAVEFORM_SAMPLE_STEP;+ time += WAVEFORM_SAMPLE_STEP+ ) {+ const index = Math.floor(time * 10);+ const normalizedX = (index / 10 - segment.start) / duration;+ const prevX =+ (index / 10 - WAVEFORM_SAMPLE_STEP - segment.start) / duration;+ const y = 1 - amplitudeAt(index);+ const prevY = 1 - amplitudeAt(index - 1);+ const cpX1 = prevX + controlStep / 2;+ const cpX2 = normalizedX - controlStep / 2;+ path.bezierCurveTo(cpX1, prevY, cpX2, y, normalizedX, y);+ }++ const closingX =+ (segment.end + WAVEFORM_PADDING_SECONDS - segment.start) / duration;+ path.lineTo(closingX, 1);+ path.closePath();++ return path;+}+function formatTime(totalSeconds: number): string {const hours = Math.floor(totalSeconds / 3600);const minutes = Math.floor((totalSeconds % 3600) / 60);@@ -48,115 +111,61 @@ function WaveformCanvas(props: {systemWaveform?: number[];micWaveform?: number[];segment: { start: number; end: number };- secsPerPixel: number;}) {const { project } = useEditorContext();-- let canvas: HTMLCanvasElement | undefined;const { width } = useSegmentContext();- const { secsPerPixel } = useTimelineContext();-- const render = (- ctx: CanvasRenderingContext2D,- h: number,- waveform: number[],- color: string,- gain = 0,- ) => {- const maxAmplitude = h;-- // yellow please- ctx.fillStyle = color;- ctx.beginPath();-- const step = 0.05 / secsPerPixel();-- ctx.moveTo(0, h);-- const norm = (w: number) => {- const ww = Number.isFinite(w) ? w : -60;- return 1.0 - Math.max(ww + gain, -60) / -60;- };-- for (- let segmentTime = props.segment.start;- segmentTime <= props.segment.end + 0.1;- segmentTime += 0.1- ) {- const index = Math.floor(segmentTime * 10);- const xTime = index / 10;-- const currentDb =- typeof waveform[index] === "number" ? waveform[index] : -60;- const amplitude = norm(currentDb) * maxAmplitude;-- const x = (xTime - props.segment.start) / secsPerPixel();- const y = h - amplitude;-- const prevX = (xTime - 0.1 - props.segment.start) / secsPerPixel();- const prevDb =- typeof waveform[index - 1] === "number" ? waveform[index - 1] : -60;- const prevAmplitude = norm(prevDb) * maxAmplitude;- const prevY = h - prevAmplitude;-- const cpX1 = prevX + step / 2;- const cpX2 = x - step / 2;-- ctx.bezierCurveTo(cpX1, prevY, cpX2, y, x, y);- }-- ctx.lineTo(- (props.segment.end + 0.3 - props.segment.start) / secsPerPixel(),- h,- );+ const segmentRange = createMemo(() => ({+ start: props.segment.start,+ end: props.segment.end,+ }));+ const micPath = createMemo(() =>+ createWaveformPath(segmentRange(), props.micWaveform),+ );+ const systemPath = createMemo(() =>+ createWaveformPath(segmentRange(), props.systemWaveform),+ );- ctx.closePath();- ctx.fill();- };+ let canvas: HTMLCanvasElement | undefined;- function renderWaveforms() {+ createEffect(() => {if (!canvas) return;const ctx = canvas.getContext("2d");if (!ctx) return;- const w = width();- if (w <= 0) return;-- const h = canvas.height;- canvas.width = w;- ctx.clearRect(0, 0, w, h);-- if (props.micWaveform)- render(- ctx,- h,- props.micWaveform,- "rgba(255,255,255,0.4)",- project.audio.micVolumeDb,- );-- if (props.systemWaveform)- render(- ctx,- h,- props.systemWaveform,- "rgba(255,150,0,0.5)",- project.audio.systemVolumeDb,- );- }+ const canvasWidth = Math.max(width(), 1);+ canvas.width = canvasWidth;+ const canvasHeight = canvas.height;+ ctx.clearRect(0, 0, canvasWidth, canvasHeight);++ const drawPath = (+ path: Path2D | undefined,+ color: string,+ gain?: number,+ ) => {+ if (!path) return;+ const scale = gainToScale(gain);+ if (scale <= 0) return;+ ctx.save();+ ctx.translate(0, -1);+ ctx.scale(1, scale);+ ctx.translate(0, 1);+ ctx.scale(canvasWidth, canvasHeight);+ ctx.fillStyle = color;+ ctx.fill(path);+ ctx.restore();+ };- createEffect(() => {- renderWaveforms();+ drawPath(micPath(), "rgba(255,255,255,0.4)", project.audio.micVolumeDb);+ drawPath(systemPath(), "rgba(255,150,0,0.5)", project.audio.systemVolumeDb);});return (<canvasref={(el) => {canvas = el;- renderWaveforms();}}class="absolute inset-0 w-full h-full pointer-events-none"- height={52}+ height={CANVAS_HEIGHT}/>);}@@ -184,6 +193,17 @@ export function ClipTrack(const segments = (): Array<TimelineSegment> =>project.timeline?.segments ?? [{ start: 0, end: duration(), timescale: 1 }];+ const segmentOffsets = createMemo(() => {+ const segs = segments();+ const offsets: number[] = new Array(segs.length);+ let sum = 0;+ for (let idx = 0; idx < segs.length; idx++) {+ offsets[idx] = sum;+ sum += (segs[idx].end - segs[idx].start) / segs[idx].timescale;+ }+ return offsets;+ });+function onHandleReleased() {const { transform } = editorState.timeline;@@ -210,17 +230,7 @@ export function ClipTrack(initialStart: number;}>(null);- const prefixOffsets = createMemo(() => {- const segs = segments();- const out: number[] = new Array(segs.length);- let sum = 0;- for (let k = 0; k < segs.length; k++) {- out[k] = sum;- sum += (segs[k].end - segs[k].start) / segs[k].timescale;- }- return out;- });- const prevDuration = createMemo(() => prefixOffsets()[i()] ?? 0);+ const prevDuration = createMemo(() => segmentOffsets()[i()] ?? 0);const relativeSegment = createMemo(() => {const ds = startHandleDrag();@@ -481,7 +491,6 @@ export function ClipTrack(micWaveform={micWaveform()}systemWaveform={systemAudioWaveform()}segment={segment}- secsPerPixel={secsPerPixel()}/>)}