From 5752c2dd68a8cefe140bde0019575f19edfbb48a Mon Sep 17 00:00:00 2001 From: Teknium <127238744+teknium1@users.noreply.github.com> Date: Thu, 3 Sep 2026 01:17:43 -0700 Subject: [PATCH] refactor(tools): iter-sentinel drain, fold ogg repair guard, speaker init tuple, PEP8 blank fix --- tools/tts_tool_delivery.py | 4 +--- tools/tts_tool_local.py | 1 + tools/tts_tool_providers.py | 2 +- tools/tts_tool_speaker.py | 11 +++-------- 4 files changed, 6 insertions(+), 12 deletions(-) diff --git a/tools/tts_tool_delivery.py b/tools/tts_tool_delivery.py index 5cb5cd6917..44ac43c5e7 100644 --- a/tools/tts_tool_delivery.py +++ b/tools/tts_tool_delivery.py @@ -344,9 +344,7 @@ def _sniff_audio_container(path: str) -> str: def _repair_ogg_container(file_str: str) -> str: """Ensure a ``.ogg`` path really holds Ogg: transcode in place, else rename to the sniffed real extension so platforms get an honest file instead of a 0-second voice bubble.""" - if not file_str.endswith(".ogg"): - return file_str - container = _sniff_audio_container(file_str) + container = _sniff_audio_container(file_str) if file_str.endswith(".ogg") else "ogg" if container in ("ogg", "unknown"): return file_str logger.info("TTS wrote %s bytes into a .ogg path (%s) — transcoding to real Ogg/Opus", container, file_str) diff --git a/tools/tts_tool_local.py b/tools/tts_tool_local.py index beb42c7172..96a08d2445 100644 --- a/tools/tts_tool_local.py +++ b/tools/tts_tool_local.py @@ -179,6 +179,7 @@ def _load_kittentts_model_for_config(tts_config: Dict[str, Any]) -> Tuple[Any, D m = KittenTTS(model_name) logger.info("[KittenTTS] Model loaded successfully") return m + return _tts_cache_get_or_load(_kittentts_model_cache, model_name, _load_kittentts_model), kt_config diff --git a/tools/tts_tool_providers.py b/tools/tts_tool_providers.py index edf27dce89..348a1ef5b8 100644 --- a/tools/tts_tool_providers.py +++ b/tools/tts_tool_providers.py @@ -450,7 +450,7 @@ def _generate_mistral_tts(text: str, output_path: str, tts_config: Dict[str, Any client_kwargs: Dict[str, Any] = {"api_key": api_key} if mi_config.get("base_url"): client_kwargs["server_url"] = mi_config["base_url"] # the Mistral SDK calls it server_url - Mistral = _origin()._import_mistral_client() + Mistral = _origin()._import_mistral_client() # ImportError must escape the RuntimeError wrap try: with Mistral(**client_kwargs) as client: response = client.audio.speech.complete( diff --git a/tools/tts_tool_speaker.py b/tools/tts_tool_speaker.py index 43013048b4..bb764d7e8c 100644 --- a/tools/tts_tool_speaker.py +++ b/tools/tts_tool_speaker.py @@ -68,12 +68,7 @@ def _play_via_tempfile(audio_iter: Iterable[bytes], stop_evt: threading.Event, s def _drain_chunks(chunk_queue: "queue.Queue[Optional[bytes]]") -> List[bytes]: """Collect one sentence's PCM chunks up to the ``None`` sentinel.""" - chunks: List[bytes] = [] - while True: - chunk = chunk_queue.get() - if chunk is None: - return chunks - chunks.append(chunk) + return list(iter(chunk_queue.get, None)) class _SyncSentencePipeline: @@ -142,8 +137,7 @@ class _StreamerPlayback: _CHUNK_QUEUE_MAX = 64 def __init__(self, streamer, stop_event: threading.Event): - self.streamer = streamer - self.stop_event = stop_event + self.streamer, self.stop_event = streamer, stop_event self.output_stream = self._open_output_stream() self._audio_queue: "queue.Queue[Optional[queue.Queue[Optional[bytes]]]]" = queue.Queue() self._prefetch_threads: List[threading.Thread] = [] @@ -217,6 +211,7 @@ class _StreamerPlayback: def _write_pcm(self, buf: bytes) -> None: self._current_stream.write(self._np.frombuffer(buf, dtype=" bool: """Close the broken PortAudio stream and open a fresh one after a failed write; False once ``_MAX_REINIT`` is exhausted (remaining sentences go through temp files)."""