Skip to main content

stream_with_progress/
stream_with_progress.rs

1/// Streams the first audio track while printing a live progress line.
2use cd_da_reader::{CdReader, TrackStreamConfig};
3
4fn main() -> Result<(), Box<dyn std::error::Error>> {
5    let reader = CdReader::open_default()?;
6    let toc = reader.read_toc()?;
7
8    let first_audio = toc
9        .tracks
10        .iter()
11        .find(|t| t.is_audio)
12        .ok_or("no audio tracks found")?;
13
14    let mut stream =
15        reader.open_track_stream(&toc, first_audio.number, TrackStreamConfig::default())?;
16
17    let total_secs = stream.total_seconds();
18    println!(
19        "Track {} — {} sectors ({:.0}s)\n",
20        first_audio.number,
21        stream.total_sectors(),
22        total_secs,
23    );
24
25    let mut pcm = Vec::new();
26    while let Some(chunk) = stream.next_chunk()? {
27        pcm.extend_from_slice(&chunk);
28
29        let cur = stream.current_seconds();
30        let pct = cur / total_secs * 100.0;
31        eprint!("\r  [{:>5.1}s / {:.1}s] {:5.1}%", cur, total_secs, pct,);
32    }
33    eprintln!("\r  [{:.1}s / {:.1}s] 100.0%", total_secs, total_secs);
34
35    let wav = CdReader::create_wav(pcm);
36    let filename = format!("track{:02}.wav", first_audio.number);
37    std::fs::write(&filename, wav)?;
38    println!("\nSaved {}", filename);
39
40    Ok(())
41}