FlacChannelWriter

Struct FlacChannelWriter 

Source
pub struct FlacChannelWriter<W: Write + Seek> { /* private fields */ }
Expand description

A FLAC writer which accepts samples as channels of signed integers

§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<W: Write + Seek> FlacChannelWriter<W>

Source

pub fn new( writer: W, options: Options, sample_rate: u32, bits_per_sample: u32, channels: u8, total_samples: Option<u64>, ) -> Result<Self, Error>

Creates new FLAC writer with the given parameters

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_samples is indicated, the number of 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_samples: Option<u64>, ) -> Result<Self, Error>

Creates new FLAC writer with CDDA parameters

Sample rate is 44100 Hz, bits-per-sample is 16, channels is 2.

Note that if total_samples is indicated, the number of 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 write<C, S>(&mut self, channels: C) -> Result<(), Error>
where C: AsRef<[S]>, S: AsRef<[i32]>,

Given a set of channels containing samples, writes them to the FLAC file

Channels should be a slice-able set of sample slices, like: [[left₀ , left₁ , left₂ , …] , [right₀ , right₁ , right₂ , …]]

The number of channels must be identical to the channel count indicated when intializing the encoder.

The number of samples in each channel must also be identical.

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 FlacChannelWriter<BufWriter<File>>

Source

pub fn create<P: AsRef<Path>>( path: P, options: Options, sample_rate: u32, bits_per_sample: u32, channels: u8, total_samples: 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_samples: Option<u64>, ) -> Result<Self, Error>

Creates new FLAC file at the given path with CDDA parameters

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.

Auto Trait Implementations§

§

impl<W> Freeze for FlacChannelWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for FlacChannelWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for FlacChannelWriter<W>
where W: Send,

§

impl<W> Sync for FlacChannelWriter<W>
where W: Sync,

§

impl<W> Unpin for FlacChannelWriter<W>
where W: Unpin,

§

impl<W> UnwindSafe for FlacChannelWriter<W>
where 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.