@@ -324,6 +324,8 @@ class FallbackChunkedStream extends ChunkedStream {
324324 this . adapter . markUnAvailable ( i ) ;
325325 continue ;
326326 }
327+ const resampler = this . adapter . createResamplerForTTS ( i ) ;
328+
327329 try {
328330 this . _logger . debug ( { tts : tts . label } , 'attempting TTS synthesis' ) ;
329331 const connOptions : APIConnectOptions = {
@@ -332,7 +334,6 @@ class FallbackChunkedStream extends ChunkedStream {
332334 } ;
333335 const stream = tts . synthesize ( this . inputText , connOptions , this . abortSignal ) ;
334336 let audioReceived = false ;
335- const resampler = this . adapter . createResamplerForTTS ( i ) ;
336337 for await ( const audio of stream ) {
337338 if ( this . abortController . signal . aborted ) {
338339 stream . close ( ) ;
@@ -384,6 +385,8 @@ class FallbackChunkedStream extends ChunkedStream {
384385 } else {
385386 throw error ;
386387 }
388+ } finally {
389+ resampler ?. close ( ) ;
387390 }
388391 }
389392 const labels = this . adapter . ttsInstances . map ( ( t ) => t . label ) . join ( ', ' ) ;
@@ -443,6 +446,7 @@ class FallbackSynthesizeStream extends SynthesizeStream {
443446 this . adapter . markUnAvailable ( i ) ;
444447 continue ;
445448 }
449+ const resampler = this . adapter . createResamplerForTTS ( i ) ;
446450
447451 try {
448452 this . _logger . debug ( { tts : originalTts . label } , 'attempting TTS stream' ) ;
@@ -453,7 +457,6 @@ class FallbackSynthesizeStream extends SynthesizeStream {
453457 } ;
454458
455459 const stream = tts . stream ( { connOptions } ) ;
456- const resampler = this . adapter . createResamplerForTTS ( i ) ;
457460 let bufferIndex = 0 ;
458461 let streamOutputCompleted = false ;
459462 const forwardBufferToTTS = async ( ) => {
@@ -575,6 +578,8 @@ class FallbackSynthesizeStream extends SynthesizeStream {
575578 } else {
576579 throw error ;
577580 }
581+ } finally {
582+ resampler ?. close ( ) ;
578583 }
579584 }
580585 await readInputLLMStream . catch ( ( ) => { } ) ;
0 commit comments