AzureTTSService: prevent word timestamp carryover on interruption

This commit is contained in:
Mark Backman
2026-01-07 15:39:37 -05:00
parent 23a9d3f4d7
commit 3f8373f76f
3 changed files with 42 additions and 10 deletions

View File

@@ -411,7 +411,12 @@ class AzureTTSService(WordTTSService, AzureBaseTTSService):
Args:
evt: Cancellation event.
"""
logger.error(f"Speech synthesis canceled: {evt.result.cancellation_details.reason}")
reason = evt.result.cancellation_details.reason
# User cancellation (from interruption) is expected, not an error
if reason == CancellationReason.CancelledByUser:
logger.debug(f"{self}: Speech synthesis canceled by user (interruption)")
else:
logger.error(f"{self}: Speech synthesis canceled: {reason}")
self._audio_queue.put_nowait(None)
async def push_frame(self, frame: Frame, direction: FrameDirection = FrameDirection.DOWNSTREAM):
@@ -440,6 +445,17 @@ class AzureTTSService(WordTTSService, AzureBaseTTSService):
"""
await super()._handle_interruption(frame, direction)
await self.stop_all_metrics()
# Stop Azure synthesis to prevent more word boundaries from being added
if self._speech_synthesizer:
try:
# stop_speaking_async() returns a ResultFuture
# We need to call .get() in a thread to wait for completion
result_future = self._speech_synthesizer.stop_speaking_async()
await asyncio.to_thread(result_future.get)
except Exception as e:
logger.error(f"{self} error stopping synthesis: {e}")
# Reset cumulative audio offset on interruption
self._cumulative_audio_offset = 0.0
# Clear the audio queue