@@ -23,7 +23,7 @@ mod pocket_april;
2323#[ path = "pocket_models.rs" ]
2424mod pocket_models;
2525
26- use pocket_april:: { prepare_april_prompt, AprilPocketTts } ;
26+ use pocket_april:: { prepare_april_prompt, AprilPocketTts , AprilSynthesisOutcome } ;
2727pub ( crate ) use pocket_models:: {
2828 april_model_info, PocketModelArtifact , APRIL_BUNDLE_ID , APRIL_MODEL_ID , APRIL_MODEL_REVISION ,
2929} ;
@@ -68,6 +68,16 @@ pub struct PocketTts {
6868 inner : Mutex < AprilPocketTts > ,
6969}
7070
71+ /// Result of a callback-driven Pocket synthesis request.
72+ #[ derive( Debug , Clone , PartialEq ) ]
73+ pub enum SynthesisOutcome {
74+ /// Synthesis finished and contains the same PCM exposed cumulatively to
75+ /// the callback.
76+ Complete ( Vec < f32 > ) ,
77+ /// The callback requested cancellation before synthesis completed.
78+ Interrupted ,
79+ }
80+
7181/// Load Buzz Desktop's pinned April INT8 model.
7282pub fn load_text_to_speech ( model_dir : & str ) -> Result < PocketTts , String > {
7383 let dir = PathBuf :: from ( model_dir) ;
@@ -104,27 +114,63 @@ impl PocketTts {
104114 /// Pocket detects language from text and this model uses one synthesis
105115 /// step, so `_lang` and `_steps` intentionally do not affect output.
106116 pub fn synth_chunk (
117+ & self ,
118+ text : & str ,
119+ lang : & str ,
120+ style : & VoiceStyle ,
121+ steps : usize ,
122+ ) -> Result < Vec < f32 > , String > {
123+ match self . synth_chunk_streaming ( text, lang, style, steps, |_, _| true ) ? {
124+ SynthesisOutcome :: Complete ( samples) => Ok ( samples) ,
125+ SynthesisOutcome :: Interrupted => Ok ( Vec :: new ( ) ) ,
126+ }
127+ }
128+
129+ /// Synthesize text while reporting cumulative PCM as decoder blocks finish.
130+ ///
131+ /// Callback sample buffers contain all PCM produced for this call so far.
132+ /// Their lengths never decrease, but equal lengths are allowed while the
133+ /// engine advances between internal model-safe text chunks. Returning
134+ /// `false` interrupts synthesis before the next decoder block.
135+ pub fn synth_chunk_streaming < F > (
107136 & self ,
108137 text : & str ,
109138 _lang : & str ,
110139 style : & VoiceStyle ,
111140 _steps : usize ,
112- ) -> Result < Vec < f32 > , String > {
141+ mut callback : F ,
142+ ) -> Result < SynthesisOutcome , String >
143+ where
144+ F : FnMut ( & [ f32 ] , f32 ) -> bool ,
145+ {
113146 let Some ( prepared) = prepare_april_prompt ( text) else {
114- return Ok ( Vec :: new ( ) ) ;
147+ return Ok ( SynthesisOutcome :: Complete ( Vec :: new ( ) ) ) ;
115148 } ;
116149 let mut engine = self
117150 . inner
118151 . lock ( )
119152 . map_err ( |_| "Pocket TTS engine lock poisoned" . to_string ( ) ) ?;
120153 let chunks = engine. split_prompt ( & prepared) ?;
154+ let chunk_count = chunks. len ( ) ;
121155 let mut samples = Vec :: new ( ) ;
122- for chunk in chunks {
156+ for ( chunk_index, chunk) in chunks. into_iter ( ) . enumerate ( ) {
157+ if chunk_index > 0 && !callback ( & samples, chunk_index as f32 / chunk_count as f32 ) {
158+ return Ok ( SynthesisOutcome :: Interrupted ) ;
159+ }
123160 let prepared = prepare_april_prompt ( & chunk)
124161 . ok_or_else ( || "Pocket TTS prompt chunk became empty" . to_string ( ) ) ?;
125- samples. extend ( engine. synth_chunk ( & prepared, style) ?) ;
162+ let outcome = engine. synth_chunk_streaming ( & prepared, style, |block, progress| {
163+ samples. extend_from_slice ( block) ;
164+ callback (
165+ & samples,
166+ ( chunk_index as f32 + progress) / chunk_count as f32 ,
167+ )
168+ } ) ?;
169+ if matches ! ( outcome, AprilSynthesisOutcome :: Interrupted ) {
170+ return Ok ( SynthesisOutcome :: Interrupted ) ;
171+ }
126172 }
127- Ok ( samples)
173+ Ok ( SynthesisOutcome :: Complete ( samples) )
128174 }
129175}
130176
@@ -163,4 +209,89 @@ mod tests {
163209 assert ! ( samples. iter( ) . all( |sample| sample. is_finite( ) ) ) ;
164210 assert ! ( samples. iter( ) . any( |sample| sample. abs( ) > 1.0e-6 ) ) ;
165211 }
212+
213+ #[ test]
214+ #[ ignore = "requires BUZZ_POCKET_TEST_MODEL_DIR" ]
215+ fn production_streaming_callbacks_are_cumulative_across_model_chunks ( ) {
216+ let dir = std:: env:: var ( "BUZZ_POCKET_TEST_MODEL_DIR" )
217+ . expect ( "set BUZZ_POCKET_TEST_MODEL_DIR to an April INT8 model directory" ) ;
218+ let engine = load_text_to_speech ( & dir) . expect ( "load April INT8 engine" ) ;
219+ let style = load_voice_style ( & Path :: new ( & dir) . join ( "reference_sample.wav" ) )
220+ . expect ( "load reference voice" ) ;
221+ engine
222+ . synth_chunk ( "Warm up." , "en" , & style, 1 )
223+ . expect ( "warm production engine" ) ;
224+ let text = "And sometimes, when I am certain the reader is rested, I will engage him with a sentence of considerable length, a sentence that burns with energy and builds with all the impetus of a crescendo, the roll of the drums, the crash of the cymbals–sounds that say listen to this, it is important." ;
225+ let mut reconstructed = Vec :: new ( ) ;
226+ let mut previous_len = 0 ;
227+ let mut saw_equal_repeat = false ;
228+ let mut callback_count = 0 ;
229+ let mut first_callback = None ;
230+ let started = std:: time:: Instant :: now ( ) ;
231+
232+ let outcome = engine
233+ . synth_chunk_streaming ( text, "en" , & style, 1 , |cumulative, _| {
234+ callback_count += 1 ;
235+ first_callback. get_or_insert_with ( || started. elapsed ( ) ) ;
236+ assert ! ( cumulative. len( ) >= previous_len) ;
237+ saw_equal_repeat |= cumulative. len ( ) == previous_len;
238+ reconstructed. extend_from_slice ( & cumulative[ previous_len..] ) ;
239+ previous_len = cumulative. len ( ) ;
240+ true
241+ } )
242+ . expect ( "stream through the production API" ) ;
243+ let SynthesisOutcome :: Complete ( samples) = outcome else {
244+ panic ! ( "uninterrupted synthesis must complete" ) ;
245+ } ;
246+ let total = started. elapsed ( ) ;
247+ let first_callback = first_callback. expect ( "decoder must produce a callback" ) ;
248+ let audio_duration =
249+ std:: time:: Duration :: from_secs_f64 ( samples. len ( ) as f64 / SAMPLE_RATE as f64 ) ;
250+ eprintln ! (
251+ "first_callback_ms={:.1} total_ms={:.1} audio_seconds={:.3} rtf={:.3} callbacks={callback_count}" ,
252+ first_callback. as_secs_f64( ) * 1000.0 ,
253+ total. as_secs_f64( ) * 1000.0 ,
254+ audio_duration. as_secs_f64( ) ,
255+ total. as_secs_f64( ) / audio_duration. as_secs_f64( ) ,
256+ ) ;
257+
258+ assert ! ( saw_equal_repeat) ;
259+ assert_eq ! ( reconstructed, samples) ;
260+ }
261+
262+ #[ test]
263+ #[ ignore = "requires BUZZ_POCKET_TEST_MODEL_DIR" ]
264+ fn production_streaming_callback_interrupts_after_first_decoder_block ( ) {
265+ let dir = std:: env:: var ( "BUZZ_POCKET_TEST_MODEL_DIR" )
266+ . expect ( "set BUZZ_POCKET_TEST_MODEL_DIR to an April INT8 model directory" ) ;
267+ let engine = load_text_to_speech ( & dir) . expect ( "load April INT8 engine" ) ;
268+ let style = load_voice_style ( & Path :: new ( & dir) . join ( "reference_sample.wav" ) )
269+ . expect ( "load reference voice" ) ;
270+ engine
271+ . synth_chunk ( "Warm up." , "en" , & style, 1 )
272+ . expect ( "warm production engine" ) ;
273+ let mut callback_at = None ;
274+ let started = std:: time:: Instant :: now ( ) ;
275+
276+ let outcome = engine
277+ . synth_chunk_streaming (
278+ "This sentence is long enough to require more than one decoder block." ,
279+ "en" ,
280+ & style,
281+ 1 ,
282+ |_, _| {
283+ callback_at = Some ( started. elapsed ( ) ) ;
284+ false
285+ } ,
286+ )
287+ . expect ( "interrupt production streaming" ) ;
288+ let callback_at = callback_at. expect ( "decoder must produce a callback" ) ;
289+ let cancellation_latency = started. elapsed ( ) . saturating_sub ( callback_at) ;
290+ eprintln ! (
291+ "callback_to_cancel_return_ms={:.1}" ,
292+ cancellation_latency. as_secs_f64( ) * 1000.0
293+ ) ;
294+
295+ assert ! ( matches!( outcome, SynthesisOutcome :: Interrupted ) ) ;
296+ }
166297}
0 commit comments