@@ -22,7 +22,7 @@ if (self.location.hostname === "localhost2") {
2222}
2323
2424const tts = await KokoroTTS . from_pretrained ( model_id , {
25- dtype : device === "wasm" ? "q8" : "fp32" , // This is the auto-select logic
25+ dtype : device === "wasm" ? "q8" : "fp32" ,
2626 device,
2727 progress_callback : ( progress ) => {
2828 self . postMessage ( { status : "loading_model_progress" , progress } ) ;
@@ -34,65 +34,51 @@ const tts = await KokoroTTS.from_pretrained(model_id, {
3434
3535self . postMessage ( { status : "loading_model_ready" , voices : tts . voices , device } ) ;
3636
37- // --- THIS IS THE MEMORY-SAFE QUEUE LOGIC ---
37+ // --- THIS IS THE MEMORY-SAFE QUEUE LOGIC FOR SENTENCES WITHIN A CHUNK ---
3838let bufferQueueSize = 0 ;
39- const MAX_QUEUE_SIZE = 6 ; // Work ahead by 6 chunks
39+ const MAX_QUEUE_SIZE = 6 ;
4040let shouldStop = false ;
4141// --- END QUEUE LOGIC ---
4242
4343self . addEventListener ( "message" , async ( e ) => {
44- const { type, text, voice, speed } = e . data ; // <-- Get speed from the message
44+ const { type, text, voice, speed } = e . data ;
4545
4646 if ( type === "stop" ) {
47- bufferQueueSize = 0 ;
4847 shouldStop = true ;
48+ bufferQueueSize = 0 ;
4949 console . log ( "Stop command received, stopping generation" ) ;
5050 return ;
5151 }
5252
53- // --- THIS IS THE MEMORY-SAFE QUEUE LOGIC ---
5453 if ( type === "buffer_processed" ) {
55- bufferQueueSize = Math . max ( 0 , bufferQueueSize - 1 ) ; // Free up a slot
54+ bufferQueueSize = Math . max ( 0 , bufferQueueSize - 1 ) ;
5655 return ;
5756 }
58- // --- END QUEUE LOGIC ---
5957
6058 if ( type === "generate" && text ) {
6159 shouldStop = false ;
62- let chunks = splitTextSmart ( text , 300 ) ;
6360
64- self . postMessage ( { status : "chunk_count" , count : chunks . length } ) ;
61+ // This now splits only the SMALL chunk it was given
62+ let sentences = splitTextSmart ( text , 300 ) ;
6563
66- for ( const chunk of chunks ) {
67- if ( shouldStop ) {
68- console . log ( "Stopping audio generation" ) ;
69- self . postMessage ( { status : "complete" } ) ;
70- break ;
71- }
72- console . log ( chunk ) ;
64+ for ( const sentence of sentences ) {
65+ if ( shouldStop ) break ;
7366
74- // --- THIS IS THE MEMORY-SAFE QUEUE LOGIC ---
75- // Wait if the queue is full
7667 while ( bufferQueueSize >= MAX_QUEUE_SIZE && ! shouldStop ) {
77- console . log ( "Waiting for buffer space..." ) ;
78- await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ; // Wait 1 sec
68+ await new Promise ( resolve => setTimeout ( resolve , 500 ) ) ;
7969 if ( shouldStop ) break ;
8070 }
81-
82- if ( shouldStop ) {
83- console . log ( "Stopping after queue wait" ) ;
84- self . postMessage ( { status : "complete" } ) ;
85- break ;
86- }
87- // --- END QUEUE LOGIC ---
71+ if ( shouldStop ) break ;
8872
89- const audio = await tts . generate ( chunk , { voice, speed } ) ;
90- let ab = audio . audio . buffer ;
73+ const audio = await tts . generate ( sentence , { voice, speed } ) ;
74+ if ( shouldStop ) break ;
9175
92- bufferQueueSize ++ ; // Increment the queue size
93- self . postMessage ( { status : "stream_audio_data" , audio : ab , text : chunk } , [ ab ] ) ;
76+ let ab = audio . audio . buffer ;
77+ bufferQueueSize ++ ;
78+ self . postMessage ( { status : "stream_audio_data" , audio : ab } , [ ab ] ) ;
9479 }
9580
81+ // When the loop finishes, this chunk is "complete"
9682 if ( ! shouldStop ) {
9783 self . postMessage ( { status : "complete" } ) ;
9884 }
0 commit comments