@@ -22,7 +22,10 @@ if (self.location.hostname === "localhost2") {
2222}
2323
2424const tts = await KokoroTTS . from_pretrained ( model_id , {
25- dtype : device === "wasm" ? "q8" : "fp32" , device,
25+ // --- THIS IS THE MODIFIED LINE ---
26+ dtype : "fp16" , // Use fp16 for better performance on mobile
27+ // --- END MODIFIED LINE ---
28+ device,
2629 progress_callback : ( progress ) => {
2730 self . postMessage ( { status : "loading_model_progress" , progress } ) ;
2831 }
@@ -33,8 +36,70 @@ const tts = await KokoroTTS.from_pretrained(model_id, {
3336
3437self . postMessage ( { status : "loading_model_ready" , voices : tts . voices , device } ) ;
3538
36- // Track how many buffers are currently in the queue
39+ // --- THIS IS THE MEMORY-SAFE QUEUE LOGIC ---
3740let bufferQueueSize = 0 ;
41+ const MAX_QUEUE_SIZE = 6 ; // Work ahead by 6 chunks (This is already set correctly)
42+ let shouldStop = false ;
43+ // --- END QUEUE LOGIC ---
44+
45+ self . addEventListener ( "message" , async ( e ) => {
46+ const { type, text, voice, speed } = e . data ; // <-- Get speed from the message
47+
48+ if ( type === "stop" ) {
49+ bufferQueueSize = 0 ;
50+ shouldStop = true ;
51+ console . log ( "Stop command received, stopping generation" ) ;
52+ return ;
53+ }
54+
55+ // --- THIS IS THE MEMORY-SAFE QUEUE LOGIC ---
56+ if ( type === "buffer_processed" ) {
57+ bufferQueueSize = Math . max ( 0 , bufferQueueSize - 1 ) ; // Free up a slot
58+ return ;
59+ }
60+ // --- END QUEUE LOGIC ---
61+
62+ if ( type === "generate" && text ) {
63+ shouldStop = false ;
64+ let chunks = splitTextSmart ( text , 300 ) ;
65+
66+ self . postMessage ( { status : "chunk_count" , count : chunks . length } ) ;
67+
68+ for ( const chunk of chunks ) {
69+ if ( shouldStop ) {
70+ console . log ( "Stopping audio generation" ) ;
71+ self . postMessage ( { status : "complete" } ) ;
72+ break ;
73+ }
74+ console . log ( chunk ) ;
75+
76+ // --- THIS IS THE MEMORY-SAFE QUEUE LOGIC ---
77+ // Wait if the queue is full
78+ while ( bufferQueueSize >= MAX_QUEUE_SIZE && ! shouldStop ) {
79+ console . log ( "Waiting for buffer space..." ) ;
80+ await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // Wait 1 sec
81+ if ( shouldStop ) break ;
82+ }
83+
84+ if ( shouldStop ) {
85+ console . log ( "Stopping after queue wait" ) ;
86+ self . postMessage ( { status : "complete" } ) ;
87+ break ;
88+ }
89+ // --- END QUEUE LOGIC ---
90+
91+ const audio = await tts . generate ( chunk , { voice, speed } ) ;
92+ let ab = audio . audio . buffer ;
93+
94+ bufferQueueSize ++ ; // Increment the queue size
95+ self . postMessage ( { status : "stream_audio_data" , audio : ab , text : chunk } , [ ab ] ) ;
96+ }
97+
98+ if ( ! shouldStop ) {
99+ self . postMessage ( { status : "complete" } ) ;
100+ }
101+ }
102+ } ) ; let bufferQueueSize = 0 ;
38103const MAX_QUEUE_SIZE = 6 ;
39104let shouldStop = false ;
40105
0 commit comments