use log::{debug, info, warn};
use std::io::Write;
use std::process::{Command, Stdio};
pub fn run_piper(
piper_bin: Option<&str>,
text: &str,
model_path: &str,
config_path: Option<&str>,
output_path: &str,
) -> std::io::Result<()> {
let piper_bin = piper_bin.unwrap_or("piper");
let config_path: String = config_path
.map(|c| c.to_string())
.unwrap_or_else(|| format!("{}.json", model_path));
let mut child = Command::new(piper_bin)
.arg("-m")
.arg(model_path)
.arg("-c")
.arg(&config_path)
.arg("-f")
.arg(output_path)
.stdin(Stdio::piped())
.spawn()?;
debug!("Spawned piper child, writing text to stdin...");
if let Some(stdin) = child.stdin.as_mut() {
stdin.write_all(text.as_bytes())?;
}
debug!("Waiting for piper...");
let status = child.wait()?;
if !status.success() {
warn!("Piper exited with error status: {:?}", status);
} else {
info!("piper exited successfully and wrote: {:?}", output_path);
}
Ok(())
}