Skip to main content

WavCodec

Enum WavCodec 

Source
#[non_exhaustive]
pub enum WavCodec { PcmU8, PcmI16, PcmI24, PcmI32, Float32, Float64, ALaw, MuLaw, }
Expand description

An encoding a WAV file can carry and this crate can decode.

Deliberately not “a SampleFormat plus a law”. WAV is little-endian and carries six of the twelve SampleFormats, so a type parameterised on SampleFormat would be able to name I16Be, a file that cannot exist, and every function taking one would need a failure case for it. The eight variants here are exactly the eight (wFormatTag, wBitsPerSample) pairs this crate reads and writes, so format_tag, bits_per_sample and the writer are total.

#[non_exhaustive]: a consumer matching on it needs a _ arm.

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

PcmU8

Unsigned 8-bit PCM, offset by 128. wFormatTag 1, 8 bits.

§

PcmI16

Signed 16-bit little-endian PCM. wFormatTag 1, 16 bits.

§

PcmI24

Signed 24-bit little-endian PCM, packed into three bytes. wFormatTag 1, 24 bits.

§

PcmI32

Signed 32-bit little-endian PCM. wFormatTag 1, 32 bits.

§

Float32

IEEE 754 binary32, little-endian. wFormatTag 3, 32 bits.

§

Float64

IEEE 754 binary64, little-endian. wFormatTag 3, 64 bits.

§

ALaw

ITU-T G.711 A-law. wFormatTag 6, 8 bits.

§

MuLaw

ITU-T G.711 mu-law. wFormatTag 7, 8 bits.

Implementations§

Source§

impl WavCodec

Source

pub fn resolve(tag: u16, bits_per_sample: u16) -> Result<Self, DecodeError>

The encoding a (wFormatTag, wBitsPerSample) pair names.

On the pair, never on the tag. Tag 1 covers four widths and tag 3 covers two; a reader that dispatched on the tag alone would decode a 24-bit file at a 16-bit stride and produce noise rather than an error. The step-0 audit found decibri already doing this correctly and named it the thing most implementations get wrong.

use decibri_decode::WavCodec;

assert_eq!(WavCodec::resolve(1, 24).unwrap(), WavCodec::PcmI24);
assert_eq!(WavCodec::resolve(7, 8).unwrap(), WavCodec::MuLaw);
// Same tag, unsupported width: rejected, and the rejection says which.
assert!(WavCodec::resolve(1, 20).is_err());
§Errors

DecodeError::UnsupportedSampleFormat for a tag this crate carries at a width it does not, and DecodeError::UnsupportedCodec for a tag it does not carry at all. The two are separate because they tell a caller different things: the first says the file is PCM and this build cannot read that width, the second says the file is not PCM.

Source

pub const fn format_tag(self) -> u16

The wFormatTag a file carrying this encoding declares.

Total: every variant is one a WAV file can name and this crate can write.

Source

pub const fn bits_per_sample(self) -> u16

The wBitsPerSample a file carrying this encoding declares.

Source

pub const fn bytes_per_sample(self) -> usize

How many bytes one sample occupies in the payload.

Source

pub const fn sample_format(self) -> Option<SampleFormat>

The linear PCM format this encoding is, or None for the companded ones.

Source

pub const fn law(self) -> Option<G711Law>

The G.711 companding law this encoding is, or None for the linear ones.

Source

pub fn decode(self, bytes: &[u8], output: &mut Vec<f32>) -> usize

Appends every whole sample in bytes to output, and returns how many it appended.

Straight through to SampleFormat::decode or G711Law::decode: there is no WAV-specific conversion anywhere in this crate, so a file decoded through the container and a headerless stream decoded through PcmDecoder give the same samples by construction rather than by agreement.

Source

pub fn encode(self, samples: &[f32], output: &mut Vec<u8>) -> usize

Appends samples to output in this encoding, and returns how many bytes it appended.

Trait Implementations§

Source§

impl Clone for WavCodec

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for WavCodec

Source§

impl Debug for WavCodec

Source§

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

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

impl Eq for WavCodec

Source§

impl Hash for WavCodec

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for WavCodec

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for WavCodec

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.