#audio #riff #sample #wave #pcm

riff-wave

Simple methods for reading and writing PCM wave files

7 releases

Uses old Rust 2015

0.1.3 Jun 22, 2022
0.1.2 May 26, 2016
0.0.3 May 25, 2016

#72 in Audio

Download history 26/week @ 2022-03-14 25/week @ 2022-03-21 9/week @ 2022-03-28 11/week @ 2022-04-04 11/week @ 2022-04-11 12/week @ 2022-04-18 45/week @ 2022-04-25 55/week @ 2022-05-02 15/week @ 2022-05-09 111/week @ 2022-05-16 38/week @ 2022-05-23 73/week @ 2022-05-30 29/week @ 2022-06-06 11/week @ 2022-06-13 123/week @ 2022-06-20 57/week @ 2022-06-27

232 downloads per month
Used in 72 crates (2 directly)

Apache-2.0

67KB
1.5K SLoC

riff-wave

Simple methods for reading and writing PCM wave files.

Examples

Reading a wave file

extern crate riff_wave;

use std::fs::File;
use std::io::BufReader;

use riff_wave::{ReadResult, WaveReader};

fn read_wave() -> ReadResult<()> {
	let file = File::open("examples/hello.wav")?;
	let reader = BufReader::new(file);
	let mut wave_reader = WaveReader::new(reader)?;

	loop {
		wave_reader.read_sample_i16()?;
	}	
}

Writing a wave file

extern crate riff_wave;

use std::f32::consts::PI;
use std::fs::File;
use std::io::BufWriter;

use riff_wave::{WaveWriter, WriteResult};

fn write_wave() -> WriteResult<()> {		
	const SAMPLE_RATE: u32 = 44100;
	const FREQUENCY: f32 = 2.0 * PI * 440.0; // radian per second

	let file = File::create("examples/hello.wav")?;
	let writer = BufWriter::new(file);

	let mut wave_writer = WaveWriter::new(1, SAMPLE_RATE, 16, writer)?;

	for n in 0..SAMPLE_RATE {
		let phase = FREQUENCY * n as f32 / SAMPLE_RATE as f32;
		let sample = (phase.sin() * 0.8 * i16::MAX as f32) as i16;

		wave_writer.write_sample_i16(sample)?;
	}

	Ok(())
}

Documentation

Documentation is available via cargo doc or via this link.

Dependencies

~165KB