Skip to main content

voicepeak_cli/
audio.rs

1use std::path::Path;
2use std::process::Command as ProcessCommand;
3use tempfile::NamedTempFile;
4
5pub fn play_audio_and_cleanup(file_path: &Path) -> Result<(), Box<dyn std::error::Error>> {
6    let status = ProcessCommand::new("mpv")
7        .arg("--no-video")
8        .arg("--really-quiet")
9        // VOICEPEAK outputs mono audio. On some devices mpv's coreaudio output
10        // fails with mono and falls back to avfoundation, which cuts off the
11        // beginning and end of playback. Upmixing to stereo keeps coreaudio.
12        .arg("--audio-channels=stereo")
13        .arg(file_path)
14        .status()?;
15
16    if !status.success() {
17        return Err("mpv failed to play audio".into());
18    }
19
20    std::fs::remove_file(file_path)?;
21    Ok(())
22}
23
24pub fn create_temp_audio_file() -> Result<std::path::PathBuf, Box<dyn std::error::Error>> {
25    let temp_file = NamedTempFile::new()?;
26    let temp_path = temp_file.path().with_extension("wav");
27    temp_file.persist(&temp_path)?;
28    Ok(temp_path)
29}