Skip to main content

TrackStream

Struct TrackStream 

Source
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>

Source

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?
examples/read_track.rs (line 49)
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}
Source

pub fn total_sectors(&self) -> u32

Total number of sectors in this track stream.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Auto Trait Implementations§

§

impl<'a> Freeze for TrackStream<'a>

§

impl<'a> RefUnwindSafe for TrackStream<'a>

§

impl<'a> Send for TrackStream<'a>

§

impl<'a> Sync for TrackStream<'a>

§

impl<'a> Unpin for TrackStream<'a>

§

impl<'a> UnwindSafe for TrackStream<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.