pub struct FlacByteWriter<W: Write + Seek, E: Endianness> { /* private fields */ }Expand description
A FLAC writer which accepts 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<W: Write + Seek, E: Endianness> FlacByteWriter<W, E>
impl<W: Write + Seek, E: Endianness> FlacByteWriter<W, E>
Sourcepub fn new(
writer: W,
options: Options,
sample_rate: u32,
bits_per_sample: u32,
channels: u8,
total_bytes: Option<u64>,
) -> Result<Self, Error>
pub fn new( writer: W, options: Options, sample_rate: u32, bits_per_sample: u32, channels: u8, total_bytes: Option<u64>, ) -> Result<Self, Error>
Creates new FLAC writer with the given parameters
The writer should be positioned at the start of the file.
sample_rate must be between 0 (for non-audio streams) and 2²⁰.
bits_per_sample must be between 1 and 32.
channels must be between 1 and 8.
Note that if total_bytes is indicated,
the number of channel-independent samples written must
be equal to that amount or an error will occur when writing
or finalizing the stream.
§Errors
Returns I/O error if unable to write initial metadata blocks. Returns error if any of the encoding parameters are invalid.
Sourcepub fn new_cdda(
writer: W,
options: Options,
total_bytes: Option<u64>,
) -> Result<Self, Error>
pub fn new_cdda( writer: W, options: Options, total_bytes: Option<u64>, ) -> Result<Self, Error>
Creates new FLAC writer with CDDA parameters
The writer should be positioned at the start of the file.
Sample rate is 44100 Hz, bits-per-sample is 16, channels is 2.
Note that if total_bytes is indicated,
the number of channel-independent samples written must
be equal to that amount or an error will occur when writing
or finalizing the stream.
§Errors
Returns I/O error if unable to write initial metadata blocks. Returns error if any of the encoding parameters are invalid.
Sourcepub fn endian(
writer: W,
_endianness: E,
options: Options,
sample_rate: u32,
bits_per_sample: u32,
channels: u8,
total_bytes: Option<u64>,
) -> Result<Self, Error>
pub fn endian( writer: W, _endianness: E, options: Options, sample_rate: u32, bits_per_sample: u32, channels: u8, total_bytes: Option<u64>, ) -> Result<Self, Error>
Creates new FLAC writer in the given endianness with the given parameters
The writer should be positioned at the start of the file.
sample_rate must be between 0 (for non-audio streams) and 2²⁰.
bits_per_sample must be between 1 and 32.
channels must be between 1 and 8.
Note that if total_bytes is indicated,
the number of bytes written must
be equal to that amount or an error will occur when writing
or finalizing the stream.
§Errors
Returns I/O error if unable to write initial metadata blocks.
Sourcepub fn finalize(self) -> Result<(), Error>
pub fn finalize(self) -> Result<(), Error>
Attempt to finalize stream
It is necessary to finalize the FLAC encoder
so that it will write any partially unwritten samples
to the stream and update the crate::metadata::Streaminfo and crate::metadata::SeekTable blocks
with their final values.
Dropping the encoder will attempt to finalize the stream automatically, but will ignore any errors that may occur.
Source§impl<E: Endianness> FlacByteWriter<BufWriter<File>, E>
impl<E: Endianness> FlacByteWriter<BufWriter<File>, E>
Sourcepub fn create<P: AsRef<Path>>(
path: P,
options: Options,
sample_rate: u32,
bits_per_sample: u32,
channels: u8,
total_bytes: Option<u64>,
) -> Result<Self, Error>
pub fn create<P: AsRef<Path>>( path: P, options: Options, sample_rate: u32, bits_per_sample: u32, channels: u8, total_bytes: Option<u64>, ) -> Result<Self, Error>
Creates new FLAC file at the given path
sample_rate must be between 0 (for non-audio streams) and 2²⁰.
bits_per_sample must be between 1 and 32.
channels must be between 1 and 8.
Note that if total_bytes is indicated,
the number of bytes written must
be equal to that amount or an error will occur when writing
or finalizing the stream.
§Errors
Returns I/O error if unable to write initial metadata blocks.
Sourcepub fn create_cdda<P: AsRef<Path>>(
path: P,
options: Options,
total_bytes: Option<u64>,
) -> Result<Self, Error>
pub fn create_cdda<P: AsRef<Path>>( path: P, options: Options, total_bytes: Option<u64>, ) -> Result<Self, Error>
Creates new FLAC file with CDDA parameters at the given path
Sample rate is 44100 Hz, bits-per-sample is 16, channels is 2.
Note that if total_bytes is indicated,
the number of bytes written must
be equal to that amount or an error will occur when writing
or finalizing the stream.
§Errors
Returns I/O error if unable to write initial metadata blocks.
Trait Implementations§
Source§impl<W: Write + Seek, E: Endianness> Drop for FlacByteWriter<W, E>
impl<W: Write + Seek, E: Endianness> Drop for FlacByteWriter<W, E>
Source§impl<W: Write + Seek, E: Endianness> Write for FlacByteWriter<W, E>
impl<W: Write + Seek, E: Endianness> Write for FlacByteWriter<W, E>
Source§fn write(&mut self, buf: &[u8]) -> Result<usize>
fn write(&mut self, buf: &[u8]) -> Result<usize>
Writes a set of sample bytes to the FLAC file
Samples are signed and encoded in the stream’s given byte order.
Samples are then 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.
Source§fn flush(&mut self) -> Result<()>
fn flush(&mut self) -> Result<()>
Source§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
can_vector)1.0.0 · Source§fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>
Source§fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>
write_all_vectored)