/ magland / rip-sound-analyzer
Sign in
magland / rip-sound-analyzer
rip-sound-analyzer / src / storage / dataset.ts
63 lines · 2.8 KBCodeBlameHistory
bf56870Export/import the whole dataset; filtered waveform by defaultJeremy Magland 1// Whole-dataset export/import: a zip holding dataset.json (sample information and analysis
2// settings) plus one 32-bit float WAV per sample with the exact recorded samples.
4import type { AnalysisSettings } from "../analysis/analyze.ts";
5import { decodeWav, encodeWavFloat32 } from "../audio/wav.ts";
6import type { RecordingMeta } from "./db.ts";
7import { readZip, writeZip, type ZipEntry } from "./zip.ts";
9const FORMAT = "rip-sound-analyzer-dataset";
10const VERSION = 1;
12interface DatasetJson {
13 format: string;
14 version: number;
15 exportedAt: string;
16 settings: AnalysisSettings;
17 samples: (RecordingMeta & { audioFile: string })[];
20export async function exportDataset(
21 recs: RecordingMeta[],
22 settings: AnalysisSettings,
23 getAudio: (id: string) => Promise<Float32Array>,
24): Promise<Blob> {
25 const entries: ZipEntry[] = [];
26 const samples: DatasetJson["samples"] = [];
27 for (const [i, m] of recs.entries()) {
28 const audioFile = `audio/${String(i + 1).padStart(3, "0")}_${m.id}.wav`;
29 const x = await getAudio(m.id);
30 entries.push({ name: audioFile, data: new Uint8Array(await encodeWavFloat32(x, m.sampleRate).arrayBuffer()) });
31 samples.push({ ...m, audioFile });
32 }
33 const json: DatasetJson = { format: FORMAT, version: VERSION, exportedAt: new Date().toISOString(), settings, samples };
34 entries.unshift({ name: "dataset.json", data: new TextEncoder().encode(JSON.stringify(json, null, 2)) });
35 return writeZip(entries);
38export interface ImportedDataset {
39 settings: AnalysisSettings | null;
40 samples: { meta: RecordingMeta; audio: Float32Array }[];
41 exportedAt: string;
44export async function readDataset(file: File): Promise<ImportedDataset> {
45 const files = readZip(await file.arrayBuffer());
46 const raw = files.get("dataset.json");
47 if (!raw) throw new Error("This zip has no dataset.json, so it is not a dataset exported by this app.");
48 const json = JSON.parse(new TextDecoder().decode(raw)) as DatasetJson;
49 if (json.format !== FORMAT) throw new Error("dataset.json is not a Rip Sound Analyzer dataset.");
50 if (json.version > VERSION) throw new Error("This dataset was written by a newer version of the app.");
51 const samples: ImportedDataset["samples"] = [];
52 for (const s of json.samples) {
53 const wavBytes = files.get(s.audioFile);
54 if (!wavBytes) throw new Error(`Missing audio file ${s.audioFile}.`);
55 const d = decodeWav(wavBytes.slice().buffer);
56 if (!d) throw new Error(`Could not read ${s.audioFile}.`);
57 if (d.sampleRate !== s.sampleRate || d.samples.length !== s.length) throw new Error(`${s.audioFile} does not match its description.`);
58 const { audioFile: _unused, ...meta } = s;
59 void _unused;
60 samples.push({ meta, audio: d.samples });
61 }
62 return { settings: json.settings ?? null, samples, exportedAt: json.exportedAt };
↑↓ move↵openescclose