Skip to main content

aha/exec/
voxcpm.rs

1//! VoxCPM exec implementation for CLI `run` subcommand
2
3use std::time::Instant;
4
5use anyhow::{Ok, Result};
6
7use crate::models::voxcpm::generate::VoxCPMGenerate;
8use crate::{exec::ExecModel, utils::get_file_path};
9
10pub struct VoxCPMExec;
11
12impl ExecModel for VoxCPMExec {
13    fn run(input: &[String], output: Option<&str>, weight_path: &str) -> Result<()> {
14        let input_text = &input[0];
15        let target_text = if input_text.starts_with("file://") {
16            // let path = &input[7..];
17            let path = get_file_path(input_text)?;
18            std::fs::read_to_string(path)?
19        } else {
20            input_text.clone()
21        };
22
23        let i_start = Instant::now();
24        let mut voxcpm_generate = VoxCPMGenerate::init(weight_path, None, None)?;
25        let i_duration = i_start.elapsed();
26        println!("Time elapsed in load model is: {:?}", i_duration);
27
28        let i_start = Instant::now();
29        let audio = voxcpm_generate.inference(
30            target_text,
31            // Some("啥子小师叔,打狗还要看主人,你再要继续,我就是你的对手".to_string()), // todo args
32            // Some("file://./assets/audio/voice_01.wav".to_string()),                     // todo args
33            None,
34            None,
35            2,
36            100, // max_len (voxcpm uses 100 vs OpenBMB/VoxCPM1.5's 4096)
37            10,
38            2.0,
39            6.0,
40        )?;
41        let i_duration = i_start.elapsed();
42        println!("Time elapsed in generate is: {:?}", i_duration);
43
44        let output_path = if let Some(out) = output {
45            out.to_string()
46        } else {
47            let timestamp = std::time::SystemTime::now()
48                .duration_since(std::time::UNIX_EPOCH)?
49                .as_secs();
50            format!("voxcpm_{}.wav", timestamp)
51        };
52
53        let sample_rate = voxcpm_generate.sample_rate();
54        crate::utils::audio_utils::save_wav_mono(&audio, &output_path, sample_rate as u32)?;
55
56        println!("Output saved to: {}", output_path);
57
58        Ok(())
59    }
60}