Add C++ and Python API for Kokoro 1.0 multilingual TTS model (#1795)

This commit is contained in:
Fangjun Kuang
2025-02-06 22:57:13 +08:00
committed by GitHub
parent 08cefe8488
commit c84a833863
20 changed files with 819 additions and 39 deletions

View File

@@ -8,6 +8,7 @@
#include "sherpa-onnx/csrc/file-utils.h"
#include "sherpa-onnx/csrc/macros.h"
#include "sherpa-onnx/csrc/text-utils.h"
namespace sherpa_onnx {
@@ -17,8 +18,16 @@ void OfflineTtsKokoroModelConfig::Register(ParseOptions *po) {
"Path to voices.bin for Kokoro models");
po->Register("kokoro-tokens", &tokens,
"Path to tokens.txt for Kokoro models");
po->Register(
"kokoro-lexicon", &lexicon,
"Path to lexicon.txt for Kokoro models. Used only for Kokoro >= v1.0"
"You can pass multiple files, separated by ','. Example: "
"./lexicon-us-en.txt,./lexicon-zh.txt");
po->Register("kokoro-data-dir", &data_dir,
"Path to the directory containing dict for espeak-ng.");
po->Register("kokoro-dict-dir", &dict_dir,
"Path to the directory containing dict for jieba. "
"Used only for Kokoro >= v1.0");
po->Register("kokoro-length-scale", &length_scale,
"Speech speed. Larger->Slower; Smaller->faster.");
}
@@ -44,6 +53,19 @@ bool OfflineTtsKokoroModelConfig::Validate() const {
return false;
}
if (!lexicon.empty()) {
std::vector<std::string> files;
SplitStringToVector(lexicon, ",", false, &files);
for (const auto &f : files) {
if (!FileExists(f)) {
SHERPA_ONNX_LOGE(
"lexicon '%s' does not exist. Please re-check --kokoro-lexicon",
f.c_str());
return false;
}
}
}
if (data_dir.empty()) {
SHERPA_ONNX_LOGE("Please provide --kokoro-data-dir");
return false;
@@ -77,6 +99,21 @@ bool OfflineTtsKokoroModelConfig::Validate() const {
return false;
}
if (!dict_dir.empty()) {
std::vector<std::string> required_files = {
"jieba.dict.utf8", "hmm_model.utf8", "user.dict.utf8",
"idf.utf8", "stop_words.utf8",
};
for (const auto &f : required_files) {
if (!FileExists(dict_dir + "/" + f)) {
SHERPA_ONNX_LOGE("'%s/%s' does not exist. Please check kokoro-dict-dir",
dict_dir.c_str(), f.c_str());
return false;
}
}
}
return true;
}
@@ -87,7 +124,9 @@ std::string OfflineTtsKokoroModelConfig::ToString() const {
os << "model=\"" << model << "\", ";
os << "voices=\"" << voices << "\", ";
os << "tokens=\"" << tokens << "\", ";
os << "lexicon=\"" << lexicon << "\", ";
os << "data_dir=\"" << data_dir << "\", ";
os << "dict_dir=\"" << dict_dir << "\", ";
os << "length_scale=" << length_scale << ")";
return os.str();