Add C API for Kokoro TTS 1.0 (#1801)
This commit is contained in:
@@ -19,6 +19,9 @@ if(SHERPA_ONNX_ENABLE_TTS)
|
||||
|
||||
add_executable(kokoro-tts-en-c-api kokoro-tts-en-c-api.c)
|
||||
target_link_libraries(kokoro-tts-en-c-api sherpa-onnx-c-api)
|
||||
|
||||
add_executable(kokoro-tts-zh-en-c-api kokoro-tts-zh-en-c-api.c)
|
||||
target_link_libraries(kokoro-tts-zh-en-c-api sherpa-onnx-c-api)
|
||||
endif()
|
||||
|
||||
if(SHERPA_ONNX_ENABLE_SPEAKER_DIARIZATION)
|
||||
|
||||
82
c-api-examples/kokoro-tts-zh-en-c-api.c
Normal file
82
c-api-examples/kokoro-tts-zh-en-c-api.c
Normal file
@@ -0,0 +1,82 @@
|
||||
// c-api-examples/kokoro-tts-zh-en-c-api.c
|
||||
//
|
||||
// Copyright (c) 2025 Xiaomi Corporation
|
||||
|
||||
// This file shows how to use sherpa-onnx C API
|
||||
// for English + 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-c-api
|
||||
|
||||
*/
|
||||
// clang-format on
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "sherpa-onnx/c-api/c-api.h"
|
||||
|
||||
static int32_t ProgressCallback(const float *samples, int32_t num_samples,
|
||||
float progress) {
|
||||
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[]) {
|
||||
SherpaOnnxOfflineTtsConfig config;
|
||||
memset(&config, 0, sizeof(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;
|
||||
|
||||
const char *filename = "./generated-kokoro-zh-en.wav";
|
||||
const char *text =
|
||||
"中英文语音合成测试。This is generated by next generation Kaldi using "
|
||||
"Kokoro without Misaki. 你觉得中英文说的如何呢?";
|
||||
|
||||
const SherpaOnnxOfflineTts *tts = SherpaOnnxCreateOfflineTts(&config);
|
||||
int32_t sid = 0; // there are 53 speakers
|
||||
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
|
||||
const SherpaOnnxGeneratedAudio *audio =
|
||||
SherpaOnnxOfflineTtsGenerate(tts, text, sid, speed);
|
||||
#else
|
||||
const SherpaOnnxGeneratedAudio *audio =
|
||||
SherpaOnnxOfflineTtsGenerateWithProgressCallback(tts, text, sid, speed,
|
||||
ProgressCallback);
|
||||
#endif
|
||||
|
||||
SherpaOnnxWriteWave(audio->samples, audio->n, audio->sample_rate, filename);
|
||||
|
||||
SherpaOnnxDestroyOfflineTtsGeneratedAudio(audio);
|
||||
SherpaOnnxDestroyOfflineTts(tts);
|
||||
|
||||
fprintf(stderr, "Input text is: %s\n", text);
|
||||
fprintf(stderr, "Speaker ID is is: %d\n", sid);
|
||||
fprintf(stderr, "Saved to: %s\n", filename);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user