|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright (c) 2026 Xiaomi Corporation |
| 4 | + |
| 5 | +""" |
| 6 | +This file demonstrates how to use sherpa-onnx Python API |
| 7 | +for SupertonicTTS. |
| 8 | +
|
| 9 | +
|
| 10 | +Usage: |
| 11 | +
|
| 12 | +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/sherpa-onnx-supertonic-tts-int8-2026-03-06.tar.bz2 |
| 13 | +tar xvf sherpa-onnx-supertonic-tts-int8-2026-03-06.tar.bz2 |
| 14 | +rm sherpa-onnx-supertonic-tts-int8-2026-03-06.tar.bz2 |
| 15 | +
|
| 16 | +python3 ./supertonic-tts.py |
| 17 | +
|
| 18 | +You can find more models at |
| 19 | +https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models |
| 20 | +
|
| 21 | +Please see |
| 22 | +https://k2-fsa.github.io/sherpa/onnx/tts/supertonic.html |
| 23 | +for details. |
| 24 | +
|
| 25 | +""" |
| 26 | + |
| 27 | +import time |
| 28 | + |
| 29 | +import sherpa_onnx |
| 30 | +import soundfile as sf |
| 31 | + |
| 32 | + |
| 33 | +def create_tts(): |
| 34 | + tts_config = sherpa_onnx.OfflineTtsConfig( |
| 35 | + model=sherpa_onnx.OfflineTtsModelConfig( |
| 36 | + supertonic=sherpa_onnx.OfflineTtsSupertonicModelConfig( |
| 37 | + duration_predictor="./sherpa-onnx-supertonic-tts-int8-2026-03-06/duration_predictor.int8.onnx", |
| 38 | + text_encoder="./sherpa-onnx-supertonic-tts-int8-2026-03-06/text_encoder.int8.onnx", |
| 39 | + vector_estimator="./sherpa-onnx-supertonic-tts-int8-2026-03-06/vector_estimator.int8.onnx", |
| 40 | + vocoder="./sherpa-onnx-supertonic-tts-int8-2026-03-06/vocoder.int8.onnx", |
| 41 | + tts_json="./sherpa-onnx-supertonic-tts-int8-2026-03-06/tts.json", |
| 42 | + unicode_indexer="./sherpa-onnx-supertonic-tts-int8-2026-03-06/unicode_indexer.bin", |
| 43 | + voice_style="./sherpa-onnx-supertonic-tts-int8-2026-03-06/voice.bin", |
| 44 | + ), |
| 45 | + debug=False, |
| 46 | + num_threads=2, |
| 47 | + provider="cpu", |
| 48 | + ) |
| 49 | + ) |
| 50 | + if not tts_config.validate(): |
| 51 | + raise ValueError( |
| 52 | + "Please read the previous error messages and re-check your config" |
| 53 | + ) |
| 54 | + |
| 55 | + return sherpa_onnx.OfflineTts(tts_config) |
| 56 | + |
| 57 | + |
| 58 | +def main(): |
| 59 | + tts = create_tts() |
| 60 | + |
| 61 | + text = "Today as always, men fall into two groups: slaves and free men. Whoever does not have two-thirds of his day for himself, is a slave, whatever he may be, a statesman, a businessman, an official, or a scholar." |
| 62 | + |
| 63 | + gen_config = sherpa_onnx.GenerationConfig() |
| 64 | + |
| 65 | + # This model has 10 speakers. Valid sid: 0-9 |
| 66 | + gen_config.sid = 6 |
| 67 | + gen_config.num_steps = 5 |
| 68 | + gen_config.speed = 1.25 # larger -> faster |
| 69 | + |
| 70 | + # We use en for English. |
| 71 | + # You can also use es, pt, fr, ko. |
| 72 | + # This single model supports 5 languages. |
| 73 | + gen_config.extra["lang"] = "en" |
| 74 | + |
| 75 | + start = time.time() |
| 76 | + audio = tts.generate(text, gen_config) |
| 77 | + end = time.time() |
| 78 | + |
| 79 | + if len(audio.samples) == 0: |
| 80 | + print("Error in generating audios. Please read previous error messages.") |
| 81 | + return |
| 82 | + |
| 83 | + elapsed_seconds = end - start |
| 84 | + audio_duration = len(audio.samples) / audio.sample_rate |
| 85 | + real_time_factor = elapsed_seconds / audio_duration |
| 86 | + |
| 87 | + output_filename = "./supertonic-en.wav" |
| 88 | + sf.write( |
| 89 | + output_filename, |
| 90 | + audio.samples, |
| 91 | + samplerate=audio.sample_rate, |
| 92 | + subtype="PCM_16", |
| 93 | + ) |
| 94 | + print(f"Saved to {output_filename}") |
| 95 | + print(f"The text is '{text}'") |
| 96 | + print(f"Elapsed seconds: {elapsed_seconds:.3f}") |
| 97 | + print(f"Audio duration in seconds: {audio_duration:.3f}") |
| 98 | + print(f"RTF: {elapsed_seconds:.3f}/{audio_duration:.3f} = {real_time_factor:.3f}") |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + main() |
0 commit comments