Struct FlacByteWriter

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

Source

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.

Source

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.

Source

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.

Source

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>

Source

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.

Source

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>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl<W: Write + Seek, E: Endianness> Write for FlacByteWriter<W, E>

Source§

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<()>

Flushes this output stream, ensuring that all intermediately buffered contents reach their destination. Read more
1.36.0 · Source§

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

Like write, except that it writes from a slice of buffers. Read more
Source§

fn is_write_vectored(&self) -> bool

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

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

Attempts to write an entire buffer into this writer. Read more
Source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · Source§

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

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<W, E> Freeze for FlacByteWriter<W, E>
where W: Freeze,

§

impl<W, E> RefUnwindSafe for FlacByteWriter<W, E>

§

impl<W, E> Send for FlacByteWriter<W, E>
where E: Send, W: Send,

§

impl<W, E> Sync for FlacByteWriter<W, E>
where E: Sync, W: Sync,

§

impl<W, E> Unpin for FlacByteWriter<W, E>
where E: Unpin, W: Unpin,

§

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