hex/src/transcription/pieces.rs
Dax cdb96f585e
capture: keep all audio in long captures (#1795) (#1894)
* capture: stream audio to disk so long captures keep all of it

A capture's timeline now writes 16 kHz mono PCM16 to its bundle's
audio.wav as it records, with the leading offset and gaps as silence, so
memory stays flat and the 20-minute drop is gone. An 8-hour guard bounds
disk use only; past it the rest is one recorded gap.

A finished capture is transcribed from the WAV in 30 s pieces, the
reader hex transcribe already used (lifted to transcription/pieces.rs),
and each piece's words move onto the capture clock. The Parakeet, Apple
(path-based), and GGUF paths keep every word without holding the
session. The pipeline never rewrites a streamed audio.wav from empty
samples, an iPhone narration is read back where it must be re-based,
and a cancelled capture closes and removes its file.

* capture: say where a capture's audio stopped

A streamed capture keeps all of its audio, but the 8-hour disk guard (or
a failed audio write) still stops it early, and only the process log
said so. The timeline now carries the stop point onto RecordedAudio,
the bundle stores it as audioStoppedAtMs (serde default, so old bundles
load unchanged), and three places say it plainly: summary.md, `hex
capture show`, and `hex capture status`.

The guard records its point at a 1 s test limit, and the summary line is
tested at 12:34 and 8:00:00 so both clock shapes are covered.

* capture: fill the replay's stop point from its bundle

The 15 s replay builds Written too, on macOS only, so the new field
broke the macOS clippy job while Linux never compiled that function.

* capture: keep a recording out of recovery until it stops

Review fixes for the streamed timeline.

A capture now streams to `audio.wav.partial` and renames it to `audio.wav`
in `Timeline::finish`, so a bundle with a movie and an in-progress audio
file is never mistaken for a capture whose build never ran (recover's
orphan rule, Scrawl.app's `scrawl recover`) and retention cleans a crashed
one like any other bundle. Cancel and discard remove the partial; a failed
publish reads it back into memory rather than pointing at a missing file.

Also: a write failure's stop point is the last sample that reached the
file, not the end of the failed chunk; a trim failure counts every written
sample; the capture's words keep one running frame total instead of
adding truncated per-piece millis; an iPhone narration's `since` drains the
decoded samples instead of copying them and treats a missing file as no
audio; the phone and replay paths carry the audio stop point into their
bundles; and empty audio answers `source: "none"`, as `transcribe_with`
does.

Tests: a movie plus partial audio is never owed, retention trashes a
crashed bundle's media, a write failure records its stop point, a missing
streamed file shifts to no samples, and the replay shift moves the stop
point with the clock.

* capture: publish audio.wav only once its build journals the capture

Follow-ups from review.

A capture now streams to audio.wav.partial and keeps that name after it
stops: the partial is renamed to audio.wav right after the build writes
unfinished.json, so audio.wav never exists without a journal, and a
stopped capture waiting in the finish queue no longer looks like an
orphan to Scrawl.app's recover. A partial that cannot take the name is
read back and written by the build, and its partial name removed; the
replay removes the unshifted partial once the narration is read for
shifting.

Also: a periodic header flush error is now a write failure (the rest of
the capture is a gap) rather than a log line; the midpoint of a failed
write records where the file stopped; and the words test cuts pieces at
odd sample counts and computes offsets from the running frame total, so
per-piece millisecond drift would fail it.

Tests: a streamed audio takes its final name only when the build settles
it (and falls back when it cannot), a capture that has not been
journaled is never owed a rebuild, a mid-capture write failure stops
where the file stopped, and the word offsets are exact at odd piece
lengths.

* capture: consume a streamed recording's partial when it is read

Follow-ups from review.

`RecordedAudio::since` and `into_memory` now remove the partial once its
samples are read: an iPhone narration (capture.rs's `host_narration`
path) used to leave the whole streamed file in the finished bundle. The
replay relies on the same path instead of removing it by hand, and the
light zip leaves `audio.wav.partial` out defensively. A read that fails
is an error to `into_memory` (so a build keeps the only copy) while
`since` still treats a missing file as no audio for an iPhone.

A failed publish now reads the audio back only when it produced the
samples the recording had; a failed read keeps the partial and removes
any half-copied `audio.wav`. `publish_audio` takes `&mut self` and points
the recording at the published file.

A journaled bundle whose `audio.wav` is missing adopts `audio.wav.partial`
before rebuilding. Docs say the guarantee is a streamed capture's, and
that a crash while recording or queued is retention's job, not
recovery's.

Tests: reading a streamed recording consumes the partial; a finished
iPhone bundle keeps none; a streamed audio is published only when its
build settles it, including the read-back and unreadable-partial cases; a
journaled capture adopts its partial.

* capture: keep a failed audio publish recoverable

Follow-ups from review.

The publish fallback read the partial with `into_memory`, which deletes
it, and then ignored whether its own write landed: a failed write left
the audio nowhere. It now reads through `read_streamed` (no deletion),
writes `audio.wav` and checks the result, and removes the partial only
once that write succeeded. A failed read or write removes any half
`audio.wav` and fails the build, which is the pipeline's existing failed
path: `recover::finished` runs only for an `Ok` build, so `unfinished.json`
stays and the next launch's recovery adopts the partial instead of the
capture quietly finishing without its audio.

`publish_audio` also treats a landed copy as success even when deleting
the partial fails, logging it rather than reporting failure and letting
the fallback overwrite a good `audio.wav`.

Tests: a blocked target keeps the partial and fails the build; a
fallback write that lands consumes the partial and writes `audio.wav`;
an unreadable partial is kept.
2026-11-16 10:28:54 -05:00

750 lines
26 KiB
Rust

//! A saved recording read as pieces: streaming decode, downmix, 16 kHz
//! resampling, and the quiet-point cut the GGUF adapters use for a long clip,
//! so a caller holds about one 30 s piece instead of the whole file. `hex
//! transcribe` and a finished capture's transcription share it.
//!
//! WAV reads through Hound, as the local API reads an upload; every other
//! container (MP4/M4A/MOV, CAF, AIFF, MP3, FLAC, Ogg, WebM/Matroska) through
//! Symphonia, with Opus through `opuscule`.
mod media;
use std::collections::VecDeque;
use std::fs::File;
use std::io::{BufReader, Cursor};
use std::path::Path;
use hound::{Sample, SampleFormat, WavReader, WavSpec, WavWriter};
use crate::dictation::StreamingResampler;
use crate::transcription::gguf_session::quiet_cut;
use crate::transcription::service::{AudioError, WavFormat, validate_wav_input, wav_format};
/// 30 s at 16 kHz: the longest piece, the longest clip the local API runs
/// on the running Hex's dictation engine's loaded copy when it holds the
/// model, instead of its service loading a second copy.
pub(crate) const PIECE_SAMPLES: usize = crate::transcription::service::SHARED_MAX_SAMPLES;
/// Frames decoded per WAV read: 85 ms at 48 kHz, so a piece is cut near its
/// end without holding much more than one piece.
const BLOCK_FRAMES: usize = 4_096;
/// Why a recording could not be read into pieces.
#[derive(Debug)]
pub(crate) enum PieceError {
/// The file could not be opened or read at all.
Io(std::io::Error),
/// A WAV whose rate or channels this build does not take.
Audio(AudioError),
/// Not a container Hex reads.
NotMedia,
/// A container with no audio track, such as a silent screen recording.
NoAudioTrack,
/// An audio track in a codec Hex can't decode, named.
UnsupportedCodec(String),
/// The file holds no audio at all.
Empty,
/// A WAV header was accepted but its samples could not all be read: the
/// file is truncated or corrupt.
Incomplete,
/// A WAV could not be read after the up-front read accepted it: it
/// changed, or could no longer be read, while Hex was transcribing it.
Changed { at_ms: u64 },
/// Compressed audio stopped decoding partway through.
Damaged { at_ms: u64 },
}
/// The 16 kHz samples of one recording, one piece at a time.
pub(crate) struct Pieces {
source: Source,
/// `None` once the input ended and its tail was flushed.
resampler: Option<StreamingResampler>,
buffer: PieceBuffer,
ready: VecDeque<Vec<f32>>,
/// The first decoded block, read while opening to prove the audio
/// decodes; it is resampled first.
first: Option<Vec<f32>>,
done: bool,
/// Source frames read so far, for where a read failed.
frames: u64,
rate: u32,
duration_ms: Option<u64>,
}
enum Source {
Wav {
reader: WavReader<BufReader<File>>,
format: WavFormat,
channels: usize,
bits: u16,
},
Media(media::Media),
}
impl std::fmt::Debug for Pieces {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter
.debug_struct("Pieces")
.field("wav", &matches!(self.source, Source::Wav { .. }))
.field("rate", &self.rate)
.field("done", &self.done)
.finish_non_exhaustive()
}
}
impl Pieces {
/// Opens `path`, picks its audio, and decodes its first block, so a file
/// Hex cannot read is refused before any model is loaded, downloaded, or
/// Hex is asked.
pub(crate) fn open(path: &Path) -> Result<Self, PieceError> {
let file = File::open(path).map_err(PieceError::Io)?;
if let Ok(reader) = WavReader::new(BufReader::new(file)) {
return Self::wav(reader);
}
let file = File::open(path).map_err(PieceError::Io)?;
let mut media = media::Media::open(file, path)?;
let first = media.read()?;
let Some(rate) = media.rate().filter(|_| !first.is_empty()) else {
return Err(PieceError::Empty);
};
let duration_ms = media.duration_ms();
let resampler = StreamingResampler::new(rate)
.map_err(|_| PieceError::UnsupportedCodec(format!("audio at {rate} Hz")))?;
Ok(Self {
frames: first.len() as u64,
source: Source::Media(media),
resampler: Some(resampler),
buffer: PieceBuffer::default(),
ready: VecDeque::new(),
first: Some(first),
done: false,
rate,
duration_ms,
})
}
fn wav(reader: WavReader<BufReader<File>>) -> Result<Self, PieceError> {
let spec = reader.spec();
validate_wav_input(&spec).map_err(PieceError::Audio)?;
let format = wav_format(&spec).map_err(PieceError::Audio)?;
if reader.duration() == 0 {
return Err(PieceError::Empty);
}
let resampler = StreamingResampler::new(spec.sample_rate)
.map_err(|_| PieceError::Audio(AudioError::Unsupported))?;
Ok(Self {
duration_ms: Some(u64::from(reader.duration()) * 1_000 / u64::from(spec.sample_rate)),
source: Source::Wav {
reader,
format,
channels: usize::from(spec.channels),
bits: spec.bits_per_sample,
},
resampler: Some(resampler),
buffer: PieceBuffer::default(),
ready: VecDeque::new(),
first: None,
done: false,
frames: 0,
rate: spec.sample_rate,
})
}
/// A WAV's every sample read once without keeping any, then rewound, so
/// a truncated or corrupt file is refused before any transcription rather
/// than after minutes of it. Compressed audio is decoded only once, as it
/// is transcribed: damage found later names where it is.
pub(crate) fn validate(&mut self) -> Result<(), PieceError> {
if !matches!(self.source, Source::Wav { .. }) {
return Ok(());
}
loop {
match self.read_wav() {
Ok(block) if block.is_empty() => break,
Ok(_) => {}
Err(_) => return Err(PieceError::Incomplete),
}
}
if let Source::Wav { reader, .. } = &mut self.source {
reader.seek(0).map_err(|_| PieceError::Incomplete)?;
}
Ok(())
}
/// The recording's length, when its header says.
pub(crate) fn duration_ms(&self) -> Option<u64> {
self.duration_ms
}
/// How far into the recording reading has got, in milliseconds.
pub(crate) fn position_ms(&self) -> u64 {
self.frames * 1_000 / u64::from(self.rate.max(1))
}
/// The next piece of at most [`PIECE_SAMPLES`] samples, or `None` at the
/// end of the file.
pub(crate) fn next(&mut self) -> Result<Option<Vec<f32>>, PieceError> {
loop {
if let Some(piece) = self.ready.pop_front() {
return Ok(Some(piece));
}
if self.done {
return Ok(self.buffer.take_rest());
}
self.fill()?;
}
}
/// Decodes one block and queues the pieces it completed. The end of the
/// file flushes the resampler's tail and then the last piece.
fn fill(&mut self) -> Result<(), PieceError> {
let block = match self.first.take() {
Some(block) => block,
None => self.read_block()?,
};
if block.is_empty() {
self.done = true;
let tail = self
.resampler
.take()
.map(StreamingResampler::finish)
.unwrap_or_default();
self.ready.extend(self.buffer.push(&tail));
return Ok(());
}
let resampled = match self.resampler.as_mut() {
Some(resampler) => resampler.push(&block),
None => block,
};
self.ready.extend(self.buffer.push(&resampled));
Ok(())
}
fn read_block(&mut self) -> Result<Vec<f32>, PieceError> {
let at_ms = self.position_ms();
let block = match &mut self.source {
Source::Media(media) => media.read().map_err(|error| match error {
PieceError::Damaged { .. } => PieceError::Damaged { at_ms },
error => error,
})?,
// `validate` read the whole WAV before this run started, so a
// failure here means it changed underneath.
Source::Wav { .. } => self.read_wav().map_err(|_| PieceError::Changed { at_ms })?,
};
self.frames += block.len() as u64;
Ok(block)
}
/// One block of mono WAV samples, before resampling.
fn read_wav(&mut self) -> Result<Vec<f32>, PieceError> {
let Source::Wav {
reader,
format,
channels,
bits,
} = &mut self.source
else {
return Ok(Vec::new());
};
read_frames(reader, *channels, BLOCK_FRAMES, *format, *bits)
}
}
/// 16 kHz samples waiting to be cut, never more than one piece plus the block
/// that completed it.
#[derive(Default)]
pub(super) struct PieceBuffer {
samples: Vec<f32>,
}
impl PieceBuffer {
/// Appends `samples` and returns every piece they completed, each cut
/// through the quietest 100 ms near its 30 s end as
/// [`crate::transcription::gguf_session::chunk_ranges`] cuts a long clip.
pub(super) fn push(&mut self, samples: &[f32]) -> Vec<Vec<f32>> {
self.samples.extend_from_slice(samples);
let mut pieces = Vec::new();
while self.samples.len() > PIECE_SAMPLES {
let rest = self
.samples
.split_off(quiet_cut(&self.samples, PIECE_SAMPLES));
pieces.push(std::mem::replace(&mut self.samples, rest));
}
pieces
}
/// The samples after the last piece, as the final piece, unless there are
/// none.
pub(super) fn take_rest(&mut self) -> Option<Vec<f32>> {
(!self.samples.is_empty()).then(|| std::mem::take(&mut self.samples))
}
}
/// `samples` as a 16 kHz mono 32-bit float WAV: what the local API takes for
/// one piece, with no sample converted or dropped.
pub(crate) fn wav_bytes(samples: &[f32]) -> Result<Vec<u8>, PieceError> {
let mut bytes = Cursor::new(Vec::new());
{
let spec = WavSpec {
channels: 1,
sample_rate: 16_000,
bits_per_sample: 32,
sample_format: SampleFormat::Float,
};
let mut writer = WavWriter::new(&mut bytes, spec).map_err(broken)?;
for sample in samples {
writer.write_sample(*sample).map_err(broken)?;
}
writer.finalize().map_err(broken)?;
}
Ok(bytes.into_inner())
}
fn broken(_: hound::Error) -> PieceError {
PieceError::Audio(AudioError::Invalid)
}
/// Up to `frames` mono samples, averaging `channels` and converting them as
/// the file's format says, exactly as [`crate::transcription::service::AudioClip`]
/// converts a whole file. The last read may be short; a broken one is
/// `Invalid`.
fn read_frames(
reader: &mut WavReader<BufReader<File>>,
channels: usize,
frames: usize,
format: WavFormat,
bits: u16,
) -> Result<Vec<f32>, PieceError> {
let scale = 2_f32.powi(i32::from(bits) - 1);
match format {
WavFormat::F32 => read_mono(reader, channels, frames, |sample: f32| sample),
WavFormat::I8 => read_mono(reader, channels, frames, |sample: i8| {
f32::from(sample) / 128.0
}),
WavFormat::I16 => read_mono(reader, channels, frames, move |sample: i16| {
f32::from(sample) / scale
}),
WavFormat::I32 => read_mono(reader, channels, frames, move |sample: i32| {
sample as f32 / scale
}),
}
}
fn read_mono<S: Sample>(
reader: &mut WavReader<BufReader<File>>,
channels: usize,
frames: usize,
convert: impl Fn(S) -> f32,
) -> Result<Vec<f32>, PieceError> {
let mut mono = Vec::with_capacity(frames);
let mut samples = reader.samples::<S>();
for _ in 0..frames {
// A whole frame or nothing: hound rejects a data chunk that is not a
// multiple of the channels, so a missing sample is a broken file.
let Some(first) = samples.next() else {
return Ok(mono);
};
let first = convert(first.map_err(broken)?);
if !first.is_finite() {
return Err(PieceError::Audio(AudioError::Invalid));
}
let mut sum = f64::from(first);
for _ in 1..channels {
let Some(sample) = samples.next() else {
return Err(PieceError::Audio(AudioError::Invalid));
};
let sample = convert(sample.map_err(broken)?);
if !sample.is_finite() {
return Err(PieceError::Audio(AudioError::Invalid));
}
sum += f64::from(sample);
}
let average = (sum / channels as f64) as f32;
if !average.is_finite() {
return Err(PieceError::Audio(AudioError::Invalid));
}
mono.push(average);
}
Ok(mono)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::transcription::service::AudioClip;
/// A WAV of `seconds` with one second of silence every five, so a cut has
/// somewhere quiet to land.
fn paced(sample_rate: u32, seconds: usize) -> Vec<f32> {
(0..sample_rate as usize * seconds)
.map(|sample| {
if sample / sample_rate as usize % 5 == 4 {
0.0
} else {
0.25
}
})
.collect()
}
fn write(path: &Path, spec: WavSpec, samples: &[f32]) {
let mut writer = WavWriter::create(path, spec).unwrap();
for sample in samples {
match spec.sample_format {
SampleFormat::Float => writer.write_sample(*sample).unwrap(),
SampleFormat::Int if spec.bits_per_sample <= 8 => {
writer.write_sample((sample * 127.0) as i8).unwrap();
}
SampleFormat::Int if spec.bits_per_sample <= 16 => {
writer.write_sample((sample * 32_767.0) as i16).unwrap();
}
SampleFormat::Int if spec.bits_per_sample <= 24 => {
writer.write_sample((sample * 8_388_607.0) as i32).unwrap();
}
SampleFormat::Int => {
writer
.write_sample((sample * 2_147_483_647.0) as i32)
.unwrap();
}
}
}
writer.finalize().unwrap();
}
/// A varying signal, so each sample format's quantization is exercised.
fn wave(sample_rate: u32, seconds: f32) -> Vec<f32> {
(0..(sample_rate as f32 * seconds) as usize)
.map(|index| (index as f32 * 0.01).sin() * 0.6)
.collect()
}
/// A temp WAV path unique to this module: tests in other modules run in
/// parallel in one process and share `TMPDIR`.
fn temp(name: &str) -> std::path::PathBuf {
std::env::temp_dir().join(format!(
"hex-transcribe-pieces-{name}-{}.wav",
std::process::id()
))
}
fn collect(path: &Path) -> Vec<Vec<f32>> {
let mut pieces = Pieces::open(path).unwrap();
let mut collected = Vec::new();
while let Some(piece) = pieces.next().unwrap() {
collected.push(piece);
}
collected
}
#[test]
fn pieces_cover_every_sample_exactly_once_in_order() {
let samples = paced(16_000, 95);
let mut buffer = PieceBuffer::default();
let mut pieces = Vec::new();
for block in samples.chunks(777) {
pieces.extend(buffer.push(block));
}
pieces.extend(buffer.take_rest());
assert!(pieces.len() > 1, "95 s is more than one piece");
assert!(
pieces.iter().all(|piece| !piece.is_empty()),
"no piece is empty"
);
assert!(
pieces.iter().all(|piece| piece.len() <= PIECE_SAMPLES),
"every piece is at most 30 s"
);
assert_eq!(
pieces.concat(),
samples,
"every sample appears once, in order"
);
}
#[test]
fn a_piece_of_exactly_thirty_seconds_is_not_cut() {
let samples = vec![0.25_f32; PIECE_SAMPLES];
let mut buffer = PieceBuffer::default();
assert!(buffer.push(&samples).is_empty());
assert_eq!(buffer.take_rest().unwrap().len(), PIECE_SAMPLES);
}
#[test]
fn a_48_khz_file_is_read_in_pieces_of_at_most_thirty_seconds() {
let path = temp("48k");
let spec = WavSpec {
channels: 1,
sample_rate: 48_000,
bits_per_sample: 16,
sample_format: SampleFormat::Int,
};
write(&path, spec, &paced(48_000, 70));
let pieces = collect(&path);
let whole = AudioClip::decode_wav(BufReader::new(File::open(&path).unwrap())).unwrap();
let _ = std::fs::remove_file(&path);
assert!(pieces.len() > 1, "70 s is more than one piece");
assert!(
pieces.iter().all(|piece| piece.len() <= PIECE_SAMPLES),
"every piece is at most 30 s of 16 kHz audio"
);
assert_eq!(
pieces.iter().map(Vec::len).sum::<usize>(),
70 * 16_000,
"the whole 48 kHz file is resampled, not truncated"
);
let streamed = pieces.concat();
assert_eq!(streamed.len(), whole.samples.len());
assert!(
streamed
.iter()
.zip(&whole.samples)
.all(|(streamed, whole)| (streamed - whole).abs() <= 1e-5),
"every piece's samples match the whole-file decoder"
);
}
#[test]
fn every_supported_sample_format_matches_the_whole_file_decoder() {
for (name, bits, sample_format) in [
("i8", 8, SampleFormat::Int),
("i16", 16, SampleFormat::Int),
("i24", 24, SampleFormat::Int),
("i32", 32, SampleFormat::Int),
("f32", 32, SampleFormat::Float),
] {
let path = temp(name);
let spec = WavSpec {
channels: 1,
sample_rate: 48_000,
bits_per_sample: bits,
sample_format,
};
write(&path, spec, &wave(48_000, 2.7));
let pieces = collect(&path);
let whole = AudioClip::decode_wav(BufReader::new(File::open(&path).unwrap())).unwrap();
let _ = std::fs::remove_file(&path);
assert_eq!(whole.duration_ms, 2_700, "{name}");
assert!(
pieces.iter().all(|piece| piece.len() <= PIECE_SAMPLES),
"{name}: every piece is at most 30 s"
);
assert_eq!(pieces.concat(), whole.samples, "{name}: same samples");
}
}
#[test]
fn streamed_samples_match_the_whole_file_decoder() {
let path = temp("match");
let spec = WavSpec {
channels: 2,
sample_rate: 48_000,
bits_per_sample: 16,
sample_format: SampleFormat::Int,
};
let left = paced(48_000, 7);
let mut stereo = Vec::with_capacity(left.len() * 2);
for (index, sample) in left.iter().enumerate() {
stereo.push(*sample);
stereo.push(if index % 3 == 0 { -*sample } else { *sample });
}
write(&path, spec, &stereo);
let pieces = collect(&path);
let whole = AudioClip::decode_wav(BufReader::new(File::open(&path).unwrap())).unwrap();
let _ = std::fs::remove_file(&path);
assert_eq!(pieces.concat(), whole.samples);
assert_eq!(whole.duration_ms, 7_000);
}
#[test]
fn a_file_with_no_frames_is_refused() {
let path = temp("empty");
write(
&path,
WavSpec {
channels: 1,
sample_rate: 16_000,
bits_per_sample: 16,
sample_format: SampleFormat::Int,
},
&[],
);
let error = Pieces::open(&path).unwrap_err();
let _ = std::fs::remove_file(&path);
assert!(matches!(error, PieceError::Empty), "{error:?}");
}
#[test]
fn a_truncated_file_is_refused_by_the_up_front_read() {
let path = temp("truncated");
write(
&path,
WavSpec {
channels: 1,
sample_rate: 16_000,
bits_per_sample: 16,
sample_format: SampleFormat::Int,
},
&paced(16_000, 3),
);
let bytes = std::fs::read(&path).unwrap();
std::fs::write(&path, &bytes[..bytes.len() / 2]).unwrap();
// The header still promises the whole recording, so opening succeeds;
// the up-front read is what finds the missing audio.
let mut pieces = Pieces::open(&path).unwrap();
let error = pieces.validate().unwrap_err();
let _ = std::fs::remove_file(&path);
assert!(matches!(error, PieceError::Incomplete), "{error:?}");
}
#[test]
fn a_file_that_changes_mid_read_reports_where_it_stopped() {
let path = temp("changed");
write(
&path,
WavSpec {
channels: 1,
sample_rate: 16_000,
bits_per_sample: 16,
sample_format: SampleFormat::Int,
},
&paced(16_000, 40),
);
let mut pieces = Pieces::open(&path).unwrap();
assert!(pieces.next().unwrap().is_some(), "the first piece reads");
let bytes = std::fs::read(&path).unwrap();
std::fs::write(&path, &bytes[..bytes.len() / 3]).unwrap();
let error = loop {
match pieces.next() {
Ok(Some(_)) => continue,
Ok(None) => panic!("expected the shortened file to fail"),
Err(error) => break error,
}
};
let _ = std::fs::remove_file(&path);
assert!(
matches!(error, PieceError::Changed { at_ms } if (29_000..31_000).contains(&at_ms)),
"{error:?}"
);
}
#[test]
fn a_file_that_is_not_media_is_refused() {
let path = temp("not-wav");
std::fs::write(&path, b"this is not a wav").unwrap();
let error = Pieces::open(&path).unwrap_err();
let _ = std::fs::remove_file(&path);
assert!(matches!(error, PieceError::NotMedia), "{error:?}");
}
fn fixture(name: &str) -> std::path::PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR"))
.join("tests/fixtures/media")
.join(name)
}
/// Each fixture is half a second of a 440 Hz sine, with video beside it
/// in `sine-video.mp4`.
#[test]
fn every_container_hex_reads_decodes_to_its_sixteen_kilohertz_audio() {
for name in [
"sine-aac.m4a",
"sine-aac.caf",
"sine-alac.caf",
"sine.aiff",
"sine.flac",
"sine.mp3",
"sine-opus.ogg",
"sine-opus.webm",
"sine-vorbis.ogg",
"sine-video.mp4",
] {
let mut pieces =
Pieces::open(&fixture(name)).unwrap_or_else(|error| panic!("{name}: {error:?}"));
pieces.validate().unwrap();
let mut samples = Vec::new();
while let Some(piece) = pieces
.next()
.unwrap_or_else(|error| panic!("{name}: {error:?}"))
{
samples.extend(piece);
}
let seconds = samples.len() as f64 / 16_000.0;
// MP3 and AAC frames pad the end a little; CAF doesn't say how
// much AAC priming to drop (2,112 frames, 0.13 s at 16 kHz), so
// it stays as leading silence.
let slack = if name == "sine-aac.caf" { 0.2 } else { 0.08 };
assert!((seconds - 0.5).abs() < slack, "{name}: {seconds} s");
let middle = &samples[samples.len() / 4..samples.len() * 3 / 4];
let rms = (middle
.iter()
.map(|&sample| f64::from(sample).powi(2))
.sum::<f64>()
/ middle.len() as f64)
.sqrt();
assert!(
(0.04..0.6).contains(&rms),
"{name}: a sine, not silence (rms {rms})"
);
}
}
#[test]
fn ogg_opus_drops_its_pre_skip_and_ends_where_its_container_says() {
let mut pieces = Pieces::open(&fixture("sine-opus.ogg")).unwrap();
let mut samples = 0;
while let Some(piece) = pieces.next().unwrap() {
samples += piece.len();
}
assert_eq!(samples, 8_000, "24,000 frames at 48 kHz, each kept once");
}
#[test]
fn a_video_with_no_audio_track_says_so() {
let error = Pieces::open(&fixture("rgbw.mp4")).unwrap_err();
assert!(matches!(error, PieceError::NoAudioTrack), "{error:?}");
}
#[test]
fn a_compressed_file_cut_short_reads_what_it_holds_or_names_the_damage() {
let bytes = std::fs::read(fixture("sine.mp3")).unwrap();
let path = temp("cut.mp3");
std::fs::write(&path, &bytes[..bytes.len() / 2]).unwrap();
let mut pieces = Pieces::open(&path).unwrap();
let mut result = Ok(0);
loop {
match pieces.next() {
Ok(Some(piece)) => *result.as_mut().unwrap() += piece.len(),
Ok(None) => break,
Err(error) => {
result = Err(error);
break;
}
}
}
let _ = std::fs::remove_file(&path);
match result {
Ok(samples) => assert!(samples < 6_000, "only what was kept: {samples}"),
Err(error) => assert!(matches!(error, PieceError::Damaged { .. }), "{error:?}"),
}
}
#[test]
fn a_missing_file_keeps_its_own_error() {
let error = Pieces::open(Path::new("/nonexistent/hex-transcribe.wav")).unwrap_err();
assert!(matches!(error, PieceError::Io(_)), "{error:?}");
}
#[test]
fn a_piece_is_a_wav_the_whole_file_decoder_reads_back() {
let samples = paced(16_000, 1);
let bytes = wav_bytes(&samples).unwrap();
let clip = AudioClip::decode_wav(Cursor::new(bytes)).unwrap();
assert_eq!(clip.duration_ms, 1_000);
assert_eq!(clip.samples, samples);
}
}