Add JavaScript API (node-addon) for Kokoro TTS 1.0 (#1808)
This commit is contained in:
9
.github/scripts/test-nodejs-addon-npm.sh
vendored
9
.github/scripts/test-nodejs-addon-npm.sh
vendored
@@ -85,12 +85,21 @@ fi
|
||||
|
||||
echo "----------tts----------"
|
||||
|
||||
curl -SL -O 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
|
||||
|
||||
node ./test_tts_non_streaming_kokoro_zh_en.js
|
||||
ls -lh *.wav
|
||||
rm -rf kokoro-multi-lang-v1_0
|
||||
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/kokoro-en-v0_19.tar.bz2
|
||||
tar xf kokoro-en-v0_19.tar.bz2
|
||||
rm kokoro-en-v0_19.tar.bz2
|
||||
|
||||
node ./test_tts_non_streaming_kokoro_en.js
|
||||
ls -lh *.wav
|
||||
rm -rf kokoro-en-v0_19
|
||||
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/matcha-icefall-en_US-ljspeech.tar.bz2
|
||||
tar xvf matcha-icefall-en_US-ljspeech.tar.bz2
|
||||
|
||||
@@ -68,6 +68,8 @@ static SherpaOnnxOfflineTtsKokoroModelConfig GetOfflineTtsKokoroModelConfig(
|
||||
SHERPA_ONNX_ASSIGN_ATTR_STR(tokens, tokens);
|
||||
SHERPA_ONNX_ASSIGN_ATTR_STR(data_dir, dataDir);
|
||||
SHERPA_ONNX_ASSIGN_ATTR_FLOAT(length_scale, lengthScale);
|
||||
SHERPA_ONNX_ASSIGN_ATTR_STR(dict_dir, dictDir);
|
||||
SHERPA_ONNX_ASSIGN_ATTR_STR(lexicon, lexicon);
|
||||
|
||||
return c;
|
||||
}
|
||||
@@ -172,6 +174,8 @@ static Napi::External<SherpaOnnxOfflineTts> CreateOfflineTtsWrapper(
|
||||
SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.voices);
|
||||
SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.tokens);
|
||||
SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.data_dir);
|
||||
SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.dict_dir);
|
||||
SHERPA_ONNX_DELETE_C_STR(c.model.kokoro.lexicon);
|
||||
|
||||
SHERPA_ONNX_DELETE_C_STR(c.model.provider);
|
||||
|
||||
|
||||
@@ -134,6 +134,7 @@ The following tables list the examples in this folder.
|
||||
|File| Description|
|
||||
|---|---|
|
||||
|[./test_tts_non_streaming_kokoro_en.js](./test_tts_non_streaming_kokoro_en.js)| Text-to-speech with a Kokoro English Model|
|
||||
|[./test_tts_non_streaming_kokoro_zh_en.js](./test_tts_non_streaming_kokoro_zh_en.js)| Text-to-speech with a Kokoro Model supporting Chinese and English|
|
||||
|[./test_tts_non_streaming_matcha_icefall_en.js](./test_tts_non_streaming_matcha_icefall_en.js)| Text-to-speech with a [MatchaTTS English Model](https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/matcha.html#matcha-icefall-en-us-ljspeech-american-english-1-female-speaker)|
|
||||
|[./test_tts_non_streaming_matcha_icefall_zhjs](./test_tts_non_streaming_matcha_icefall_zh.js)| Text-to-speech with a [MatchaTTS Chinese Model](https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/matcha.html#matcha-icefall-zh-baker-chinese-1-female-speaker)|
|
||||
|[./test_tts_non_streaming_vits_piper_en.js](./test_tts_non_streaming_vits_piper_en.js)| Text-to-speech with a [piper](https://github.com/rhasspy/piper) English model|
|
||||
@@ -348,6 +349,16 @@ npm install naudiodon2
|
||||
node ./test_vad_asr_non_streaming_sense_voice_microphone.js
|
||||
```
|
||||
|
||||
### Text-to-speech with Kokoro TTS models (Chinese + English TTS)
|
||||
|
||||
```bash
|
||||
curl -SL -O 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
|
||||
|
||||
node ./test_tts_non_streaming_kokoro_zh_en.js
|
||||
```
|
||||
|
||||
### Text-to-speech with Kokoro TTS models (English TTS)
|
||||
|
||||
```bash
|
||||
|
||||
49
nodejs-addon-examples/test_tts_non_streaming_kokoro_zh_en.js
Normal file
49
nodejs-addon-examples/test_tts_non_streaming_kokoro_zh_en.js
Normal file
@@ -0,0 +1,49 @@
|
||||
// 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/kokoro.html
|
||||
// to download model files
|
||||
function createOfflineTts() {
|
||||
const config = {
|
||||
model: {
|
||||
kokoro: {
|
||||
model: './kokoro-multi-lang-v1_0/model.onnx',
|
||||
voices: './kokoro-multi-lang-v1_0/voices.bin',
|
||||
tokens: './kokoro-multi-lang-v1_0/tokens.txt',
|
||||
dataDir: './kokoro-multi-lang-v1_0/espeak-ng-data',
|
||||
dictDir: './kokoro-multi-lang-v1_0/dict',
|
||||
lexicon:
|
||||
'./kokoro-multi-lang-v1_0/lexicon-us-en.txt,./kokoro-multi-lang-v1_0/lexicon-zh.txt',
|
||||
},
|
||||
debug: true,
|
||||
numThreads: 1,
|
||||
provider: 'cpu',
|
||||
},
|
||||
maxNumSentences: 1,
|
||||
};
|
||||
return new sherpa_onnx.OfflineTts(config);
|
||||
}
|
||||
|
||||
const tts = createOfflineTts();
|
||||
|
||||
const text =
|
||||
'中英文语音合成测试。This is generated by next generation Kaldi using Kokoro without Misaki. 你觉得中英文说的如何呢?'
|
||||
|
||||
let start = Date.now();
|
||||
const audio = tts.generate({text: text, sid: 48, speed: 1.0});
|
||||
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), 'secodns')
|
||||
console.log('Elapsed', elapsed_seconds.toFixed(3), 'secodns')
|
||||
console.log(
|
||||
`RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
|
||||
real_time_factor.toFixed(3))
|
||||
|
||||
const filename = 'test-kokoro-zh-en-48.wav';
|
||||
sherpa_onnx.writeWave(
|
||||
filename, {samples: audio.samples, sampleRate: audio.sampleRate});
|
||||
|
||||
console.log(`Saved to ${filename}`);
|
||||
Reference in New Issue
Block a user