I made a video on how to use KokoroTTS ONNX but did not show the contents of the handy script that I made and another that I had an LLM modify.
Say.sh
#!/bin/bash
echo "uv run save.py"
echo "uv run say.py -m \"Test message\" -v \"af_sarah\" -s 0.8 -o output.wav"
Say.py
"""
https://github.com/thewh1teagle/kokoro-onnx
curl -LsSf https://astral.sh/uv/install.sh | sh
uv init -p 3.12
uv add kokoro-onnx soundfile
OR
python -m venv env
source env/bin/activate
pip install -U kokoro-onnx soundfile
wget https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/kokoro-v1.0.onnx
wget https://github.com/thewh1teagle/kokoro-onnx/releases/download/model-files-v1.0/voices-v1.0.bin
uv run examples/save.py
OR
source env/bin/activate
python save.py
"""
import argparse
import os
import soundfile as sf
from kokoro_onnx import Kokoro
def get_unique_filename(base_name="audio.wav"):
"""Generate a unique filename by appending a number if file exists."""
if not os.path.exists(base_name):
return base_name
# Split filename and extension
name, ext = os.path.splitext(base_name)
counter = 1
while True:
new_name = f"{name}_{counter}{ext}"
if not os.path.exists(new_name):
return new_name
counter += 1
def main():
parser = argparse.ArgumentParser(description="Generate audio using Kokoro TTS")
parser.add_argument(
"-m", "--message",
type=str,
default="Hello. This audio generated by kokoro!",
help="Text message to convert to speech"
)
parser.add_argument(
"-v", "--voice",
type=str,
default="af_sarah",
help="Voice model to use (default: af_sarah)"
)
parser.add_argument(
"-s", "--speed",
type=float,
default=1.0,
help="Speech speed multiplier (default: 1.0)"
)
parser.add_argument(
"-o", "--output",
type=str,
default="audio.wav",
help="Output filename (default: audio.wav)"
)
args = parser.parse_args()
# Get unique filename if output already exists
output_file = get_unique_filename(args.output)
kokoro = Kokoro("kokoro-v1.0.onnx", "voices-v1.0.bin")
samples, sample_rate = kokoro.create(
args.message, voice=args.voice, speed=args.speed, lang="en-us"
)
sf.write(output_file, samples, sample_rate)
print(f"Created {output_file}")
if __name__ == "__main__":
main()
The second is little more than save.py with support for arguments.