### CoreML GPU Acceleration Setup Source: https://github.com/lyrcaxis/kokorosharp/blob/main/RUNNING_ON_GPU.md Enables CoreML execution provider. Requires building and installing the CoreML ONNX runtime. Install the base KokoroSharp package. ```cs var options = new SessionOptions(); options.AppendExecutionProvider_CoreML(); KokoroTTS tts = KokoroTTS.LoadModel(sessionOptions: options); tts.SpeakFast("Hello from GPU!", KokoroVoiceManager.GetVoice("af_heart")); ``` -------------------------------- ### CPU Runtime Example Source: https://github.com/lyrcaxis/kokorosharp/blob/main/RUNNING_ON_GPU.md Basic usage of the CPU runtime. Install KokoroSharp.CPU package. ```cs KokoroTTS tts = KokoroTTS.LoadModel(); tts.SpeakFast("Hello from GPU!", KokoroVoiceManager.GetVoice("af_heart")); ``` -------------------------------- ### CUDA GPU Acceleration Setup Source: https://github.com/lyrcaxis/kokorosharp/blob/main/RUNNING_ON_GPU.md Enables CUDA execution provider for NVIDIA GPUs. Requires CUDA Toolkit and cuDNN installed and in system PATH. Restart IDE/terminal after installation. ```cs var options = new SessionOptions(); options.AppendExecutionProvider_CUDA(); KokoroTTS tts = KokoroTTS.LoadModel(sessionOptions: options); tts.SpeakFast("Hello from GPU!", KokoroVoiceManager.GetVoice("af_heart")); ``` -------------------------------- ### Load, Retrieve, and Mix Kokoro Voices Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Demonstrates loading voices from default or custom paths, retrieving individual voices by name or filtering by language/gender, and mixing voices with specified weights. Weights are automatically normalized. ```csharp // Voices load lazily on first access; or load them manually from a custom path KokoroVoiceManager.LoadVoicesFromPath("voices"); // default NuGet path KokoroVoiceManager.LoadVoicesFromPath("/app/myvoices"); // custom path // Retrieve a single voice by name (LG_name convention: L=language, G=gender) KokoroVoice sarah = KokoroVoiceManager.GetVoice("af_sarah"); // American-English Female KokoroVoice george = KokoroVoiceManager.GetVoice("bm_george"); // British-English Male KokoroVoice siwis = KokoroVoiceManager.GetVoice("ff_siwis"); // French Female // List all loaded voices foreach (var v in KokoroVoiceManager.Voices) { Console.WriteLine($"{v.Name} {v.Language} {v.Gender}"); } // Filter by language and/or gender List enVoices = KokoroVoiceManager.GetVoices(KokoroLanguage.AmericanEnglish); List enFemale = KokoroVoiceManager.GetVoices(KokoroLanguage.AmericanEnglish, KokoroGender.Female); List multiLang = KokoroVoiceManager.GetVoices([KokoroLanguage.French, KokoroLanguage.Spanish]); // Mix two voices (extension method convenience) KokoroVoice mixed2 = sarah.MixWith(KokoroVoiceManager.GetVoice("af_nicole"), wA: 0.6f, wB: 0.4f); // Mix multiple voices with individual weights (weights are normalised automatically) KokoroVoice blend = KokoroVoiceManager.Mix([ (sarah, 2f), (KokoroVoiceManager.GetVoice("af_nicole"), 3f), (KokoroVoiceManager.GetVoice("hf_beta"), 1f) // Hindi Female – cross-language mixing is allowed ]); // Rename the mixed voice so the engine knows which language/gender rules to apply blend.Rename("Blend", KokoroLanguage.AmericanEnglish, KokoroGender.Female); // voice name → "af_Blend" // Save and reload a (mixed) voice mixed2.Export("voices/my_mix.npy"); KokoroVoice reloaded = KokoroVoice.FromPath("voices/my_mix.npy"); ``` -------------------------------- ### Manage Queued Audio Playback with KokoroPlayback Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt KokoroPlayback provides a thread-safe FIFO queue for playing audio samples. It supports volume control, stopping playback, and post-processing of audio samples. Integrates with NAudio and OpenAL. ```csharp KokoroPlayback playback = new KokoroPlayback(); playback.NicifySamples = true; // trim silence + suppress near-zero noise // Enqueue raw float samples (obtained from KokoroModel.Infer or KokoroWavSynthesizer) playback.Enqueue(someFloatArray); // Volume control playback.SetVolume(0.8f); // Stop current audio; optionally clear pending queue playback.StopPlayback(clearQueue: false); playback.StopPlayback(clearQueue: true); // flush everything // Post-process samples manually (static helpers) float[] trimmed = KokoroPlayback.PostProcessSamples(rawSamples); byte[] pcm = KokoroPlayback.GetBytes(trimmed); // 16-bit PCM bytes // Audio format constants WaveFormat fmt = KokoroPlayback.waveFormat; // 24000 Hz, 16-bit, mono // Dispose when done (stops playback, frees WaveOut/OpenAL resources) playback.Dispose(); ``` -------------------------------- ### KokoroPlayback Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt A thread-safe audio queue for playing speech samples. Supports volume control, stopping playback, and post-processing of audio samples. ```APIDOC ## `KokoroPlayback` — Queued Audio Playback Thread-safe audio queue that plays `float[]` sample arrays in FIFO order at 24 kHz / 16-bit mono. Integrates with `NAudio` on Windows and `OpenAL` on Linux/macOS. `KokoroTTS` owns one internally; for advanced use cases, create your own instance. ```csharp KokoroPlayback playback = new KokoroPlayback(); playback.NicifySamples = true; // trim silence + suppress near-zero noise // Enqueue raw float samples (obtained from KokoroModel.Infer or KokoroWavSynthesizer) playback.Enqueue(someFloatArray); // Volume control playback.SetVolume(0.8f); // Stop current audio; optionally clear pending queue playback.StopPlayback(clearQueue: false); playback.StopPlayback(clearQueue: true); // flush everything // Post-process samples manually (static helpers) float[] trimmed = KokoroPlayback.PostProcessSamples(rawSamples); byte[] pcm = KokoroPlayback.GetBytes(trimmed); // 16-bit PCM bytes // Audio format constants WaveFormat fmt = KokoroPlayback.waveFormat; // 24000 Hz, 16-bit, mono // Dispose when done (stops playback, frees WaveOut/OpenAL resources) playback.Dispose(); ``` ``` -------------------------------- ### SynthesisHandle and Lifecycle Packets Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Demonstrates how to use SynthesisHandle for per-request tracking of speech synthesis, including inference-level and playback-level callbacks. ```APIDOC ## `SynthesisHandle` and Lifecycle Packets — Fine-Grained Speech Callbacks `SynthesisHandle` is returned by every `Speak`/`SpeakFast` call. Its delegates mirror the global `KokoroTTS` events but are scoped to a single synthesis request, enabling per-request tracking without shared state. ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); SynthesisHandle handle = tts.SpeakFast("This sentence will be tracked closely.", voice); // Inference-level callbacks (fired before playback starts) handle.OnInferenceStepStarted += step => Console.WriteLine($"Inferring segment of {step.Tokens.Length} tokens"); handle.OnInferenceStepCompleted += (step, pb) => { Console.WriteLine($"Segment inferred; playback handle state: {pb.State}"); pb.OnStarted += () => Console.WriteLine(" ↳ Audio started"); pb.OnSpoken += () => Console.WriteLine(" ↳ Audio finished"); }; // Playback-level callbacks handle.OnSpeechStarted += (SpeechStartPacket p) => { Console.WriteLine($"Playback started: \"{p.TextToSpeak}\" "); Console.WriteLine($" Phonemes: {new string(p.PhonemesToSpeak)}"); }; handle.OnSpeechProgressed += (SpeechProgressPacket p) => { Console.WriteLine($"Segment done (best guess): \"{p.SpokenText_BestGuess}\" "); }; handle.OnSpeechCompleted += (SpeechCompletionPacket p) => { Console.WriteLine($"Speech complete: \"{p.SpokenText}\" "); }; handle.OnSpeechCanceled += (SpeechCancellationPacket p) => { Console.WriteLine($"Canceled after ~{p.PhonemesSpoken_PrevSegments_Certain.Length} certain phonemes"); Console.WriteLine($" Best guess spoken: \"{p.SpokenText_BestGuess}\" "); }; // Access the underlying job and ready playback handles at any time Console.WriteLine(handle.Job.State); // Queued / Running / Completed / Canceled Console.WriteLine(handle.ReadyPlaybackHandles.Count); // number of inferred-but-not-yet-played segments ``` ``` -------------------------------- ### KokoroModel - Direct ONNX Inference Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Shows how to use KokoroModel for direct, low-level ONNX inference, accepting pre-tokenized input and returning raw audio samples. ```APIDOC ## `KokoroModel` — Direct ONNX Inference (Lowest Level) The raw ONNX session wrapper. Accepts pre-tokenized input and returns raw audio samples. Use this only when bypassing both `KokoroEngine` and `KokoroTTS` entirely. ```csharp var model = new KokoroModel("kokoro.onnx"); // SessionOptions default: CPU, 8 threads KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); int[] tokens = Tokenizer.Tokenize("Low-level inference."); float[,,] features = voice.Features; // Synchronous inference – max 510 tokens (enforced automatically with a warning) float[] samples = model.Infer(tokens, features, speed: 1f); Console.WriteLine($"Got {samples.Length} audio samples at 24 kHz"); // Write directly to WAV byte[] pcm = KokoroPlayback.GetBytes(samples); using var writer = new WaveFileWriter("raw.wav", KokoroPlayback.waveFormat); writer.Write(pcm, 0, pcm.Length); // Maximum token constant Console.WriteLine(KokoroModel.maxTokens); // 510 model.Dispose(); ``` ``` -------------------------------- ### Load Kokoro ONNX Model Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Loads the Kokoro ONNX model. It can auto-download the model if not found locally. Supports synchronous, asynchronous, and callback-based loading. Custom session options can be provided for GPU acceleration. ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); // float32 full precision using KokoroTTS tts = KokoroTTS.LoadModel(KModel.int8); // quantized, smaller & faster using KokoroTTS tts = KokoroTTS.LoadModel("path/to/kokoro.onnx"); // load from explicit path ``` ```csharp KokoroTTS tts = await KokoroTTS.LoadModelAsync( model: KModel.float32, OnDownloadProgress: pct => Console.WriteLine($"Download: {pct:P0}"), sessionOptions: null // null → default CPU backend with 8 threads ); ``` ```csharp KokoroTTS.LoadModel( model: KModel.float16, OnComplete: tts => { /* tts is ready */ }, OnDownloadProgress: pct => Console.WriteLine($"{pct:P0}") ); ``` ```csharp var opts = new SessionOptions(); opts.AppendExecutionProvider_CUDA(); using KokoroTTS gpuTts = KokoroTTS.LoadModel(sessionOptions: opts); ``` ```csharp Console.WriteLine(KokoroTTS.IsDownloaded(KModel.float32)); // true / false ``` -------------------------------- ### Load Model and Speak Text with KokoroSharp Source: https://github.com/lyrcaxis/kokorosharp/blob/main/README.md This snippet demonstrates the basic usage of KokoroSharp for text-to-speech. It loads the default model, retrieves a voice, and then speaks user-provided input from the console. Language detection is automated based on the loaded voice. ```csharp KokoroTTS tts = KokoroTTS.LoadModel(); // Load or download the model (~320MB for full precision) KokoroVoice heartVoice = KokoroVoiceManager.GetVoice("af_heart"); // Grab a voice of your liking, while (true) { tts.SpeakFast(Console.ReadLine(), heartVoice); } // .. and have it speak your text! // Note: Language detection is automated based on what the loaded voice supports. ``` -------------------------------- ### KokoroEngine.EnqueueJob Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Allows enqueuing jobs for linear dispatch by the background worker. Supports single-step and multi-step jobs, with options for callbacks and cancellation. ```APIDOC ## `KokoroEngine.EnqueueJob` — Low-Level Job Scheduling `KokoroEngine` owns the ONNX model and a background worker that dispatches `KokoroJob` instances linearly. Enqueuing jobs lets multiple requests be queued without blocking the caller. Canceled jobs are skipped when their turn arrives. ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); // KokoroTTS inherits KokoroEngine KokoroPlayback playback = new(); KokoroVoice sarah = KokoroVoiceManager.GetVoice("af_sarah"); KokoroVoice nicole = KokoroVoiceManager.GetVoice("af_nicole"); // Tokenise text manually int[] tokens = Tokenizer.Tokenize("Welcome back."); // Single-step job — entire text inferred at once (max 510 tokens) KokoroJob job1 = tts.EnqueueJob(KokoroJob.Create(tokens, sarah, speed: 1f, playback.Enqueue)); // Multi-step job — pre-segmented; each segment heard as soon as it's ready List segments = SegmentationSystem.SplitToSegments( Tokenizer.Tokenize("First sentence. Second sentence. Third sentence."), new DefaultSegmentationConfig { MaxFirstSegmentLength = 80 } ); KokoroJob job2 = tts.EnqueueJob(KokoroJob.Create(segments, nicole, speed: 1.1f, playback.Enqueue)); // Per-step callbacks job2.Steps[0].OnStepStarted = () => Console.WriteLine("Step 0 sent to model"); job2.Steps[0].OnStepComplete = samples => Console.WriteLine($"Step 0: {samples.Length} samples"); // Cancel a job (pending or in-flight) job1.Cancel(); Console.WriteLine(job1.State); // KokoroJobState.Canceled // Custom pause job (inject silence into the audio queue) tts.EnqueueJob(new KokoroPauseJob { PauseTime = 2f, OnComplete = playback.Enqueue }); ``` ``` -------------------------------- ### KokoroTTS.LoadModel Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Loads the Kokoro ONNX model into memory, optionally downloading it if not found. Supports synchronous, asynchronous, and callback-based loading, with options for model precision, custom paths, and GPU acceleration. ```APIDOC ## `KokoroTTS.LoadModel` — Load or Auto-Download the ONNX Model Loads the Kokoro ONNX model into memory and returns a ready-to-use `KokoroTTS` instance. If the model file is not present on disk it is downloaded automatically from the official release URL. Three overloads are available: synchronous (blocking), async (`Task`-returning), and callback-based (fire-and-forget). ```csharp // Synchronous – blocks the thread during first-time download (~320 MB for float32) using KokoroTTS tts = KokoroTTS.LoadModel(); // float32 full precision using KokoroTTS tts = KokoroTTS.LoadModel(KModel.int8); // quantized, smaller & faster using KokoroTTS tts = KokoroTTS.LoadModel("path/to/kokoro.onnx"); // load from explicit path // Async – preferred for UI applications KokoroTTS tts = await KokoroTTS.LoadModelAsync( model: KModel.float32, OnDownloadProgress: pct => Console.WriteLine($"Download: {pct:P0}"), sessionOptions: null // null → default CPU backend with 8 threads ); // Callback-based – fire-and-forget KokoroTTS.LoadModel( model: KModel.float16, OnComplete: tts => { /* tts is ready */ }, OnDownloadProgress: pct => Console.WriteLine($"{pct:P0}") ); // GPU (CUDA) – pass custom SessionOptions var opts = new SessionOptions(); opts.AppendExecutionProvider_CUDA(); using KokoroTTS gpuTts = KokoroTTS.LoadModel(sessionOptions: opts); Console.WriteLine(KokoroTTS.IsDownloaded(KModel.float32)); // true / false ``` ``` -------------------------------- ### KokoroVoice Metadata and Embedding Operations Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Shows how to access voice metadata (name, language, gender) and perform implicit conversions to/from raw float embeddings. Also covers loading and saving voices directly from/to .npy files and renaming voices. ```csharp KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); Console.WriteLine(voice.Name); // "af_heart" Console.WriteLine(voice.Language); // KokoroLanguage.AmericanEnglish Console.WriteLine(voice.Gender); // KokoroGender.Female // Implicit cast to raw feature array float[,,] raw = voice; KokoroVoice fromRaw = raw; // cast back (Name will be empty; rename before use) // Load directly from .npy file KokoroVoice onyx = KokoroVoice.FromPath("voices/am_onyx.npy"); // Load raw .npy into float[,,] via NumSharp and pass directly to a job float[,,] michael = NumSharp.np.Load(@"voices/am_michael.npy"); tts.EnqueueJob(KokoroJob.Create(tokens, michael, speed: 0.9f, playback.Enqueue)); // Save voice.Export("backup/af_heart.npy"); // Rename for correct language detection after mixing voice.Rename("Heart", KokoroLanguage.AmericanEnglish, KokoroGender.Female); ``` -------------------------------- ### Track Speech Synthesis with SynthesisHandle Callbacks Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Use SynthesisHandle delegates to track individual speech synthesis requests. Callbacks are available for inference steps and playback events, allowing for fine-grained control and monitoring. ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); SynthesisHandle handle = tts.SpeakFast("This sentence will be tracked closely.", voice); // Inference-level callbacks (fired before playback starts) handle.OnInferenceStepStarted += step => Console.WriteLine($"Inferring segment of {step.Tokens.Length} tokens"); handle.OnInferenceStepCompleted += (step, pb) => { Console.WriteLine($"Segment inferred; playback handle state: {pb.State}"); pb.OnStarted += () => Console.WriteLine(" ↳ Audio started"); pb.OnSpoken += () => Console.WriteLine(" ↳ Audio finished"); }; // Playback-level callbacks handle.OnSpeechStarted += (SpeechStartPacket p) => { Console.WriteLine($"Playback started: \"{p.TextToSpeak}\" "); Console.WriteLine($" Phonemes: {new string(p.PhonemesToSpeak)}"); }; handle.OnSpeechProgressed += (SpeechProgressPacket p) => { Console.WriteLine($"Segment done (best guess): \"{p.SpokenText_BestGuess}\" "); }; handle.OnSpeechCompleted += (SpeechCompletionPacket p) => { Console.WriteLine($"Speech complete: \"{p.SpokenText}\" "); }; handle.OnSpeechCanceled += (SpeechCancellationPacket p) => { Console.WriteLine($"Canceled after ~{p.PhonemesSpoken_PrevSegments_Certain.Length} certain phonemes"); Console.WriteLine($" Best guess spoken: \"{p.SpokenText_BestGuess}\" "); }; // Access the underlying job and ready playback handles at any time Console.WriteLine(handle.Job.State); // Queued / Running / Completed / Canceled Console.WriteLine(handle.ReadyPlaybackHandles.Count); // number of inferred-but-not-yet-played segments ``` -------------------------------- ### Direct ONNX Inference with KokoroModel Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Utilize KokoroModel for low-level ONNX inference, accepting pre-tokenized input and returning raw audio samples. This is intended for scenarios bypassing KokoroEngine and KokoroTTS. ```csharp var model = new KokoroModel("kokoro.onnx"); // SessionOptions default: CPU, 8 threads KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); int[] tokens = Tokenizer.Tokenize("Low-level inference."); float[,,] features = voice.Features; // Synchronous inference – max 510 tokens (enforced automatically with a warning) float[] samples = model.Infer(tokens, features, speed: 1f); Console.WriteLine($"Got {samples.Length} audio samples at 24 kHz"); // Write directly to WAV byte[] pcm = KokoroPlayback.GetBytes(samples); using var writer = new WaveFileWriter("raw.wav", KokoroPlayback.waveFormat); writer.Write(pcm, 0, pcm.Length); // Maximum token constant Console.WriteLine(KokoroModel.maxTokens); // 510 model.Dispose(); ``` -------------------------------- ### Enqueue Speech Synthesis Jobs with KokoroEngine Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Use KokoroEngine.EnqueueJob for asynchronous speech synthesis. Supports single-step or multi-step jobs, custom callbacks for job steps, and job cancellation. Can also enqueue pause jobs. ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); // KokoroTTS inherits KokoroEngine KokoroPlayback playback = new(); KokoroVoice sarah = KokoroVoiceManager.GetVoice("af_sarah"); KokoroVoice nicole = KokoroVoiceManager.GetVoice("af_nicole"); // Tokenise text manually int[] tokens = Tokenizer.Tokenize("Welcome back."); // Single-step job — entire text inferred at once (max 510 tokens) KokoroJob job1 = tts.EnqueueJob(KokoroJob.Create(tokens, sarah, speed: 1f, playback.Enqueue)); // Multi-step job — pre-segmented; each segment heard as soon as it's ready List segments = SegmentationSystem.SplitToSegments( Tokenizer.Tokenize("First sentence. Second sentence. Third sentence."), new DefaultSegmentationConfig { MaxFirstSegmentLength = 80 } ); KokoroJob job2 = tts.EnqueueJob(KokoroJob.Create(segments, nicole, speed: 1.1f, playback.Enqueue)); // Per-step callbacks job2.Steps[0].OnStepStarted = () => Console.WriteLine("Step 0 sent to model"); job2.Steps[0].OnStepComplete = samples => Console.WriteLine($"Step 0: {samples.Length} samples"); // Cancel a job (pending or in-flight) job1.Cancel(); Console.WriteLine(job1.State); // KokoroJobState.Canceled // Custom pause job (inject silence into the audio queue) tts.EnqueueJob(new KokoroPauseJob { PauseTime = 2f, OnComplete = playback.Enqueue }); ``` -------------------------------- ### Tokenize Text to Phoneme Tokens with eSpeak-NG Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Use Tokenizer.Tokenize for the full pipeline, converting text to eSpeak-NG phonemes and then to token arrays. Supports language codes and preprocessing. ```csharp // Full pipeline: text → eSpeak-NG phonemes → token array int[] tokens = Tokenizer.Tokenize("Hello, world!", langCode: "en-us", preprocess: true); ``` ```csharp // Use voice's lang code directly KokoroVoice voice = KokoroVoiceManager.GetVoice("ff_siwis"); int[] frTokens = Tokenizer.Tokenize("Bonjour le monde!", voice.GetLangCode()); ``` ```csharp // Phonemize only (returns IPA string, no tokenisation) string phonemes = Tokenizer.Phonemize("Hello!", "en-us"); Console.WriteLine(phonemes); // e.g. "həlˈoʊ!" ``` ```csharp // Tokenise pre-existing phonemes (for custom phonemizers on Android/iOS) char[] myPhonemes = phonemes.Where(Tokenizer.Vocab.ContainsKey).ToArray(); int[] manualTokens = Tokenizer.TokenizePhonemes(myPhonemes); ``` ```csharp // Phoneme literals – inline pronunciation overrides int[] overrideTokens = Tokenizer.Tokenize("[tomato](/təmeɪtoʊ/) vs [tomato](/təmɑːtoʊ/)."); ``` ```csharp // Override eSpeak-NG binary path (for self-managed espeak installations) Tokenizer.eSpeakNGPath = "/usr/lib/myespeak"; ``` ```csharp // Supported lang codes (drawn from KokoroLangCodeHelper) // en-us, en-gb, ja, cmn, es, fr, hi, it, pt-br string code = KokoroLanguage.BritishEnglish.GetLangCode(); // "en-gb" ``` -------------------------------- ### KokoroTTS.Speak / KokoroTTS.SpeakFast Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt High-level speech synthesis methods. `Speak` synthesizes the entire text in one pass for highest quality. `SpeakFast` segments text for near-instant first audio response while processing the rest in the background. Both return a `SynthesisHandle` for tracking. ```APIDOC ## `KokoroTTS.Speak` / `KokoroTTS.SpeakFast` — High-Level Speech Synthesis `Speak` synthesises the entire input text as a single inference pass (highest quality, up to 510 tokens). `SpeakFast` segments the text first so the first audio chunk is returned almost instantly while the rest is processed in the background. Both methods stop any currently playing audio before starting and return a `SynthesisHandle` for lifecycle tracking. ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); // American-English Female // Simplest possible usage tts.Speak("Hello, world!", voice); // SpeakFast – segmented streaming, near-instant first audio SynthesisHandle handle = tts.SpeakFast("The quick brown fox jumps over the lazy dog.", voice); // Subscribe to per-handle lifecycle events handle.OnSpeechStarted += p => Console.WriteLine($"[Start] {p.TextToSpeak}"); handle.OnSpeechProgressed += p => Console.WriteLine($"[Progress] {p.SpokenText_BestGuess}"); handle.OnSpeechCompleted += p => Console.WriteLine($"[Done] {new string(p.PhonemesSpoken)}"); handle.OnSpeechCanceled += p => Console.WriteLine($"[Canceled] {p.SpokenText_BestGuess}"); // Or subscribe globally on the KokoroTTS instance (fired for every Speak call) tts.OnSpeechStarted += s => Console.WriteLine($"Started: {new string(s.PhonemesToSpeak)}"); tts.OnSpeechProgressed += p => Console.WriteLine($"Progress: {p.SpokenText_BestGuess}"); tts.OnSpeechCompleted += c => Console.WriteLine($"Completed: {c.SpokenText}"); tts.OnSpeechCanceled += c => Console.WriteLine($"Canceled: {c.SpokenText_BestGuess}"); // Custom pipeline: slower speech, no text pre-processing var config = new KokoroTTSPipelineConfig { Speed = 0.85f, PreprocessText = false }; tts.SpeakFast("Bonjour le monde!", KokoroVoiceManager.GetVoice("ff_siwis"), config); // Stop immediately tts.StopPlayback(); // Volume control [0.0 – 1.0] tts.SetVolume(0.7f); // Enable/disable audio nicification (silence trim + noise reduction) tts.NicifyAudio = true; ``` ``` -------------------------------- ### Synthesize Speech to Bytes or WAV with KokoroWavSynthesizer Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt KokoroWavSynthesizer synthesizes speech without playback, returning raw PCM bytes. It supports blocking and asynchronous synthesis, and can provide progress callbacks for long texts. ```csharp // Create synthesizer (same model loading as KokoroTTS, no playback device needed) var synth = new KokoroWavSynthesizer("kokoro.onnx"); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); // Blocking – returns all PCM bytes once synthesis is complete byte[] pcmBytes = synth.Synthesize("Hello, world!", voice); synth.SaveAudioToFile(pcmBytes, "output.wav"); // Async – non-blocking byte[] pcmBytes2 = await synth.SynthesizeAsync( "The quick brown fox.", voice, pipelineConfig: new KokoroTTSPipelineConfig { Speed = 1.2f } ); await File.WriteAllBytesAsync("fast.wav", pcmBytes2); // Streaming callback form – OnProgress fires per segment synth.Synthesize( text: "Long text split across multiple segments.", voice: voice, OnProgress: samples => Console.WriteLine($"Got {samples.Length} samples"), OnComplete: () => Console.WriteLine("All segments done") ); ``` -------------------------------- ### High-Level Speech Synthesis with KokoroTTS Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Synthesizes speech using either a single inference pass for highest quality or segmented streaming for near-instant first audio. Supports lifecycle event subscriptions and custom pipeline configurations. ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); // American-English Female // Simplest possible usage tts.Speak("Hello, world!", voice); ``` ```csharp SynthesisHandle handle = tts.SpeakFast("The quick brown fox jumps over the lazy dog.", voice); // Subscribe to per-handle lifecycle events handle.OnSpeechStarted += p => Console.WriteLine($"[Start] {p.TextToSpeak}"); handle.OnSpeechProgressed += p => Console.WriteLine($"[Progress] {p.SpokenText_BestGuess}"); handle.OnSpeechCompleted += p => Console.WriteLine($"[Done] {new string(p.PhonemesSpoken)}"); handle.OnSpeechCanceled += p => Console.WriteLine($"[Canceled] {p.SpokenText_BestGuess}"); ``` ```csharp // Or subscribe globally on the KokoroTTS instance (fired for every Speak call) tts.OnSpeechStarted += s => Console.WriteLine($"Started: {new string(s.PhonemesToSpeak)}"); tts.OnSpeechProgressed += p => Console.WriteLine($"Progress: {p.SpokenText_BestGuess}"); tts.OnSpeechCompleted += c => Console.WriteLine($"Completed: {c.SpokenText}"); tts.OnSpeechCanceled += c => Console.WriteLine($"Canceled: {c.SpokenText_BestGuess}"); ``` ```csharp // Custom pipeline: slower speech, no text pre-processing var config = new KokoroTTSPipelineConfig { Speed = 0.85f, PreprocessText = false }; tts.SpeakFast("Bonjour le monde!", KokoroVoiceManager.GetVoice("ff_siwis"), config); ``` ```csharp // Stop immediately tts.StopPlayback(); ``` ```csharp // Volume control [0.0 – 1.0] tts.SetVolume(0.7f); ``` ```csharp // Enable/disable audio nicification (silence trim + noise reduction) tts.NicifyAudio = true; ``` -------------------------------- ### Tokenizer - Text Preprocessing and Phonemization Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt The Tokenizer module converts plain text to Kokoro phoneme tokens using eSpeak-NG. It also supports phoneme-only tokenization and phoneme literals for custom overrides. ```APIDOC ## Tokenizer Static module that converts plain text to Kokoro phoneme tokens via eSpeak-NG. Also exposes phoneme-only tokenization for platforms that supply their own phonemizer (e.g., Android/iOS). Phoneme literals bypass phonemization entirely. ### Full pipeline: text → eSpeak-NG phonemes → token array ```csharp int[] tokens = Tokenizer.Tokenize("Hello, world!", langCode: "en-us", preprocess: true); ``` ### Use voice's lang code directly ```csharp KokoroVoice voice = KokoroVoiceManager.GetVoice("ff_siwis"); int[] frTokens = Tokenizer.Tokenize("Bonjour le monde!", voice.GetLangCode()); ``` ### Phonemize only (returns IPA string, no tokenisation) ```csharp string phonemes = Tokenizer.Phonemize("Hello!", "en-us"); Console.WriteLine(phonemes); // e.g. "həlˈoʊ!" ``` ### Tokenise pre-existing phonemes (for custom phonemizers on Android/iOS) ```csharp char[] myPhonemes = phonemes.Where(Tokenizer.Vocab.ContainsKey).ToArray(); int[] manualTokens = Tokenizer.TokenizePhonemes(myPhonemes); ``` ### Phoneme literals – inline pronunciation overrides ```csharp int[] overrideTokens = Tokenizer.Tokenize("[tomato](/təmeɪtoʊ/) vs [tomato](/təmɑːtoʊ/)."); ``` ### Override eSpeak-NG binary path (for self-managed espeak installations) ```csharp Tokenizer.eSpeakNGPath = "/usr/lib/myespeak"; ``` ### Supported lang codes (drawn from KokoroLangCodeHelper) * en-us, en-gb, ja, cmn, es, fr, hi, it, pt-br ```csharp string code = KokoroLanguage.BritishEnglish.GetLangCode(); // "en-gb" ``` ``` -------------------------------- ### KokoroTTSPipelineConfig and DefaultSegmentationConfig - Pipeline Customization Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt `KokoroTTSPipelineConfig` bundles segmentation strategy, inter-segment pause durations, speech speed, and preprocessing toggle. `DefaultSegmentationConfig` fine-tunes built-in segmentation heuristics. ```APIDOC ## KokoroTTSPipelineConfig and DefaultSegmentationConfig — Pipeline Customization `KokoroTTSPipelineConfig` bundles the segmentation strategy, inter-segment pause durations, speech speed, and preprocessing toggle into a single object passed to `Speak`/`SpeakFast`/`SynthesizeAsync`. `DefaultSegmentationConfig` fine-tunes the built-in segmentation heuristics. ### Default config (all defaults) ```csharp var config = new KokoroTTSPipelineConfig(); ``` ### Custom speed (model-level, not just playback rate) ```csharp config.Speed = 0.9f; // recommended range: [0.5, 1.3] ``` ### Disable preprocessing (e.g., text is already clean IPA) ```csharp config.PreprocessText = false; ``` ### Customize inter-segment pauses ```csharp config.SecondsOfPauseBetweenProperSegments = new PauseAfterSegmentStrategy( CommaPause: 0.05f, PeriodPause: 0.4f, QuestionMarkPause: 0.4f, ExclamationMarkPause:0.3f, NewLinePause: 0.6f ); ``` ### Custom segmentation parameters ```csharp var segConfig = new DefaultSegmentationConfig { MinFirstSegmentLength = 10, // respond after at least 10 tokens MaxFirstSegmentLength = 80, // first chunk ≤ 80 tokens MaxSecondSegmentLength = 100, MinFollowupSegmentsLength = 150 }; var configWithSeg = new KokoroTTSPipelineConfig(segConfig); ``` ### Fully custom segmentation delegate ```csharp var configCustom = new KokoroTTSPipelineConfig(tokens => { // Chunk into fixed 50-token blocks var result = new List(); for (int i = 0; i < tokens.Length; i += 50) result.Add(tokens[i..Math.Min(i + 50, tokens.Length)]); return result; }); using KokoroTTS tts = KokoroTTS.LoadModel(); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); tts.SpeakFast("Custom pipeline in action.", voice, configCustom); ``` ``` -------------------------------- ### Customize TTS Pipeline Configuration Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Configure TTS pipeline behavior using KokoroTTSPipelineConfig, adjusting speech speed, preprocessing, and inter-segment pauses. DefaultSegmentationConfig fine-tunes segmentation heuristics. ```csharp // Default config (all defaults) var config = new KokoroTTSPipelineConfig(); ``` ```csharp // Custom speed (model-level, not just playback rate) config.Speed = 0.9f; // recommended range: [0.5, 1.3] ``` ```csharp // Disable preprocessing (e.g., text is already clean IPA) config.PreprocessText = false; ``` ```csharp // Customize inter-segment pauses config.SecondsOfPauseBetweenProperSegments = new PauseAfterSegmentStrategy( CommaPause: 0.05f, PeriodPause: 0.4f, QuestionMarkPause: 0.4f, ExclamationMarkPause:0.3f, NewLinePause: 0.6f ); ``` ```csharp // Custom segmentation parameters var segConfig = new DefaultSegmentationConfig { MinFirstSegmentLength = 10, // respond after at least 10 tokens MaxFirstSegmentLength = 80, // first chunk ≤ 80 tokens MaxSecondSegmentLength = 100, MinFollowupSegmentsLength = 150 }; var configWithSeg = new KokoroTTSPipelineConfig(segConfig); ``` ```csharp // Fully custom segmentation delegate var configCustom = new KokoroTTSPipelineConfig(tokens => { // Chunk into fixed 50-token blocks var result = new List(); for (int i = 0; i < tokens.Length; i += 50) result.Add(tokens[i..Math.Min(i + 50, tokens.Length)]); return result; }); ``` ```csharp using KokoroTTS tts = KokoroTTS.LoadModel(); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); tts.SpeakFast("Custom pipeline in action.", voice, configCustom); ``` -------------------------------- ### KokoroWavSynthesizer Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Synthesizes speech into raw PCM bytes or WAV files without direct playback. Supports blocking and asynchronous synthesis, with options for streaming callbacks. ```APIDOC ## `KokoroWavSynthesizer` — Synthesize to Bytes / WAV File Derives from `KokoroEngine` and synthesizes speech without playing it, returning raw PCM bytes suitable for saving to disk or streaming over a network. Uses the same segmentation pipeline as `KokoroTTS` but collects audio instead of routing it to a speaker. ```csharp // Create synthesizer (same model loading as KokoroTTS, no playback device needed) var synth = new KokoroWavSynthesizer("kokoro.onnx"); KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); // Blocking – returns all PCM bytes once synthesis is complete byte[] pcmBytes = synth.Synthesize("Hello, world!", voice); synth.SaveAudioToFile(pcmBytes, "output.wav"); // Async – non-blocking byte[] pcmBytes2 = await synth.SynthesizeAsync( "The quick brown fox.", voice, pipelineConfig: new KokoroTTSPipelineConfig { Speed = 1.2f } ); await File.WriteAllBytesAsync("fast.wav", pcmBytes2); // Streaming callback form – OnProgress fires per segment synth.Synthesize( text: "Long text split across multiple segments.", voice: voice, OnProgress: samples => Console.WriteLine($"Got {samples.Length} samples"), OnComplete: () => Console.WriteLine("All segments done") ); ``` ``` -------------------------------- ### KokoroVoiceManager - Voice Operations Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Manage voice embeddings by loading from paths, retrieving by name or criteria, and performing multi-voice blending. ```APIDOC ## KokoroVoiceManager Static registry that loads `.npy` voice embeddings from disk, makes them retrievable by name or by language/gender, and provides weighted multi-voice blending. ### Loading Voices ```csharp // Voices load lazily on first access; or load them manually from a custom path KokoroVoiceManager.LoadVoicesFromPath("voices"); // default NuGet path KokoroVoiceManager.LoadVoicesFromPath("/app/myvoices"); // custom path ``` ### Retrieving Voices ```csharp // Retrieve a single voice by name (LG_name convention: L=language, G=gender) KokoroVoice sarah = KokoroVoiceManager.GetVoice("af_sarah"); // American-English Female KokoroVoice george = KokoroVoiceManager.GetVoice("bm_george"); // British-English Male KokoroVoice siwis = KokoroVoiceManager.GetVoice("ff_siwis"); // French Female // List all loaded voices foreach (var v in KokoroVoiceManager.Voices) { Console.WriteLine($"{v.Name} {v.Language} {v.Gender}"); } // Filter by language and/or gender List enVoices = KokoroVoiceManager.GetVoices(KokoroLanguage.AmericanEnglish); List enFemale = KokoroVoiceManager.GetVoices(KokoroLanguage.AmericanEnglish, KokoroGender.Female); List multiLang = KokoroVoiceManager.GetVoices([KokoroLanguage.French, KokoroLanguage.Spanish]); ``` ### Mixing Voices ```csharp // Mix two voices (extension method convenience) KokoroVoice mixed2 = sarah.MixWith(KokoroVoiceManager.GetVoice("af_nicole"), wA: 0.6f, wB: 0.4f); // Mix multiple voices with individual weights (weights are normalised automatically) KokoroVoice blend = KokoroVoiceManager.Mix([ (sarah, 2f), (KokoroVoiceManager.GetVoice("af_nicole"), 3f), (KokoroVoiceManager.GetVoice("hf_beta"), 1f) // Hindi Female – cross-language mixing is allowed ]); // Rename the mixed voice so the engine knows which language/gender rules to apply blend.Rename("Blend", KokoroLanguage.AmericanEnglish, KokoroGender.Female); // voice name → "af_Blend" ``` ### Saving and Reloading Voices ```csharp // Save and reload a (mixed) voice mixed2.Export("voices/my_mix.npy"); KokoroVoice reloaded = KokoroVoice.FromPath("voices/my_mix.npy"); ``` ``` -------------------------------- ### KokoroVoice - Voice Metadata and Embedding Source: https://context7.com/lyrcaxis/kokorosharp/llms.txt Represents a single voice embedding, providing access to its metadata (language, gender) and raw feature data. ```APIDOC ## KokoroVoice Wraps speaker embeddings (`float[,,]` tensor of shape `[510, 1, 256]`) and exposes language/gender metadata inferred from the naming convention `LG_voiceName`. ### Accessing Voice Properties ```csharp KokoroVoice voice = KokoroVoiceManager.GetVoice("af_heart"); Console.WriteLine(voice.Name); // "af_heart" Console.WriteLine(voice.Language); // KokoroLanguage.AmericanEnglish Console.WriteLine(voice.Gender); // KokoroGender.Female ``` ### Raw Feature Interoperability ```csharp // Implicit cast to raw feature array float[,,] raw = voice; KokoroVoice fromRaw = raw; // cast back (Name will be empty; rename before use) ``` ### Loading and Saving Voices ```csharp // Load directly from .npy file KokoroVoice onyx = KokoroVoice.FromPath("voices/am_onyx.npy"); // Load raw .npy into float[,,] via NumSharp and pass directly to a job float[,,] michael = NumSharp.np.Load(@"voices/am_michael.npy"); tts.EnqueueJob(KokoroJob.Create(tokens, michael, speed: 0.9f, playback.Enqueue)); // Save voice.Export("backup/af_heart.npy"); ``` ### Renaming Voices ```csharp // Rename for correct language detection after mixing voice.Rename("Heart", KokoroLanguage.AmericanEnglish, KokoroGender.Female); ``` ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.