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>
impl<R: Read, E: Endianness> FlacByteReader<R, E>
Sourcepub fn new(reader: R) -> Result<Self, Error>
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.
Sourcepub fn endian(reader: R, _endian: E) -> Result<Self, Error>
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§impl<R: Read + Seek, E: Endianness> FlacByteReader<R, E>
impl<R: Read + Seek, E: Endianness> FlacByteReader<R, E>
Sourcepub fn new_seekable(reader: R) -> Result<Self, Error>
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));Trait Implementations§
Source§impl<R: Read, E: Endianness> BufRead for FlacByteReader<R, E>
impl<R: Read, E: Endianness> BufRead for FlacByteReader<R, E>
Source§fn fill_buf(&mut self) -> Result<&[u8]>
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)
fn consume(&mut self, amt: usize)
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 moreSource§fn has_data_left(&mut self) -> Result<bool, Error>
fn has_data_left(&mut self) -> Result<bool, Error>
buf_read_has_data_left)read. Read more1.83.0 · Source§fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
fn skip_until(&mut self, byte: u8) -> Result<usize, Error>
byte or EOF is reached. Read more1.0.0 · Source§fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_line(&mut self, buf: &mut String) -> Result<usize, Error>
0xA byte) is reached, and append
them to the provided String buffer. Read moreSource§impl<R: Clone, E: Clone> Clone for FlacByteReader<R, E>
impl<R: Clone, E: Clone> Clone for FlacByteReader<R, E>
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<R: Read, E: Endianness> Metadata for FlacByteReader<R, E>
impl<R: Read, E: Endianness> Metadata for FlacByteReader<R, E>
Source§fn channel_count(&self) -> u8
fn channel_count(&self) -> u8
Source§fn channel_mask(&self) -> ChannelMask
fn channel_mask(&self) -> ChannelMask
Source§fn sample_rate(&self) -> u32
fn sample_rate(&self) -> u32
Source§fn bits_per_sample(&self) -> u32
fn bits_per_sample(&self) -> u32
Source§fn total_samples(&self) -> Option<u64>
fn total_samples(&self) -> Option<u64>
Source§fn decoded_len(&self) -> Option<u64>
fn decoded_len(&self) -> Option<u64>
Source§impl<R: Read, E: Endianness> Read for FlacByteReader<R, E>
impl<R: Read, E: Endianness> Read for FlacByteReader<R, E>
Source§fn read(&mut self, buf: &mut [u8]) -> Result<usize>
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>
fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>
read, except that it reads into a slice of buffers. Read moreSource§fn is_read_vectored(&self) -> bool
fn is_read_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize, Error>
buf. Read more1.0.0 · Source§fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>
buf. Read more1.6.0 · Source§fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>
buf. Read moreSource§fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)Source§fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>
read_buf)cursor. Read more1.0.0 · Source§fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
fn by_ref(&mut self) -> &mut Selfwhere
Self: Sized,
Read. Read moreSource§impl<R: Read + Seek, E: Endianness> Seek for FlacByteReader<R, E>
impl<R: Read + Seek, E: Endianness> Seek for FlacByteReader<R, E>
Source§fn seek(&mut self, pos: SeekFrom) -> Result<u64>
fn seek(&mut self, pos: SeekFrom) -> Result<u64>
1.55.0 · Source§fn rewind(&mut self) -> Result<(), Error>
fn rewind(&mut self) -> Result<(), Error>
Source§fn stream_len(&mut self) -> Result<u64, Error>
fn stream_len(&mut self) -> Result<u64, Error>
seek_stream_len)