Skip to main content

WavWriter

Struct WavWriter 

Source
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: fmt size 16.
  • IEEE float, up to 2 channels: fmt size 18 plus a fact chunk.
  • a container wider than the valid bits, or more than 2 channels: fmt size 40 (WAVE_FORMAT_EXTENSIBLE, channel mask 0), plus fact for float.

Implementations§

Source§

impl WavWriter<BufWriter<File>>

Source

pub fn create<P: AsRef<Path>>(path: P, spec: WavSpec) -> Result<Self>

Create (or truncate) a file and write the header for spec.

Source§

impl<W: Write + Seek> WavWriter<W>

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub fn duration(&self) -> u32

Frames (samples per channel) written so far.

Source

pub fn len(&self) -> u32

Samples written so far, across all channels.

Source

pub fn is_empty(&self) -> bool

Whether no sample has been written yet.

Source

pub fn spec(&self) -> WavSpec

The stream parameters.

Source

pub fn spec_ex(&self) -> WavSpecEx

The stream parameters plus the container width.

Trait Implementations§

Source§

impl<W: Write + Seek> Debug for WavWriter<W>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<W: Write + Seek> Drop for WavWriter<W>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<W> Freeze for WavWriter<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for WavWriter<W>
where W: RefUnwindSafe,

§

impl<W> Send for WavWriter<W>
where W: Send,

§

impl<W> Sync for WavWriter<W>
where W: Sync,

§

impl<W> Unpin for WavWriter<W>
where W: Unpin,

§

impl<W> UnsafeUnpin for WavWriter<W>
where W: UnsafeUnpin,

§

impl<W> UnwindSafe for WavWriter<W>
where W: UnwindSafe,

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

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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.