pub struct WavWriter<W: Write + Seek> { /* private fields */ }Expand description
Block-buffered WAV encoder over any io::Write + io::Seek.
The header is written at construction with placeholder sizes. Samples
are encoded into a 64 KiB block that is flushed whole. finalize
writes the pad byte for an odd data size and patches the RIFF size,
the data size, and (for float formats) the fact frame count.
An incomplete last frame is truncated to whole frames in the header
and returns Error::UnfinishedFrame. Dropping the writer runs the
same steps and discards the result, so call finalize to observe
that error.
Header layout by spec:
- integer PCM, up to 2 channels, valid bits filling the container:
fmtsize 16. - IEEE float, up to 2 channels:
fmtsize 18 plus afactchunk. - a container wider than the valid bits, or more than 2 channels:
fmtsize 40 (WAVE_FORMAT_EXTENSIBLE, channel mask 0), plusfactfor float.
Implementations§
Source§impl<W: Write + Seek> WavWriter<W>
impl<W: Write + Seek> WavWriter<W>
Sourcepub fn new(writer: W, spec: WavSpec) -> Result<Self>
pub fn new(writer: W, spec: WavSpec) -> Result<Self>
Write the header for spec to writer, using the narrowest
container that holds bits_per_sample.
Sourcepub fn new_with_spec_ex(writer: W, spec: WavSpecEx) -> Result<Self>
pub fn new_with_spec_ex(writer: W, spec: WavSpecEx) -> Result<Self>
Write the header for spec to writer, with an explicit container
width (for example 24 valid bits in 4-byte containers).
Sourcepub fn write_sample<S: Sample>(&mut self, sample: S) -> Result<()>
pub fn write_sample<S: Sample>(&mut self, sample: S) -> Result<()>
Encode one sample.
Integer types write to integer files and float types to float
files. A mismatch, an f64 into a 32-bit float file, or an integer
value outside the file’s range is Error::TooWide.
Sourcepub fn write_samples<S: Sample>(&mut self, samples: &[S]) -> Result<()>
pub fn write_samples<S: Sample>(&mut self, samples: &[S]) -> Result<()>
Encode a slice of samples.
Type and size checks happen before any sample is written. A per-value range failure leaves the preceding samples written.
Sourcepub fn flush(&mut self) -> Result<()>
pub fn flush(&mut self) -> Result<()>
Push buffered samples to the sink and flush it. Sizes in the header
stay as they are. finalize updates them.
Sourcepub fn finalize(self) -> Result<()>
pub fn finalize(self) -> Result<()>
Write the pad byte if needed, patch the header sizes, and flush.
Returns Error::UnfinishedFrame when the sample count is not a
multiple of the channel count. The header is patched to a whole
number of frames first so the container stays readable. Drop runs
these same steps and discards the result, so Drop cannot report
Error::UnfinishedFrame. Call this method to observe that error.
Sample bytes written before the incomplete final frame may remain in
the sink past the declared RIFF boundary. The declared sizes still
bound a reader, and a physical truncation is not possible over a
generic Write + Seek sink.