| 1 | //! Runs a 48 kHz mono 16-bit WAV through the mic chain and writes every stage next to it. |
| 2 | //! Usage: cargo run -p calling --release --example mic_chain_wav -- <input.wav> <output-dir> [auto|low|off] |
| 3 | |
| 4 | use calling::audio::{ClipGenerator, FRAME_SAMPLES}; |
| 5 | use calling::mic_chain::MicChain; |
| 6 | use calling::wav_dump::StageDumps; |
| 7 | use calling::{MicSettings, NoiseSuppression}; |
| 8 | |
| 9 | fn main() { |
| 10 | let mut arguments = std::env::args().skip(1); |
| 11 | let (Some(input), Some(output)) = (arguments.next(), arguments.next()) else { |
| 12 | eprintln!("usage: mic_chain_wav <input.wav> <output-dir> [auto|low|off]"); |
| 13 | std::process::exit(2); |
| 14 | }; |
| 15 | let level = arguments |
| 16 | .next() |
| 17 | .and_then(|key| NoiseSuppression::from_key(&key)) |
| 18 | .unwrap_or_default(); |
| 19 | let bytes = std::fs::read(&input).expect("read input"); |
| 20 | let mut clip = ClipGenerator::from_wav(&bytes).expect("48 kHz mono 16-bit WAV"); |
| 21 | let frames = bytes.len().saturating_sub(44) / 2 / FRAME_SAMPLES; |
| 22 | let settings = MicSettings { |
| 23 | noise_suppression: level, |
| 24 | music_mode: false, |
| 25 | }; |
| 26 | let dumps = StageDumps::create(std::path::Path::new(&output)).expect("create output dir"); |
| 27 | let mut chain = MicChain::new( |
| 28 | settings.apm_config(), |
| 29 | settings.ml_noise_suppression(), |
| 30 | Some(dumps), |
| 31 | ); |
| 32 | let waiting = std::time::Instant::now(); |
| 33 | while settings.ml_noise_suppression() |
| 34 | && !calling::noise_stage::is_ready() |
| 35 | && waiting.elapsed().as_secs() < 30 |
| 36 | { |
| 37 | std::thread::sleep(std::time::Duration::from_millis(20)); |
| 38 | } |
| 39 | let started = std::time::Instant::now(); |
| 40 | for _ in 0..frames { |
| 41 | let mut frame = clip.next_frame(); |
| 42 | chain.process(&mut frame); |
| 43 | } |
| 44 | eprintln!("{frames} frames in {:?}", started.elapsed()); |
| 45 | } |