import { analyze, DEFAULT_SETTINGS, type Analysis, type AnalysisSettings } from "./analysis/analyze.ts"; import { loadAudio, type RecordingMeta } from "./storage/db.ts"; import type { Theme } from "./components/CanvasPlot.tsx"; // ---------- samples ---------- export const tearDuration = (m: RecordingMeta): number | null => (m.segment ? m.segment.end - m.segment.start : null); // Slowest first; samples without a tear range go last. export function sortSamples(recs: RecordingMeta[]): RecordingMeta[] { return [...recs].sort((a, b) => (tearDuration(b) ?? -1) - (tearDuration(a) ?? -1) || a.createdAt - b.createdAt); } export function sampleTitle(m: RecordingMeta): string { const d = tearDuration(m); return d === null ? "no tear marked" : `${fmtSec(d)} tear`; } // Colour encodes tear duration on a fixed log scale (0.2 s … 15 s), so a sample keeps its // colour when others are added. Faster tears get the strongest colour. export const DURATION_SCALE: [number, number] = [0.2, 15]; export function durationColor(d: number | null, th: Theme): string { if (d === null || !th.ramp.length) return th.muted; const [lo, hi] = DURATION_SCALE; const t = Math.min(1, Math.max(0, Math.log(d / lo) / Math.log(hi / lo))); // 0 = fast, 1 = slow return th.ramp[Math.round((1 - t) * (th.ramp.length - 1))]; } // ---------- settings ---------- const SETTINGS_KEY = "rsa-settings-v2"; 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 }; }