fix(tts): handle empty audio from kokoro_onnx.create() gracefully
Browse files- visualnovel/tts.py +11 -1
visualnovel/tts.py
CHANGED
|
@@ -169,11 +169,21 @@ class KokoroTTS:
|
|
| 169 |
fallback[seed % len(fallback)] if fallback else next(iter(self._available_voices))
|
| 170 |
)
|
| 171 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
cache_key = hashlib.sha256(f"{text}\x00{voice_id}\x00{seed}".encode()).hexdigest()
|
| 173 |
if cache_key in self._cache:
|
| 174 |
return self._cache[cache_key]
|
| 175 |
|
| 176 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 177 |
|
| 178 |
buf = io.BytesIO()
|
| 179 |
sf.write(buf, samples, sample_rate, format="WAV")
|
|
|
|
| 169 |
fallback[seed % len(fallback)] if fallback else next(iter(self._available_voices))
|
| 170 |
)
|
| 171 |
|
| 172 |
+
text = text.strip()
|
| 173 |
+
if not text:
|
| 174 |
+
return None
|
| 175 |
+
|
| 176 |
cache_key = hashlib.sha256(f"{text}\x00{voice_id}\x00{seed}".encode()).hexdigest()
|
| 177 |
if cache_key in self._cache:
|
| 178 |
return self._cache[cache_key]
|
| 179 |
|
| 180 |
+
try:
|
| 181 |
+
samples, sample_rate = self._kokoro.create(text, voice=voice_id, speed=1.0, lang="en-us")
|
| 182 |
+
except (ValueError, Exception):
|
| 183 |
+
return None
|
| 184 |
+
|
| 185 |
+
if samples is None or (hasattr(samples, "__len__") and len(samples) == 0):
|
| 186 |
+
return None
|
| 187 |
|
| 188 |
buf = io.BytesIO()
|
| 189 |
sf.write(buf, samples, sample_rate, format="WAV")
|