|
| 1 | +// c-api-examples/kokoro-tts-en-c-api.c |
| 2 | +// |
| 3 | +// Copyright (c) 2025 Xiaomi Corporation |
| 4 | + |
| 5 | +// This file shows how to use sherpa-onnx C API |
| 6 | +// for English TTS with Kokoro. |
| 7 | +// |
| 8 | +// clang-format off |
| 9 | +/* |
| 10 | +Usage |
| 11 | +
|
| 12 | +
|
| 13 | +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-en-v0_19.tar.bz2 |
| 14 | +tar xf kokoro-en-v0_19.tar.bz2 |
| 15 | +rm kokoro-en-v0_19.tar.bz2 |
| 16 | +
|
| 17 | +./kokoro-tts-en-c-api |
| 18 | +
|
| 19 | + */ |
| 20 | +// clang-format on |
| 21 | + |
| 22 | +#include <stdio.h> |
| 23 | +#include <stdlib.h> |
| 24 | +#include <string.h> |
| 25 | + |
| 26 | +#include "sherpa-onnx/c-api/c-api.h" |
| 27 | + |
| 28 | +static int32_t ProgressCallback(const float *samples, int32_t num_samples, |
| 29 | + float progress) { |
| 30 | + fprintf(stderr, "Progress: %.3f%%\n", progress * 100); |
| 31 | + // return 1 to continue generating |
| 32 | + // return 0 to stop generating |
| 33 | + return 1; |
| 34 | +} |
| 35 | + |
| 36 | +int32_t main(int32_t argc, char *argv[]) { |
| 37 | + SherpaOnnxOfflineTtsConfig config; |
| 38 | + memset(&config, 0, sizeof(config)); |
| 39 | + config.model.kokoro.model = "./kokoro-en-v0_19/model.onnx"; |
| 40 | + config.model.kokoro.voices = "./kokoro-en-v0_19/voices.bin"; |
| 41 | + config.model.kokoro.tokens = "./kokoro-en-v0_19/tokens.txt"; |
| 42 | + config.model.kokoro.data_dir = "./kokoro-en-v0_19/espeak-ng-data"; |
| 43 | + |
| 44 | + config.model.num_threads = 2; |
| 45 | + |
| 46 | + // If you don't want to see debug messages, please set it to 0 |
| 47 | + config.model.debug = 1; |
| 48 | + |
| 49 | + const char *filename = "./generated-kokoro-en.wav"; |
| 50 | + const char *text = |
| 51 | + "Today as always, men fall into two groups: slaves and free men. Whoever " |
| 52 | + "does not have two-thirds of his day for himself, is a slave, whatever " |
| 53 | + "he may be: a statesman, a businessman, an official, or a scholar. " |
| 54 | + "Friends fell out often because life was changing so fast. The easiest " |
| 55 | + "thing in the world was to lose touch with someone."; |
| 56 | + |
| 57 | + const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&config); |
| 58 | + // mapping of sid to voice name |
| 59 | + // 0->af, 1->af_bella, 2->af_nicole, 3->af_sarah, 4->af_sky, 5->am_adam |
| 60 | + // 6->am_michael, 7->bf_emma, 8->bf_isabella, 9->bm_george, 10->bm_lewis |
| 61 | + int32_t sid = 0; |
| 62 | + float speed = 1.0; // larger -> faster in speech speed |
| 63 | + |
| 64 | +#if 0 |
| 65 | + // If you don't want to use a callback, then please enable this branch |
| 66 | + const SherpaOnnxGeneratedAudio *audio = |
| 67 | + SherpaOnnxOfflineTtsGenerate(tts, text, sid, speed); |
| 68 | +#else |
| 69 | + const SherpaOnnxGeneratedAudio *audio = |
| 70 | + SherpaOnnxOfflineTtsGenerateWithProgressCallback(tts, text, sid, speed, |
| 71 | + ProgressCallback); |
| 72 | +#endif |
| 73 | + |
| 74 | + SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate, filename); |
| 75 | + |
| 76 | + SherpaOnnxDestroyOfflineTtsGeneratedAudio(audio); |
| 77 | + SherpaOnnxDestroyOfflineTts(tts); |
| 78 | + |
| 79 | + fprintf(stderr, "Input text is: %s\n", text); |
| 80 | + fprintf(stderr, "Speaker ID is is: %d\n", sid); |
| 81 | + fprintf(stderr, "Saved to: %s\n", filename); |
| 82 | + |
| 83 | + return 0; |
| 84 | +} |
0 commit comments