kokoro_tts_flutter 0.2.0 copy "kokoro_tts_flutter: ^0.2.0" to clipboard
kokoro_tts_flutter: ^0.2.0 copied to clipboard

A Flutter implementation of Kokoro-TTS using ONNX Runtime

Kokoro TTS Flutter #

Kokoro TTS Flutter is a text-to-speech (TTS) library for Flutter, powered by ONNX Runtime and the Kokoro-TTS model. It supports multi-language synthesis and advanced phonemization using the malsami G2P engine.

Features #

  • Multi-language text-to-speech synthesis
  • ONNX Runtime support for fast inference
  • Advanced phonemization with malsami
  • Example code for generating audio from text

Installation #

Add to your pubspec.yaml:

dependencies:
  kokoro_tts_flutter: ^0.1.0

Model Files Required #

Before using the library, download the following model files and place them in your project's assets folder:

Using voices.json #

For better compatibility with Flutter, you can convert the binary voices file to JSON format: Use this Python script to convert the binary voices file to JSON:

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)

This allows you to use JSON files instead of binary files in your Flutter app, making it easier to work with the voice data.

Usage Example #

Your folder structure should look like:

assets/
  kokoro-v1.0.onnx
  voice.json
  ... (other assets)

Update your pubspec.yaml to include the assets:

flutter:
  assets:
    - assets/

See example/ for a complete example. Basic usage:

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
}

Troubleshootings #

Additional configurations may be required for certain platforms. Check the troubleshooting section of flutter_onnxruntime library for the details.

License #

This project is licensed under the MIT License. See LICENSE for details.

7
likes
150
points
22
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter implementation of Kokoro-TTS using ONNX Runtime

Homepage
Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

async, collection, ffi, flutter, flutter_onnxruntime, http, just_audio, malsami, path, path_provider

More

Packages that depend on kokoro_tts_flutter