|
| 1 | +// cxx-api-examples/matcha-tts-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 MatchaTTS. |
| 7 | +// |
| 8 | +// clang-format off |
| 9 | +/* |
| 10 | +Usage |
| 11 | +
|
| 12 | +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/matcha-icefall-en_US-ljspeech.tar.bz2 |
| 13 | +tar xvf matcha-icefall-en_US-ljspeech.tar.bz2 |
| 14 | +rm matcha-icefall-en_US-ljspeech.tar.bz2 |
| 15 | +
|
| 16 | +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/vocoder-models/hifigan_v2.onnx |
| 17 | +
|
| 18 | +./matcha-tts-en-cxx-api |
| 19 | +
|
| 20 | + */ |
| 21 | +// clang-format on |
| 22 | + |
| 23 | +#include <string> |
| 24 | + |
| 25 | +#include "sherpa-onnx/c-api/cxx-api.h" |
| 26 | + |
| 27 | +static int32_t ProgressCallback(const float *samples, int32_t num_samples, |
| 28 | + float progress, void *arg) { |
| 29 | + fprintf(stderr, "Progress: %.3f%%\n", progress * 100); |
| 30 | + // return 1 to continue generating |
| 31 | + // return 0 to stop generating |
| 32 | + return 1; |
| 33 | +} |
| 34 | + |
| 35 | +int32_t main(int32_t argc, char *argv[]) { |
| 36 | + using namespace sherpa_onnx::cxx; // NOLINT |
| 37 | + OfflineTtsConfig config; |
| 38 | + |
| 39 | + config.model.matcha.acoustic_model = |
| 40 | + "./matcha-icefall-en_US-ljspeech/model-steps-3.onnx"; |
| 41 | + |
| 42 | + config.model.matcha.vocoder = "./hifigan_v2.onnx"; |
| 43 | + |
| 44 | + config.model.matcha.tokens = "./matcha-icefall-en_US-ljspeech/tokens.txt"; |
| 45 | + |
| 46 | + config.model.matcha.data_dir = |
| 47 | + "./matcha-icefall-en_US-ljspeech/espeak-ng-data"; |
| 48 | + |
| 49 | + config.model.num_threads = 1; |
| 50 | + |
| 51 | + // If you don't want to see debug messages, please set it to 0 |
| 52 | + config.model.debug = 1; |
| 53 | + |
| 54 | + std::string filename = "./generated-matcha-en-cxx.wav"; |
| 55 | + std::string text = |
| 56 | + "Today as always, men fall into two groups: slaves and free men. Whoever " |
| 57 | + "does not have two-thirds of his day for himself, is a slave, whatever " |
| 58 | + "he may be: a statesman, a businessman, an official, or a scholar. " |
| 59 | + "Friends fell out often because life was changing so fast. The easiest " |
| 60 | + "thing in the world was to lose touch with someone."; |
| 61 | + |
| 62 | + auto tts = OfflineTts::Create(config); |
| 63 | + int32_t sid = 0; |
| 64 | + float speed = 1.0; // larger -> faster in speech speed |
| 65 | + |
| 66 | +#if 0 |
| 67 | + // If you don't want to use a callback, then please enable this branch |
| 68 | + GeneratedAudio audio = tts.Generate(text, sid, speed); |
| 69 | +#else |
| 70 | + GeneratedAudio audio = tts.Generate(text, sid, speed, ProgressCallback); |
| 71 | +#endif |
| 72 | + |
| 73 | + WriteWave(filename, {audio.samples, audio.sample_rate}); |
| 74 | + |
| 75 | + fprintf(stderr, "Input text is: %s\n", text.c_str()); |
| 76 | + fprintf(stderr, "Speaker ID is is: %d\n", sid); |
| 77 | + fprintf(stderr, "Saved to: %s\n", filename.c_str()); |
| 78 | + |
| 79 | + return 0; |
| 80 | +} |
0 commit comments