-
Notifications
You must be signed in to change notification settings - Fork 533
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CXX API for Kokoro TTS 1.0 (#1802)
- Loading branch information
1 parent
7330f75
commit d815204
Showing
7 changed files
with
111 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// cxx-api-examples/kokoro-tts-zh-en-cxx-api.c | ||
// | ||
// Copyright (c) 2025 Xiaomi Corporation | ||
|
||
// This file shows how to use sherpa-onnx CXX API | ||
// for Chinese TTS with Kokoro. | ||
// | ||
// clang-format off | ||
/* | ||
Usage | ||
wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-multi-lang-v1_0.tar.bz2 | ||
tar xf kokoro-multi-lang-v1_0.tar.bz2 | ||
rm kokoro-multi-lang-v1_0.tar.bz2 | ||
./kokoro-tts-zh-en-cxx-api | ||
*/ | ||
// clang-format on | ||
|
||
#include <string> | ||
|
||
#include "sherpa-onnx/c-api/cxx-api.h" | ||
|
||
static int32_t ProgressCallback(const float *samples, int32_t num_samples, | ||
float progress, void *arg) { | ||
fprintf(stderr, "Progress: %.3f%%\n", progress * 100); | ||
// return 1 to continue generating | ||
// return 0 to stop generating | ||
return 1; | ||
} | ||
|
||
int32_t main(int32_t argc, char *argv[]) { | ||
using namespace sherpa_onnx::cxx; // NOLINT | ||
OfflineTtsConfig config; | ||
|
||
config.model.kokoro.model = "./kokoro-multi-lang-v1_0/model.onnx"; | ||
config.model.kokoro.voices = "./kokoro-multi-lang-v1_0/voices.bin"; | ||
config.model.kokoro.tokens = "./kokoro-multi-lang-v1_0/tokens.txt"; | ||
config.model.kokoro.data_dir = "./kokoro-multi-lang-v1_0/espeak-ng-data"; | ||
config.model.kokoro.dict_dir = "./kokoro-multi-lang-v1_0/dict"; | ||
config.model.kokoro.lexicon = | ||
"./kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/" | ||
"lexicon-zh.txt"; | ||
|
||
config.model.num_threads = 2; | ||
|
||
// If you don't want to see debug messages, please set it to 0 | ||
config.model.debug = 1; | ||
|
||
std::string filename = "./generated-kokoro-zh-en-cxx.wav"; | ||
std::string text = | ||
"中英文语音合成测试。This is generated by next generation Kaldi using " | ||
"Kokoro without Misaki. 你觉得中英文说的如何呢?"; | ||
|
||
auto tts = OfflineTts::Create(config); | ||
int32_t sid = 50; | ||
float speed = 1.0; // larger -> faster in speech speed | ||
|
||
#if 0 | ||
// If you don't want to use a callback, then please enable this branch | ||
GeneratedAudio audio = tts.Generate(text, sid, speed); | ||
#else | ||
GeneratedAudio audio = tts.Generate(text, sid, speed, ProgressCallback); | ||
#endif | ||
|
||
WriteWave(filename, {audio.samples, audio.sample_rate}); | ||
|
||
fprintf(stderr, "Input text is: %s\n", text.c_str()); | ||
fprintf(stderr, "Speaker ID is is: %d\n", sid); | ||
fprintf(stderr, "Saved to: %s\n", filename.c_str()); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters