forge
Explore
Log in

Don't you dare go Hollow

edith

Clone
★ 0

0 watching0 forksRust18.3 MiB7f487c1Tahinli3 months ago659 commits

seek_dump.rs52 lines, 2.0 KiBHistoryRawDownload
1//! Where a seek actually lands, in *content*: seeks a file to `start` and
2//! writes the next `dur` seconds of the mixer's own samples as raw f32, for
3//! correlating against an ffmpeg decode of the same window.
4//!
5//! `cargo run --release -p engine --example seek_dump -- <file> <start_secs> <dur_secs> <out.f32>`
6//!
7//! Its reason to exist is the same as `probe_fps`'s: the files this has to be
8//! measured on are films, too big to keep as fixtures, and "the sound is out of
9//! step after a scrub" is a number -- the offset between what came out here and
10//! what ffmpeg decodes at the same second -- long before it is an opinion.
11
12use std::io::Write;
13
14fn main() {
15 let mut args = std::env::args().skip(1);
16 let path = args
17 .next()
18 .expect("usage: seek_dump <file> <start> <dur> <out.f32>");
19 let start: f64 = args.next().expect("missing start").parse().expect("start");
20 let dur: f64 = args
21 .next()
22 .expect("missing duration")
23 .parse()
24 .expect("duration");
25 let out = args.next().expect("missing output path");
26
27 let (meta, rx) = engine::AudioSession::open_at(&path, start)
28 .expect("open")
29 .expect("no audio track");
30 eprintln!("{} Hz, {} ch", meta.sample_rate, meta.channels);
31 let want = (dur * f64::from(meta.sample_rate)) as usize * usize::from(meta.channels);
32 let mut samples: Vec<f32> = Vec::with_capacity(want);
33 for chunk in rx {
34 if samples.is_empty() {
35 eprintln!("first chunk at sample {}", chunk.start_sample);
36 }
37 samples.extend(chunk.samples);
38 if samples.len() >= want {
39 break;
40 }
41 }
42 samples.truncate(want);
43 let mut w = std::io::BufWriter::new(std::fs::File::create(&out).expect("create"));
44 for s in &samples {
45 w.write_all(&s.to_le_bytes()).expect("write");
46 }
47 w.flush().expect("flush");
48 eprintln!(
49 "wrote {} frames to {out}",
50 samples.len() / usize::from(meta.channels)
51 );
52}