#[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
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
impl WavCodec
Sourcepub fn resolve(tag: u16, bits_per_sample: u16) -> Result<Self, DecodeError>
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.
Sourcepub const fn format_tag(self) -> u16
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.
Sourcepub const fn bits_per_sample(self) -> u16
pub const fn bits_per_sample(self) -> u16
The wBitsPerSample a file carrying this encoding declares.
Sourcepub const fn bytes_per_sample(self) -> usize
pub const fn bytes_per_sample(self) -> usize
How many bytes one sample occupies in the payload.
Sourcepub const fn sample_format(self) -> Option<SampleFormat>
pub const fn sample_format(self) -> Option<SampleFormat>
The linear PCM format this encoding is, or None for the companded
ones.
Sourcepub const fn law(self) -> Option<G711Law>
pub const fn law(self) -> Option<G711Law>
The G.711 companding law this encoding is, or None for the linear
ones.
Sourcepub fn decode(self, bytes: &[u8], output: &mut Vec<f32>) -> usize
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.