Skip to content

Commit cf0219b

Browse files
committed
test(studio): cover waveform canvas painting
1 parent 2c22bc4 commit cf0219b

2 files changed

Lines changed: 61 additions & 50 deletions

File tree

‎packages/studio/src/player/components/AudioWaveform.test.tsx‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ vi.mock("../../hooks/useThumbnailLease", () => ({
1111
useThumbnailLease: leaseSpy,
1212
}));
1313

14-
import { AudioWaveform } from "./AudioWaveform";
14+
import { AudioWaveform, drawWaveformCanvas } from "./AudioWaveform";
1515

1616
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
1717

@@ -21,6 +21,30 @@ afterEach(() => {
2121
});
2222

2323
describe("AudioWaveform", () => {
24+
it("paints a baseline and peak bar for every mapped waveform bin", () => {
25+
const fillRect = vi.fn();
26+
const context = {
27+
scale: vi.fn(),
28+
clearRect: vi.fn(),
29+
fillRect,
30+
fillStyle: "",
31+
} as unknown as CanvasRenderingContext2D;
32+
const canvas = document.createElement("canvas");
33+
Object.defineProperties(canvas, {
34+
clientWidth: { value: 6 },
35+
clientHeight: { value: 20 },
36+
});
37+
vi.spyOn(canvas, "getContext").mockReturnValue(context);
38+
drawWaveformCanvas(canvas, [0.25, 1], false, 0, 1);
39+
expect(fillRect).toHaveBeenCalledTimes(4);
40+
expect(fillRect.mock.calls.map(([x, y, width, height]) => [x, y, width, height])).toEqual([
41+
[0, 18, 3, 2],
42+
[0, 15, 3, 5],
43+
[3, 18, 3, 2],
44+
[3, 0, 3, 20],
45+
]);
46+
});
47+
2448
it("leases waveform decoding with the clip's project, session, and viewport priority", () => {
2549
const host = document.createElement("div");
2650
document.body.append(host);

‎packages/studio/src/player/components/AudioWaveform.tsx‎

Lines changed: 36 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -24,27 +24,6 @@ const BAR_STEP = 3;
2424

2525
type BarGeometry = { x: number; width: number; height: number };
2626

27-
function mapWaveformBars(
28-
peaks: readonly number[],
29-
width: number,
30-
height: number,
31-
trimStartFraction: number,
32-
trimEndFraction: number,
33-
): BarGeometry[] {
34-
const bars = decimatePeaks(
35-
peaks,
36-
trimStartFraction,
37-
trimEndFraction,
38-
Math.max(1, Math.ceil(width / BAR_STEP)),
39-
);
40-
const barWidth = Math.max(1, width / bars.length);
41-
return bars.map((amplitude, index) => ({
42-
x: (index * width) / bars.length,
43-
width: barWidth,
44-
height: Math.max(3, amplitude * height),
45-
}));
46-
}
47-
4827
function paintWaveformBars(
4928
context: CanvasRenderingContext2D,
5029
bars: readonly BarGeometry[],
@@ -62,6 +41,41 @@ function paintWaveformBars(
6241
});
6342
}
6443

44+
export function drawWaveformCanvas(
45+
canvas: HTMLCanvasElement,
46+
peaks: readonly number[],
47+
muted: boolean,
48+
trimStartFraction: number,
49+
trimEndFraction: number,
50+
) {
51+
const width = Math.max(1, canvas.clientWidth);
52+
const height = Math.max(1, canvas.clientHeight);
53+
const scale = window.devicePixelRatio || 1;
54+
canvas.width = Math.ceil(width * scale);
55+
canvas.height = Math.ceil(height * scale);
56+
const context = canvas.getContext("2d");
57+
if (!context) return;
58+
context.scale(scale, scale);
59+
context.clearRect(0, 0, width, height);
60+
const amplitudes = decimatePeaks(
61+
peaks,
62+
trimStartFraction,
63+
trimEndFraction,
64+
Math.max(1, Math.ceil(width / BAR_STEP)),
65+
);
66+
const bars = amplitudes.map((amplitude, index) => ({
67+
x: (index * width) / amplitudes.length,
68+
width: Math.max(1, width / amplitudes.length),
69+
height: Math.max(3, amplitude * height),
70+
}));
71+
const channelToken = muted ? "--timeline-waveform-muted-rgb" : "--timeline-waveform-bar-rgb";
72+
const waveformBarRgb = getComputedStyle(canvas).getPropertyValue(channelToken);
73+
const waveformBaselineRgb = getComputedStyle(canvas).getPropertyValue(
74+
"--timeline-waveform-baseline-rgb",
75+
);
76+
paintWaveformBars(context, bars, height, waveformBarRgb, waveformBaselineRgb, amplitudes);
77+
}
78+
6579
function extractPeaks(channelData: Float32Array, barCount: number): number[] {
6680
const peaks: number[] = [];
6781
const samplesPerBar = Math.floor(channelData.length / barCount);
@@ -168,34 +182,7 @@ export const AudioWaveform = memo(function AudioWaveform({
168182
const draw = useCallback(() => {
169183
const canvas = canvasRef.current;
170184
if (!canvas || !peaks) return;
171-
const width = Math.max(1, canvas.clientWidth);
172-
const height = Math.max(1, canvas.clientHeight);
173-
const scale = window.devicePixelRatio || 1;
174-
canvas.width = Math.ceil(width * scale);
175-
canvas.height = Math.ceil(height * scale);
176-
const context = canvas.getContext("2d");
177-
if (!context) return;
178-
context.scale(scale, scale);
179-
context.clearRect(0, 0, width, height);
180-
const mappedBars = mapWaveformBars(
181-
peaks,
182-
width,
183-
height,
184-
trimStartFraction ?? 0,
185-
trimEndFraction ?? 1,
186-
);
187-
const amplitudes = decimatePeaks(
188-
peaks,
189-
trimStartFraction ?? 0,
190-
trimEndFraction ?? 1,
191-
mappedBars.length,
192-
);
193-
const channelToken = muted ? "--timeline-waveform-muted-rgb" : "--timeline-waveform-bar-rgb";
194-
const waveformBarRgb = getComputedStyle(canvas).getPropertyValue(channelToken);
195-
const waveformBaselineRgb = getComputedStyle(canvas).getPropertyValue(
196-
"--timeline-waveform-baseline-rgb",
197-
);
198-
paintWaveformBars(context, mappedBars, height, waveformBarRgb, waveformBaselineRgb, amplitudes);
185+
drawWaveformCanvas(canvas, peaks, muted, trimStartFraction ?? 0, trimEndFraction ?? 1);
199186
}, [muted, peaks, trimEndFraction, trimStartFraction]);
200187

201188
const setCanvasRef = useCallback(

0 commit comments

Comments
 (0)