import { analyze, DEFAULT_SETTINGS, type Analysis, type AnalysisSettings } from "./analysis/analyze.ts"; import { loadAudio, type RecordingMeta } from "./storage/db.ts"; import type { MarkerShape } from "./components/CanvasPlot.tsx"; import { SHAPES } from "./components/CanvasPlot.tsx"; // The fixed set of speeds. `id` is what is stored as a recording's label. export interface Speed { id: string; name: string; target: string; // intended tear duration } export const SPEEDS: Speed[] = [ { id: "very slow (~10 s)", name: "Very slow", target: "~10 s" }, { id: "slow (~5 s)", name: "Slow", target: "~5 s" }, { id: "medium (~2 s)", name: "Medium", target: "~2 s" }, { id: "fast (~1 s)", name: "Fast", target: "~1 s" }, { id: "very fast (<0.5 s)", name: "Very fast", target: "<0.5 s" }, ]; export const speedName = (id: string) => { const s = SPEEDS.find((v) => v.id === id); return s ? `${s.name} (${s.target})` : id; }; // Colour and marker follow the speed, never its position in a filtered list. export function speedStyle(id: string): { slot: number | null; shape: MarkerShape } { const i = SPEEDS.findIndex((s) => s.id === id); return i >= 0 ? { slot: i, shape: SHAPES[i] } : { slot: null, shape: "circle" }; } export function colorFor(slot: number | null, series: string[], muted: string) { return slot === null ? muted : series[slot]; } // ---------- settings ---------- const SETTINGS_KEY = "rsa-settings"; export function loadSettings(): AnalysisSettings { try { const s = JSON.parse(localStorage.getItem(SETTINGS_KEY) ?? "null"); if (s) return { ...DEFAULT_SETTINGS, ...s }; } catch { /* ignore */ } return { ...DEFAULT_SETTINGS }; } export function storeSettings(s: AnalysisSettings) { try { localStorage.setItem(SETTINGS_KEY, JSON.stringify(s)); } catch { /* ignore */ } } // ---------- audio + analysis caches ---------- const audioCache = new Map>(); export function getAudio(id: string): Promise { let p = audioCache.get(id); if (!p) { p = loadAudio(id); audioCache.set(id, p); } return p; } export function putAudio(id: string, x: Float32Array) { audioCache.set(id, Promise.resolve(x)); } export function forgetAudio(id: string) { audioCache.delete(id); } const analysisCache = new Map(); function key(m: RecordingMeta, s: AnalysisSettings) { return JSON.stringify([m.id, m.segment, s]); } export async function getAnalysis(m: RecordingMeta, s: AnalysisSettings): Promise { if (!m.segment) return null; const k = key(m, s); const hit = analysisCache.get(k); if (hit) return hit; const x = await getAudio(m.id); const a = analyze(x, m.sampleRate, m.segment, s); analysisCache.set(k, a); return a; } // ---------- formatting ---------- export const db10 = (v: number) => 10 * Math.log10(Math.max(v, 1e-30)); export function fmtHz(v: number) { if (!Number.isFinite(v)) return "–"; return v >= 1000 ? `${(v / 1000).toFixed(2)} kHz` : `${v.toFixed(0)} Hz`; } export function fmtSec(v: number) { return v < 1 ? `${(v * 1000).toFixed(0)} ms` : `${v.toFixed(2)} s`; } export function fmtDate(t: number) { return new Date(t).toLocaleString(undefined, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit", second: "2-digit" }); } export function download(blob: Blob, name: string) { const url = URL.createObjectURL(blob); const a = document.createElement("a"); a.href = url; a.download = name; a.click(); setTimeout(() => URL.revokeObjectURL(url), 1000); } export function safeName(s: string) { return s.replace(/[^a-z0-9.~-]+/gi, "_").replace(/^_+|_+$/g, ""); } // Simple least-squares fit y = a + b x, with standard error of b. export function linearFit(xs: number[], ys: number[]): { a: number; b: number; seB: number; n: number } | null { const n = xs.length; if (n < 3) return null; const mx = xs.reduce((s, v) => s + v, 0) / n; const my = ys.reduce((s, v) => s + v, 0) / n; let sxx = 0; let sxy = 0; for (let i = 0; i < n; i++) { sxx += (xs[i] - mx) ** 2; sxy += (xs[i] - mx) * (ys[i] - my); } if (sxx <= 0) return null; const b = sxy / sxx; const a = my - b * mx; let sse = 0; for (let i = 0; i < n; i++) sse += (ys[i] - a - b * xs[i]) ** 2; const seB = Math.sqrt(sse / (n - 2) / sxx); return { a, b, seB, n }; }