extern crate hound;
use std::fs::File;
use std::i16;
use std::io;
use std::io::Read;
static NUM_CHANNELS: u16 = 1;
static BIT_DEPTH: u16 = 16;
static SAMPLE_RATE: u32 = 44_100;
pub fn read(filename: &str) -> Result<String, io::Error> {
let mut score_file = File::open(filename)?;
let mut score_contents = String::new();
score_file.read_to_string(&mut score_contents)?;
Ok(score_contents)
}
pub fn save(waveform: Vec<f32>, filename: &str) -> Result<(), hound::Error> {
let spec = hound::WavSpec {
channels: NUM_CHANNELS,
sample_rate: SAMPLE_RATE,
bits_per_sample: BIT_DEPTH,
sample_format: hound::SampleFormat::Int,
};
let mut writer = hound::WavWriter::create(filename, spec)?;
for sample in waveform {
let amplitude = i16::MAX as f32;
writer.write_sample((sample * amplitude) as i16)?;
}
Ok(writer.finalize()?)
}