#wave #audio #codec #wav

hound

A wav encoding and decoding library

14 releases (stable)

3.4.0 Apr 7, 2018
3.3.1 Feb 18, 2018
3.3.0 Dec 2, 2017
3.2.0 Oct 14, 2017
0.1.0 Mar 31, 2015

#3 in Encoding

Download history 823/week @ 2018-09-19 1119/week @ 2018-09-26 1295/week @ 2018-10-03 1370/week @ 2018-10-10 1851/week @ 2018-10-17 1236/week @ 2018-10-24 1459/week @ 2018-10-31 1127/week @ 2018-11-07 1483/week @ 2018-11-14 1228/week @ 2018-11-21 1403/week @ 2018-11-28 1215/week @ 2018-12-05 1505/week @ 2018-12-12

3,739 downloads per month
Used in 24 crates (19 directly)

Apache-2.0

108KB
1.5K SLoC

Hound

A wav encoding and decoding library in Rust.

Build Status Crates.io version Changelog Documentation

Hound can read and write the WAVE audio format, an ubiquitous format for raw, uncompressed audio. The main motivation to write it was to test Claxon, a FLAC decoding library written in Rust.

Examples

The following example renders a 440 Hz sine wave, and stores it as a mono wav file with a sample rate of 44.1 kHz and 16 bits per sample.

use std::f32::consts::PI;
use std::i16;
use hound;

let spec = hound::WavSpec {
    channels: 1,
    sample_rate: 44100,
    bits_per_sample: 16,
    sample_format: hound::SampleFormat::Int,
};
let mut writer = hound::WavWriter::create("sine.wav", spec).unwrap();
for t in (0 .. 44100).map(|x| x as f32 / 44100.0) {
    let sample = (t * 440.0 * 2.0 * PI).sin();
    let amplitude = i16::MAX as f32;
    writer.write_sample((sample * amplitude) as i16).unwrap();
}

The file is finalized implicitly when the writer is dropped, call writer.finalize() to observe errors.

The following example computes the root mean square (RMS) of an audio file with at most 16 bits per sample.

use hound;

let mut reader = hound::WavReader::open("testsamples/pop.wav").unwrap();
let sqr_sum = reader.samples::<i16>()
                    .fold(0.0, |sqr_sum, s| {
    let sample = s.unwrap() as f64;
    sqr_sum + sample * sample
});
println!("RMS is {}", (sqr_sum / reader.len() as f64).sqrt());

Features

Read Write
Format PCMWAVEFORMAT, WAVEFORMATEX, WAVEFORMATEXTENSIBLE PCMWAVEFORMAT, WAVEFORMATEXTENSIBLE
Encoding Integer PCM, IEEE Float Integer PCM, IEEE Float
Bits per sample 8, 16, 24, 32 (integer), 32 (float) 8, 16, 24, 32 (integer), 32 (float)

Contributing

Contributions in the form of bug reports, feature requests, or pull requests are welcome. See contributing.md.

License

Hound is licensed under the Apache 2.0 license. It may be used in free software as well as closed-source applications, both for commercial and non-commercial use under the conditions given in the license. If you want to use Hound in your GPLv2-licensed software, you can add an exception to your copyright notice. Please do not open an issue if you disagree with the choice of license.

No runtime deps