@@ -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 ( ) ;
@@ -366,7 +367,6 @@ class FallbackChunkedStream extends ChunkedStream {
366367 } ) ;
367368 audioReceived = true ;
368369 }
369- resampler . close ( ) ;
370370 }
371371
372372 // Verify audio was actually received - silent failures should trigger fallback
@@ -385,6 +385,8 @@ class FallbackChunkedStream extends ChunkedStream {
385385 } else {
386386 throw error ;
387387 }
388+ } finally {
389+ resampler ?. close ( ) ;
388390 }
389391 }
390392 const labels = this . adapter . ttsInstances . map ( ( t ) => t . label ) . join ( ', ' ) ;
@@ -444,6 +446,7 @@ class FallbackSynthesizeStream extends SynthesizeStream {
444446 this . adapter . markUnAvailable ( i ) ;
445447 continue ;
446448 }
449+ const resampler = this . adapter . createResamplerForTTS ( i ) ;
447450
448451 try {
449452 this . _logger . debug ( { tts : originalTts . label } , 'attempting TTS stream' ) ;
@@ -454,7 +457,6 @@ class FallbackSynthesizeStream extends SynthesizeStream {
454457 } ;
455458
456459 const stream = tts . stream ( { connOptions } ) ;
457- const resampler = this . adapter . createResamplerForTTS ( i ) ;
458460 let bufferIndex = 0 ;
459461 let streamOutputCompleted = false ;
460462 const forwardBufferToTTS = async ( ) => {
@@ -520,7 +522,6 @@ class FallbackSynthesizeStream extends SynthesizeStream {
520522 } ) ;
521523 this . audioPushed = true ;
522524 }
523- resampler . close ( ) ;
524525 }
525526 } finally {
526527 // processOutput and forwardBufferToTTS run in parallel.
@@ -530,6 +531,7 @@ class FallbackSynthesizeStream extends SynthesizeStream {
530531 // keep polling indefinitely, blocking fallback to the next TTS.
531532 // This flag tells it to exit early.
532533 streamOutputCompleted = true ;
534+ resampler ?. close ( ) ;
533535 }
534536 } ;
535537 const [ outputResult , forwardBufferResult ] = await Promise . allSettled ( [
0 commit comments