Struct hound::SampleWriter16
source · [−]Expand description
A writer that specifically only writes integer samples of 16 bits per sample.
The writer buffers written samples internally so they can be written in a single batch later on. This has two advantages when performance is important:
- There is no need for error handling during writing, only on flush. This eliminates a lot of branches.
- The buffer can be written once, which reduces the overhead of the write
call. Because writing to an
io::BufWriter
is implemented with amemcpy
(even for single bytes), there is a large overhead to writing small amounts of data such as a 16-bit sample. By writing large blocks (or by not usingBufWriter
) this overhead can be avoided.
A SampleWriter16
can be obtained by calling WavWriter::get_i16_writer
.
Implementations
sourceimpl<'parent, W: Write + Seek> SampleWriter16<'parent, W>
impl<'parent, W: Write + Seek> SampleWriter16<'parent, W>
sourcepub fn write_sample<S: Sample>(&mut self, sample: S)
pub fn write_sample<S: Sample>(&mut self, sample: S)
Writes a single sample for one channel.
WAVE interleaves channel data, so the channel that this writes the sample to depends on previous writes.
Unlike WavWriter::write_sample()
, no range check is performed. Only
the least significant 16 bits are considered, everything else is
discarded. Apart from that check, this method is more efficient than
WavWriter::write_sample()
, because it can avoid dispatching on the
number of bits. That was done already when the SampleWriter16
was
constructed.
Note that nothing is actually written until flush()
is called.
sourcepub unsafe fn write_sample_unchecked<S: Sample>(&mut self, sample: S)
pub unsafe fn write_sample_unchecked<S: Sample>(&mut self, sample: S)
Like write_sample()
, but does not perform a bounds check when writing
to the internal buffer.
It is the responsibility of the programmer to ensure that no more samples are written than allocated when the writer was created.