Skip to main content

read_track/
read_track.rs

1use cd_da_reader::{CdReader, RetryConfig, TrackStreamConfig};
2
3fn main() -> Result<(), Box<dyn std::error::Error>> {
4    let drive_path = default_drive_path();
5    read_cd(drive_path)
6}
7
8#[cfg(target_os = "windows")]
9fn default_drive_path() -> &'static str {
10    r"\\.\E:"
11}
12
13#[cfg(target_os = "macos")]
14fn default_drive_path() -> &'static str {
15    "disk14"
16}
17
18#[cfg(target_os = "linux")]
19fn default_drive_path() -> &'static str {
20    "/dev/sr0"
21}
22
23fn read_cd(path: &str) -> Result<(), Box<dyn std::error::Error>> {
24    let reader = CdReader::open(path)?;
25    let toc = reader.read_toc()?;
26    println!("{toc:#?}");
27
28    let last_audio_track = toc
29        .tracks
30        .iter()
31        .rev()
32        .find(|track| track.is_audio)
33        .ok_or_else(|| std::io::Error::other("no audio tracks in TOC"))?;
34
35    println!("Reading track {}", last_audio_track.number);
36    let stream_cfg = TrackStreamConfig {
37        sectors_per_chunk: 27,
38        retry: RetryConfig {
39            max_attempts: 5,
40            initial_backoff_ms: 30,
41            max_backoff_ms: 500,
42            reduce_chunk_on_retry: true,
43            min_sectors_per_read: 1,
44        },
45    };
46    let mut stream = reader.open_track_stream(&toc, last_audio_track.number, stream_cfg)?;
47
48    let mut pcm = Vec::new();
49    while let Some(chunk) = stream.next_chunk()? {
50        pcm.extend_from_slice(&chunk);
51    }
52    let wav = CdReader::create_wav(pcm);
53    std::fs::write("myfile.wav", wav)?;
54
55    Ok(())
56}