From ecf89205b750f0a1c8c736cadc37cb850a4f1245 Mon Sep 17 00:00:00 2001 From: efugier Date: Wed, 15 May 2024 11:37:50 +0200 Subject: [PATCH] tmp --- src/voice/mod.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/voice/mod.rs diff --git a/src/voice/mod.rs b/src/voice/mod.rs new file mode 100644 index 0000000..ec84c1a --- /dev/null +++ b/src/voice/mod.rs @@ -0,0 +1,46 @@ +use device_query::{DeviceQuery, DeviceState, Keycode}; +use std::process::{Child, Command}; +use std::thread; +use std::time::Duration; + +fn start_recording() -> Option { + if cfg!(target_os = "windows") { + Command::new("cmd") + .args(&["/C", "start", "rec", "output.wav"]) + .spawn() + .ok() + } else if cfg!(target_os = "macos") { + Command::new("sh") + .arg("-c") + .arg("sox -d output.wav") + .spawn() + .ok() + } else { + Command::new("arecord").arg("output.wav").spawn().ok() + } +} + +fn stop_recording(process: &mut Child) { + process.kill().expect("Failed to stop recording"); +} + +fn main() { + println!("Press any key. Press space to exit."); + + let device_state = DeviceState::new(); + let mut recording_process = start_recording().expect("Failed to start recording."); + + loop { + let keys: Vec = device_state.get_keys(); + + if keys.contains(&Keycode::Space) { + stop_recording(&mut recording_process); + println!("Recording stopped."); + break; + } + + thread::sleep(Duration::from_millis(100)); + } + + println!("Exiting program."); +}