feat(tts): Chatterbox-Provider (hohe Qualitaet + Voice-Cloning) anbinden
Loest #7. Der chatterbox-Stub wird durch eine echte Anbindung an den lokalen
Chatterbox-HTTP-Dienst ersetzt (POST /speak -> /status pollen -> GET /audio, WAV).
no_playback=true -> der Dienst spielt nicht lokal ab, liefert nur Bytes. WAV->PCM
(24 kHz, Resampling bei Bedarf). Waehlbar via tts_provider=chatterbox; piper bleibt
der schnelle Default (chatterbox ist ~echtzeit-langsam, dafuer klonbare Stimme).
- config: CHATTERBOX_BASE_URL/_VOICE/_LANG/_SPEED/_TIMEOUT; Registry verdrahtet
- Tests: gemockter httpx (Synthese, WAV/Resample, Job-Fehler, Stimmenwahl)
- Doku: README, Architektur (#7), deploy/README (GPU-Pinning per UUID, no_playback)
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-18 09:44:31 +02:00
|
|
|
import io
|
|
|
|
|
import wave
|
|
|
|
|
|
|
|
|
|
import httpx
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
import app.providers.tts.chatterbox as cb
|
|
|
|
|
from app.providers.tts.chatterbox import ChatterboxTTSProvider
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _wav(rate=24000, payload=b"\x01\x02" * 2400) -> bytes:
|
|
|
|
|
buf = io.BytesIO()
|
|
|
|
|
with wave.open(buf, "wb") as w:
|
|
|
|
|
w.setnchannels(1)
|
|
|
|
|
w.setsampwidth(2)
|
|
|
|
|
w.setframerate(rate)
|
|
|
|
|
w.writeframes(payload)
|
|
|
|
|
return buf.getvalue()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _install(monkeypatch, handler):
|
|
|
|
|
real = httpx.AsyncClient # echte Klasse sichern (vor dem Patch)
|
|
|
|
|
|
|
|
|
|
def make_client(*args, **kwargs):
|
|
|
|
|
kwargs.pop("timeout", None)
|
|
|
|
|
return real(transport=httpx.MockTransport(handler))
|
|
|
|
|
monkeypatch.setattr(cb.httpx, "AsyncClient", make_client)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _ok_handler(status="done", wav_rate=24000):
|
|
|
|
|
def handler(request: httpx.Request) -> httpx.Response:
|
|
|
|
|
p = request.url.path
|
|
|
|
|
if request.method == "POST" and p == "/speak":
|
|
|
|
|
return httpx.Response(200, json={"job_id": "j1", "status": "pending"})
|
|
|
|
|
if p == "/status":
|
|
|
|
|
return httpx.Response(200, json={"recent_jobs": [{"id": "j1", "status": status,
|
|
|
|
|
"error": "boom" if status == "error" else None}]})
|
|
|
|
|
if p == "/audio/j1":
|
|
|
|
|
return httpx.Response(200, content=_wav(wav_rate), headers={"content-type": "audio/wav"})
|
|
|
|
|
return httpx.Response(404)
|
|
|
|
|
return handler
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_synthesize_returns_pcm(monkeypatch):
|
|
|
|
|
_install(monkeypatch, _ok_handler())
|
|
|
|
|
p = ChatterboxTTSProvider("http://x:9999", voice="/ref.wav")
|
|
|
|
|
pcm = __import__("asyncio").run(p.synthesize("Hallo Welt"))
|
|
|
|
|
assert pcm == b"\x01\x02" * 2400 # 24000 Hz -> kein Resampling, Frames unveraendert
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_synthesize_wav_format(monkeypatch):
|
|
|
|
|
_install(monkeypatch, _ok_handler())
|
|
|
|
|
p = ChatterboxTTSProvider("http://x:9999")
|
|
|
|
|
wav = __import__("asyncio").run(p.synthesize("Hallo", audio_format="wav"))
|
|
|
|
|
assert wav.startswith(b"RIFF") and b"WAVE" in wav[:16]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_resample_when_rate_differs(monkeypatch):
|
|
|
|
|
_install(monkeypatch, _ok_handler(wav_rate=22050))
|
|
|
|
|
p = ChatterboxTTSProvider("http://x:9999", target_rate=24000)
|
|
|
|
|
pcm = __import__("asyncio").run(p.synthesize("Hallo"))
|
|
|
|
|
assert isinstance(pcm, bytes) and len(pcm) > 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_job_error_raises(monkeypatch):
|
|
|
|
|
_install(monkeypatch, _ok_handler(status="error"))
|
|
|
|
|
p = ChatterboxTTSProvider("http://x:9999")
|
|
|
|
|
with pytest.raises(RuntimeError):
|
|
|
|
|
__import__("asyncio").run(p.synthesize("Hallo"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_empty_text_raises(monkeypatch):
|
|
|
|
|
p = ChatterboxTTSProvider("http://x:9999")
|
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
|
__import__("asyncio").run(p.synthesize(" "))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_ref_voice_selection():
|
|
|
|
|
p = ChatterboxTTSProvider("http://x:9999", voice="/default.wav")
|
feat: Geräte-TTS, native Stimmen, Favicon, Auth-Gate, UI-Fixes
Web-UI / TTS:
- Geräte-TTS ("📱 Gerät"): Antwort wird on-device vorgelesen (Web Speech
API), Server sendet nur Text (text_only) -> spart Bandbreite/Kosten.
Mobil-Default, geräte-lokale Speicherung, iOS-Autoplay-Freischaltung.
- Vorlese-Symbol (🔊) je Bubble: Hybrid-Replay (Assistent-PCM gecacht,
Eingabe via /api/speak); SVG-Icon mit kontrastreicher Farbe.
- Kombiniertes Sprachmenü (Flex + feste Sprachen) statt separatem Modus-Menü.
- "Neues Gespräch"-Button (frische Session gegen Sprach-Trägheit).
- Dark-Mode: lesbare <option>-Popups (Kontrast-Fix).
- Favicon (SVG + PNG-Fallbacks) aus mund.png.
TTS-Backend:
- Sprache wird an alle TTS-Provider durchgereicht; Piper-Stimme folgt der
Sprache; Chatterbox mehrsprachig + cross-lingual.
- Native Referenz-Stimmen je Sprache (config/voices/<lang>.wav, FLEURS CC-BY),
loudness-normalisiert.
LLM-Sprache:
- Antwort folgt zuverlässig der gewählten Sprache (verstärkte Anweisung +
Erinnerung an der letzten Nutzer-Nachricht gegen History-Trägheit).
Admin / Auth:
- Wörterbuch: alle 8 Sprachen, Zeilen editierbar, alphabetische Sortierung.
- Web-UI hinter Auth-Gate (Redirect auf SSO_LOGIN_URL / 401); Favicons offen.
- Log-Tab: Hinweis, wenn der systemd-Dienst nicht aktiv ist.
- Einstellungen: Hinweis "pro Nutzer überschreibbar" bei Sprache/Modus/Qualität.
Doku (BEDIENUNGSANLEITUNG.md): Geräte-TTS §6.5.0, Fix/Flex §6.6, native
Stimmen §6.5.3, llama.cpp<->Ollama-Wechsel §4.7, Auth/SSO §7.4.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-20 13:12:04 +02:00
|
|
|
assert p._ref_voice(None, "de") == "/default.wav"
|
|
|
|
|
assert p._ref_voice("Zephyr", "de") == "/default.wav" # Cloud-Name -> Default
|
|
|
|
|
assert p._ref_voice("/custom.wav", "de") == "/custom.wav" # Pfad -> uebernommen
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_ref_voice_per_language(tmp_path):
|
|
|
|
|
# Native Sprach-Referenz gewinnt über die Default-Stimme, fehlt sie -> Default.
|
|
|
|
|
(tmp_path / "fr.wav").write_bytes(b"RIFF")
|
|
|
|
|
p = ChatterboxTTSProvider("http://x:9999", voice="/default.wav", voices_dir=str(tmp_path))
|
|
|
|
|
assert p._ref_voice(None, "fr") == str(tmp_path / "fr.wav") # native fr-Stimme
|
|
|
|
|
assert p._ref_voice(None, "de") == "/default.wav" # keine de.wav -> Default
|
|
|
|
|
assert p._ref_voice("/custom.wav", "fr") == "/custom.wav" # expliziter Pfad gewinnt
|