Kokoros (Kokoro Rust)
repository·main·Indexed 21 days ago
https://github.com/lucasjinreal/kokorosA high-performance Rust implementation of the Kokoro TTS model. It features a CLI tool (`koko`) for text-to-speech generation from strings, files, or stdin, and an OpenAI-compatible API server supporting streaming, parallel processing, and word-level timestamps. Supports multiple audio formats including mp3, wav, pcm, opus, aac, and flac, with language support for English, Mandarin, Japanese, German, and French.
What's inside kokoros
- The project tracks the development status of Kokoro. Planned and supported language capabilities include English, Mandarin, Japanese, German, and French.
Run Kokoros using Docker
mainYou can use pre-built images from GHCR or build locally.
Pull or Build:
docker pull ghcr.io/lucasjinreal/kokoros:main # OR docker build -t kokoros .Run CLI mode (mount a volume for output):
docker run -v ./tmp:/app/tmp kokoros text "Hello from docker!" -o tmp/hello.wavRun OpenAI Server:
docker run -p 3000:3000 kokoros openai
docker run -p 3000:3000 kokoros openaiInstall Kokoro Rust via Cargo
mainTo install and build
kokoroslocally, follow these steps:Install System Dependencies:
- macOS:
brew install pkg-config opus - Linux (Ubuntu/Debian):
sudo apt-get install pkg-config libopus-dev
- macOS:
Download Models and Voices: Run the provided script to download the Kokoro ONNX model and voices data:
bash download_all.shAlternatively, download them separately using:
bash scripts/download_models.sh bash scripts/download_voices.shBuild the Project:
cargo build --release(Optional) System-wide Installation: To make the
kokobinary available globally and install voice data to$HOME/.cache/kokoros/:bash install.sh
# Full installation sequence bash download_all.sh cargo build --release bash install.shBuild Kokoro Rust using Nix
mainYou can use Nix to manage the build environment. For standard builds:
nix develop cargo build --releaseFor builds with CUDA support:
nix develop .#cuda cargo build --features kokoros/cuda --releaseOpenAI-compatible TTS HTTP API Overview
mainThe
kokoros-openaipackage provides an HTTP server that implements an API compatible with OpenAI's text-to-speech (TTS) endpoints. This allows developers to use existing OpenAI clients and integrations with the Kokoros engine.Implemented Endpoints
POST /v1/audio/speech: Generates speech from text. Supports both streaming and non-streaming modes.GET /v1/audio/voices: Returns a list of available voices (including mapped OpenAI voices).GET /v1/models: Returns a static list of available models.GET /v1/models/{model}: Returns information about a specific model.
Supported Audio Formats
mp3(MPEG)wav(WAV)pcm(Raw 16-bit PCM)opus(OGG/Opus)aac(AAC)flac(FLAC)
Note on Streaming: When using the streaming mode (
stream: true), the server forces theaudio/pcmformat for optimal performance. Other formats will fall back to PCM during streaming.Get word-level timestamps
mainWhen using the
--timestampsglobal flag, the CLI will generate a sidecar.tsvfile for every generated.wavfile. The TSV file uses the same base name as the audio file and contains word-level start and end times.TSV Format:
word start_sec end_secExample: If you save audio to
output.wav, the timestamps will be inoutput.tsv.koko text "Hello world" --timestamps -o output.wavMap OpenAI voices to Kokoro voices
mainThe API provides backwards compatibility by mapping common OpenAI voice names to their closest Kokoro equivalents:
OpenAI Voice Kokoro Equivalent alloyaf_alloyechoam_echonovaaf_novaonyxam_onyxshimmeraf_skyfableaf_bellacoralaf_nicolesageaf_sarahmarinaf_riverasham_adamballadam_michaelverseam_ericcedaram_liamConfigure parallel processing for the OpenAI server
mainWhen running the OpenAI-compatible server, you can use the
--instancesflag to control parallel processing. This helps balance latency (Time-to-First-Audio) and total throughput.- Low Latency (Real-time): Use
--instances 1. - Balanced (Default): Use 2 instances (default behavior).
- High Throughput (Batch): Use higher numbers (e.g.,
--instances 4) to improve total processing time, especially on multi-core CPUs or NVIDIA GPUs.
# Best for real-time (lowest latency) ./target/release/koko openai --instances 1 # Best for batch processing (highest throughput) ./target/release/koko openai --instances 4- Low Latency (Real-time): Use
Understand Kokoro voice naming and categories
mainKokoro voices are categorized by a two-letter prefix representing the language and gender. When the system logs available voices, it groups them into the following categories:
Prefix Category afAmerican Female amAmerican Male bfBritish Female bmBritish Male efEuropean Female emEuropean Male ffFrench Female hfHindi Female hmHindi Male ifItalian Female imItalian Male jfJapanese Female jmJapanese Male pfPortuguese Female pmPortuguese Male zfChinese Female zmChinese Male If a prefix does not match these, the prefix itself is used as the category name.
Understand OrtKoko Model Strategies
mainThe
OrtKokoengine uses two internalModelStrategymodes depending on the ONNX model's output count:- Standard Strategy: Activated when the model has a single output. It expects the audio output to be keyed as
audio(or falls back towaveforms). It returnsNonefor durations. - Timestamped Strategy: Activated when the model has multiple outputs. It expects the audio output to be keyed as
waveform(or falls back toaudio) and specifically looks for adurationsoutput tensor. It returnsSome(Vec<f32>)for durations.
You can inspect the active strategy using the
.strategy()method.- Standard Strategy: Activated when the model has a single output. It expects the audio output to be keyed as
Mix voice styles
mainKokoro allows blending multiple voice styles using a specific string syntax in the
style_nameparameter. The format isstyle_name.weight, where weight is a decimal (e.g.,0.5). Multiple styles are joined by+.Example Syntax:
af_heart.0.5+af_bella.0.5blends 50% ofaf_heartand 50% ofaf_bella.// In a TTSOpts or API call: let mixed_style = "af_heart.0.5+af_bella.0.5";Initialize TTSKoko
mainTo use the Kokoro TTS engine, initialize a
TTSKokoinstance. You can useTTSKoko::new(model_path, voices_path)for default settings orTTSKoko::from_configto provide a customInitConfig(which allows specifying custom URLs for model and voice files and the sample rate).// Using default config let tts = TTSKoko::new("path/to/model.onnx", "path/to/voices.bin").await; // Using custom config let cfg = InitConfig { model_url: "https://example.com/model.onnx".into(), voices_url: "https://example.com/voices.bin".into(), sample_rate: 24000, }; let tts = TTSKoko::from_config("path/to/model.onnx", "path/to/voices.bin", cfg).await;