-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Child> { | ||
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<Keycode> = 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."); | ||
} |