Struct FlacChannelReader

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

A FLAC reader which outputs non-interleaved channels

§Example

use flac_codec::{
    encode::{FlacChannelWriter, Options},
    decode::FlacChannelReader,
};
use std::io::{Cursor, Seek};

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

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

// write our samples, divided by channel
let written_samples = vec![
    vec![1, 2, 3, 4, 5],
    vec![-1, -2, -3, -4, -5],
];
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 = FlacChannelReader::new(flac).unwrap();

// read a buffer's worth of samples
let read_samples = reader.fill_buf().unwrap();

// ensure the channels match
assert_eq!(read_samples.len(), written_samples.len());
assert_eq!(read_samples[0], written_samples[0]);
assert_eq!(read_samples[1], written_samples[1]);

Implementations§

Source§

impl<R: Read> FlacChannelReader<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 fill_buf(&mut self) -> Result<Vec<&[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 returned as a Vec of channel slices, like: [[left₀ , left₁ , left₂ , …], [right₀ , right₁ , right₂ , …]]

The returned buffer will always contain at least one channel. All channel slices will always be the same length.

§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 channel-indepedent 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> FlacChannelReader<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.

FlacChannelReader::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 FlacChannelReader.

Source§

impl FlacChannelReader<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> FlacChannelReader<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 FlacChannelReader<R>

Source§

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

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

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

Performs copy-assignment from source. Read more
Source§

impl<R: Read> Metadata for FlacChannelReader<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 FlacChannelReader<R>
where R: Freeze,

§

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

§

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

§

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

§

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

§

impl<R> UnwindSafe for FlacChannelReader<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.