Skip to content

About

OpenAI-compatible text-to-speech endpoint that reads each sentence with the Piper voice of its language, through speaches

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

voice-router

An OpenAI-compatible text-to-speech endpoint (POST /v1/audio/speech) that reads each sentence with the voice of its language. It sits in front of speaches and its Piper voices, which each speak one language only: a German voice reads English with a German accent. Clients such as Open WebUI send all text with one fixed voice; voice-router does the switching.

For each request it:

  1. cuts the text into sentences,
  2. detects each sentence's language offline with lingua-go (the models are compiled into the binary),
  3. joins neighbouring sentences in the same language and has speaches speak each run with that language's voice, as raw PCM,
  4. returns the pieces as one WAV file (or raw PCM), with a short pause where the language changes.

A sentence keeps the fallback language (default German) unless the detector is sure (confidence ≥ 0.7) and the sentence has at least 3 words. Short sentences ("Okay.", "Hallo Jan!") and German full of English words ("Das Meeting ist um 10 Uhr.") are therefore read in the fallback voice.

Privacy: the text and audio go only to speaches. Nothing is logged but the number, language and length of the segments and the timings. The language detector makes no network calls.

Run locally

SPEACHES_URL=http://127.0.0.1:8000 go run ./cmd/voice-router
curl -s http://127.0.0.1:8080/v1/audio/speech -H 'Content-Type: application/json' \
  -d '{"model":"auto","input":"Hier ist die Zusammenfassung. The pod restarted twice."}' -o out.wav

speaches needs every model from VOICES downloaded (POST /v1/models/<id> on speaches).

API

Described in api/openapi.yaml, the source of truth: oapi-codegen generates the wire types and a typed server interface into internal/httpapi/oapi, and every request is validated against the spec. After changing the spec: go generate ./... && go test ./....

Method and path What it does
POST /v1/audio/speech {"model":"auto","input":"…","response_format":"wav"}. model must be auto; voice is ignored; response_format is wav (default) or pcm (16-bit mono at SAMPLE_RATE); speed 0.25 to 4. Input up to 4096 characters. Errors use OpenAI's {"error":{"message","type"}} with 400, 502 (speaches failed) or 504 (timeout)
GET /healthz ok. Doesn't call speaches, so a probe never wakes its host
GET /metrics Prometheus: voice_router_requests_total{result}, voice_router_segments_total{language}, voice_router_request_duration_seconds, voice_router_audio_seconds_total

Differences from OpenAI's API: the default format is wav, and mp3, opus, aac and flac are not offered (joining PCM and writing one WAV header needs no encoder). Open WebUI accepts WAV and converts it to MP3 itself.

Configuration

All settings are environment variables:

Variable Default Meaning
SPEACHES_URL (required) Base URL of speaches, e.g. http://10.42.0.1:11436
LISTEN_ADDR :8080 Listen address
VOICES de=speaches-ai/piper-de_DE-thorsten-medium:thorsten,en=speaches-ai/piper-en_US-ryan-high:ryan lang=model:voice, comma-separated, at least two ISO 639-1 languages
FALLBACK_LANGUAGE de Language for unsure or short sentences; needs a voice
MIN_CONFIDENCE 0.7 Detector confidence (0 to 1) needed to leave the fallback
MIN_WORDS 3 Words a sentence needs to leave the fallback
SAMPLE_RATE 22050 Hz asked of speaches and returned (Piper medium and high voices)
PAUSE 150ms Silence where the language changes
SPEACHES_TIMEOUT 170s Per call to speaches, including waking its host

Deploy

The Helm chart is in charts/voice-router and is published to oci://ghcr.io/subpathdev/charts/voice-router, the image to ghcr.io/subpathdev/voice-router. Set config.speachesURL; turn on networkPolicy and allow only your gateway and the way to speaches. The API has no authentication of its own: put a gateway with authentication in front.

Releases: CI works out the next version from conventional commits with svu on every push to main (feat: minor, fix: patch), then publishes the image, the chart and a GitHub release.

Code layout

Package Layer What it holds
internal/speech Domain Values (Text, which prints redacted, Language, Voice, Speed, SampleRate, Audio), the sentence and language rules, the Service (NewService(detector, synthesizer, voices, fallback, rate, options…)), and the ports Detector and Synthesizer
internal/lingua Adapter Detector with lingua-go, limited to the configured languages
internal/speaches Adapter Synthesizer: speaches' /v1/audio/speech, always PCM at the configured rate
internal/wav Adapter WAV file writer
internal/metrics Adapter Prometheus counters and the /metrics handler, behind the Recorder the handlers need
internal/httpapi Transport HTTP handlers on the generated interface and request validation. It needs a Speaker and an Encoder; logger, Recorder and metrics handler are functional options
internal/httpapi/oapi Generated oapi-codegen output from api/openapi.yaml; never edited by hand
internal/config Reads and checks the environment
cmd/voice-router main app holds the parts (config, service, encoder, metrics, logger); serve builds the HTTP handler and serves

Tests: table tests for the rules (internal/speech), the real lingua models (internal/lingua), the speaches client against a test server, handlers against a fake Speaker, and one end-to-end test of the whole service against a fake speaches (cmd/voice-router). The handler and end-to-end tests also check that the text never reaches the log or an error body.

go vet ./... && go test -race ./... && golangci-lint run

License

Apache 2.0, see LICENSE.

About

OpenAI-compatible text-to-speech endpoint that reads each sentence with the Piper voice of its language, through speaches

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages