Skip to main content

stream_with_progress/
stream_with_progress.rs

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