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>
impl<W: Write + Seek> FlacChannelWriter<W>
Sourcepub fn new(
writer: W,
options: Options,
sample_rate: u32,
bits_per_sample: u32,
channels: u8,
total_samples: Option<u64>,
) -> Result<Self, Error>
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.
Sourcepub fn new_cdda(
writer: W,
options: Options,
total_samples: Option<u64>,
) -> Result<Self, Error>
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.
Sourcepub fn write<C, S>(&mut self, channels: C) -> Result<(), Error>
pub fn write<C, S>(&mut self, channels: C) -> Result<(), Error>
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.
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 FlacChannelWriter<BufWriter<File>>
impl FlacChannelWriter<BufWriter<File>>
Sourcepub 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>
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.
Sourcepub fn create_cdda<P: AsRef<Path>>(
path: P,
options: Options,
total_samples: Option<u64>,
) -> Result<Self, Error>
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.