Crate hound

source ·
Expand description

Hound, a wav encoding and decoding library.

Examples

The following example renders a 440 Hz sine wave, and stores it as 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();
}
writer.finalize().unwrap();

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());

Structs

  • A writer that specifically only writes integer samples of 16 bits per sample.
  • An iterator that yields samples of type S read from a WavReader.
  • A reader that reads the WAVE format from the underlying reader.
  • An iterator that yields samples of type S read from a WavReader.
  • Specifies properties of the audio data.
  • Specifies properties of the audio data, as well as the layout of the stream.
  • A writer that accepts samples and writes the WAVE format.

Enums

  • The error type for operations on WavReader and WavWriter.
  • Specifies whether a sample is stored as an “IEEE Float” or an integer.

Traits

  • A type that can be used to represent audio samples.

Functions

Type Aliases

  • A type for results generated by Hound where the error type is hard-wired.