Refactor _update_settings method in RimeNonJsonTTSService for improved readability and maintainability

This commit is contained in:
Gokul Js
2025-11-19 04:53:27 +05:30
parent 4393191166
commit 860d9c4f29

View File

@@ -845,61 +845,70 @@ class RimeNonJsonTTSService(InterruptibleTTSService):
except Exception as e: except Exception as e:
logger.error(f"{self} exception: {e}") logger.error(f"{self} exception: {e}")
yield ErrorFrame(error=f"{self} error: {e}") yield ErrorFrame(error=f"{self} error: {e}")
async def _update_settings(self, settings: Mapping[str, Any]): async def _update_settings(self, settings: Mapping[str, Any]):
"""Update service settings and reconnect if necessary. """Update service settings and reconnect if necessary.
Since all settings are WebSocket URL query parameters, Since all settings are WebSocket URL query parameters,
any setting change requires reconnecting to apply the new values. any setting change requires reconnecting to apply the new values.
""" """
needs_reconnect = False needs_reconnect = False
# Track previous values from self._settings only # Track previous values from self._settings only
prev_settings = self._settings.copy() prev_settings = self._settings.copy()
# Let parent class handle standard settings (voice, model, language) # Let parent class handle standard settings (voice, model, language)
await super()._update_settings(settings) await super()._update_settings(settings)
# Check if voice changed and update settings dict # Check if voice changed and update settings dict
if "voice" in settings or "voice_id" in settings: if "voice" in settings or "voice_id" in settings:
self._settings["speaker"] = self._voice_id self._settings["speaker"] = self._voice_id
if prev_settings.get("speaker") != self._voice_id: if prev_settings.get("speaker") != self._voice_id:
logger.info(f"Switching TTS voice to: [{self._voice_id}]") logger.info(f"Switching TTS voice to: [{self._voice_id}]")
needs_reconnect = True needs_reconnect = True
# Check if model changed and update settings dict # Check if model changed and update settings dict
if "model" in settings: if "model" in settings:
self._settings["modelId"] = self._model self._settings["modelId"] = self._model
if prev_settings.get("modelId") != self._model: if prev_settings.get("modelId") != self._model:
logger.info(f"Switching TTS model to: [{self._model}]") logger.info(f"Switching TTS model to: [{self._model}]")
needs_reconnect = True needs_reconnect = True
# Handle language explicitly # Handle language explicitly
if "language" in settings: if "language" in settings:
new_lang = self.language_to_service_language(settings["language"]) new_lang = self.language_to_service_language(settings["language"])
if new_lang and new_lang != prev_settings.get("lang"): if new_lang and new_lang != prev_settings.get("lang"):
logger.info(f"Updating language to: [{new_lang}]") logger.info(f"Updating language to: [{new_lang}]")
self._settings["lang"] = new_lang self._settings["lang"] = new_lang
needs_reconnect = True needs_reconnect = True
# Check other parameters # Check other parameters
for key in ["segment", "repetition_penalty", "temperature", "top_p"]: for key in ["segment", "repetition_penalty", "temperature", "top_p"]:
if key in settings and settings[key] != prev_settings.get(key): if key in settings and settings[key] != prev_settings.get(key):
logger.info(f"Updating {key} to: [{settings[key]}]") logger.info(f"Updating {key} to: [{settings[key]}]")
self._settings[key] = settings[key] self._settings[key] = settings[key]
needs_reconnect = True needs_reconnect = True
# Handle extra parameters # Handle extra parameters
for key, value in settings.items(): for key, value in settings.items():
if key not in ["voice", "voice_id", "model", "language", "segment", if key not in [
"repetition_penalty", "temperature", "top_p", "audio_format"]: "voice",
if value != prev_settings.get(key): "voice_id",
logger.info(f"Updating extra parameter {key} to: [{value}]") "model",
self._settings[key] = value "language",
needs_reconnect = True "segment",
"repetition_penalty",
# Reconnect if any setting changed "temperature",
if needs_reconnect: "top_p",
logger.debug("Settings changed, reconnecting WebSocket with new parameters") "audio_format",
await self._disconnect() ]:
if value != prev_settings.get(key):
logger.info(f"Updating extra parameter {key} to: [{value}]")
self._settings[key] = value
needs_reconnect = True
# Reconnect if any setting changed
if needs_reconnect:
logger.debug("Settings changed, reconnecting WebSocket with new parameters")
await self._disconnect()
await self._connect() await self._connect()