import { useEffect } from "react"; import { mic, useMic } from "../audio/mic.ts"; import type { CaptureInfo } from "../audio/recorder.ts"; import { decodeWav } from "../audio/wav.ts"; const MAX_SECONDS = 60; interface Props { replacing: boolean; // re-recording an existing sample onDone: (samples: Float32Array, fs: number, info: CaptureInfo | null, fileName?: string) => Promise; onCancel: (() => void) | null; } export function RecordPanel({ replacing, onDone, onCancel }: Props) { const m = useMic(); const elapsed = m.status === "recording" ? (performance.now() - m.startedAt) / 1000 : 0; const stop = async () => { const r = await mic.stop(); if (!r) return; if (r.samples.length < r.fs * 0.5) { alert("That recording was shorter than half a second and was not kept."); return; } await onDone(r.samples, r.fs, r.info); }; useEffect(() => { if (m.status === "recording" && elapsed > MAX_SECONDS) void stop(); }); // space bar starts / stops (ignored while typing or when a button has focus) useEffect(() => { const onKey = (e: KeyboardEvent) => { if (e.code !== "Space" || e.repeat) return; if ((e.target as HTMLElement).closest("input, textarea, select, button")) return; const st = mic.get().status; if (st === "ready") { e.preventDefault(); mic.start(); } else if (st === "recording") { e.preventDefault(); void stop(); } }; window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }); // leaving this panel mid-recording discards the take useEffect(() => () => void mic.cancel(), []); const loadFile = async (f: File | undefined) => { if (!f) return; const d = decodeWav(await f.arrayBuffer()); if (!d) { alert("Only WAV files can be loaded."); return; } await onDone(d.samples, d.sampleRate, null, f.name); }; const s = m.info?.trackSettings ?? {}; const processingOn = ["echoCancellation", "noiseSuppression", "autoGainControl"].filter((k) => s[k] === true); const levelDb = 20 * Math.log10(Math.max(m.peak, 1e-6)); const meterPct = Math.max(0, Math.min(100, ((levelDb + 60) / 60) * 100)); return (
{replacing && (
Re-recording this sample. The current recording stays until you finish; the new one then replaces it.
)}
  1. Press Start (or the space bar) and keep still for about a second.
  2. Tear one strip off the full 11 inch sheet, at whatever speed you want to study.
  3. Keep still for another second, then press Stop.
{m.status === "closed" || m.status === "opening" ? ( ) : m.status === "recording" ? ( ) : ( )} {m.status !== "closed" && m.status !== "opening" && (
{levelDb > -119 ? `${levelDb.toFixed(0)} dBFS` : "–"}
)} {onCancel && ( )}
{m.clipped && m.status === "recording" && (
⚠ Clipping: the sound hit full scale, so its energy will be underestimated. Tear further from the microphone and re-record.
)} {m.error &&
{m.error}
} {m.info && (
{m.info.deviceLabel || "Microphone"} {m.devices.length > 1 && ( )} {m.info.contextSampleRate} Hz {processingOn.length ? ( ⚠ Browser processing still on: {processingOn.join(", ")} ) : ( ✓ Browser processing off )} {typeof s.sampleRate === "number" && s.sampleRate !== m.info.contextSampleRate && ( ⚠ Audio is being resampled from {String(s.sampleRate)} Hz )}
)}
); }