Skip to main content

open_track_stream

Function open_track_stream 

Source
pub fn open_track_stream<'a, R: AudioSectorReader>(
    src: &'a R,
    toc: &Toc,
    track_no: u8,
) -> Result<AudioTrackStream<'a, R>, CdReaderError>
Expand description

Open a streaming reader for a track assuming the TOC includes the inter-session gap (TrackBounds::SessionGap). See AudioTrackStream.

ยงErrors

Returns CdReaderError::Io if the track is absent or its bounds are invalid.

Examples found in repository?
examples/file_backend.rs (line 92)
37fn main() -> Result<(), Box<dyn std::error::Error>> {
38    let output_dir = common::fresh_output_dir("file_backend")?;
39
40    // A real backing derives this TOC from the image's own track metadata.
41    // Here we fabricate a 2-track disc: 2 seconds + 3 seconds of audio.
42    let track1_sectors = 75 * 2;
43    let track2_sectors = 75 * 3;
44    let total_sectors = track1_sectors + track2_sectors;
45
46    let toc = Toc {
47        first_track: 1,
48        last_track: 2,
49        tracks: vec![
50            Track {
51                number: 1,
52                start_lba: 0,
53                start_msf: lba_to_msf(0),
54                is_audio: true,
55            },
56            Track {
57                number: 2,
58                start_lba: track1_sectors,
59                start_msf: lba_to_msf(track1_sectors),
60                is_audio: true,
61            },
62        ],
63        leadout_lba: total_sectors,
64    };
65
66    // Silence, just for the demo โ€” a real backing decodes actual audio here.
67    let disc = InMemoryDisc {
68        pcm: vec![0u8; total_sectors as usize * 2352],
69    };
70
71    for track in &toc.tracks {
72        let pcm = read_track(&disc, &toc, track.number)?;
73        println!(
74            "track {}: {} bytes ({} sectors)",
75            track.number,
76            pcm.len(),
77            pcm.len() / 2352
78        );
79
80        let wav = create_wav(pcm);
81        let output_path = output_dir.join(format!("track{:02}.wav", track.number));
82        std::fs::write(&output_path, wav)?;
83        println!("  wrote {}", output_path.display());
84    }
85
86    // The same backing can be streamed instead of buffered: pull sector-aligned
87    // chunks so a player never holds a whole track in memory at once. (A backing
88    // whose tracks are addressed contiguously โ€” a gap-stripped extract โ€” would
89    // open with `TrackBounds::Gapless`, or supply its own bounds via
90    // `open_track_stream_at`; this demo TOC has no trailing data track, so plain
91    // `open_track_stream` is equivalent.)
92    let mut stream = open_track_stream(&disc, &toc, 1)?;
93    let (mut chunks, mut bytes) = (0u32, 0usize);
94    while let Some(chunk) = stream.next_chunk()? {
95        chunks += 1;
96        bytes += chunk.len();
97    }
98    println!(
99        "streamed track 1: {bytes} bytes in {chunks} chunks ({:.1}s of audio)",
100        stream.total_seconds()
101    );
102
103    Ok(())
104}