Struct FlacByteReader

Source
pub struct FlacByteReader<R, E> { /* private fields */ }
Expand description

A FLAC reader which outputs PCM samples as bytes

§Example

use flac_codec::{
    byteorder::LittleEndian,
    encode::{FlacByteWriter, Options},
    decode::{FlacByteReader, Metadata},
};
use std::io::{Cursor, Read, Seek, Write};

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

let mut writer = FlacByteWriter::endian(
    &mut flac,           // our wrapped writer
    LittleEndian,        // .wav-style byte order
    Options::default(),  // default encoding options
    44100,               // sample rate
    16,                  // bits-per-sample
    1,                   // channel count
    Some(2000),          // total bytes
).unwrap();

// write 1000 samples as 16-bit, signed, little-endian bytes (2000 bytes total)
let written_bytes = (0..1000).map(i16::to_le_bytes).flatten().collect::<Vec<u8>>();
assert!(writer.write_all(&written_bytes).is_ok());

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

flac.rewind().unwrap();

// open reader around written FLAC file
let mut reader = FlacByteReader::endian(flac, LittleEndian).unwrap();

// read 2000 bytes
let mut read_bytes = vec![];
assert!(reader.read_to_end(&mut read_bytes).is_ok());

// ensure MD5 sum of signed, little-endian samples matches hash in file
let mut md5 = md5::Context::new();
md5.consume(&read_bytes);
assert_eq!(&md5.compute().0, reader.md5().unwrap());

// ensure input and output matches
assert_eq!(read_bytes, written_bytes);

Implementations§

Source§

impl<R: Read, E: Endianness> FlacByteReader<R, E>

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 endian(reader: R, _endian: E) -> Result<Self, Error>

Opens new FLAC reader in the given endianness

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§

impl<R: Read + Seek, E: Endianness> FlacByteReader<R, E>

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.

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

§Example
use flac_codec::{
    byteorder::LittleEndian,
    encode::{FlacByteWriter, Options},
    decode::FlacByteReader,
};
use std::io::{Cursor, Read, Seek, SeekFrom, Write};

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

let mut writer = FlacByteWriter::endian(
    &mut flac,           // our wrapped writer
    LittleEndian,        // .wav-style byte order
    Options::default(),  // default encoding options
    44100,               // sample rate
    16,                  // bits-per-sample
    1,                   // channel count
    Some(2000),          // total bytes
).unwrap();

// write 1000 samples as 16-bit, signed, little-endian bytes (2000 bytes total)
let written_bytes = (0..1000).map(i16::to_le_bytes).flatten().collect::<Vec<u8>>();
assert!(writer.write_all(&written_bytes).is_ok());

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

flac.rewind().unwrap();

// open reader around written FLAC file
let mut reader: FlacByteReader<_, LittleEndian> =
    FlacByteReader::new_seekable(flac).unwrap();

// read 2000 bytes
let mut read_bytes_1 = vec![];
assert!(reader.read_to_end(&mut read_bytes_1).is_ok());

// ensure input and output matches
assert_eq!(read_bytes_1, written_bytes);

// rewind reader to halfway through file
assert!(reader.seek(SeekFrom::Start(1000)).is_ok());

// read 1000 bytes
let mut read_bytes_2 = vec![];
assert!(reader.read_to_end(&mut read_bytes_2).is_ok());

// ensure output matches back half of input
assert_eq!(read_bytes_2.len(), 1000);
assert!(written_bytes.ends_with(&read_bytes_2));
Source§

impl<E: Endianness> FlacByteReader<BufReader<File>, E>

Source

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

Opens FLAC file from the given path

Trait Implementations§

Source§

impl<R: Read, E: Endianness> BufRead for FlacByteReader<R, E>

Source§

fn fill_buf(&mut self) -> Result<&[u8]>

Reads samples to the given buffer as bytes in our stream’s endianness

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

§Errors

Returns any error that occurs when reading the stream, converted to an I/O error.

Source§

fn consume(&mut self, amt: usize)

Marks the given amount of additional bytes from the internal buffer as having been read. Subsequent calls to read only return bytes that have not been marked as read. Read more
Source§

fn has_data_left(&mut self) -> Result<bool, Error>

🔬This is a nightly-only experimental API. (buf_read_has_data_left)
Checks if there is any data left to be read. Read more
1.0.0 · Source§

fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> Result<usize, Error>

Reads all bytes into buf until the delimiter byte or EOF is reached. Read more
1.83.0 · Source§

fn skip_until(&mut self, byte: u8) -> Result<usize, Error>

Skips all bytes until the delimiter byte or EOF is reached. Read more
1.0.0 · Source§

fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until a newline (the 0xA byte) is reached, and append them to the provided String buffer. Read more
1.0.0 · Source§

fn split(self, byte: u8) -> Split<Self>
where Self: Sized,

Returns an iterator over the contents of this reader split on the byte byte. Read more
1.0.0 · Source§

fn lines(self) -> Lines<Self>
where Self: Sized,

Returns an iterator over the lines of this reader. Read more
Source§

impl<R: Clone, E: Clone> Clone for FlacByteReader<R, E>

Source§

fn clone(&self) -> FlacByteReader<R, E>

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, E: Endianness> Metadata for FlacByteReader<R, E>

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

impl<R: Read, E: Endianness> Read for FlacByteReader<R, E>

Source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Reads samples to the given buffer as bytes in our stream’s endianness

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

This is the same format used by common PCM container formats like WAVE and AIFF

§Errors

Returns any error that occurs when reading the stream, converted to an I/O error.

1.36.0 · Source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
Source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · Source§

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

Reads all bytes until EOF in this source, placing them into buf. Read more
1.0.0 · Source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Reads all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · Source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Reads the exact number of bytes required to fill buf. Read more
Source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
Source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Reads the exact number of bytes required to fill cursor. Read more
1.0.0 · Source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · Source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · Source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · Source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
Source§

impl<R: Read + Seek, E: Endianness> Seek for FlacByteReader<R, E>

Source§

fn seek(&mut self, pos: SeekFrom) -> Result<u64>

Seek to an offset, in bytes, in a stream. Read more
1.55.0 · Source§

fn rewind(&mut self) -> Result<(), Error>

Rewind to the beginning of a stream. Read more
Source§

fn stream_len(&mut self) -> Result<u64, Error>

🔬This is a nightly-only experimental API. (seek_stream_len)
Returns the length of this stream (in bytes). Read more
1.51.0 · Source§

fn stream_position(&mut self) -> Result<u64, Error>

Returns the current seek position from the start of the stream. Read more
1.80.0 · Source§

fn seek_relative(&mut self, offset: i64) -> Result<(), Error>

Seeks relative to the current position. Read more

Auto Trait Implementations§

§

impl<R, E> Freeze for FlacByteReader<R, E>
where R: Freeze,

§

impl<R, E> RefUnwindSafe for FlacByteReader<R, E>

§

impl<R, E> Send for FlacByteReader<R, E>
where R: Send, E: Send,

§

impl<R, E> Sync for FlacByteReader<R, E>
where R: Sync, E: Sync,

§

impl<R, E> Unpin for FlacByteReader<R, E>
where R: Unpin, E: Unpin,

§

impl<R, E> UnwindSafe for FlacByteReader<R, E>
where R: UnwindSafe, E: 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.