Struct hound::SampleWriter16

source ·
pub struct SampleWriter16<'parent, W>
where W: Write + Seek + 'parent,
{ /* private fields */ }
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 a memcpy (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 using BufWriter) this overhead can be avoided.

A SampleWriter16 can be obtained by calling WavWriter::get_i16_writer.

Implementations§

source§

impl<'parent, W: Write + Seek> SampleWriter16<'parent, W>

source

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.

source

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.

source

pub fn flush(self) -> Result<()>

Flush the internal buffer to the underlying writer.

§Panics

Panics if insufficient samples (less than specified when the writer was constructed) have been written with write_sample().

Auto Trait Implementations§

§

impl<'parent, W> Freeze for SampleWriter16<'parent, W>

§

impl<'parent, W> RefUnwindSafe for SampleWriter16<'parent, W>
where W: RefUnwindSafe,

§

impl<'parent, W> Send for SampleWriter16<'parent, W>
where W: Send,

§

impl<'parent, W> Sync for SampleWriter16<'parent, W>
where W: Sync,

§

impl<'parent, W> Unpin for SampleWriter16<'parent, W>

§

impl<'parent, W> !UnwindSafe for SampleWriter16<'parent, W>

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>,

§

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>,

§

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.