Files
qbz/crates/qbz-rip/examples/rip_one.rs
T
vicrodh 5d3d51a882 feat(rip): rip a CD to tagged FLAC, bit-exact and seekable
New crate `qbz-rip`: CD audio in, tagged FLAC out. Frontend-agnostic — it
knows discs, samples and files, and reports progress through a callback so a
UI can drive it without this crate depending on one.

`flacenc`, pure Rust: no libFLAC to build, nothing extra to bundle into
Flatpak, Snap or AppImage, and macOS gets it for free. `lofty` (already in the
tree) writes the Vorbis comments.

`RipSource` is the portability seam, and it is the only part that differs
between platforms: Linux reads sectors through ioctls, while macOS MOUNTS an
audio CD and presents each track as an AIFF under /Volumes — so there a "read"
is a file read. Everything after the seam is shared.

Two defects were found by ripping a real disc and looking at the result, and
neither would have failed a test:

  1. The tags were never written. Tagging happened on the `.flac.part` file,
     and lofty identifies a file by its EXTENSION — so it wrote nothing, and
     said nothing. Tagging now happens after the rename, with a test that
     reads the bytes back and asserts the strings are in them.

  2. The files could not be SEEKED. `flac -d --skip=2:00` returned
     FLAC__STREAM_DECODER_SEEK_ERROR and `flac -t` warned 3102 times over 3103
     frames. The cause is precise: a stream's last frame is short (12 709 620
     samples at a 4096 block leaves 3828), flacenc reports that honestly as
     min_blocksize 3828 / max 4096 — but in FLAC `min != max` MEANS
     variable-blocksize, and a decoder reading one expects SAMPLE numbers in
     the frame headers while the frames correctly carry FRAME numbers.
     libFLAC's own encoder writes min == max for exactly this reason. Two
     bytes of STREAMINFO, and now: zero warnings, seek works.

Verified against the drive, not inferred: 176 400 bytes decoded out of the
FLAC are byte-identical to the same sectors read straight off the disc. The
whole album ripped from the UI — seven tracks, 436 MB, every one decoding
clean, tagged with TITLE/ARTIST/ALBUM/TRACKNUMBER/TRACKTOTAL/DATE/ALBUMARTIST,
and no partial file left behind.

Filenames are sanitised for the strictest filesystem the library might be
copied to, including Windows' reserved DEVICE names and its habit of silently
dropping a trailing dot — which would turn two tracks into one collision.

cc
2026-08-21 02:27:24 -06:00

53 lines
1.8 KiB
Rust

//! Rip ONE track off the disc in the drive, tagged, into a directory.
//! `cargo run -p qbz-rip --example rip_one -- <track#> <destdir>`
fn main() {
let mut a = std::env::args().skip(1);
let want: u32 = a.next().expect("track number").parse().unwrap();
let dest = std::path::PathBuf::from(a.next().expect("destination"));
let dev = qbz_disc::list_devices().into_iter().next().expect("a drive");
let toc = qbz_disc::read_toc(&dev).expect("toc");
let starts: Vec<u32> = toc.audio_tracks().map(|t| t.start_lsn).collect();
let id = qbz_disc::discid::disc_id(&starts, toc.leadout_lsn).expect("disc id");
println!("disc id {id}");
let t = toc
.audio_tracks()
.find(|t| t.number as u32 == want)
.expect("that track");
println!("track {} — {} sectors, {}s", t.number, t.sectors, t.duration_secs());
let plan = qbz_rip::RipPlan {
destination: dest,
album: "Fear Inoculum".into(),
album_artist: "Tool".into(),
year: Some(2019),
tracks: vec![qbz_rip::RipTrack {
number: t.number as u32,
title: "Chocolate Chip Trip".into(),
artist: "Tool".into(),
source: qbz_rip::RipSource::Cd {
device: dev.clone(),
start_lsn: t.start_lsn,
sectors: t.sectors,
},
}],
};
let started = std::time::Instant::now();
let mut last = -1i32;
let out = qbz_rip::rip(&plan, |p| {
let pct = (p.fraction * 100.0) as i32;
if pct / 10 != last / 10 {
println!(" {pct}%");
last = pct;
}
true
})
.expect("rip");
println!("wrote {:?} in {:.1}s", out, started.elapsed().as_secs_f32());
for p in &out {
let md = std::fs::metadata(p).unwrap();
println!(" {} bytes", md.len());
}
}