Repository navigation
Add CXX API for KittenTTS #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||
| // cxx-api-examples/kitten-tts-en-cxx-api.cc | ||||||
| // | ||||||
| // Copyright (c) 2025 Xiaomi Corporation | ||||||
|
|
||||||
| // This file shows how to use sherpa-onnx CXX API | ||||||
| // for English TTS with Kitten. | ||||||
| // | ||||||
| // clang-format off | ||||||
| /* | ||||||
| Usage | ||||||
|
|
||||||
| wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kitten-nano-en-v0_1-fp16.tar.bz2 | ||||||
| tar xf kitten-nano-en-v0_1-fp16.tar.bz2 | ||||||
| rm kitten-nano-en-v0_1-fp16.tar.bz2 | ||||||
|
|
||||||
| ./kitten-tts-en-cxx-api | ||||||
|
|
||||||
| */ | ||||||
| // clang-format on | ||||||
|
|
||||||
| #include <cstdint> | ||||||
| #include <cstdio> | ||||||
| #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[]) { | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use a standard-conforming main signature C++ requires main to return int (not int32_t). Using int32_t is non-standard and may fail on some platforms. -int32_t main(int32_t argc, char *argv[]) {
+int main(int argc, char *argv[]) {📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||
| using namespace sherpa_onnx::cxx; // NOLINT | ||||||
| OfflineTtsConfig config; | ||||||
|
|
||||||
| config.model.kitten.model = "./kitten-nano-en-v0_1-fp16/model.fp16.onnx"; | ||||||
| config.model.kitten.voices = "./kitten-nano-en-v0_1-fp16/voices.bin"; | ||||||
| config.model.kitten.tokens = "./kitten-nano-en-v0_1-fp16/tokens.txt"; | ||||||
| config.model.kitten.data_dir = "./kitten-nano-en-v0_1-fp16/espeak-ng-data"; | ||||||
|
|
||||||
| 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-kitten-en-cxx.wav"; | ||||||
| std::string 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. " | ||||||
| "Friends fell out often because life was changing so fast. The easiest " | ||||||
| "thing in the world was to lose touch with someone."; | ||||||
|
|
||||||
| auto tts = OfflineTts::Create(config); | ||||||
| int32_t sid = 0; | ||||||
| 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: %d\n", sid); | ||||||
| fprintf(stderr, "Saved to: %s\n", filename.c_str()); | ||||||
|
|
||||||
| return 0; | ||||||
| } | ||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add missing headers for types and I/O
This file uses int32_t and fprintf but doesn’t include the corresponding headers. Depending on transitive includes is fragile and can break on some toolchains.
Apply:
Also qualify calls to fprintf (see below) or include <stdio.h> instead if you prefer using the global namespace.
📝 Committable suggestion
🤖 Prompt for AI Agents