Five-slice navigable map of the entire codebase under
docs/architecture-map/. Each slice is a self-contained
breadcrumbed sub-tree:
01-frontend (16) Svelte/SvelteKit UI
02-tauri-runtime (26) src-tauri commands + lifecycle
03-audio-transcription (16) audio + transcription crates
04-llm-formatting-mcp (19) llm, ai-formatting, mcp, cloud
05-core-storage-hotkey-build core, storage, hotkey, workspace,
(26) CI, dev glue
Plus master README.md and data-flow-end-to-end.md tracing
audio bytes from microphone to FTS5 search to MCP read.
Generated by 5 parallel subagents on 2026/05/09 against
HEAD 3c47000. Each page has YAML frontmatter, file:line code
refs, sibling cross-links, plain-English summaries.
Aggregated debt surfaced (full lists in master README):
RB-08 macOS power assertion, schema head drift v14 vs v15,
VAD blocked on ort version conflict, streaming primitives
not wired into live.rs, no prompt versioning, MCP has no
auth, cloud-providers in-memory keystore, SettingsPage
2 484 LOC, commands/live.rs 1 737 LOC, dual theme system,
brand rename to Lumenote pending across the codebase.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
5.3 KiB
name, type, slice, last_verified
| name | type | slice | last_verified |
|---|---|---|---|
| Audio WAV I/O (hound) | architecture-map-page | 03-audio-transcription | 2026/05/09 |
Audio WAV I/O (hound)
Where you are: Architecture map → Audio + Transcription → Audio WAV I/O
Plain English summary. Two helpers cover the simple round-trip case: write_wav writes f32 samples to a 16-bit PCM WAV; read_wav reads them back, converting either int or float WAV payloads to f32. A third type, WavWriter, is the crash-safe append writer used during long live captures: it flushes the WAV header every 8000 samples (≈500 ms at 16 kHz), so a process kill leaves a valid, playable file on disk up to the last flush.
At a glance
- Crate:
magnotia-audio - Path:
crates/audio/src/wav.rs - LOC: 287
- External deps:
hound 3.5. - Internal callers (best effort, slice 2 reconciles):
WavWriteris consumed by the live-session command insrc-tauri/src/commands/live.rs(brief item #19).read_wavandwrite_wavare used by the file-import command and by the audio-decode tests.
Public surface:
pub struct WavWriter—crates/audio/src/wav.rs:21.pub fn WavWriter::create(path: &Path, sample_rate: u32, channels: u16) -> Result<Self>—crates/audio/src/wav.rs:36.pub fn WavWriter::append(&mut self, samples: &[f32]) -> Result<()>—crates/audio/src/wav.rs:58.pub fn WavWriter::flush(&mut self) -> Result<()>—crates/audio/src/wav.rs:78.pub fn WavWriter::finalize(self) -> Result<()>—crates/audio/src/wav.rs:90.pub fn write_wav(path: &Path, audio: &AudioSamples) -> Result<()>—crates/audio/src/wav.rs:99.pub fn read_wav(path: &Path) -> Result<AudioSamples>—crates/audio/src/wav.rs:132.
What's in here
WavWriter (crash-safe live capture)
Wraps a hound::WavWriter<BufWriter<File>> plus a samples_since_flush counter and a flush_every threshold (DEFAULT_FLUSH_EVERY_SAMPLES = 8_000, 500 ms at 16 kHz).
createconstructs a 16-bit PCMWavSpecfrom the suppliedsample_rate/channels. Sample rate and channel count come fromLocalEngine::capabilities()rather than being hardcoded — this matters once non-Whisper engines (Parakeet, future Moonshine) declare different rates.appendclamps each f32 to[-1.0, 1.0], scales toi16, writes the sample, and flushes the header everyflush_everysamples.flushcallshound::WavWriter::flush(which rewrites the RIFF and data chunk sizes in the header) and resets the counter.finalizewrites the terminal header and consumes the writer. A dropped-without-finalize writer leaves a valid file up to the last flush; callers that care about the unflushed tail should always call finalize.
write_wav and read_wav
write_wav is the simple one-shot writer over AudioSamples. Same clamp + i16 scale.
read_wav reads either int or float WAV payloads and returns f32 in [-1, 1]. For int payloads it scales by 1 << (bits_per_sample - 1). Unlike a previous version, per-sample errors are propagated rather than silently dropped (see watch-out below).
Data flow
Live capture path (production use):
StreamingResampler.push_samples → Vec<f32> 16 kHz mono
└─ WavWriter::append(chunk)
└─ for each f32:
clamp [-1,1] → (i16) write_sample
└─ if samples_since_flush >= 8000:
hound flush (rewrites size headers)
…session ends…
└─ WavWriter::finalize()
└─ terminal header write + close
File round-trip (used in tests + simple imports):
AudioSamples → write_wav → 16-bit PCM WAV → read_wav → AudioSamples
Watch-outs
- Crash-safety guarantee is "up to the last flush", not "every sample". Worst-case loss is
flush_every / sample_rateseconds (default 500 ms at 16 kHz). Thewav_writer_survives_crashtest (wav.rs:170) usesstd::mem::forgetto skip Drop and proves the flushed prefix is recoverable. finalizeis consuming. Once called you cannot append further. The live-session command must hold the writer until session end and only finalize at the close path.- i16 quantisation noise. Both writers downcast f32 → i16. WAV round-trip introduces ~1 / 32768 absolute error per sample, validated by the
wav_roundtriptest (wav.rs:265). - Truncated WAV bug fixed in tree (2026-04-22 review). Previous
read_wavusedfilter_map(|s| s.ok())and silently returned a shortAudioSamplesfor corrupt input. Current code surfacesMagnotiaError::AudioDecodeFailed. Regression test atwav.rs:235. - Channels and sample rate are taken from
AudioSamplesdirectly inwrite_wav.WavWriter::createtakes them as explicit args because it precedes the data; the caller (live session) readsLocalEngine::capabilities()to know what to pass. - No async wrapper.
WavWriteroperations are synchronous (Buffered file I/O). Live session must call them on a blocking task or accept the syscall cost on the audio thread.
See also
- Transcription engines overview —
LocalEngine::capabilities()is the source ofsample_rate/channelsforWavWriter::create. - Audio file decoding —
decode_audio_fileis the symphonia-based path used for non-WAV files. - Tests at
crates/audio/src/wav.rs:165(crash-safety, finalize round-trip, truncated-input regression).