diff --git a/Cargo.lock b/Cargo.lock index 00b1d12..aa3545a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -201,6 +201,12 @@ dependencies = [ "tinyvec", ] +[[package]] +name = "built" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56ed6191a7e78c36abdb16ab65341eefd73d64d303fffccdbb00d51e4205967b" + [[package]] name = "bumpalo" version = "3.20.3" @@ -434,6 +440,21 @@ dependencies = [ "libc", ] +[[package]] +name = "crc" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5eb8a2a1cd12ab0d987a5d5e825195d372001a4094a0376319d5a0ad71c1ba0d" +dependencies = [ + "crc-catalog", +] + +[[package]] +name = "crc-catalog" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "217698eaf96b4a3f0bc4f3662aaa55bdf913cd54d7204591faa790070c6d0853" + [[package]] name = "crc32fast" version = "1.5.4" @@ -778,6 +799,16 @@ dependencies = [ "typeid", ] +[[package]] +name = "errno" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" +dependencies = [ + "libc", + "windows-sys 0.61.2", +] + [[package]] name = "fallible-iterator" version = "0.3.0" @@ -830,6 +861,24 @@ version = "0.1.15" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1c66915cc5391097de7a2b0b972e8505aa33c0df401fd3a6811dbdb32881ba48" +[[package]] +name = "flacenc" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74c892c2b5fa08f967e8b5ad29121570b4762202f2402033ce08479ec65eccd0" +dependencies = [ + "built", + "crc", + "crossbeam-channel", + "heapless", + "log", + "md-5", + "num-traits", + "rustversion", + "seq-macro", + "serde", +] + [[package]] name = "flate2" version = "1.1.10" @@ -1240,6 +1289,15 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "hash32" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "47d60b12902ba28e2730cd37e95b8c9223af2808df9e902d4df49588d1470606" +dependencies = [ + "byteorder", +] + [[package]] name = "hashbrown" version = "0.12.3" @@ -1270,6 +1328,17 @@ dependencies = [ "hashbrown 0.14.5", ] +[[package]] +name = "heapless" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0bfb9eb618601c89945a70e254898da93b13be0388091d42117462b265bb3fad" +dependencies = [ + "hash32", + "serde", + "stable_deref_trait", +] + [[package]] name = "heck" version = "0.4.1" @@ -1288,6 +1357,12 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" +[[package]] +name = "hound" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62adaabb884c94955b19907d60019f4e145d091c75345379e70d1ee696f7854f" + [[package]] name = "html5ever" version = "0.39.0" @@ -1801,6 +1876,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "linux-raw-sys" +version = "0.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53" + [[package]] name = "litemap" version = "0.8.3" @@ -1833,6 +1914,16 @@ dependencies = [ "web_atoms", ] +[[package]] +name = "md-5" +version = "0.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d89e7ee0cfbedfc4da3340218492196241d89eefb6dab27de5df917a6d2e78cf" +dependencies = [ + "cfg-if", + "digest", +] + [[package]] name = "memchr" version = "2.8.3" @@ -1942,12 +2033,30 @@ version = "1.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "650eef8c711430f1a879fdd01d4745a7deea475becfb90269c06775983bbf086" +[[package]] +name = "num-complex" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" +dependencies = [ + "num-traits", +] + [[package]] name = "num-conv" version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" +[[package]] +name = "num-integer" +version = "0.1.47" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ce2d95d4b3734dc35aa2f45e1aa22cd416814592a4f9d9205e11affd5b8e10b" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.19" @@ -2377,6 +2486,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" +[[package]] +name = "primal-check" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0d895b311e3af9902528fbb8f928688abbd95872819320517cc24ca6b2bd08" +dependencies = [ + "num-integer", +] + [[package]] name = "proc-macro-crate" version = "1.3.1" @@ -2475,6 +2593,15 @@ version = "0.6.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "20675572f6f24e9e76ef639bc5552774ed45f1c30e2951e1e99c59888861c539" +[[package]] +name = "realfft" +version = "3.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "538895ccf2006b473aafbe8d72770e21038961ab67aae34f3c577485fc703a7d" +dependencies = [ + "rustfft", +] + [[package]] name = "recorder-audio" version = "0.1.0" @@ -2487,9 +2614,13 @@ name = "recorder-core" version = "0.1.0" dependencies = [ "chrono", + "flacenc", + "hound", + "rubato", "rusqlite", "serde", "serde_json", + "tempfile", "thiserror 1.0.69", ] @@ -2595,6 +2726,18 @@ dependencies = [ "web-sys", ] +[[package]] +name = "rubato" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5d18b486e7d29a408ef3f825bc1327d8f87af091c987ca2f5b734625940e234" +dependencies = [ + "num-complex", + "num-integer", + "num-traits", + "realfft", +] + [[package]] name = "rusqlite" version = "0.31.0" @@ -2624,6 +2767,33 @@ dependencies = [ "semver", ] +[[package]] +name = "rustfft" +version = "6.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21db5f9893e91f41798c88680037dba611ca6674703c1a18601b01a72c8adb89" +dependencies = [ + "num-complex", + "num-integer", + "num-traits", + "primal-check", + "strength_reduce", + "transpose", +] + +[[package]] +name = "rustix" +version = "1.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa" +dependencies = [ + "bitflags 2.13.2", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.61.2", +] + [[package]] name = "rustversion" version = "1.0.23" @@ -2725,6 +2895,12 @@ dependencies = [ "serde_core", ] +[[package]] +name = "seq-macro" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc711410fbe7399f390ca1c3b60ad0f53f80e95c5eb935e52268a0e2cd49acc" + [[package]] name = "serde" version = "1.0.229" @@ -2995,6 +3171,12 @@ version = "3.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ff7589a1859b09232522bb9edf076ca7fcf14ded96685d3fa629db73023ffbb7" +[[package]] +name = "strength_reduce" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fe895eb47f22e2ddd4dabc02bce419d2e643c8e3b585c78158b349195bc24d82" + [[package]] name = "string_cache" version = "0.9.0" @@ -3409,6 +3591,19 @@ dependencies = [ "toml 1.2.0+spec-1.1.0", ] +[[package]] +name = "tempfile" +version = "3.27.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd" +dependencies = [ + "fastrand", + "getrandom 0.4.3", + "once_cell", + "rustix", + "windows-sys 0.61.2", +] + [[package]] name = "tendril" version = "0.5.1" @@ -3693,6 +3888,16 @@ dependencies = [ "once_cell", ] +[[package]] +name = "transpose" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ad61aed86bc3faea4300c7aee358b4c6d0c8d6ccc36524c96e4c92ccf26e77e" +dependencies = [ + "num-integer", + "strength_reduce", +] + [[package]] name = "tray-icon" version = "0.25.1" diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index 5804067..7470b13 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -13,3 +13,9 @@ serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" chrono = { version = "0.4", features = ["serde"] } thiserror = "1.0" +rubato = "0.15" +flacenc = "0.5" +hound = "3.5" + +[dev-dependencies] +tempfile = "3" diff --git a/crates/core/src/audio/conditioner.rs b/crates/core/src/audio/conditioner.rs new file mode 100644 index 0000000..04d9efa --- /dev/null +++ b/crates/core/src/audio/conditioner.rs @@ -0,0 +1,201 @@ +//! Downmixes and resamples live capture callbacks to mono 16 kHz. +//! +//! Capture callbacks arrive at any input sample rate, with 1-8 channels, and +//! in arbitrary chunk sizes. `rubato`'s fixed-size sinc resampler needs fixed +//! input chunks, so [`Conditioner`] buffers incoming (already downmixed) +//! samples internally and only feeds the resampler full chunks, carrying the +//! remainder across calls to `process`. [`Conditioner::flush`] drains and +//! zero-pads whatever is left at the end of a recording. + +use rubato::{ + Resampler, SincFixedIn, SincInterpolationParameters, SincInterpolationType, WindowFunction, +}; + +/// Output sample rate produced by the conditioner, matching the on-disk track format. +pub const OUT_RATE: u32 = 16_000; + +/// Fixed input chunk size (in frames) fed to the sinc resampler. +const CHUNK_SIZE: usize = 1024; + +/// Downmixes interleaved multi-channel audio to mono and resamples it to +/// [`OUT_RATE`] (16 kHz). +pub struct Conditioner { + in_rate: u32, + in_channels: u16, + resampler: Option>, + /// Downmixed mono samples at `in_rate`, waiting for a full chunk. + pending: Vec, +} + +fn sinc_params() -> SincInterpolationParameters { + SincInterpolationParameters { + sinc_len: 256, + f_cutoff: 0.95, + interpolation: SincInterpolationType::Linear, + oversampling_factor: 256, + window: WindowFunction::BlackmanHarris2, + } +} + +impl Conditioner { + /// Creates a conditioner for input audio at `in_rate` Hz with `in_channels` + /// interleaved channels. When `in_rate` is already 16 kHz, resampling is + /// skipped and only downmixing happens. + pub fn new(in_rate: u32, in_channels: u16) -> Self { + let resampler = if in_rate == OUT_RATE { + None + } else { + let ratio = OUT_RATE as f64 / in_rate as f64; + Some( + SincFixedIn::::new(ratio, 2.0, sinc_params(), CHUNK_SIZE, 1) + .expect("valid resampler configuration"), + ) + }; + Self { + in_rate, + in_channels, + resampler, + pending: Vec::new(), + } + } + + /// Averages interleaved channels down to mono frames. + fn downmix(&self, interleaved: &[f32]) -> Vec { + let channels = (self.in_channels as usize).max(1); + interleaved + .chunks(channels) + .map(|frame| frame.iter().copied().sum::() / frame.len() as f32) + .collect() + } + + /// Conditions one capture callback's worth of interleaved audio, returning + /// zero or more mono 16 kHz samples. Any chunk length is accepted; partial + /// resampler input is buffered until the next call (or [`Conditioner::flush`]). + pub fn process(&mut self, interleaved: &[f32]) -> Vec { + let mono = self.downmix(interleaved); + + let Some(resampler) = self.resampler.as_mut() else { + return mono; + }; + + self.pending.extend(mono); + + let mut out = Vec::new(); + while self.pending.len() >= CHUNK_SIZE { + let chunk: Vec = self.pending.drain(..CHUNK_SIZE).collect(); + let waves_in = [chunk]; + let waves_out = resampler + .process(&waves_in, None) + .expect("fixed-size resample chunk"); + out.extend(waves_out.into_iter().next().unwrap_or_default()); + } + out + } + + /// Drains any buffered partial chunk at the end of a recording, zero-padding + /// it to the resampler's fixed input size and trimming the output to the + /// proportional length (`pending_len * out_rate / in_rate`). + pub fn flush(&mut self) -> Vec { + let Some(resampler) = self.resampler.as_mut() else { + return Vec::new(); + }; + if self.pending.is_empty() { + return Vec::new(); + } + + let remaining = self.pending.len(); + let waves_in = [std::mem::take(&mut self.pending)]; + let waves_out = resampler + .process_partial(Some(&waves_in), None) + .expect("zero-padded partial resample"); + let mut out = waves_out.into_iter().next().unwrap_or_default(); + + let proportional = + ((remaining as f64) * (OUT_RATE as f64) / (self.in_rate as f64)).round() as usize; + out.truncate(proportional.min(out.len())); + out + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::f32::consts::PI; + + fn sine_stereo(seconds: f32, rate: u32, freq: f32) -> Vec { + let frames = (seconds * rate as f32) as usize; + let mut out = Vec::with_capacity(frames * 2); + for i in 0..frames { + let t = i as f32 / rate as f32; + let s = (2.0 * PI * freq * t).sin(); + out.push(s); + out.push(s); + } + out + } + + #[test] + fn conditioner_48k_stereo_to_16k_mono() { + let mut conditioner = Conditioner::new(48_000, 2); + let input = sine_stereo(1.0, 48_000, 440.0); + + let mut out = conditioner.process(&input); + out.extend(conditioner.flush()); + + assert!( + (out.len() as i64 - 16_000i64).abs() <= 160, + "expected ~16000 samples, got {}", + out.len() + ); + } + + #[test] + fn conditioner_preserves_sine_rms() { + let rate = 48_000; + let input = sine_stereo(1.0, rate, 440.0); + + // Reference RMS computed directly on the (already identical-channel) + // mono signal, i.e. without any resampling artifacts. + let mono_ref: Vec = input.chunks(2).map(|f| (f[0] + f[1]) / 2.0).collect(); + let ref_rms = super::super::level::rms_level(&mono_ref); + + let mut conditioner = Conditioner::new(rate, 2); + let mut out = conditioner.process(&input); + out.extend(conditioner.flush()); + let out_rms = super::super::level::rms_level(&out); + + let diff = (out_rms - ref_rms).abs() / ref_rms; + assert!( + diff < 0.10, + "rms drifted by {:.1}% (ref={ref_rms}, out={out_rms})", + diff * 100.0 + ); + } + + #[test] + fn conditioner_accepts_arbitrary_chunk_sizes() { + let mut conditioner = Conditioner::new(48_000, 1); + // Feed input in odd, non-chunk-aligned pieces. + let input = vec![0.5f32; 48_000]; + let mut out = Vec::new(); + for piece in input.chunks(777) { + out.extend(conditioner.process(piece)); + } + out.extend(conditioner.flush()); + + assert!( + (out.len() as i64 - 16_000i64).abs() <= 160, + "expected ~16000 samples, got {}", + out.len() + ); + } + + #[test] + fn conditioner_skips_resampling_at_16k() { + let mut conditioner = Conditioner::new(16_000, 2); + let input = vec![0.25f32, 0.75f32, 0.5f32, -0.5f32]; // 2 stereo frames + let out = conditioner.process(&input); + assert_eq!(out, vec![0.5f32, 0.0f32]); + assert!(conditioner.flush().is_empty()); + } +} diff --git a/crates/core/src/audio/flac.rs b/crates/core/src/audio/flac.rs new file mode 100644 index 0000000..1334c6f --- /dev/null +++ b/crates/core/src/audio/flac.rs @@ -0,0 +1,86 @@ +//! Encodes a recorded mono 16 kHz WAV track to FLAC for upload. + +use std::io; +use std::path::Path; + +use flacenc::component::BitRepr; +use flacenc::error::Verify; + +/// Reads the 16 kHz mono 16-bit PCM WAV at `wav` and encodes it to FLAC at +/// `out`, using `flacenc`'s default encoder settings. +pub fn encode_flac(wav: &Path, out: &Path) -> io::Result<()> { + let mut reader = + hound::WavReader::open(wav).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + let spec = reader.spec(); + + let samples: Vec = reader + .samples::() + .map(|s| s.map(i32::from)) + .collect::>() + .map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?; + + let config = flacenc::config::Encoder::default() + .into_verified() + .map_err(|(_, e)| { + io::Error::new( + io::ErrorKind::InvalidData, + format!("invalid flac encoder config: {e:?}"), + ) + })?; + + let source = flacenc::source::MemSource::from_samples( + &samples, + spec.channels as usize, + spec.bits_per_sample as usize, + spec.sample_rate as usize, + ); + + let stream = flacenc::encode_with_fixed_block_size(&config, source, config.block_size) + .map_err(|e| io::Error::other(format!("flac encode failed: {e:?}")))?; + + let mut sink = flacenc::bitsink::ByteSink::new(); + stream + .write(&mut sink) + .map_err(|e| io::Error::other(format!("flac bitstream write failed: {e:?}")))?; + + std::fs::write(out, sink.as_slice()) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::audio::wav::{TrackWriter, SAMPLE_RATE}; + use std::f32::consts::PI; + + #[test] + fn flac_is_valid_and_smaller() { + let dir = tempfile::tempdir().unwrap(); + let wav_path = dir.path().join("sine.wav"); + let flac_path = dir.path().join("sine.flac"); + + let mut writer = TrackWriter::create(&wav_path).unwrap(); + let seconds = 3.0; + let frames = (SAMPLE_RATE as f32 * seconds) as usize; + let samples: Vec = (0..frames) + .map(|i| { + let t = i as f32 / SAMPLE_RATE as f32; + 0.5 * (2.0 * PI * 440.0 * t).sin() + }) + .collect(); + writer.write(&samples).unwrap(); + writer.finalize().unwrap(); + + encode_flac(&wav_path, &flac_path).unwrap(); + + let flac_bytes = std::fs::read(&flac_path).unwrap(); + assert_eq!(&flac_bytes[0..4], b"fLaC"); + + let wav_len = std::fs::metadata(&wav_path).unwrap().len(); + assert!( + (flac_bytes.len() as u64) < wav_len, + "flac ({}) should be smaller than wav ({})", + flac_bytes.len(), + wav_len + ); + } +} diff --git a/crates/core/src/audio/gap.rs b/crates/core/src/audio/gap.rs new file mode 100644 index 0000000..4922170 --- /dev/null +++ b/crates/core/src/audio/gap.rs @@ -0,0 +1,164 @@ +//! Fills gaps left by irregular capture timing with silence. +//! +//! Capture callbacks arrive with their own jitter: a chunk may be a little +//! early, a little late, or (rarely) missing entirely because the capture +//! device dropped it. [`GapFiller`] tracks how many samples have been written +//! so far and, when a chunk arrives noticeably behind the wall-clock time it +//! should have arrived at, prepends silence to keep the track in sync. Normal +//! 10-20 ms packet jitter must never insert silence, so a 100 ms tolerance is +//! used throughout. + +use std::time::Duration; + +/// Tolerance before a lag is considered a real gap rather than normal jitter. +const TOLERANCE: Duration = Duration::from_millis(100); + +/// Tracks written samples against wall-clock elapsed time and inserts silence +/// for genuine gaps (more than 100 ms behind), never for ordinary jitter. +pub struct GapFiller { + rate: u32, + written: u64, +} + +impl GapFiller { + /// Creates a gap filler for a track sampled at `rate` Hz. + pub fn new(rate: u32) -> Self { + Self { rate, written: 0 } + } + + /// Number of samples written (including any inserted silence) so far. + pub fn written(&self) -> u64 { + self.written + } + + fn samples_for(&self, d: Duration) -> u64 { + (d.as_secs_f64() * self.rate as f64).round() as u64 + } + + fn tolerance_samples(&self) -> u64 { + self.samples_for(TOLERANCE) + } + + /// Accepts one capture chunk that arrived at wall-clock `elapsed` since the + /// start of recording (i.e. `elapsed` is the expected time of the chunk's + /// last sample). Returns the samples to write: zero or more leading zeros + /// (if the track has fallen behind by more than 100 ms) followed by `chunk` + /// itself. Never drops or delays samples when the track is already caught + /// up or ahead. + pub fn fill(&mut self, elapsed: Duration, chunk: &[f32]) -> Vec { + let target = self.samples_for(elapsed); + let chunk_len = chunk.len() as u64; + let threshold = self.tolerance_samples(); + + let mut out = Vec::new(); + if self.written + chunk_len < target.saturating_sub(threshold) { + let zeros = target - self.written - chunk_len; + out.resize(zeros as usize, 0.0); + self.written += zeros; + } + out.extend_from_slice(chunk); + self.written += chunk_len; + out + } + + /// Returns zeros up to the sample count implied by `elapsed`, but only + /// when the track is lagging by more than 100 ms; otherwise returns an + /// empty vector. Intended to be called on a timer and once more at stop, + /// so that silence gaps are not left open indefinitely between packets. + pub fn pad_to(&mut self, elapsed: Duration) -> Vec { + let target = self.samples_for(elapsed); + let threshold = self.tolerance_samples(); + + if target > self.written + threshold { + let zeros = target - self.written; + self.written += zeros; + vec![0.0; zeros as usize] + } else { + Vec::new() + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn gap_filler_inserts_silence_for_missing_packets() { + let mut gap = GapFiller::new(16_000); + + // First chunk arrives right on time: 100 ms of audio (1600 samples). + let first = vec![1.0f32; 1_600]; + let out1 = gap.fill(Duration::from_millis(100), &first); + assert_eq!(out1.len(), 1_600); + assert_eq!(gap.written(), 1_600); + + // Second chunk, another 1600 samples, arrives at elapsed = 1.0s - + // far later than expected, so the gap must be filled with silence. + let second = vec![1.0f32; 1_600]; + let out2 = gap.fill(Duration::from_secs(1), &second); + + // 16000 total expected by t=1.0s, minus what's already written (1600) + // minus the new chunk (1600) = 12800 zeros, then the chunk's data. + let expected_zeros = 16_000 - 1_600 - 1_600; + assert!( + out2.len() >= expected_zeros, + "expected at least {expected_zeros} samples before the new chunk's data, got {}", + out2.len() + ); + assert_eq!(&out2[out2.len() - 1_600..], &second[..]); + assert_eq!(gap.written(), 16_000); + } + + #[test] + fn gap_filler_no_padding_when_on_time() { + let mut gap = GapFiller::new(16_000); + + // 10 chunks of 20ms (320 samples) each, arriving with small jitter + // (+/- a few ms), never more than the 100ms tolerance. + let chunk = vec![0.1f32; 320]; + for i in 0..10u64 { + let nominal = Duration::from_millis((i + 1) * 20); + let jitter = Duration::from_millis(5); + let elapsed = nominal + jitter; + let out = gap.fill(elapsed, &chunk); + assert_eq!(out.len(), 320, "no silence should be inserted on chunk {i}"); + } + assert_eq!(gap.written(), 3_200); + } + + #[test] + fn gap_filler_never_drops_or_delays_when_ahead() { + let mut gap = GapFiller::new(16_000); + // A chunk that arrives "ahead" of its nominal elapsed time (e.g. the + // clock reports a smaller elapsed than samples already written). + let chunk = vec![0.2f32; 1_600]; + let out1 = gap.fill(Duration::from_millis(100), &chunk); + assert_eq!(out1.len(), 1_600); + + let out2 = gap.fill(Duration::from_millis(150), &chunk); + // Even though elapsed barely advanced, the whole chunk must still be + // written in full - never dropped or delayed. + assert_eq!(out2.len(), 1_600); + assert_eq!(gap.written(), 3_200); + } + + #[test] + fn gap_filler_pad_to_end() { + let mut gap = GapFiller::new(16_000); + let chunk = vec![0.3f32; 1_600]; + gap.fill(Duration::from_millis(100), &chunk); + + // Nothing lagging yet (written matches elapsed within tolerance). + assert!(gap.pad_to(Duration::from_millis(150)).is_empty()); + + // Recording stops at 2.0s with no more packets: pad_to should fill + // the remainder up to 32000 samples. + let tail = gap.pad_to(Duration::from_secs(2)); + assert_eq!(tail.len(), 32_000 - 1_600); + assert_eq!(gap.written(), 32_000); + + // Calling again at the same elapsed must not add more. + assert!(gap.pad_to(Duration::from_secs(2)).is_empty()); + } +} diff --git a/crates/core/src/audio/level.rs b/crates/core/src/audio/level.rs new file mode 100644 index 0000000..c79cf83 --- /dev/null +++ b/crates/core/src/audio/level.rs @@ -0,0 +1,42 @@ +//! RMS level metering, used for the live input meters in the UI. + +/// Computes the root-mean-square level of `samples`, normalized to `0.0..=1.0`. +/// +/// Returns `0.0` for an empty slice. The result is clamped to `[0.0, 1.0]` +/// in case of samples outside the nominal `-1.0..=1.0` range. +pub fn rms_level(samples: &[f32]) -> f32 { + if samples.is_empty() { + return 0.0; + } + let sum_sq: f64 = samples.iter().map(|&s| (s as f64) * (s as f64)).sum(); + let mean_sq = sum_sq / samples.len() as f64; + (mean_sq.sqrt() as f32).clamp(0.0, 1.0) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rms_level_bounds() { + let silence = vec![0.0f32; 1600]; + assert_eq!(rms_level(&silence), 0.0); + + let full_scale_square: Vec = (0..1600) + .map(|i| if i % 2 == 0 { 1.0 } else { -1.0 }) + .collect(); + let level = rms_level(&full_scale_square); + assert!((level - 1.0).abs() < 1e-6, "level = {level}"); + } + + #[test] + fn rms_level_empty_is_zero() { + assert_eq!(rms_level(&[]), 0.0); + } + + #[test] + fn rms_level_clamps_out_of_range_samples() { + let samples = vec![2.0f32; 10]; + assert_eq!(rms_level(&samples), 1.0); + } +} diff --git a/crates/core/src/audio/mod.rs b/crates/core/src/audio/mod.rs new file mode 100644 index 0000000..37aad24 --- /dev/null +++ b/crates/core/src/audio/mod.rs @@ -0,0 +1,20 @@ +//! Pure audio building blocks for the call recorder: conditioning, gap +//! filling, crash-safe WAV writing and FLAC encoding. +//! +//! These are plain, dependency-free-of-the-OS processing stages. Live capture +//! callbacks (any rate, 1-8 channels, arbitrary chunk sizes, irregular +//! timing) are fed through [`Conditioner`] -> [`GapFiller`] -> [`TrackWriter`] +//! per track by the capture layer (a later task); this module only implements +//! the building blocks themselves. + +mod conditioner; +mod flac; +mod gap; +mod level; +mod wav; + +pub use conditioner::Conditioner; +pub use flac::encode_flac; +pub use gap::GapFiller; +pub use level::rms_level; +pub use wav::{repair_wav, TrackWriter}; diff --git a/crates/core/src/audio/wav.rs b/crates/core/src/audio/wav.rs new file mode 100644 index 0000000..a859d54 --- /dev/null +++ b/crates/core/src/audio/wav.rs @@ -0,0 +1,270 @@ +//! Crash-safe WAV writer and header repair for mono 16 kHz 16-bit PCM tracks. +//! +//! Each track is written as a minimal 44-byte canonical RIFF/WAVE header +//! followed by raw little-endian `i16` samples. The header's size fields are +//! rewritten and the file is flushed at least every 16 000 samples (1 second), +//! so a process killed mid-recording leaves a file whose header understates +//! its true length by at most ~1 second. [`repair_wav`] recovers a correct +//! header for such a file purely from its length on disk. + +use std::fs::{File, OpenOptions}; +use std::io::{self, BufWriter, Read, Seek, SeekFrom, Write}; +use std::path::Path; +use std::time::Duration; + +/// Track sample rate written to disk. +pub const SAMPLE_RATE: u32 = 16_000; +const CHANNELS: u16 = 1; +const BITS_PER_SAMPLE: u16 = 16; +const BYTES_PER_SAMPLE: u32 = (BITS_PER_SAMPLE / 8) as u32; +const HEADER_LEN: u64 = 44; + +/// Rewrite the header (and flush) at least this often. +const FLUSH_INTERVAL_SAMPLES: u64 = 16_000; + +/// Writes mono 16 kHz 16-bit PCM audio to a WAV file, refreshing the header +/// periodically so the file stays playable even if the process is killed. +pub struct TrackWriter { + file: BufWriter, + samples_written: u64, + samples_since_header_update: u64, +} + +fn build_header(data_len_bytes: u32) -> [u8; 44] { + let mut header = [0u8; 44]; + header[0..4].copy_from_slice(b"RIFF"); + header[4..8].copy_from_slice(&(36 + data_len_bytes).to_le_bytes()); + header[8..12].copy_from_slice(b"WAVE"); + header[12..16].copy_from_slice(b"fmt "); + header[16..20].copy_from_slice(&16u32.to_le_bytes()); + header[20..22].copy_from_slice(&1u16.to_le_bytes()); // PCM + header[22..24].copy_from_slice(&CHANNELS.to_le_bytes()); + header[24..28].copy_from_slice(&SAMPLE_RATE.to_le_bytes()); + let byte_rate = SAMPLE_RATE * CHANNELS as u32 * BYTES_PER_SAMPLE; + header[28..32].copy_from_slice(&byte_rate.to_le_bytes()); + let block_align = CHANNELS * BYTES_PER_SAMPLE as u16; + header[32..34].copy_from_slice(&block_align.to_le_bytes()); + header[34..36].copy_from_slice(&BITS_PER_SAMPLE.to_le_bytes()); + header[36..40].copy_from_slice(b"data"); + header[40..44].copy_from_slice(&data_len_bytes.to_le_bytes()); + header +} + +fn duration_for_samples(samples: u64) -> Duration { + Duration::from_secs_f64(samples as f64 / SAMPLE_RATE as f64) +} + +impl TrackWriter { + /// Creates `path`, writing a placeholder (zero-length) header. + pub fn create(path: &Path) -> io::Result { + let file = File::create(path)?; + let mut writer = Self { + file: BufWriter::new(file), + samples_written: 0, + samples_since_header_update: 0, + }; + writer.file.write_all(&build_header(0))?; + Ok(writer) + } + + /// Clamps `samples` to `[-1.0, 1.0]`, converts them to 16-bit PCM and + /// appends them to the file. Rewrites the RIFF/data sizes and flushes to + /// disk at least every 16 000 samples. + pub fn write(&mut self, samples: &[f32]) -> io::Result<()> { + for &s in samples { + let clamped = s.clamp(-1.0, 1.0); + let v = (clamped * i16::MAX as f32).round() as i16; + self.file.write_all(&v.to_le_bytes())?; + } + self.samples_written += samples.len() as u64; + self.samples_since_header_update += samples.len() as u64; + + if self.samples_since_header_update >= FLUSH_INTERVAL_SAMPLES { + self.update_header_and_flush()?; + } + Ok(()) + } + + fn update_header_and_flush(&mut self) -> io::Result<()> { + let data_len_bytes = (self.samples_written * BYTES_PER_SAMPLE as u64) as u32; + self.file.seek(SeekFrom::Start(0))?; + self.file.write_all(&build_header(data_len_bytes))?; + self.file.seek(SeekFrom::End(0))?; + self.file.flush()?; + self.samples_since_header_update = 0; + Ok(()) + } + + /// Writes the final header sizes, flushes the file and returns the total + /// recorded duration. + pub fn finalize(mut self) -> io::Result { + self.update_header_and_flush()?; + Ok(duration_for_samples(self.samples_written)) + } +} + +/// Recomputes a WAV file's RIFF/data header sizes from its actual length on +/// disk, truncating a trailing odd byte if present. Returns the resulting +/// duration. +/// +/// Errors with [`io::ErrorKind::InvalidData`] if the file is shorter than the +/// 44-byte canonical header or doesn't start with a RIFF/WAVE header. +pub fn repair_wav(path: &Path) -> io::Result { + let mut file = OpenOptions::new().read(true).write(true).open(path)?; + let len = file.metadata()?.len(); + if len < HEADER_LEN { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "wav file shorter than the canonical 44-byte header", + )); + } + + let mut header = [0u8; 44]; + file.read_exact(&mut header)?; + if &header[0..4] != b"RIFF" || &header[8..12] != b"WAVE" { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "not a RIFF/WAVE file", + )); + } + + let mut data_len_bytes = len - HEADER_LEN; + if !data_len_bytes.is_multiple_of(2) { + data_len_bytes -= 1; + file.set_len(HEADER_LEN + data_len_bytes)?; + } + let data_len_bytes_u32 = data_len_bytes as u32; + + file.seek(SeekFrom::Start(0))?; + file.write_all(&build_header(data_len_bytes_u32))?; + file.flush()?; + + Ok(duration_for_samples( + data_len_bytes / BYTES_PER_SAMPLE as u64, + )) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn wav_roundtrip_duration() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("track.wav"); + + let mut writer = TrackWriter::create(&path).unwrap(); + // 2 seconds of silence at 16 kHz. + let samples = vec![0.0f32; SAMPLE_RATE as usize * 2]; + writer.write(&samples).unwrap(); + let duration = writer.finalize().unwrap(); + + assert!((duration.as_secs_f64() - 2.0).abs() < 1e-6); + + let reader = hound::WavReader::open(&path).unwrap(); + let spec = reader.spec(); + assert_eq!(spec.sample_rate, SAMPLE_RATE); + assert_eq!(spec.channels, 1); + assert_eq!(spec.bits_per_sample, 16); + + let data_size = std::fs::metadata(&path).unwrap().len() - HEADER_LEN; + assert_eq!(data_size, 32_000 * 2); + } + + #[test] + fn writer_header_is_refreshed_periodically() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("track.wav"); + + let mut writer = TrackWriter::create(&path).unwrap(); + // Just over one flush interval (16 000 samples). + writer.write(&vec![0.0f32; 16_100]).unwrap(); + + // Without calling finalize, the header on disk should already reflect + // (at least) the first 16 000 samples, because `write` flushed. + let bytes = std::fs::read(&path).unwrap(); + let header_data_size = u32::from_le_bytes(bytes[40..44].try_into().unwrap()); + assert!(header_data_size >= 16_000 * 2); + + writer.finalize().unwrap(); + } + + #[test] + fn repair_truncated_wav() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("crashed.wav"); + + { + let mut writer = TrackWriter::create(&path).unwrap(); + // 1.5 seconds at 16 kHz. + let samples = vec![0.1f32; (SAMPLE_RATE as f64 * 1.5) as usize]; + writer.write(&samples).unwrap(); + // Simulate a crash: drop without calling finalize. + } + + // Corrupt the size fields to simulate a header a crash never updated. + { + let mut file = OpenOptions::new().write(true).open(&path).unwrap(); + file.seek(SeekFrom::Start(4)).unwrap(); + file.write_all(&0u32.to_le_bytes()).unwrap(); + file.seek(SeekFrom::Start(40)).unwrap(); + file.write_all(&0u32.to_le_bytes()).unwrap(); + } + + let duration = repair_wav(&path).unwrap(); + assert!( + (duration.as_secs_f64() - 1.5).abs() < 0.1, + "duration = {:?}", + duration + ); + + let reader = hound::WavReader::open(&path).unwrap(); + let spec = reader.spec(); + assert_eq!(spec.sample_rate, SAMPLE_RATE); + let count = reader.into_samples::().count(); + assert!((count as f64 / SAMPLE_RATE as f64 - 1.5).abs() < 0.1); + } + + #[test] + fn repair_wav_rejects_short_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("tiny.wav"); + std::fs::write(&path, b"RIFF").unwrap(); + + let err = repair_wav(&path).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::InvalidData); + } + + #[test] + fn repair_wav_rejects_non_wave_file() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("not-a-wav.bin"); + std::fs::write(&path, vec![0u8; 100]).unwrap(); + + let err = repair_wav(&path).unwrap_err(); + assert_eq!(err.kind(), io::ErrorKind::InvalidData); + } + + #[test] + fn repair_wav_truncates_trailing_odd_byte() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("odd.wav"); + + { + let mut writer = TrackWriter::create(&path).unwrap(); + writer.write(&vec![0.2f32; 100]).unwrap(); + writer.finalize().unwrap(); + } + // Append a stray trailing byte. + { + let mut file = OpenOptions::new().append(true).open(&path).unwrap(); + file.write_all(&[0xAB]).unwrap(); + } + + let before = std::fs::metadata(&path).unwrap().len(); + repair_wav(&path).unwrap(); + let after = std::fs::metadata(&path).unwrap().len(); + assert_eq!(after, before - 1); + assert_eq!((after - HEADER_LEN) % 2, 0); + } +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index f083447..c23a111 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -4,6 +4,7 @@ //! transcription and summarization logic, kept independent of the Tauri //! application shell so it can be unit-tested without a GUI or network. +pub mod audio; pub mod model; pub mod retention; pub mod store;