-
Notifications
You must be signed in to change notification settings - Fork 554
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JavaScript API (node-addon) for Kokoro TTS models (#1725)
- Loading branch information
1 parent
46f2e32
commit e8d499d
Showing
4 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// 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-en-v0_19/model.onnx', | ||
voices: './kokoro-en-v0_19/voices.bin', | ||
tokens: './kokoro-en-v0_19/tokens.txt', | ||
dataDir: './kokoro-en-v0_19/espeak-ng-data', | ||
}, | ||
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.' | ||
|
||
|
||
let start = Date.now(); | ||
const audio = tts.generate({text: text, sid: 6, 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-en-6.wav'; | ||
sherpa_onnx.writeWave( | ||
filename, {samples: audio.samples, sampleRate: audio.sampleRate}); | ||
|
||
console.log(`Saved to ${filename}`); |