|
| 1 | +// cxx-api-examples/kokoro-tts-zh-en-cxx-api.c |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Xiaomi Corporation |
| 4 | + |
| 5 | +// This file shows how to use sherpa-onnx CXX API |
| 6 | +// for Chinese TTS with Kokoro. |
| 7 | +// |
| 8 | +// clang-format off |
| 9 | +/* |
| 10 | +Usage |
| 11 | +
|
| 12 | +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-multi-lang-v1_0.tar.bz2 |
| 13 | +tar xf kokoro-multi-lang-v1_0.tar.bz2 |
| 14 | +rm kokoro-multi-lang-v1_0.tar.bz2 |
| 15 | +
|
| 16 | +./kokoro-tts-zh-en-cxx-api |
| 17 | +
|
| 18 | + */ |
| 19 | +// clang-format on |
| 20 | + |
| 21 | +#include <string> |
| 22 | + |
| 23 | +#include "sherpa-onnx/c-api/cxx-api.h" |
| 24 | + |
| 25 | +static int32_t ProgressCallback(const float *samples, int32_t num_samples, |
| 26 | + float progress, void *arg) { |
| 27 | + fprintf(stderr, "Progress: %.3f%%\n", progress * 100); |
| 28 | + // return 1 to continue generating |
| 29 | + // return 0 to stop generating |
| 30 | + return 1; |
| 31 | +} |
| 32 | + |
| 33 | +int32_t main(int32_t argc, char *argv[]) { |
| 34 | + using namespace sherpa_onnx::cxx; // NOLINT |
| 35 | + OfflineTtsConfig config; |
| 36 | + |
| 37 | + config.model.kokoro.model = "./kokoro-multi-lang-v1_0/model.onnx"; |
| 38 | + config.model.kokoro.voices = "./kokoro-multi-lang-v1_0/voices.bin"; |
| 39 | + config.model.kokoro.tokens = "./kokoro-multi-lang-v1_0/tokens.txt"; |
| 40 | + config.model.kokoro.data_dir = "./kokoro-multi-lang-v1_0/espeak-ng-data"; |
| 41 | + config.model.kokoro.dict_dir = "./kokoro-multi-lang-v1_0/dict"; |
| 42 | + config.model.kokoro.lexicon = |
| 43 | + "./kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/" |
| 44 | + "lexicon-zh.txt"; |
| 45 | + |
| 46 | + config.model.num_threads = 2; |
| 47 | + |
| 48 | + // If you don't want to see debug messages, please set it to 0 |
| 49 | + config.model.debug = 1; |
| 50 | + |
| 51 | + std::string filename = "./generated-kokoro-zh-en-cxx.wav"; |
| 52 | + std::string text = |
| 53 | + "中英文语音合成测试。This is generated by next generation Kaldi using " |
| 54 | + "Kokoro without Misaki. 你觉得中英文说的如何呢?"; |
| 55 | + |
| 56 | + auto tts = OfflineTts::Create(config); |
| 57 | + int32_t sid = 50; |
| 58 | + float speed = 1.0; // larger -> faster in speech speed |
| 59 | + |
| 60 | +#if 0 |
| 61 | + // If you don't want to use a callback, then please enable this branch |
| 62 | + GeneratedAudio audio = tts.Generate(text, sid, speed); |
| 63 | +#else |
| 64 | + GeneratedAudio audio = tts.Generate(text, sid, speed, ProgressCallback); |
| 65 | +#endif |
| 66 | + |
| 67 | + WriteWave(filename, {audio.samples, audio.sample_rate}); |
| 68 | + |
| 69 | + fprintf(stderr, "Input text is: %s\n", text.c_str()); |
| 70 | + fprintf(stderr, "Speaker ID is is: %d\n", sid); |
| 71 | + fprintf(stderr, "Saved to: %s\n", filename.c_str()); |
| 72 | + |
| 73 | + return 0; |
| 74 | +} |
0 commit comments