Crate hound [] [src]

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

SampleWriter16

A writer that specifically only writes integer samples of 16 bits per sample.

WavIntoSamples

An iterator that yields samples of type S read from a WavReader.

WavReader

A reader that reads the WAVE format from the underlying reader.

WavSamples

An iterator that yields samples of type S read from a WavReader.

WavSpec

Specifies properties of the audio data.

WavWriter

A writer that accepts samples and writes the WAVE format.

Enums

Error

The error type for operations on WavReader and WavWriter.

SampleFormat

Specifies whether a sample is stored as an "IEEE Float" or an integer.

Traits

Sample

A type that can be used to represent audio samples.

Functions

read_wave_header

Reads the RIFF WAVE header, returns the supposed file size.

Type Definitions

Result

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