SPB Git

Repository Public

audio.ts 4.3 KB 66 lines typescript

1/**2 * file: src/lib/editor/audio.ts3 * description: pure helpers of the `audio` block (voice note, SPEC §2): time formatting, transcript stamps (`[mm:ss]`, the same as the4 *   Markdown export), waveform peaks, the transcript line being spoken, and the choice of a recording container. No browser API here.5 * author: Simon-Pierre Boucher — contact@spboucher.ai6 */7import type { TranscriptLine } from "./next-node-defs";89const whole = (seconds: unknown) => { const n = Number(seconds); return Number.isFinite(n) && n > 0 ? Math.floor(n) : 0; };10const two = (n: number) => String(n).padStart(2, "0");1112/** `m:ss`, or `h:mm:ss` from one hour. Negative, NaN and Infinity read as 0:00. */13export function formatTime(seconds: number): string {14  const s = whole(seconds); const h = Math.floor(s / 3600), m = Math.floor((s % 3600) / 60), r = s % 60;15  return h > 0 ? `${h}:${two(m)}:${two(r)}` : `${m}:${two(r)}`;16}17/** `[mm:ss]` — minutes are not folded into hours (a line at 1 h 02 min 03 s is `[62:03]`), as in the text export. */18export function transcriptStamp(seconds: number): string {19  const s = whole(seconds);20  return `[${two(Math.floor(s / 60))}:${two(s % 60)}]`;21}22/**23 * Waveform envelope: `buckets` values in 0..1, each the largest absolute sample of its slice, normalised by the loudest slice.24 * Silence or an empty input gives zeros; `buckets` ≤ 0 gives []. When there are fewer samples than buckets, the nearest sample is used.25 */26export function computePeaks(samples: Float32Array | number[], buckets: number): number[] {27  const n = Number.isFinite(buckets) ? Math.floor(buckets) : 0;28  if (n <= 0) return [];29  const len = samples?.length ?? 0; const peaks = new Array<number>(n).fill(0);30  if (len === 0) return peaks;31  let top = 0;32  for (let b = 0; b < n; b++) {33    const start = Math.floor((b * len) / n); const end = Math.max(start + 1, Math.floor(((b + 1) * len) / n));34    let max = 0;35    for (let i = start; i < end && i < len; i++) { const v = Math.abs(samples[i]); if (v > max) max = v; }36    if (!Number.isFinite(max)) max = 0;37    peaks[b] = max; if (max > top) top = max;38  }39  if (top <= 0) return peaks;40  for (let b = 0; b < n; b++) peaks[b] = Math.min(1, peaks[b] / top);41  return peaks;42}43/** Only well-formed lines (`{t: seconds ≥ 0, text}`), in the stored order. */44export const transcriptLines = (transcript: unknown): TranscriptLine[] => (Array.isArray(transcript) ? (transcript.filter((l) => l && typeof l === "object" && Number.isFinite((l as TranscriptLine).t) && typeof (l as TranscriptLine).text === "string") as TranscriptLine[]) : []);45/** Index of the line being spoken at time `t`: the one with the latest start ≤ t (first of equals), or -1 before the first line. */46export function activeLineIndex(transcript: readonly TranscriptLine[], t: number): number {47  if (!Array.isArray(transcript) || !Number.isFinite(t)) return -1;48  let best = -1; let bestT = -Infinity;49  for (let i = 0; i < transcript.length; i++) { const lt = Number(transcript[i]?.t); if (Number.isFinite(lt) && lt <= t && lt > bestT) { best = i; bestT = lt; } }50  return best;51}52export interface RecordingFormat { /** value for MediaRecorder's `mimeType` ("" = let the browser choose) */ mime: string; /** file extension without the dot */ ext: string }53const RECORDING_FORMATS: RecordingFormat[] = [54  { mime: "audio/mp4", ext: "m4a" }, { mime: "audio/webm;codecs=opus", ext: "webm" }, { mime: "audio/webm", ext: "webm" }, { mime: "audio/ogg;codecs=opus", ext: "ogg" },55];56/** First container the browser can record: audio/mp4 (.m4a, plays everywhere incl. the native apps), then WebM/Opus, WebM, Ogg/Opus. */57export function pickRecordingMime(isSupported: (mime: string) => boolean): RecordingFormat {58  for (const f of RECORDING_FORMATS) { try { if (isSupported(f.mime)) return { ...f }; } catch { /* a throwing probe means "no" */ } }59  return { mime: "", ext: "webm" };60}61/** Extension for a recorded blob whose real type is known afterwards (the recorder may not honour the request). */62export function extensionForMime(mime: string, fallback = "webm"): string {63  const base = String(mime ?? "").split(";")[0].trim().toLowerCase();64  return base === "audio/mp4" || base === "audio/x-m4a" || base === "audio/aac" ? "m4a" : base === "audio/webm" ? "webm" : base === "audio/ogg" ? "ogg" : base === "audio/mpeg" ? "mp3" : base === "audio/wav" || base === "audio/x-wav" ? "wav" : fallback;65}66
Files main
Menu

Theme