mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 13:04:05 +08:00
Fixes #3731 espeak-ng and piper-phonemize are no longer built into sherpa-onnx. Text frontends are now: - **lexicon** — built in, no extra dependency - **phonemize** — phonemize outside sherpa-onnx (e.g. [piper-phonemize](https://github.com/csukuangfj/piper-phonemize)) and pass the result in. That library is GPL, so it stays optional: sherpa-onnx is GPL-free after this PR. ## API - `SherpaOnnxGenerationConfig` gains `phoneme_codepoints` and `tokens` (mutually exclusive alternatives to `text`) - `data_dir` on the TTS model configs is deprecated and ignored; the ABI slot is kept - all bindings updated: C/C++, Python, Java, Kotlin, Go, Rust, C#, Swift, Pascal, Dart/Flutter, node-addon, WASM, HarmonyOS
59 lines
2.1 KiB
JavaScript
59 lines
2.1 KiB
JavaScript
// Copyright (c) 2026 Xiaomi Corporation
|
|
//
|
|
// The text is phonemized outside of sherpa-onnx with piper-phonemize
|
|
// (https://www.npmjs.com/package/piper-phonemize) and the resulting phoneme
|
|
// codepoints are passed via GenerationConfig.phonemeCodepoints. No lexicon
|
|
// file and no espeak-ng-data are needed. If you want to use a lexicon
|
|
// instead, please see test-offline-tts-kokoro-en-lexicon.js.
|
|
|
|
const sherpa_onnx = require('sherpa-onnx');
|
|
const piperPhonemize = require('piper-phonemize');
|
|
|
|
function createOfflineTts() {
|
|
let offlineTtsKokoroModelConfig = {
|
|
model: './kokoro-en-v0_19/model.onnx',
|
|
voices: './kokoro-en-v0_19/voices.bin',
|
|
tokens: './kokoro-en-v0_19/tokens.txt',
|
|
lengthScale: 1.0,
|
|
};
|
|
let offlineTtsModelConfig = {
|
|
offlineTtsKokoroModelConfig: offlineTtsKokoroModelConfig,
|
|
numThreads: 1,
|
|
debug: 1,
|
|
provider: 'cpu',
|
|
};
|
|
|
|
let offlineTtsConfig = {
|
|
offlineTtsModelConfig: offlineTtsModelConfig,
|
|
maxNumSentences: 1,
|
|
};
|
|
|
|
return sherpa_onnx.createOfflineTts(offlineTtsConfig);
|
|
}
|
|
|
|
const tts = createOfflineTts();
|
|
const speakerId = 0;
|
|
const speed = 1.0;
|
|
const 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.';
|
|
|
|
// The espeak-ng voice is taken from the model metadata. Fall back to
|
|
// en-us if the model has no language information.
|
|
const voice = tts.lang || 'en-us';
|
|
const phonemeCodepoints = piperPhonemize.phonemize(text, voice)
|
|
// piper may emit empty sentences (e.g. a trailing segment after
|
|
// punctuation); the C API requires every sentence to be non-empty.
|
|
.filter((sentence) => sentence.length > 0);
|
|
|
|
const generationConfig = {
|
|
phonemeCodepoints: phonemeCodepoints,
|
|
sid: speakerId,
|
|
speed: speed,
|
|
silenceScale: 0.2,
|
|
};
|
|
// The text has already been phonemized; pass an empty string here.
|
|
const audio = tts.generateWithConfig('', generationConfig);
|
|
tts.save('./test-kokoro-en-phonemize.wav', audio);
|
|
console.log('Saved to test-kokoro-en-phonemize.wav successfully.');
|
|
tts.free();
|