Support runtime language updates in Azure STT
Extract recognizer setup/teardown into _connect/_disconnect so _update_settings can reconnect when language changes at runtime.
This commit is contained in:
@@ -159,23 +159,15 @@ class AzureSTTService(STTService):
|
|||||||
return language_to_azure_language(language)
|
return language_to_azure_language(language)
|
||||||
|
|
||||||
async def _update_settings(self, delta: STTSettings) -> dict[str, Any]:
|
async def _update_settings(self, delta: STTSettings) -> dict[str, Any]:
|
||||||
"""Apply a settings delta.
|
"""Apply a settings delta and reconnect if language changed."""
|
||||||
|
|
||||||
Settings are stored but not applied to the active recognizer.
|
|
||||||
"""
|
|
||||||
changed = await super()._update_settings(delta)
|
changed = await super()._update_settings(delta)
|
||||||
|
|
||||||
# TODO: someday we could reconnect here to apply updated settings.
|
if "language" in changed:
|
||||||
# Code might look something like the below:
|
self._speech_config.speech_recognition_language = (
|
||||||
# if "language" in changed:
|
self._settings.language or language_to_azure_language(Language.EN_US)
|
||||||
# self._speech_config.speech_recognition_language = self._settings.language
|
)
|
||||||
# if self._speech_recognizer:
|
await self._disconnect()
|
||||||
# # Requires refactoring to set up and tear down recognizer, as
|
await self._connect()
|
||||||
# # language is applied at recognizer initialization
|
|
||||||
# await self._disconnect()
|
|
||||||
# await self._connect()
|
|
||||||
|
|
||||||
self._warn_unhandled_updated_settings(changed)
|
|
||||||
|
|
||||||
return changed
|
return changed
|
||||||
|
|
||||||
@@ -202,14 +194,32 @@ class AzureSTTService(STTService):
|
|||||||
async def start(self, frame: StartFrame):
|
async def start(self, frame: StartFrame):
|
||||||
"""Start the speech recognition service.
|
"""Start the speech recognition service.
|
||||||
|
|
||||||
Initializes the Azure speech recognizer with audio stream configuration
|
|
||||||
and begins continuous speech recognition.
|
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
frame: Frame indicating the start of processing.
|
frame: Frame indicating the start of processing.
|
||||||
"""
|
"""
|
||||||
await super().start(frame)
|
await super().start(frame)
|
||||||
|
await self._connect()
|
||||||
|
|
||||||
|
async def stop(self, frame: EndFrame):
|
||||||
|
"""Stop the speech recognition service.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: Frame indicating the end of processing.
|
||||||
|
"""
|
||||||
|
await super().stop(frame)
|
||||||
|
await self._disconnect()
|
||||||
|
|
||||||
|
async def cancel(self, frame: CancelFrame):
|
||||||
|
"""Cancel the speech recognition service.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
frame: Frame indicating cancellation.
|
||||||
|
"""
|
||||||
|
await super().cancel(frame)
|
||||||
|
await self._disconnect()
|
||||||
|
|
||||||
|
async def _connect(self):
|
||||||
|
"""Initialize the Azure speech recognizer and begin continuous recognition."""
|
||||||
if self._audio_stream:
|
if self._audio_stream:
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -231,37 +241,15 @@ class AzureSTTService(STTService):
|
|||||||
error_msg=f"Uncaught exception during initialization: {e}", exception=e
|
error_msg=f"Uncaught exception during initialization: {e}", exception=e
|
||||||
)
|
)
|
||||||
|
|
||||||
async def stop(self, frame: EndFrame):
|
async def _disconnect(self):
|
||||||
"""Stop the speech recognition service.
|
"""Stop recognition and close audio streams."""
|
||||||
|
|
||||||
Cleanly shuts down the Azure speech recognizer and closes audio streams.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
frame: Frame indicating the end of processing.
|
|
||||||
"""
|
|
||||||
await super().stop(frame)
|
|
||||||
|
|
||||||
if self._speech_recognizer:
|
|
||||||
self._speech_recognizer.stop_continuous_recognition_async()
|
|
||||||
|
|
||||||
if self._audio_stream:
|
|
||||||
self._audio_stream.close()
|
|
||||||
|
|
||||||
async def cancel(self, frame: CancelFrame):
|
|
||||||
"""Cancel the speech recognition service.
|
|
||||||
|
|
||||||
Immediately stops recognition and closes resources.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
frame: Frame indicating cancellation.
|
|
||||||
"""
|
|
||||||
await super().cancel(frame)
|
|
||||||
|
|
||||||
if self._speech_recognizer:
|
if self._speech_recognizer:
|
||||||
self._speech_recognizer.stop_continuous_recognition_async()
|
self._speech_recognizer.stop_continuous_recognition_async()
|
||||||
|
self._speech_recognizer = None
|
||||||
|
|
||||||
if self._audio_stream:
|
if self._audio_stream:
|
||||||
self._audio_stream.close()
|
self._audio_stream.close()
|
||||||
|
self._audio_stream = None
|
||||||
|
|
||||||
@traced_stt
|
@traced_stt
|
||||||
async def _handle_transcription(
|
async def _handle_transcription(
|
||||||
|
|||||||
Reference in New Issue
Block a user