mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 21:11:03 +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
54 lines
1.7 KiB
JavaScript
54 lines
1.7 KiB
JavaScript
// Copyright (c) 2025 Xiaomi Corporation
|
|
const sherpa_onnx = require('sherpa-onnx-node');
|
|
|
|
// please refer to
|
|
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/matcha.html#matcha-icefall-en-us-ljspeech-american-english-1-female-speaker
|
|
// to download model files
|
|
function createOfflineTts() {
|
|
const config = {
|
|
model: {
|
|
matcha: {
|
|
acousticModel: './matcha-icefall-en_US-ljspeech/model-steps-3.onnx',
|
|
vocoder: './vocos-22khz-univ.onnx',
|
|
lexicon: './lexicon-en-us.txt',
|
|
tokens: './matcha-icefall-en_US-ljspeech/tokens.txt',
|
|
},
|
|
debug: true,
|
|
numThreads: 1,
|
|
provider: 'cpu',
|
|
},
|
|
maxNumSentences: 1,
|
|
};
|
|
return new sherpa_onnx.OfflineTts(config);
|
|
}
|
|
|
|
const tts = createOfflineTts();
|
|
|
|
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.';
|
|
|
|
const generationConfig = new sherpa_onnx.GenerationConfig({
|
|
sid: 0,
|
|
speed: 1.0,
|
|
silenceScale: 0.2,
|
|
});
|
|
|
|
|
|
let start = Date.now();
|
|
const audio = tts.generate({text, generationConfig});
|
|
let stop = Date.now();
|
|
const elapsed_seconds = (stop - start) / 1000;
|
|
const duration = audio.samples.length / audio.sampleRate;
|
|
const real_time_factor = elapsed_seconds / duration;
|
|
console.log('Wave duration', duration.toFixed(3), 'seconds');
|
|
console.log('Elapsed', elapsed_seconds.toFixed(3), 'seconds');
|
|
console.log(
|
|
`RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
|
|
real_time_factor.toFixed(3));
|
|
|
|
const filename = 'test-matcha-en-lexicon.wav';
|
|
sherpa_onnx.writeWave(
|
|
filename, {samples: audio.samples, sampleRate: audio.sampleRate});
|
|
|
|
console.log(`Saved to ${filename}`);
|