pub struct TrackStream<'a> { /* private fields */ }Expand description
Track-scoped streaming reader for CD-DA PCM data. You can iterate through the data manually; this allows to receive initial data much faster and also allows you to navigate to specific points.
To create one, use “reader.open_track_stream” method in order to have correct drive’s lifecycle management.
Implementations§
Source§impl<'a> TrackStream<'a>
impl<'a> TrackStream<'a>
Sourcepub fn next_chunk(&mut self) -> Result<Option<Vec<u8>>, CdReaderError>
pub fn next_chunk(&mut self) -> Result<Option<Vec<u8>>, CdReaderError>
Read the next chunk of PCM data.
Returns Ok(None) when end-of-track is reached.
Examples found in repository?
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}Sourcepub fn total_sectors(&self) -> u32
pub fn total_sectors(&self) -> u32
Total number of sectors in this track stream.
Sourcepub fn current_sector(&self) -> u32
pub fn current_sector(&self) -> u32
Current stream position as a track-relative sector index. Keep in mind that if you are playing the sound directly, this is likely not the track’s current position because you probably keep some of the data in your buffer.
Sourcepub fn seek_to_sector(&mut self, sector: u32) -> Result<(), CdReaderError>
pub fn seek_to_sector(&mut self, sector: u32) -> Result<(), CdReaderError>
Seek to an absolute track-relative sector position.
Valid range is 0..=total_sectors().
If the sector value is higher than the total, it will throw an error.
Sourcepub fn current_seconds(&self) -> f32
pub fn current_seconds(&self) -> f32
Current stream position in seconds. Functionally equivalent to “current_sector”, but converted to seconds.
Audio CD timing uses 75 sectors = 1 second.
Sourcepub fn total_seconds(&self) -> f32
pub fn total_seconds(&self) -> f32
Total stream duration in seconds. Functionally equivalent to “total_sectors”, but converted to seconds.
Audio CD timing uses 75 sectors = 1 second.
Sourcepub fn seek_to_seconds(&mut self, seconds: f32) -> Result<(), CdReaderError>
pub fn seek_to_seconds(&mut self, seconds: f32) -> Result<(), CdReaderError>
Seek to an absolute track-relative time position in seconds.
Input is converted to sector offset and clamped to track bounds.