Skip to main content

Crate wav_codec

Crate wav_codec 

Source
Expand description

WAV (RIFF/WAVE) reader and writer over std I/O.

wav_codec reads and writes the uncompressed WAV encodings that real files carry: PCM at 8, 16, 24, and 32 bits (packed or in wider containers via WAVE_FORMAT_EXTENSIBLE) and IEEE float at 32 and 64 bits. Both directions work over the std traits, so any io::Read source decodes and any io::Write + io::Seek sink encodes.

§Reading

use wav_codec::WavReader;

let mut reader = WavReader::open("input.wav")?;
let spec = reader.spec();
println!("{} Hz, {} channels", spec.sample_rate, spec.channels);
let samples: Vec<i16> = reader.samples::<i16>().collect::<Result<_, _>>()?;

Every sample item is a Result. A stream that ends inside a sample, or short of the declared data size, yields exactly one Err and then the iterator is fused, so corruption cannot pass as a short clean read.

§Writing

use wav_codec::{SampleFormat, WavSpec, WavWriter};

let spec = WavSpec {
    channels: 1,
    sample_rate: 44100,
    bits_per_sample: 16,
    sample_format: SampleFormat::Int,
};
let mut writer = WavWriter::create("sine.wav", spec)?;
for n in 0..44100u32 {
    let t = n as f32 / 44100.0;
    let v = (t * 440.0 * 2.0 * std::f32::consts::PI).sin();
    writer.write_sample((v * i16::MAX as f32) as i16)?;
}
writer.finalize()?;

finalize writes the pad byte for an odd data size and patches the RIFF and data sizes. An incomplete last frame returns Error::UnfinishedFrame after the header is patched to a whole number of frames. Dropping a writer runs the same steps and discards the result, so Drop cannot report UnfinishedFrame. Call finalize to observe that error.

§Sample types

file encodingreadable aswritable from
8-bit PCM (offset binary)i8, i16, i32i8, i16, i32 (in range)
16-bit PCMi16, i32i8, i16, i32 (in range)
24-bit PCM, 3 or 4 bytesi32i8, i16, i32 (in range)
32-bit PCMi32i8, i16, i32
32-bit floatf32, f64f32
64-bit floatf64f32, f64

8-bit files are unsigned on disk and exposed as i8 with the value stored_byte - 128. A read with a type too narrow for the file, or a type of the wrong class, fails with Error::TooWide.

§Scope

Out of scope, rejected with a clean error: compressed format tags (ADPCM, a-law, mu-law, MP3), RF64, RIFX, W64, and any WAVE_FORMAT_EXTENSIBLE GUID other than PCM and IEEE float. The crate is an adapter over std::io, so no_std is unsupported.

Structs§

IntoSamples
Owning sample iterator from WavReader::into_samples.
Samples
Borrowing sample iterator from WavReader::samples.
WavReader
Streaming WAV decoder over any io::Read.
WavSpec
Stream parameters: channel count, sample rate, precision, and encoding.
WavSpecEx
A WavSpec plus the container width, for files whose samples sit in containers wider than their valid bits.
WavWriter
Block-buffered WAV encoder over any io::Write + io::Seek.

Enums§

Error
Errors from reading or writing a WAV stream.
SampleFormat
How samples are encoded on disk.

Traits§

Sample
A sample type the reader can decode into and the writer can encode from.

Type Aliases§

Result
Result alias with this crate’s Error.