1 // Hound -- A wav encoding and decoding library in Rust 2 // Copyright (C) 2015 Ruud van Asseldonk 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // A copy of the License has been included in the root of the repository. 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, 9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 10 // See the License for the specific language governing permissions and 11 // limitations under the License. 12 13 // This example computes the mean value and rms of a file, where samples are 14 // first interpreted as 16-bit signed integer, and then as a 16-bit unsigned 15 // integer. This should allow us to determine whether the samples stored are 16 // signed or unsigned: for signed the average value is expected to be 0, and 17 // for unsigned the value is expected to be 2^16 - 1. 18 // Note that this example is not a particularly good example of proper coding 19 // style; it does not handle failure properly, and it assumes that the provided 20 // file has 16 bits per sample. 21 22 // TODO: This example should probably be removed, it is just here for verifying 23 // and assumption at this point. 24 25 extern crate hound; 26 27 use std::env; 28 main()29 fn main() { 30 let fname = env::args().nth(1).expect("no file given"); 31 let mut reader = hound::WavReader::open(&fname).unwrap(); 32 let samples: Vec<i16> = reader.samples().map(|s| s.unwrap()).collect(); 33 34 let (ts, tu, n) = samples.iter().fold((0.0, 0.0, 0.0), |(ts, tu, n), &s| { 35 let signed = s as f64; 36 let unsigned = (s as u16) as f64; 37 (ts + signed, tu + unsigned, n + 1.0) 38 }); 39 let ms = ts / n; 40 let mu = tu / n; 41 println!("mean signed: {} (should be 0, deviation is {})", ms, ms.abs()); 42 println!("mean unsigned: {} (should be 2^16 - 1, deviation is {})", mu, (mu - 32767.0).abs()); 43 44 let (ts, tu) = samples.iter().fold((0.0, 0.0), |(ts, tu), &s| { 45 let ds = s as f64 - ms; 46 let du = (s as u16) as f64 - mu; 47 (ts + ds * ds, tu + du * du) 48 }); 49 let rmss = (ts / n).sqrt(); 50 let rmsu = (tu / n).sqrt(); 51 println!("rms signed: {}", rmss); 52 println!("rms unsigned: {}", rmsu); 53 } 54