| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | use std::hint::black_box; |
| 13 | use std::time::Instant; |
| 14 | |
| 15 | use ec_riff::{RF64_THRESHOLD, WavOptions, WavReader, WavWriter}; |
| 16 | |
| 17 | fn read_once(path: &str) -> (usize, f64) { |
| 18 | let t = Instant::now(); |
| 19 | let mut reader = WavReader::open(path).expect("open wav"); |
| 20 | let samples = reader.read_all_i32().expect("read samples"); |
| 21 | let secs = t.elapsed().as_secs_f64(); |
| 22 | black_box(&samples); |
| 23 | (samples.len(), secs) |
| 24 | } |
| 25 | |
| 26 | fn main() { |
| 27 | let args: Vec<String> = std::env::args().collect(); |
| 28 | match args.get(1).map(String::as_str) { |
| 29 | Some("read") => { |
| 30 | let reps: usize = args.get(3).map_or(5, |s| s.parse().expect("reps")); |
| 31 | let bytes = std::fs::metadata(&args[2]).expect("stat").len(); |
| 32 | let (samples, _) = read_once(&args[2]); |
| 33 | let best = (0..reps).map(|_| read_once(&args[2]).1).fold(f64::MAX, f64::min); |
| 34 | println!( |
| 35 | "samples={samples} bytes={bytes} best_read_s={best:.4} mb_s={:.2} reps={reps}", |
| 36 | bytes as f64 / best / 1e6 |
| 37 | ); |
| 38 | } |
| 39 | Some("write") => { |
| 40 | let (input, out, mode) = (&args[2], &args[3], args[4].as_str()); |
| 41 | let reps: usize = args.get(5).map_or(5, |s| s.parse().expect("reps")); |
| 42 | let mut reader = WavReader::open(input).expect("open wav"); |
| 43 | let spec = reader.spec(); |
| 44 | let samples = reader.read_all_i32().expect("read samples"); |
| 45 | let frames = samples.len() as u64 / u64::from(spec.channels); |
| 46 | let options = match mode { |
| 47 | "riff" => WavOptions { expected_frames: frames, rf64_threshold: RF64_THRESHOLD }, |
| 48 | "rf64" => WavOptions { expected_frames: frames, rf64_threshold: 0 }, |
| 49 | _ => panic!("mode must be riff or rf64"), |
| 50 | }; |
| 51 | let data_bytes = samples.len() as u64 * u64::from(spec.bits_per_sample) / 8; |
| 52 | let mut best = f64::MAX; |
| 53 | for _ in 0..reps { |
| 54 | let t = Instant::now(); |
| 55 | let mut w = WavWriter::create_with_options(out, spec, options).expect("create"); |
| 56 | w.write_samples(&samples).expect("write samples"); |
| 57 | w.finalize().expect("finalize"); |
| 58 | best = best.min(t.elapsed().as_secs_f64()); |
| 59 | } |
| 60 | println!( |
| 61 | "mode={mode} samples={} data_bytes={data_bytes} best_write_s={best:.4} mb_s={:.2} reps={reps}", |
| 62 | samples.len(), |
| 63 | data_bytes as f64 / best / 1e6 |
| 64 | ); |
| 65 | } |
| 66 | _ => panic!("usage: benchp1 read <in.wav> [reps] | write <in.wav> <out.wav> <riff|rf64> [reps]"), |
| 67 | } |
| 68 | } |