hex/src/transcription/pieces/media.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

392 lines
14 KiB
Rust

//! Compressed and non-WAV audio through Symphonia: the container's audio
//! track decoded a packet at a time and averaged to mono at its own rate.
//! Opus, which Symphonia 0.5 has no decoder for, goes through `opuscule`
//! (as Rune's foreign-audio player decodes it).
use std::fs::File;
use std::path::Path;
use symphonia::core::audio::SampleBuffer;
use symphonia::core::codecs::{
CODEC_TYPE_AAC, CODEC_TYPE_NULL, CODEC_TYPE_OPUS, CodecParameters, DecoderOptions,
};
use symphonia::core::errors::Error;
use symphonia::core::formats::{FormatOptions, FormatReader};
use symphonia::core::io::MediaSourceStream;
use symphonia::core::meta::MetadataOptions;
use symphonia::core::probe::Hint;
use super::PieceError;
/// Damaged packets skipped in a row before the file counts as damaged; a
/// player skips one, but a run of them is not audio worth transcribing.
const MAX_SKIPPED_PACKETS: usize = 16;
pub(super) struct Media {
format: Box<dyn FormatReader>,
track: u32,
decoder: Decoder,
/// The decoded rate, once the first packet decodes.
rate: Option<u32>,
/// Frames the container says the track holds, for the duration.
n_frames: Option<u64>,
/// Frames still to hand out when the container says where the audio
/// ends (Ogg Opus, whose last granule counts the pre-skip).
remaining: Option<u64>,
}
enum Decoder {
Symphonia {
decoder: Box<dyn symphonia::core::codecs::Decoder>,
buffer: Option<SampleBuffer<f32>>,
},
Opus(Box<Opus>),
}
impl Media {
/// Probes the container and picks its audio: the default track when it
/// decodes, else the first that does. Video and other tracks are
/// skipped.
pub(super) fn open(file: File, path: &Path) -> Result<Self, PieceError> {
let ogg = {
use std::io::Read as _;
let mut magic = [0_u8; 4];
(&file).read_exact(&mut magic).is_ok() && &magic == b"OggS"
};
let file = {
use std::io::Seek as _;
let mut file = file;
file.rewind().map_err(PieceError::Io)?;
file
};
let mut hint = Hint::new();
if let Some(extension) = path.extension().and_then(|extension| extension.to_str()) {
hint.with_extension(extension);
}
let stream = MediaSourceStream::new(Box::new(file), Default::default());
let options = FormatOptions {
enable_gapless: true,
..Default::default()
};
let format = match symphonia::default::get_probe().format(
&hint,
stream,
&options,
&MetadataOptions::default(),
) {
Ok(probed) => probed.format,
Err(Error::IoError(error)) if error.kind() != std::io::ErrorKind::UnexpectedEof => {
return Err(PieceError::Io(error));
}
Err(_) => return Err(PieceError::NotMedia),
};
let (track, params, decoder) = choose(format.as_ref())?;
let remaining = match (&decoder, params.n_frames, ogg) {
(Decoder::Opus(opus), Some(end), true) => Some(end.saturating_sub(opus.pre_skip)),
_ => None,
};
Ok(Self {
format,
track,
decoder,
rate: None,
n_frames: remaining.or(params.n_frames),
remaining,
})
}
/// The decoded sample rate; known once [`Self::read`] returned audio.
pub(super) fn rate(&self) -> Option<u32> {
self.rate
}
/// The track's length, when its container says.
pub(super) fn duration_ms(&self) -> Option<u64> {
let frames = self.n_frames?;
let rate = u64::from(self.rate?);
Some(frames * 1_000 / rate)
}
/// The next decoded block of mono samples, empty at the end. A damaged
/// packet is skipped as a player would; a run of them, or a container
/// that stops reading, is [`PieceError::Damaged`] (the caller fills in
/// where).
pub(super) fn read(&mut self) -> Result<Vec<f32>, PieceError> {
let mut skipped = 0;
loop {
if self.remaining == Some(0) {
return Ok(Vec::new());
}
let packet = match self.format.next_packet() {
Ok(packet) => packet,
Err(Error::IoError(error)) if error.kind() == std::io::ErrorKind::UnexpectedEof => {
return Ok(Vec::new());
}
Err(_) => return Err(PieceError::Damaged { at_ms: 0 }),
};
if packet.track_id() != self.track {
continue;
}
let decoded = match &mut self.decoder {
Decoder::Symphonia { decoder, buffer } => match decoder.decode(&packet) {
Ok(audio) => {
let spec = *audio.spec();
let channels = spec.channels.count();
let frames = audio.frames();
if buffer
.as_ref()
.is_none_or(|buffer| buffer.capacity() < frames * channels)
{
*buffer = Some(SampleBuffer::new(audio.capacity() as u64, spec));
}
let buffer = buffer.as_mut().expect("the buffer was just made");
buffer.copy_interleaved_ref(audio);
Some((spec.rate, mono(buffer.samples(), channels)?))
}
Err(Error::DecodeError(_)) => None,
Err(_) => return Err(PieceError::Damaged { at_ms: 0 }),
},
Decoder::Opus(opus) => opus.decode(&packet.data)?.map(|samples| (48_000, samples)),
};
let Some((rate, mut samples)) = decoded else {
skipped += 1;
if skipped >= MAX_SKIPPED_PACKETS {
return Err(PieceError::Damaged { at_ms: 0 });
}
continue;
};
skipped = 0;
match self.rate {
None => self.rate = Some(rate),
Some(known) if known != rate => {
return Err(PieceError::UnsupportedCodec(
"audio whose sample rate changes partway".into(),
));
}
Some(_) => {}
}
if let Some(remaining) = &mut self.remaining {
let keep = samples
.len()
.min(usize::try_from(*remaining).unwrap_or(usize::MAX));
samples.truncate(keep);
*remaining -= keep as u64;
}
if !samples.is_empty() {
return Ok(samples);
}
}
}
}
/// The track to decode and its decoder, or why there is none: no audio track
/// at all, or only audio in codecs this build can't decode.
fn choose(format: &dyn FormatReader) -> Result<(u32, CodecParameters, Decoder), PieceError> {
let default = format.default_track().map(|track| track.id);
let tracks = format.tracks();
let ordered = tracks
.iter()
.filter(|track| Some(track.id) == default)
.chain(tracks.iter().filter(|track| Some(track.id) != default));
let mut undecodable = None;
for track in ordered {
let params = &track.codec_params;
if params.codec == CODEC_TYPE_NULL {
// A video or data track, or audio a container can't describe.
if params.sample_rate.is_some() || params.channels.is_some() {
undecodable.get_or_insert_with(|| "an unrecognized codec".to_owned());
}
continue;
}
let decoder = if params.codec == CODEC_TYPE_OPUS {
Opus::new(params.extra_data.as_deref().unwrap_or_default())
.map(|opus| Decoder::Opus(Box::new(opus)))
} else {
let params = &aac_config(params);
symphonia::default::get_codecs()
.make(params, &DecoderOptions::default())
.map(|decoder| Decoder::Symphonia {
decoder,
buffer: None,
})
.map_err(|_| codec_name(params))
};
match decoder {
Ok(decoder) => return Ok((track.id, params.clone(), decoder)),
Err(name) => {
undecodable.get_or_insert(name);
}
}
}
Err(match undecodable {
Some(name) => PieceError::UnsupportedCodec(name),
None => PieceError::NoAudioTrack,
})
}
/// AAC's decoder takes the AudioSpecificConfig, but CAF's magic cookie (as
/// written by Apple's encoders) wraps it in an MPEG-4 ES descriptor; unwrap
/// it, leaving any other codec's parameters as they are.
fn aac_config(params: &CodecParameters) -> CodecParameters {
let mut params = params.clone();
if params.codec == CODEC_TYPE_AAC
&& let Some(config) = params.extra_data.as_deref().and_then(es_decoder_config)
{
params.extra_data = Some(config.into());
}
params
}
/// The DecoderSpecificInfo inside an ES_Descriptor (ISO/IEC 14496-1 §7.2.6.5),
/// or `None` when `data` isn't one.
fn es_decoder_config(data: &[u8]) -> Option<Vec<u8>> {
/// One descriptor's tag and body, and what follows it.
fn descriptor(data: &[u8]) -> Option<(u8, &[u8], &[u8])> {
let (&tag, mut rest) = data.split_first()?;
let mut length = 0_usize;
for _ in 0..4 {
let (&byte, after) = rest.split_first()?;
rest = after;
length = length << 7 | usize::from(byte & 0x7f);
if byte & 0x80 == 0 {
break;
}
}
(rest.len() >= length).then(|| (tag, &rest[..length], &rest[length..]))
}
let (tag, es, _) = descriptor(data)?;
if tag != 0x03 || es.len() < 3 {
return None;
}
let flags = es[2];
let mut body = &es[3..];
if flags & 0x80 != 0 {
body = body.get(2..)?;
}
if flags & 0x40 != 0 {
let length = usize::from(*body.first()?);
body = body.get(1 + length..)?;
}
if flags & 0x20 != 0 {
body = body.get(2..)?;
}
while !body.is_empty() {
let (tag, config, rest) = descriptor(body)?;
if tag == 0x04 {
// objectTypeIndication, streamType, bufferSizeDB, maxBitrate,
// avgBitrate, then the nested descriptors.
let mut nested = config.get(13..)?;
while !nested.is_empty() {
let (tag, info, rest) = descriptor(nested)?;
if tag == 0x05 {
return Some(info.to_vec());
}
nested = rest;
}
return None;
}
body = rest;
}
None
}
fn codec_name(params: &CodecParameters) -> String {
symphonia::default::get_codecs()
.get_codec(params.codec)
.map_or_else(
|| format!("codec {}", params.codec),
|codec| codec.long_name.to_owned(),
)
}
/// Interleaved samples averaged to mono, refusing a sample that is not a
/// number, as the WAV reader does.
fn mono(interleaved: &[f32], channels: usize) -> Result<Vec<f32>, PieceError> {
let channels = channels.max(1);
interleaved
.chunks_exact(channels)
.map(|frame| {
let average =
frame.iter().map(|&sample| f64::from(sample)).sum::<f64>() / channels as f64;
let average = average as f32;
if average.is_finite() {
Ok(average)
} else {
Err(PieceError::Damaged { at_ms: 0 })
}
})
.collect()
}
/// An Opus stream (RFC 7845's mapping family 0, mono or stereo) through
/// `opuscule`: its pre-skip dropped and output gain applied.
struct Opus {
decoder: opuscule::Decoder,
channels: usize,
pre_skip: u64,
/// Decoded frames still to drop for the pre-skip.
skip: usize,
gain: f32,
buffer: Vec<f32>,
}
impl Opus {
/// From the `OpusHead` identification header (RFC 7845 §5.1).
fn new(head: &[u8]) -> Result<Self, String> {
if head.len() < 19 || &head[..8] != b"OpusHead" {
return Err("Opus without its OpusHead header".into());
}
let channels = usize::from(head[9]);
if head[18] != 0 || !(1..=2).contains(&channels) {
return Err(format!("Opus with {channels} channels"));
}
let pre_skip = u16::from_le_bytes([head[10], head[11]]);
let gain_db = f32::from(i16::from_le_bytes([head[16], head[17]])) / 256.0;
let layout = if channels == 1 {
opuscule::Channels::Mono
} else {
opuscule::Channels::Stereo
};
Ok(Self {
decoder: opuscule::Decoder::new(opuscule::SampleRate::Hz48000, layout),
channels,
pre_skip: u64::from(pre_skip),
skip: usize::from(pre_skip),
gain: 10_f32.powf(gain_db / 20.0),
// 120 ms at 48 kHz, the longest packet.
buffer: vec![0.0; 5_760 * channels],
})
}
/// One packet as mono samples, or `None` for a damaged packet.
fn decode(&mut self, packet: &[u8]) -> Result<Option<Vec<f32>>, PieceError> {
let frames = match self.decoder.decode(Some(packet), &mut self.buffer, false) {
Ok(frames) => frames,
Err(opuscule::Error::InvalidPacket) => return Ok(None),
Err(_) => return Err(PieceError::Damaged { at_ms: 0 }),
};
let start = self.skip.min(frames);
self.skip -= start;
let gain = self.gain;
let samples: Vec<f32> = self.buffer[start * self.channels..frames * self.channels]
.iter()
.map(|sample| sample * gain)
.collect();
mono(&samples, self.channels).map(Some)
}
}
#[cfg(test)]
mod tests {
#[test]
fn a_caf_aac_cookie_unwraps_to_its_audio_specific_config() {
// Apple's encoder's `kuki` for 22.05 kHz mono AAC-LC.
let cookie = [
0x03, 0x80, 0x80, 0x80, 0x22, 0x00, 0x00, 0x00, 0x04, 0x80, 0x80, 0x80, 0x14, 0x40,
0x14, 0x00, 0x18, 0x00, 0x00, 0x00, 0xa5, 0xd8, 0x00, 0x00, 0x7d, 0x00, 0x05, 0x80,
0x80, 0x80, 0x02, 0x13, 0x88, 0x06, 0x80, 0x80, 0x80, 0x01, 0x02,
];
assert_eq!(super::es_decoder_config(&cookie), Some(vec![0x13, 0x88]));
assert_eq!(super::es_decoder_config(&[0x13, 0x88]), None);
}
}