Kokoro-ios Documentation

repository·main·Indexed 43 days ago

https://github.com/mlalma/kokoro-ios

A Swift port of the Kokoro TTS model using the MLX Swift framework for high-quality, faster-than-real-time English audio generation on Apple platforms. Includes the KokoroSwift package and KokoroTTS engine with support for MisakiSwift and eSpeakNG G2P processors.

Tokens
662
Snippets
2
Records
4
Agent score
5%

What's inside Kokoro-ios

  1. Model and Voice Style requirements

    main

    KokoroSwift is a TTS engine and does not bundle the large model files or voice styles. You are responsible for providing:

    1. Model Files: The Kokoro TTS model files located at a valid local URL.
    2. Voice Styles: Voice embeddings provided as MLXArray objects.

    For guidance on how to bundle these files within an application, refer to the Kokoro Test App example project.

  2. Install KokoroSwift via Swift Package Manager

    main

    Add KokoroSwift to your project using Swift Package Manager (SPM). Add the repository URL to your dependencies and then link the KokoroSwift product to your target.

    // In Package.swift
    dependencies: [
        .package(url: "https://github.com/mlalma/kokoro-ios.git", from: "1.0.0")
    ]
    
    // In your target definition
    .target(
        name: "YourTarget",
        dependencies: [
            .product(name: "KokoroSwift", package: "kokoro-ios")
        ]
    )
  3. Generate speech with KokoroTTS

    main

    To use the TTS engine, initialize KokoroTTS with a local path to your model files and a G2P (Grapheme-to-Phoneme) option. You must provide a voice embedding as an MLXArray (which represents the voice style) to generate audio from text.

    import KokoroSwift
    
    // Initialize the TTS engine
    let modelPath = URL(fileURLWithPath: "path/to/your/model")
    let tts = KokoroTTS(modelPath: modelPath, g2p: .misaki)
    
    // Generate speech
    // Note: voiceEmbedding must be an `MLXArray` representing a voice style
    let voiceEmbedding = ... 
    let text = "Hello, this is a test of Kokoro TTS."
    let audioBuffer = try tts.generateAudio(voice: voiceEmbedding, language: .enUS, text: text)
    
    // audioBuffer now contains the synthesized speech
  4. Configure G2P (Grapheme-to-Phoneme) options

    main

    The KokoroTTS initializer accepts a g2p parameter to specify the Grapheme-to-Phoneme processor.

    Supported options:

    • .misaki: MisakiSwift (the default processor).
    • .espeak: eSpeakNG (alternative processor, currently commented out in the current version).