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