### Flutter TTS with int8 Quantized ONNX Model Source: https://github.com/yansigit/kokoro-tts-flutter/blob/main/README.md This Dart example shows how to configure and use an `int8`-quantized ONNX model with Kokoro TTS Flutter. By setting the `isInt8` flag to `true` in `KokoroConfig`, the library utilizes the optimized model for improved performance and reduced memory usage. Remember to place the `int8` model file in the assets folder. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; void main() async { // Example configuration for an int8 model const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0-int8.onnx', // Path to your int8 model voicesPath: 'assets/voices.json', isInt8: true, // Enable int8 model support ); final kokoro = Kokoro(config); await kokoro.initialize(); // ... rest of your code } ``` -------------------------------- ### Trim Audio Silence using AudioUtils Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Removes leading and trailing silence from generated audio using RMS-based detection. The utility provides configurable silence thresholds and frame parameters. Accepts Float32List audio data and returns the trimmed audio along with start and end indices. Requires the 'kokoro_tts_flutter' package. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; import 'dart:typed_data'; void processAudio() { // Sample audio data (simulated) final audio = Float32List.fromList([ 0.0, 0.0, 0.01, 0.5, 0.7, 0.6, 0.4, 0.01, 0.0, 0.0 ]); // Trim silence from audio final (trimmedAudio, indices) = AudioUtils.trimSilence( audio, topDb: 60.0, // Silence threshold in dB frameLength: 2048, // Analysis frame length hopLength: 512, // Hop length between frames ); print('Original length: ${audio.length}'); print('Trimmed length: ${trimmedAudio.length}'); print('Trim indices: start=${indices[0]}, end=${indices[1]}'); } ``` -------------------------------- ### Initialize Tokenizer for Phonemization Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Sets up the tokenizer for converting text into phonemes using the malsami G2P engine. It loads vocabulary mappings and lexicon data to enable text-to-phoneme conversion. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future useTokenizer() async { // Create tokenizer with optional custom lexicon final tokenizer = Tokenizer( config: TokenizerConfig(lexiconPath: 'assets/lexicon.json'), ); // Initialize the tokenizer await tokenizer.ensureInitialized(); // Convert text to phonemes final phonemes = await tokenizer.phonemize( 'Hello world!', lang: 'en-us', ); print('Phonemes: $phonemes'); // Tokenize phonemes to IDs final tokens = tokenizer.tokenize(phonemes); print('Token IDs: $tokens'); } ``` -------------------------------- ### Basic Text-to-Speech Synthesis in Flutter Source: https://github.com/yansigit/kokoro-tts-flutter/blob/main/README.md This Dart code demonstrates the basic usage of the Kokoro TTS Flutter library. It initializes the TTS engine with model and voice paths, phonemizes input text, and then generates audio samples for the specified voice. Ensure model and voice files are correctly placed in the assets folder and declared in pubspec.yaml. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; void main() async { const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); final kokoro = Kokoro(config); await kokoro.initialize(); final tokenizer = Tokenizer(); await tokenizer.ensureInitialized(); final phonemes = await tokenizer.phonemize('Hello world!', lang: 'en-us'); final ttsResult = await kokoro.createTTS( text: phonemes, voice: 'af_heart', isPhonemes: true, ); // ttsResult.audio contains the generated audio samples } ``` -------------------------------- ### Initialize Kokoro TTS Engine Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Configures and initializes the Kokoro TTS engine, loading the ONNX model, voice style vectors, and tokenizer resources. The engine must be initialized before speech generation. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; void main() async { // Configure the TTS engine with model and voice files const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); // Create and initialize the engine final kokoro = Kokoro(config); await kokoro.initialize(); // Engine is now ready to generate speech print('Kokoro TTS initialized successfully'); // Get available voices final voices = kokoro.getVoices(); print('Available voices: $voices'); } ``` -------------------------------- ### Use Int8 Quantized Models for Performance Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Enables the use of int8 quantized ONNX models for improved inference performance and reduced memory footprint. This configuration maintains audio quality while speeding up generation. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future useQuantizedModel() async { // Configure with int8 quantized model const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0-int8.onnx', voicesPath: 'assets/voices.json', isInt8: true, // Enable int8 model support ); final kokoro = Kokoro(config); await kokoro.initialize(); // Generate speech using quantized model final ttsResult = await kokoro.createTTS( text: 'Quantized models are faster and use less memory.', voice: 'af_heart', speed: 1.0, lang: 'en-us', ); print('Generated audio duration: ${ttsResult.duration}s'); } ``` -------------------------------- ### Convert Binary Voices to JSON using Python Source: https://github.com/yansigit/kokoro-tts-flutter/blob/main/README.md This Python script converts binary voice data files (e.g., voices-v1.0.bin) into JSON format, improving compatibility with Flutter applications. It loads the binary file using NumPy and exports all voices or specific ones like 'af_heart' into readable JSON files. ```python import numpy as np import json data = np.load("voices-v1.0.bin") # Export all voices to voices.json all_voices = {k: v.tolist() for k, v in data.items()} with open("voices.json", "w") as f: json.dump(all_voices, f) # Optionally, export just af_heart if "af_heart" in data: af_heart = data["af_heart"].tolist() with open("af_heart.json", "w") as f: json.dump(af_heart, f) ``` -------------------------------- ### Work with Voice Objects in Kokoro TTS Flutter Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Allows querying available voices and accessing voice metadata, including language, gender, and style vectors. Voice objects provide detailed control over voice selection and style. Requires the 'kokoro_tts_flutter' package and voice metadata files. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future exploreVoices() async { const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); final kokoro = Kokoro(config); await kokoro.initialize(); // Get all available voice IDs final voiceIds = kokoro.getVoices(); print('Available voices: $voiceIds'); // Get detailed voice information final voice = kokoro.getVoice('af_heart'); if (voice != null) { print('Voice ID: ${voice.id}'); print('Voice name: ${voice.name}'); print('Language: ${voice.languageCode}'); print('Gender: ${voice.gender}'); print('Style vectors: ${voice.styleVectors.length}'); } // Use voice object directly final ttsResult = await kokoro.createTTS( text: 'Using voice object directly', voice: voice, // Pass Voice object instead of String ID speed: 1.0, lang: 'en-us', ); } ``` -------------------------------- ### Concatenate Audio Buffers using AudioUtils Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Combines multiple audio buffers into a single continuous audio stream. This utility handles both Float32List and Int8List formats automatically. Accepts a list of audio buffers and returns a single concatenated buffer. Requires the 'kokoro_tts_flutter' package. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; import 'dart:typed_data'; void combineAudio() { // Multiple audio buffers final buffer1 = Float32List.fromList([0.1, 0.2, 0.3]); final buffer2 = Float32List.fromList([0.4, 0.5, 0.6]); final buffer3 = Float32List.fromList([0.7, 0.8, 0.9]); // Concatenate all buffers final combined = AudioUtils.concatenateAudio([ buffer1, buffer2, buffer3, ]); print('Combined length: ${combined.length}'); print('Combined audio: $combined'); } ``` -------------------------------- ### Generate Speech Directly from Phonemes using Dart Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Generates speech directly from pre-computed phonemes, bypassing text-to-phoneme conversion for custom pronunciation. Requires the Kokoro TTS Flutter library and a Tokenizer. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future usePhonemes() async { const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); final kokoro = Kokoro(config); await kokoro.initialize(); // First, convert text to phonemes final tokenizer = Tokenizer(); await tokenizer.ensureInitialized(); final phonemes = await tokenizer.phonemize( 'Hello world!', lang: 'en-us', ); // Generate speech directly from phonemes final ttsResult = await kokoro.createTTS( text: phonemes, voice: 'af_heart', speed: 1.0, lang: 'en-us', isPhonemes: true, // Indicate input is already phonemes ); print('Generated from phonemes: ${ttsResult.duration}s'); } ``` -------------------------------- ### Calculate Audio RMS using AudioUtils Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Computes Root Mean Square values for audio frames, useful for volume analysis and silence detection. RMS calculation divides audio into frames and computes energy levels. Accepts Float32List audio data and parameters for frame length and hop length. Requires the 'kokoro_tts_flutter' package. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; import 'dart:typed_data'; void analyzeAudio() { // Sample audio data final audio = Float32List.fromList([ 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 0.9, 0.8, 0.7, 0.6, 0.5, 0.4, 0.3, 0.2, 0.1, 0.0 ]); // Calculate RMS values for frames final rmsValues = AudioUtils.rms( audio, frameLength: 4, // 4 samples per frame hopLength: 2, // 2 samples between frames ); print('RMS values: $rmsValues'); print('Number of frames: ${rmsValues.length}'); } ``` -------------------------------- ### Stream Audio Generation with Kokoro TTS Flutter Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Generates speech in streaming mode for long texts, yielding audio chunks as they are generated. This enables progressive playback and reduced memory usage for lengthy content. Requires the 'kokoro_tts_flutter' package and pre-trained model/voice files. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future streamSpeech() async { const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); final kokoro = Kokoro(config); await kokoro.initialize(); // Stream audio generation for long text final stream = kokoro.createTTSStream( text: 'This is a very long text that will be generated in chunks. ' 'Each chunk will be yielded as it becomes available.', voice: 'af_heart', speed: 1.0, lang: 'en-us', trim: true, ); // Process each audio chunk as it arrives await for (final chunk in stream) { print('Received chunk: ${chunk.duration}s, ${chunk.audio.length} samples'); // Play or process audio chunk here } } ``` -------------------------------- ### Generate Speech from Text Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Converts text into speech audio with customizable voice, speed, and language settings. This function handles phonemization automatically and returns audio samples along with metadata. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future generateSpeech() async { const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); final kokoro = Kokoro(config); await kokoro.initialize(); // Generate speech from text final ttsResult = await kokoro.createTTS( text: 'Hello world! This is a test.', voice: 'af_heart', speed: 1.0, lang: 'en-us', isPhonemes: false, trim: true, ); // Access generated audio data print('Audio duration: ${ttsResult.duration} seconds'); print('Sample rate: ${ttsResult.sampleRate} Hz'); print('Audio samples: ${ttsResult.audio.length}'); print('Phonemes used: ${ttsResult.phonemes}'); // Convert to PCM format for playback final pcmData = ttsResult.toPcm(); final int16Data = ttsResult.toInt16PCM(); } ``` -------------------------------- ### Dispose TTS Engine using Dart Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Releases resources utilized by the TTS engine, including the ONNX model runner. It is crucial to dispose of the engine when finished to free memory and prevent resource leaks. Requires the Kokoro TTS Flutter library. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future lifecycle() async { const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); final kokoro = Kokoro(config); await kokoro.initialize(); try { // Use the TTS engine final ttsResult = await kokoro.createTTS( text: 'Goodbye!', voice: 'af_heart', lang: 'en-us', ); print('Generated speech: ${ttsResult.duration}s'); } finally { // Always dispose when done await kokoro.dispose(); print('TTS engine disposed'); } } ``` -------------------------------- ### Adjust Speech Speed using Dart Source: https://context7.com/yansigit/kokoro-tts-flutter/llms.txt Controls the speaking rate of generated speech by adjusting the speed parameter. Supported speeds range from 0.5 to 2.0, affecting playback rate without altering pitch. Requires the Kokoro TTS Flutter library. ```dart import 'package:kokoro_tts_flutter/kokoro_tts_flutter.dart'; Future adjustSpeed() async { const config = KokoroConfig( modelPath: 'assets/kokoro-v1.0.onnx', voicesPath: 'assets/voices.json', ); final kokoro = Kokoro(config); await kokoro.initialize(); // Generate at different speeds final normalSpeed = await kokoro.createTTS( text: 'Normal speed speech.', voice: 'af_heart', speed: 1.0, // Normal speed lang: 'en-us', ); final slowSpeed = await kokoro.createTTS( text: 'Slow speed speech.', voice: 'af_heart', speed: 0.75, // 75% speed lang: 'en-us', ); final fastSpeed = await kokoro.createTTS( text: 'Fast speed speech.', voice: 'af_heart', speed: 1.5, // 150% speed lang: 'en-us', ); print('Normal: ${normalSpeed.duration}s'); print('Slow: ${slowSpeed.duration}s'); print('Fast: ${fastSpeed.duration}s'); } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.