Kokoros (Kokoro Rust)

repository·main·Indexed 21 days ago

https://github.com/lucasjinreal/kokoros

A 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.

Tokens
9.3K
Snippets
41
Records
46
Agent score
73%

What's inside kokoros

  1. Run Kokoros using Docker

    main

    You can use pre-built images from GHCR or build locally.

    1. Pull or Build:

      docker pull ghcr.io/lucasjinreal/kokoros:main
      # OR
      docker build -t kokoros .
    2. Run CLI mode (mount a volume for output):

      docker run -v ./tmp:/app/tmp kokoros text "Hello from docker!" -o tmp/hello.wav
    3. Run OpenAI Server:

      docker run -p 3000:3000 kokoros openai
    docker run -p 3000:3000 kokoros openai
  2. Install Kokoro Rust via Cargo

    main

    To install and build kokoros locally, follow these steps:

    1. Install System Dependencies:

      • macOS: brew install pkg-config opus
      • Linux (Ubuntu/Debian): sudo apt-get install pkg-config libopus-dev
    2. Download Models and Voices: Run the provided script to download the Kokoro ONNX model and voices data:

      bash download_all.sh

      Alternatively, download them separately using:

      bash scripts/download_models.sh
      bash scripts/download_voices.sh
    3. Build the Project:

      cargo build --release
    4. (Optional) System-wide Installation: To make the koko binary 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.sh
  3. OpenAI-compatible TTS HTTP API Overview

    main

    The kokoros-openai package 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 the audio/pcm format for optimal performance. Other formats will fall back to PCM during streaming.

  4. Get word-level timestamps

    main

    When using the --timestamps global flag, the CLI will generate a sidecar .tsv file for every generated .wav file. 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_sec

    Example: If you save audio to output.wav, the timestamps will be in output.tsv.

    koko text "Hello world" --timestamps -o output.wav
  5. Map OpenAI voices to Kokoro voices

    main

    The API provides backwards compatibility by mapping common OpenAI voice names to their closest Kokoro equivalents:

    OpenAI VoiceKokoro Equivalent
    alloyaf_alloy
    echoam_echo
    novaaf_nova
    onyxam_onyx
    shimmeraf_sky
    fableaf_bella
    coralaf_nicole
    sageaf_sarah
    marinaf_river
    asham_adam
    balladam_michael
    verseam_eric
    cedaram_liam
  6. Configure parallel processing for the OpenAI server

    main

    When running the OpenAI-compatible server, you can use the --instances flag 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
  7. Understand Kokoro voice naming and categories

    main

    Kokoro 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:

    PrefixCategory
    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.

  8. Understand OrtKoko Model Strategies

    main

    The OrtKoko engine uses two internal ModelStrategy modes depending on the ONNX model's output count:

    1. Standard Strategy: Activated when the model has a single output. It expects the audio output to be keyed as audio (or falls back to waveforms). It returns None for durations.
    2. Timestamped Strategy: Activated when the model has multiple outputs. It expects the audio output to be keyed as waveform (or falls back to audio) and specifically looks for a durations output tensor. It returns Some(Vec<f32>) for durations.

    You can inspect the active strategy using the .strategy() method.

  9. Mix voice styles

    main

    Kokoro allows blending multiple voice styles using a specific string syntax in the style_name parameter. The format is style_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.5 blends 50% of af_heart and 50% of af_bella.

    // In a TTSOpts or API call:
    let mixed_style = "af_heart.0.5+af_bella.0.5";
  10. Initialize TTSKoko

    main

    To use the Kokoro TTS engine, initialize a TTSKoko instance. You can use TTSKoko::new(model_path, voices_path) for default settings or TTSKoko::from_config to provide a custom InitConfig (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;