d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 1// Everything is kept in this browser's IndexedDB. Metadata and audio live in separate
2// stores so the list can load without pulling in every sample.
4import type { Segment } from "../analysis/analyze.ts";
5import type { CaptureInfo } from "../audio/recorder.ts";
7export interface RecordingMeta {
8 id: string;
9 createdAt: number;
10 label: string; // speed group chosen by the user, e.g. "slow (~5 s)"
11 notes: string;
12 material: string;
13 source: "mic" | "file" | "synthetic";
14 fileName?: string;
15 sampleRate: number;
16 length: number; // samples
17 segment: Segment | null; // the tear, in seconds
18 segmentManual: boolean;
8bf83cfMeasure energy and find the tear in a 15–20 kHz bandJeremy Magland 19 detectBand?: { lo: number; hi: number }; // band the automatic range was found in
d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 20 capture?: CaptureInfo;
21}
23const DB_NAME = "rip-sound-analyzer";
25function openDb(): Promise<IDBDatabase> {
26 return new Promise((resolve, reject) => {
27 const req = indexedDB.open(DB_NAME, 1);
28 req.onupgradeneeded = () => {
29 const db = req.result;
30 db.createObjectStore("recordings", { keyPath: "id" });
31 db.createObjectStore("audio");
32 };
33 req.onsuccess = () => resolve(req.result);
34 req.onerror = () => reject(req.error);
35 });
36}
38let dbp: Promise<IDBDatabase> | null = null;
39const db = () => (dbp ??= openDb());
41function done<T>(req: IDBRequest<T>): Promise<T> {
42 return new Promise((resolve, reject) => {
43 req.onsuccess = () => resolve(req.result);
44 req.onerror = () => reject(req.error);
45 });
46}
48export async function listRecordings(): Promise<RecordingMeta[]> {
49 const d = await db();
50 const all = await done(d.transaction("recordings").objectStore("recordings").getAll());
51 return (all as RecordingMeta[]).sort((a, b) => a.createdAt - b.createdAt);
52}
54export async function saveRecording(meta: RecordingMeta, samples: Float32Array): Promise<void> {
55 const d = await db();
56 const tx = d.transaction(["recordings", "audio"], "readwrite");
57 tx.objectStore("recordings").put(meta);
58 tx.objectStore("audio").put(samples, meta.id);
59 await new Promise<void>((resolve, reject) => {
60 tx.oncomplete = () => resolve();
61 tx.onerror = () => reject(tx.error);
62 });
63}
65export async function updateMeta(meta: RecordingMeta): Promise<void> {
66 const d = await db();
67 await done(d.transaction("recordings", "readwrite").objectStore("recordings").put(meta));
68}
70export async function loadAudio(id: string): Promise<Float32Array> {
71 const d = await db();
72 return (await done(d.transaction("audio").objectStore("audio").get(id))) as Float32Array;
73}
75export async function deleteRecording(id: string): Promise<void> {
76 const d = await db();
77 const tx = d.transaction(["recordings", "audio"], "readwrite");
78 tx.objectStore("recordings").delete(id);
79 tx.objectStore("audio").delete(id);
80 await new Promise<void>((resolve, reject) => {
81 tx.oncomplete = () => resolve();
82 tx.onerror = () => reject(tx.error);
83 });
84}
135ff3aAdd a button to clear all samples from the browserJeremy Magland 86export async function clearAll(): Promise<void> {
87 const d = await db();
88 const tx = d.transaction(["recordings", "audio"], "readwrite");
89 tx.objectStore("recordings").clear();
90 tx.objectStore("audio").clear();
91 await new Promise<void>((resolve, reject) => {
92 tx.oncomplete = () => resolve();
93 tx.onerror = () => reject(tx.error);
94 });
95}
d0a8b32Rip Sound Analyzer: record tears, inspect one, compare across speedsJeremy Magland 97export function newId(): string {
98 return crypto.randomUUID();
99}