Skip to main content

WavWriter

Struct WavWriter 

Source
pub struct WavWriter<W: Write + Seek> { /* private fields */ }
Expand description

A streaming WAV writer that finalises a correct header.

Streaming synthesis does not know the sample count until the run ends, so a provisional header is written first and patched by WavWriter::finish. That seek-back is why the sink must be Seek: an unseekable sink cannot carry a correct length, and silently emitting a wrong one is the corruption this type exists to prevent.

If the run is cut short — cancellation, a full disk — calling finish still yields a valid file describing the samples that made it, which is the partial-output promise in plan §9.6.

Implementations§

Source§

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

Source

pub fn new(sink: W, sample_rate: u32) -> Result<Self>

Begin a file, writing a provisional header.

§Errors

If the provisional header cannot be written.

Source

pub fn write_samples(&mut self, pcm: &[f32]) -> Result<()>

Append one packet of decoded f32 samples.

§Errors

If the sink rejects the write. The count of samples already accepted stays accurate, so a later WavWriter::finish still describes the file truthfully.

Source

pub const fn samples_written(&self) -> usize

Samples accepted so far.

Source

pub const fn duration_millis(&self) -> u64

Duration of the audio written so far, in milliseconds.

Source

pub fn finish(self) -> Result<W>

Patch the header to the real length and flush.

§Errors

If seeking back to the header, rewriting it, or flushing fails.

Trait Implementations§

Source§

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

Source§

fn drop(&mut self)

Best-effort finalisation for a writer dropped without WavWriter::finish.

A dropped writer means an abnormal end — a panic, an early return, a cancelled run. Leaving the provisional zero-length header would make the file claim it contains no audio while holding a megabyte of it. The error is deliberately swallowed because Drop cannot report, which is exactly why finish exists and should be called explicitly.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, _span: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

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

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V