@@ -3,6 +3,9 @@ use std::sync::{Arc, Mutex};
33
44use anyhow:: { Result , anyhow} ;
55use sherpa_rs:: tts:: { TtsAudio , VitsTts , VitsTtsConfig } ;
6+ use tokio:: time:: Instant ;
7+
8+ use crate :: inference:: InferenceResult ;
69
710#[ derive( Clone ) ]
811pub struct TtsEngine {
@@ -34,12 +37,39 @@ impl TtsEngine {
3437
3538 /// # Errors
3639 /// - when the mutex is poisoned
37- pub fn synthesize ( & self , text : & str , sid : i32 , speed : f32 ) -> Result < TtsAudio > {
40+ pub fn synthesize (
41+ & self ,
42+ text : & str ,
43+ sid : i32 ,
44+ speed : f32 ,
45+ ) -> Result < InferenceResult < TtsAudio > > {
3846 let mut tts = self
3947 . tts
4048 . lock ( )
4149 . map_err ( |e| anyhow ! ( "TTS mutex poisoned: {e:#?}" ) ) ?;
4250
43- tts. create ( text, sid, speed) . map_err ( |e| anyhow ! ( "{e:#?}" ) )
51+ let start = Instant :: now ( ) ;
52+
53+ let speech = tts
54+ . create ( text, sid, speed)
55+ . map_err ( |e| anyhow ! ( "{e:#?}" ) ) ?;
56+
57+ let time = start. elapsed ( ) . as_secs_f64 ( ) ;
58+ let time_ms = time * 1000.0 ;
59+ #[ allow( clippy:: cast_precision_loss) ]
60+ let speedup = if time_ms > 0.0 {
61+ ( f64:: from ( speech. duration ) ) / time_ms
62+ } else {
63+ 0.0
64+ } ;
65+
66+ let result = InferenceResult {
67+ duration : u64:: try_from ( speech. duration ) . unwrap_or ( 0 ) ,
68+ output : speech,
69+ speedup,
70+ time,
71+ } ;
72+
73+ Ok ( result)
4474 }
4575}
0 commit comments