Skip to main content

WavReader

Struct WavReader 

Source
pub struct WavReader<R> { /* private fields */ }
Expand description

Streaming WAV decoder over any io::Read.

Construction parses the RIFF header, the fmt chunk, and every chunk up to and including the data chunk header. Samples are then decoded from a 64 KiB block buffer that is refilled with bulk reads clamped to the declared data boundary, so the underlying reader is never read past the end of the audio payload.

Implementations§

Source§

impl WavReader<BufReader<File>>

Source

pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>

Open a WAV file from disk.

Source§

impl<R: Read> WavReader<R>

Source

pub fn new(reader: R) -> Result<Self>

Parse the headers from reader and position it at the first sample.

Fails on a malformed container (Error::Format), an encoding outside the crate’s scope (Error::Unsupported), or a stream that ends inside the headers (Error::Io).

Source

pub fn spec(&self) -> WavSpec

The stream parameters.

Source

pub fn spec_ex(&self) -> WavSpecEx

The stream parameters plus the container width.

Source

pub fn duration(&self) -> u32

Number of frames (samples per channel) the data chunk declares.

Source

pub fn len(&self) -> u32

Total number of samples across all channels the data chunk declares.

For a stream the declared count can exceed what exists. The sample iterators report that shortfall as an error.

Source

pub fn is_empty(&self) -> bool

Whether the data chunk declares zero samples.

Source

pub fn samples<S: Sample>(&mut self) -> Samples<'_, R, S> ⓘ

Iterate over the remaining samples, borrowing the reader.

Items are Result<S>. The first error fuses the iterator. A type that cannot represent the file’s samples yields one Error::TooWide.

Source

pub fn into_samples<S: Sample>(self) -> IntoSamples<R, S> ⓘ

Iterate over the remaining samples, consuming the reader.

Source

pub fn read_samples<S: Sample>(&mut self, buf: &mut [S]) -> Result<usize>

Decode samples into buf and return how many were written.

The buffer is filled completely unless the data chunk ends first, so a count below buf.len() means the end of the audio, and Ok(0) on a later call confirms it. A stream that ends before the declared data size returns Err instead of a short count.

Source

pub fn into_inner(self) -> R

Return the underlying reader.

Bytes pulled into the block buffer but not yet yielded are lost.

Source§

impl<R: Read + Seek> WavReader<R>

Source

pub fn seek(&mut self, frame: u32) -> Result<()>

Position the reader at frame (a frame is one sample per channel).

Seeking advances by the container width, so a 24-in-32 file seeks in 4-byte steps. frame may equal duration, which leaves no samples to read. Anything past that is InvalidInput.

Trait Implementations§

Source§

impl<R> Debug for WavReader<R>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<R> Freeze for WavReader<R>
where R: Freeze,

§

impl<R> RefUnwindSafe for WavReader<R>
where R: RefUnwindSafe,

§

impl<R> Send for WavReader<R>
where R: Send,

§

impl<R> Sync for WavReader<R>
where R: Sync,

§

impl<R> Unpin for WavReader<R>
where R: Unpin,

§

impl<R> UnsafeUnpin for WavReader<R>
where R: UnsafeUnpin,

§

impl<R> UnwindSafe for WavReader<R>
where R: 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.