Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

refactor: offload peak computation to Web Worker, replace module cache with useRef

Davide Mendoliaf67e997688cf
diff --git a/src/hooks/audioPeaksWorker.ts b/src/hooks/audioPeaksWorker.ts
new file mode 100644
index 000000000..27812fd96
--- /dev/null
+++ b/src/hooks/audioPeaksWorker.ts
@@ -0,0 +1,40 @@
+/**
+ * Web Worker: computes min/max peak pairs from raw audio channel data.
+ *
+ * Input message: { channels: Float32Array[]; duration: number }
+ * Output message: Float32Array of length 2*N — [min0, max0, min1, max1, …]
+ *
+ * Channel buffers are transferred (zero-copy) from the caller.
+ * The peaks buffer is transferred back.
+ */
+self.onmessage = (event: MessageEvent<{ channels: Float32Array[]; duration: number }>) => {
+ const { channels, duration } = event.data;
+ const nCh = channels.length;
+ if (nCh === 0) {
+ (self as unknown as Worker).postMessage(new Float32Array(0));
+ return;
+ }
+
+ const totalSamples = channels[0].length;
+ const N = Math.min(24000, Math.ceil(duration * 200));
+ const blockSize = totalSamples / N;
+ const peaks = new Float32Array(N * 2); // [min0, max0, min1, max1, …]
+
+ for (let i = 0; i < N; i++) {
+ const start = Math.floor(i * blockSize);
+ const end = Math.floor((i + 1) * blockSize);
+ let minVal = 0;
+ let maxVal = 0;
+ for (let j = start; j < end; j++) {
+ let sample = 0;
+ for (let c = 0; c < nCh; c++) sample += channels[c][j];
+ sample /= nCh;
+ if (sample < minVal) minVal = sample;
+ if (sample > maxVal) maxVal = sample;
+ }
+ peaks[i * 2] = minVal;
+ peaks[i * 2 + 1] = maxVal;
+ }
+
+ (self as unknown as Worker).postMessage(peaks, [peaks.buffer]);
+};
diff --git a/src/hooks/useAudioPeaks.ts b/src/hooks/useAudioPeaks.ts
index 00e60675c..18de90a54 100644
--- a/src/hooks/useAudioPeaks.ts
+++ b/src/hooks/useAudioPeaks.ts
@@ -1,42 +1,44 @@
-import { useEffect, useState } from "react";
+import { useEffect, useRef, useState } from "react";
import { loadFileAsArrayBuffer } from "@/lib/exporter/streamingDecoder";
-// Module-level cache keyed by URL — survives re-mounts within the same page session.
-const peaksCache = new Map<string, Float32Array>();
-
let _audioCtx: AudioContext | null = null;
function getAudioCtx(): AudioContext {
if (!_audioCtx) _audioCtx = new AudioContext();
return _audioCtx;
}
-function computePeaks(audioBuffer: AudioBuffer): Float32Array {
- const N = Math.min(24000, Math.ceil(audioBuffer.duration * 200));
- const nCh = audioBuffer.numberOfChannels;
- const totalSamples = audioBuffer.length;
- const blockSize = totalSamples / N;
- const peaks = new Float32Array(N * 2); // [min0, max0, min1, max1, …]
-
- const channels: Float32Array[] = [];
- for (let c = 0; c < nCh; c++) channels.push(audioBuffer.getChannelData(c));
+/**
+ * Offloads peak computation to a Web Worker (zero-copy via Transferable).
+ * Returns a Promise that resolves with the peaks Float32Array.
+ */
+function computePeaksInWorker(audioBuffer: AudioBuffer): Promise<Float32Array> {
+ return new Promise((resolve, reject) => {
+ const worker = new Worker(new URL("./audioPeaksWorker.ts", import.meta.url), {
+ type: "module",
+ });
- for (let i = 0; i < N; i++) {
- const start = Math.floor(i * blockSize);
- const end = Math.floor((i + 1) * blockSize);
- let minVal = 0;
- let maxVal = 0;
- for (let j = start; j < end; j++) {
- let sample = 0;
- for (let c = 0; c < nCh; c++) sample += channels[c][j];
- sample /= nCh;
- if (sample < minVal) minVal = sample;
- if (sample > maxVal) maxVal = sample;
+ // slice() creates an owned copy so the transfer is safe and the
+ // AudioBuffer remains valid if anything else holds a reference.
+ const channels: Float32Array[] = [];
+ for (let c = 0; c < audioBuffer.numberOfChannels; c++) {
+ channels.push(audioBuffer.getChannelData(c).slice());
}
- peaks[i * 2] = minVal;
- peaks[i * 2 + 1] = maxVal;
- }
- return peaks;
+ worker.onmessage = (e: MessageEvent<Float32Array>) => {
+ worker.terminate();
+ resolve(e.data);
+ };
+
+ worker.onerror = (e) => {
+ worker.terminate();
+ reject(e);
+ };
+
+ worker.postMessage(
+ { channels, duration: audioBuffer.duration },
+ channels.map((ch) => ch.buffer),
+ );
+ });
}
/**
@@ -45,11 +47,15 @@ function computePeaks(audioBuffer: AudioBuffer): Float32Array {
* decoding is in progress, and stays `null` when the file has no audio
* track or decoding fails (silent degradation).
*
- * Results are cached at module scope by URL so re-mounts are free.
+ * - File loading uses the Electron IPC bridge for local paths (same as the exporter).
+ * - Peak computation runs in a Web Worker to avoid blocking the main thread.
+ * - Results are cached in a ref scoped to the hook instance (survives re-renders
+ * and waveform toggle off/on, but not component unmount).
*/
export function useAudioPeaks(videoUrl?: string): Float32Array | null {
+ const cacheRef = useRef<Map<string, Float32Array>>(new Map());
const [peaks, setPeaks] = useState<Float32Array | null>(() =>
- videoUrl ? (peaksCache.get(videoUrl) ?? null) : null,
+ videoUrl ? (cacheRef.current.get(videoUrl) ?? null) : null,
);
useEffect(() => {
@@ -58,7 +64,7 @@ export function useAudioPeaks(videoUrl?: string): Float32Array | null {
return;
}
- const cached = peaksCache.get(videoUrl);
+ const cached = cacheRef.current.get(videoUrl);
if (cached) {
setPeaks(cached);
return;
@@ -72,8 +78,9 @@ export function useAudioPeaks(videoUrl?: string): Float32Array | null {
if (cancelled) return;
const audioBuffer = await getAudioCtx().decodeAudioData(arrayBuffer);
if (cancelled) return;
- const p = computePeaks(audioBuffer);
- peaksCache.set(videoUrl, p);
+ const p = await computePeaksInWorker(audioBuffer);
+ if (cancelled) return;
+ cacheRef.current.set(videoUrl, p);
setPeaks(p);
} catch {
// No audio track or unsupported format — silent degradation.