Expand description
§wav_io
Utilities for WAV files
This crate can read, write, split, resample WAV files
§Supported format
- PCM 8, 16, 24, 32 bits Int
- PCM 32, 64 bits Float
§Functoins
- read & write
- resample
- split by silence
- make sine wave & MML (music macro language)
§Quick Start
Write Wav file:
use std::f32::consts::PI;
fn main() {
// make sine wave
let head = wav_io::new_mono_header();
let mut samples: Vec<f32> = vec![];
for t in 0..head.sample_rate {
let v = ((t as f32 / head.sample_rate as f32) * 440.0 * 2.0 * PI).sin() * 0.6;
samples.push(v);
}
// write to file
let mut file_out = std::fs::File::create("./sine.wav").unwrap();
wav_io::write_to_file(&mut file_out, &head, &samples).unwrap();
}
Read Wav file:
use std::fs::File;
use std::f32::consts::PI;
fn main() {
// open file
let file_in = File::open("./sine.wav").unwrap();
// read from file
let (head, samples) = wav_io::read_from_file(file_in).unwrap();
// show header info
println!("header={:?}", head);
// show samples
println!("samples.len={}", samples.len());
for (i, v) in samples.iter().enumerate() {
println!("{}: {}v", i, v);
if (i > 32) { break; } // show first 32 samples
}
}
§Other Example
use std::fs::File;
use std::f32::consts::PI;
use wav_io::{reader, writer, utils, resample, splitter, header::*, tone};
fn main() {
// resample
let file_in = File::open("./tone.wav").unwrap();
let wav = reader::from_file(file_in).unwrap();
let new_sample_rate = 16_000;
let mut file_out = File::create("./tone-resample.wav").unwrap();
let samples2 = resample::linear(wav.samples, wav.header.channels, wav.header.sample_rate, new_sample_rate);
let mut wav2 = WavData{header: wav.header, samples: samples2};
wav2.header.sample_rate = new_sample_rate;
writer::to_file(&mut file_out, &wav2).unwrap();
// melody
let mut header = WavHeader::new_mono();
let mut samples = vec![];
let opt = tone::ToneOptions::new();
header.sample_rate = opt.sample_rate;
tone::write_mml(&mut samples, "l4 cegr rdfr egrr c1", &opt);
let mut file_out = File::create("./melody.wav").unwrap();
writer::to_file(&mut file_out, &WavData{header, samples}).unwrap();
// split
let file_in = File::open("./melody.wav").unwrap();
let mut wav = reader::from_file(file_in).unwrap();
let mut samples = wav.samples;
if wav.header.channels >= 2 {
samples = utils::stereo_to_mono(samples);
wav.header.channels = 1;
}
let range_vec = splitter::split_samples(&mut samples, wav.header.sample_rate, &splitter::WavSplitOption::new());
for (i, range) in range_vec.iter().enumerate() {
let fname = format!("sub-{}.wav", i);
println!("split_samples={}", fname);
let mut file_out = File::create(fname).unwrap();
let sub = splitter::sub_samples(&samples, *range);
let wav = WavData{header: wav.header.clone(), samples: sub};
writer::to_file(&mut file_out, &wav).unwrap();
}
}
Modules§
- Wav file header
- Wav file Reader
- Wav file Resampler
- Wav file Splitter
- Tone Generator
- Utilities
- Wav file Writer
Functions§
- convert f32 to i16 samples
- convert i16 to f32 samples
- new wav header
- new mono wav header
- new stereo wav header
- Read from Wav file
- Write Wav data
- Write to Wav file