FlacSampleReader

Struct FlacSampleReader 

Source
pub struct FlacSampleReader<R> { /* private fields */ }
Expand description

A FLAC reader which outputs PCM samples as signed integers

§Example

use flac_codec::{
    encode::{FlacSampleWriter, Options},
    decode::FlacSampleReader,
};
use std::io::{Cursor, Seek};

let mut flac = Cursor::new(vec![]);  // a FLAC file in memory

let mut writer = FlacSampleWriter::new(
    &mut flac,           // our wrapped writer
    Options::default(),  // default encoding options
    44100,               // sample rate
    16,                  // bits-per-sample
    1,                   // channel count
    Some(1000),          // total samples
).unwrap();

// write 1000 samples
let written_samples = (0..1000).collect::<Vec<i32>>();
assert!(writer.write(&written_samples).is_ok());

// finalize writing file
assert!(writer.finalize().is_ok());

flac.rewind().unwrap();

// open reader around written FLAC file
let mut reader = FlacSampleReader::new(flac).unwrap();

// read 1000 samples
let mut read_samples = vec![0; 1000];
assert!(matches!(reader.read(&mut read_samples), Ok(1000)));

// ensure they match
assert_eq!(read_samples, written_samples);

Implementations§

Source§

impl<R: Read> FlacSampleReader<R>

Source

pub fn new(reader: R) -> Result<Self, Error>

Opens new FLAC reader which wraps the given reader

The reader must be positioned at the start of the FLAC stream. If the file has non-FLAC data at the beginning (such as ID3v2 tags), one should skip such data before initializing a FlacByteReader.

Source

pub fn metadata(&self) -> &BlockList

Returns FLAC metadata blocks

Source

pub fn read(&mut self, samples: &mut [i32]) -> Result<usize, Error>

Attempts to fill the buffer with samples and returns quantity read

Returned samples are interleaved by channel, like: [left₀ , right₀ , left₁ , right₁ , left₂ , right₂ , …]

§Errors

Returns error if some error occurs reading FLAC file

Source

pub fn read_to_end(&mut self, buf: &mut Vec<i32>) -> Result<usize, Error>

Reads all samples from source FLAC, placing them in buf

If successful, returns the total number of samples read

Source

pub fn fill_buf(&mut self) -> Result<&[i32], Error>

Returns complete buffer of all read samples

Analogous to std::io::BufRead::fill_buf, this should be paired with FlacSampleReader::consume to consume samples in the filled buffer once used.

Returned samples are interleaved by channel, like: [left₀ , right₀ , left₁ , right₁ , left₂ , right₂ , …]

§Errors

Returns error if some error occurs reading FLAC file to fill buffer.

Source

pub fn consume(&mut self, amt: usize)

Informs the reader that amt samples have been consumed.

Analagous to std::io::BufRead::consume, which marks samples as having been read.

May panic if attempting to consume more bytes than are available in the buffer.

Source§

impl<R: Read + Seek> FlacSampleReader<R>

Source

pub fn new_seekable(reader: R) -> Result<Self, Error>

Opens a new seekable FLAC reader which wraps the given reader

If a stream is both readable and seekable, it’s vital to use this method to open it if one also wishes to seek within the FLAC stream. Otherwise, an I/O error will result when attempting to seek.

FlacSampleReader::open calls this method to ensure all File-based streams are also seekable.

The reader must be positioned at the start of the FLAC stream. If the file has non-FLAC data at the beginning (such as ID3v2 tags), one should skip such data before initializing a FlacSampleReader.

§Example
use flac_codec::{
    encode::{FlacSampleWriter, Options},
    decode::FlacSampleReader,
};
use std::io::{Cursor, Seek};

let mut flac = Cursor::new(vec![]);  // a FLAC file in memory

let mut writer = FlacSampleWriter::new(
    &mut flac,           // our wrapped writer
    Options::default(),  // default encoding options
    44100,               // sample rate
    16,                  // bits-per-sample
    1,                   // channel count
    Some(1000),          // total samples
).unwrap();

// write 1000 samples
let written_samples = (0..1000).collect::<Vec<i32>>();
assert!(writer.write(&written_samples).is_ok());

// finalize writing file
assert!(writer.finalize().is_ok());

flac.rewind().unwrap();

// open reader around written FLAC file
let mut reader = FlacSampleReader::new_seekable(flac).unwrap();

// read 1000 samples
let mut read_samples_1 = vec![0; 1000];
assert!(matches!(reader.read(&mut read_samples_1), Ok(1000)));

// ensure they match
assert_eq!(read_samples_1, written_samples);

// rewind reader to halfway through file
assert!(reader.seek(500).is_ok());

// read 500 samples
let mut read_samples_2 = vec![0; 500];
assert!(matches!(reader.read(&mut read_samples_2), Ok(500)));

// ensure output matches back half of input
assert_eq!(read_samples_2.len(), 500);
assert!(written_samples.ends_with(&read_samples_2));
Source§

impl FlacSampleReader<BufReader<File>>

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error>

Opens FLAC file from the given path

Source§

impl<R: Read + Seek> FlacSampleReader<R>

Source

pub fn seek(&mut self, sample: u64) -> Result<(), Error>

Seeks to the given channel-independent sample

The sample is relative to the beginning of the stream

Trait Implementations§

Source§

impl<R: Clone> Clone for FlacSampleReader<R>

Source§

fn clone(&self) -> FlacSampleReader<R>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<R: Read> IntoIterator for FlacSampleReader<R>

Source§

type IntoIter = FlacSampleIterator<R>

Which kind of iterator are we turning this into?
Source§

type Item = Result<i32, Error>

The type of the elements being iterated over.
Source§

fn into_iter(self) -> FlacSampleIterator<R>

Creates an iterator from a value. Read more
Source§

impl<R: Read> Metadata for FlacSampleReader<R>

Source§

fn channel_count(&self) -> u8

Returns channel count Read more
Source§

fn channel_mask(&self) -> ChannelMask

Returns channel mask Read more
Source§

fn sample_rate(&self) -> u32

Returns sample rate, in Hz
Source§

fn bits_per_sample(&self) -> u32

Returns decoder’s bits-per-sample Read more
Source§

fn total_samples(&self) -> Option<u64>

Returns total number of channel-independent samples, if known
Source§

fn md5(&self) -> Option<&[u8; 16]>

Returns MD5 of entire stream, if known Read more
Source§

fn decoded_len(&self) -> Option<u64>

Returns total length of decoded file, in bytes
Source§

fn duration(&self) -> Option<Duration>

Returns duration of file

Auto Trait Implementations§

§

impl<R> Freeze for FlacSampleReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for FlacSampleReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for FlacSampleReader<R>
where R: Send,

§

impl<R> Sync for FlacSampleReader<R>
where R: Sync,

§

impl<R> Unpin for FlacSampleReader<R>
where R: Unpin,

§

impl<R> UnwindSafe for FlacSampleReader<R>
where R: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.