refactor(tools): iter-sentinel drain, fold ogg repair guard, speaker init tuple, PEP8 blank fix

This commit is contained in:
Teknium
2026-09-03 01:17:43 -07:00
parent 58b0f218c4
commit 5752c2dd68
4 changed files with 6 additions and 12 deletions

View File

@@ -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)

View File

@@ -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

View File

@@ -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(

View File

@@ -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="<i2").reshape(-1, 1))
def _recover_stream(self) -> 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)."""