FlacSampleWriter

Struct FlacSampleWriter 

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

A FLAC writer which accepts samples as signed integers

§Example

use flac_codec::{
    encode::{FlacSampleWriter, Options},
    decode::FlacSampleReader,
};
use std::io::{Cursor, Seek};

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

let mut writer = FlacSampleWriter::new(
    &mut flac,           // our wrapped writer
    Options::default(),  // default encoding options
    44100,               // sample rate
    16,                  // bits-per-sample
    1,                   // channel count
    Some(1000),          // total samples
).unwrap();

// write 1000 samples
let written_samples = (0..1000).collect::<Vec<i32>>();
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 = FlacSampleReader::new(flac).unwrap();

// read 1000 samples
let mut read_samples = vec![0; 1000];
assert!(matches!(reader.read(&mut read_samples), Ok(1000)));

// ensure they match
assert_eq!(read_samples, written_samples);

Implementations§

Source§

impl<W: Write + Seek> FlacSampleWriter<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(&mut self, samples: &[i32]) -> Result<(), Error>

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

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

This may output 0 or more actual FLAC frames, depending on the quantity of samples and the amount previously written.

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 FlacSampleWriter<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 FlacSampleWriter<W>
where W: Freeze,

§

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

§

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

§

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

§

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

§

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