speaker_helper.client module
Asynchronous client for the Voicebox text-to-speech REST engine.
Module summary
VoiceboxClient is the thin, well-typed layer speaker-helper puts
between application code and the Voicebox HTTP API. It hides three awkward
details of that API:
Profiles. Every synthesis needs a
profile_id. This client creates a preset profile once (idempotently — reusing an existing one) from the engine’s preset voices, preferring a voice whose language matches the target.Two synthesis paths.
POST /generate/streamreturns WAV bytes synchronously but refuses to run until the engine model is downloaded; the asyncPOST /generatepath does trigger the download. The client streams by default and transparently falls back to the async path (enqueue → poll the SSE status → fetch/audio/{id}) the first time, when the model is absent.Voice discovery.
GET /profiles/presets/{engine}is normalised into typedVoiceobjects.
httpx and soundfile are imported lazily so importing speaker-helper
stays cheap for callers that only need its types.
Usage example
>>> import asyncio
>>> from speaker_helper.config import Settings
>>> from speaker_helper.client import VoiceboxClient
>>> async def demo() -> float:
... async with VoiceboxClient(Settings.from_mapping({"engine": "kokoro"})) as c:
... result = await c.synthesize("Bonjour le monde.")
... return result.duration_s
>>> # asyncio.run(demo()) # requires a running Voicebox
- class speaker_helper.client.VoiceboxClient(settings)[source]
Bases:
objectA typed async client over the Voicebox TTS REST API.
- Parameters:
settings (Settings) – Connection and voice configuration. See
speaker_helper.config.Settings.
Notes
The client owns an
httpx.AsyncClient. Use it as an async context manager (async with) or callaclose()when done. A single client caches the resolvedprofile_idso repeated syntheses skip the profile bootstrap.- async aclose()[source]
Close the underlying HTTP client and release its connections.
- Return type:
None
- async clone_voice(name, samples, *, language=None)[source]
Clone a voice from reference
samplesand use it for synthesis.- Parameters:
name (str) – Profile name for the clone (idempotency key). A matching profile is reused rather than duplicated.
samples (list of VoiceSample) – Reference recordings. Any sample without a
reference_textis transcribed withvocal-helperbefore upload.language (str or None) – Language of the cloned voice (defaults to the configured language).
- Returns:
The cloned profile id (also set as this client’s active profile).
- Return type:
- Raises:
VoiceboxError – On a Voicebox error.
- async ensure_profile()[source]
Return a usable
profile_id, creating a preset one idempotently.- Returns:
The profile id to pass to synthesis calls.
- Return type:
Notes
Reuses an existing profile for the resolved preset voice rather than creating a duplicate (Voicebox rejects duplicate names), so a fresh client never fails on the second run. Safe under concurrency via an internal lock.
- async health()[source]
Return the Voicebox
/healthpayload.- Returns:
The server health document (backend variant, gpu availability, …).
- Return type:
- Raises:
VoiceboxError – If the server is unreachable or returns a non-2xx status.
- async synthesize(text, *, language=None)[source]
Synthesise
textinto a singleAudioResult.- Parameters:
- Returns:
The synthesised WAV plus measured duration and compute time.
- Return type:
- Raises:
ValueError – If
textis empty or whitespace-only.VoiceboxError – On a Voicebox error or an undecodable response.
Notes
Uses
POST /generate/stream(synchronous WAV bytes). The first call for a not-yet-downloaded engine model transparently falls back to the async/generatepath, which triggers the download.
- exception speaker_helper.client.VoiceboxError[source]
Bases:
RuntimeErrorRaised when Voicebox returns an error or an unexpected response.