KokoroSharp

repository·main·Indexed 42 days ago

https://github.com/lyrcaxis/kokorosharp

A C# inference engine for the Kokoro TTS model using the ONNX runtime. It provides text-to-speech synthesis across multiple languages and speakers with support for CPU, NVIDIA GPU (CUDA), and Apple CoreML acceleration. Available via NuGet packages including KokoroSharp.CPU and KokoroSharp.Android, it supports direct audio playback and WAV byte synthesis with per-phoneme timestamps.

Tokens
1.6K
Snippets
7
Records
9
Agent score
4%

What's inside KokoroSharp

  1. Quickstart with KokoroSharp.CPU

    main

    To perform basic text-to-speech synthesis using the CPU package, follow these steps:

    1. Load the model using KokoroTTS.LoadModel(). This will automatically download the model (~320MB) if it is not present.
    2. Retrieve a voice using KokoroVoiceManager.GetVoice(string voiceName).
    3. Use tts.SpeakFast(string text, KokoroVoice voice) to synthesize and play the text.

    Language detection is automated based on the capabilities of the selected voice.

    KokoroTTS tts = KokoroTTS.LoadModel(); // Load or download the model
    KokoroVoice heartVoice = KokoroVoiceManager.GetVoice("af_heart"); 
    
    // Speak text from console input
    while (true) 
    {
        string input = Console.ReadLine();
        tts.SpeakFast(input, heartVoice);
    }
  2. Initialize KokoroSharp on Android

    main

    When using the KokoroSharp.Android package, you must call KokoroSharpAndroid.Init() before starting the standard workflow. This initialization step also handles the synchronous downloading of voices.

    KokoroSharpAndroid.Init();
    // Proceed with standard KokoroTTS workflow
  3. Use CUDA acceleration with NVIDIA GPUs

    main

    To use NVIDIA GPU acceleration via CUDA, follow these steps:

    1. Install Packages: Choose KokoroSharp.GPU.Windows, KokoroSharp.GPU.Linux, or KokoroSharp.GPU via NuGet.
    2. Install Dependencies:
    3. Restart: Restart your IDE or terminal to sync the system path.
    4. Configure Code: Use SessionOptions to append the CUDA execution provider when loading the model.
    var options = new SessionOptions();
    options.AppendExecutionProvider_CUDA();
    KokoroTTS tts = KokoroTTS.LoadModel(sessionOptions: options);
    tts.SpeakFast("Hello from GPU!", KokoroVoiceManager.GetVoice("af_heart"));
  4. Install KokoroSharp via NuGet

    main

    KokoroSharp can be installed on Windows, Linux, and MacOS using the NuGet Package Manager or the .NET CLI.

    • For CPU-based inference (plug-and-play), use the KokoroSharp.CPU package.
    • For GPU support, refer to the RUNNING_ON_GPU.md guide in the repository.
    • For Android, use the KokoroSharp.Android package.
    • For iOS, support is available via the standard package, utilizing a custom CrossPlatformHelper.CustomAudioPlayer for playback.
    # Example: Install via .NET CLI
    dotnet add package KokoroSharp.CPU
  5. Choose the correct KokoroSharp package for your platform

    main

    KokoroSharp provides different packages depending on your target platform and whether you want CPU or GPU acceleration:

    PackagePlatformGPU SupportRequirements
    KokoroSharpAll platformsRuntime-dependantDeveloper must provide the runtime
    KokoroSharp.CPUAll PlatformsCPU-ONLYPlug & Play (no extra setup)
    KokoroSharp.GPUWindows, Linux, Mac (Intel)NVIDIA (CUDA)CUDA Toolkit + cuDNN
    KokoroSharp.GPU.WindowsWindowsNVIDIA (CUDA)CUDA Toolkit + cuDNN
    KokoroSharp.GPU.LinuxLinuxNVIDIA (CUDA)CUDA Toolkit + cuDNN
  6. Use CoreML acceleration on Apple hardware

    main

    While there is no dedicated KokoroSharp package for CoreML, you can use it if you build and install the CoreML ONNX runtime manually.

    1. Install the base KokoroSharp package via NuGet.
    2. Build and install the CoreML ONNX runtime on your system.
    3. Use options.AppendExecutionProvider_CoreML() when loading the model.
    var options = new SessionOptions();
    options.AppendExecutionProvider_CoreML();
    KokoroTTS tts = KokoroTTS.LoadModel(sessionOptions: options);
    tts.SpeakFast("Hello from GPU!", KokoroVoiceManager.GetVoice("af_heart"));
  7. Troubleshoot KokoroSharp runtime errors

    main

    If you encounter errors during execution, check the following:

    • EntryPointNotFoundException:
      • You likely have the wrong KokoroSharp package installed.
      • You might have multiple conflicting runtime packages installed. Delete excess runtimes to resolve this.
    • DllNotFoundException:
      • You have no runtime installed. For a simple, plug-and-play experience, use the KokoroSharp.CPU package.

    Note: Always restart your IDE or terminal after installing system-level dependencies (like CUDA or cuDNN) to ensure the SYSTEM PATH is updated.

  8. Use Kokoro v1.1-zh (Chinese model & voices)

    main

    To use the Chinese-specific model and voices, load the model using the KModel.zh_float32 constant. The v1.1-zh voices are bundled in the package under voices/voices-zh and are loaded automatically.

    KokoroTTS tts = KokoroTTS.LoadModel(KModel.zh_float32); 
    tts.SpeakFast("你好,世界!", KokoroVoiceManager.GetVoice("zf_001"));
  9. Synthesize audio to WAV bytes without playback

    main

    If you need to generate raw audio data instead of playing it immediately, use the KokoroWavSynthesizer class.

    • LoadModel(): Loads the synthesizer model.
    • Synthesize(string text, KokoroVoice voice): Returns the audio as a byte[]. An async version is also available.
    • SynthesizeWithTimestampsAsync(string text, KokoroVoice voice): Returns audio with per-phoneme timestamps, useful for lip-syncing.
    • SaveAudioToFile(byte[] audioBytes, string filePath): A static helper to save the byte array to a file.
    KokoroWavSynthesizer synth = KokoroWavSynthesizer.LoadModel();
    byte[] audioBytes = synth.Synthesize("Hello world!", KokoroVoiceManager.GetVoice("af_heart"));
    
    // Save the result to a file
    KokoroWavSynthesizer.SaveAudioToFile(audioBytes, "hello.wav");