-
Notifications
You must be signed in to change notification settings - Fork 477
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-api) for MatchaTTS models. (#1677)
- Loading branch information
1 parent
6489038
commit 0e299f3
Showing
6 changed files
with
194 additions
and
4 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
48 changes: 48 additions & 0 deletions
48
nodejs-addon-examples/test_tts_non_streaming_matcha_icefall_en.js
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,48 @@ | ||
// 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: './hifigan_v2.onnx', | ||
lexicon: './matcha-icefall-en_US-ljspeech/lexicon.txt', | ||
tokens: './matcha-icefall-en_US-ljspeech/tokens.txt', | ||
dataDir: './matcha-icefall-en_US-ljspeech/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: 0, 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-matcha-en.wav'; | ||
sherpa_onnx.writeWave( | ||
filename, {samples: audio.samples, sampleRate: audio.sampleRate}); | ||
|
||
console.log(`Saved to ${filename}`); |
50 changes: 50 additions & 0 deletions
50
nodejs-addon-examples/test_tts_non_streaming_matcha_icefall_zh.js
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,50 @@ | ||
// 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-zh-baker-chinese-1-female-speaker | ||
// to download model files | ||
function createOfflineTts() { | ||
const config = { | ||
model: { | ||
matcha: { | ||
acousticModel: './matcha-icefall-zh-baker/model-steps-3.onnx', | ||
vocoder: './hifigan_v2.onnx', | ||
lexicon: './matcha-icefall-zh-baker/lexicon.txt', | ||
tokens: './matcha-icefall-zh-baker/tokens.txt', | ||
dictDir: './matcha-icefall-zh-baker/dict', | ||
}, | ||
debug: true, | ||
numThreads: 1, | ||
provider: 'cpu', | ||
}, | ||
maxNumSentences: 1, | ||
ruleFsts: | ||
'./matcha-icefall-zh-baker/phone.fst,./matcha-icefall-zh-baker/date.fst,./matcha-icefall-zh-baker/number.fst', | ||
}; | ||
return new sherpa_onnx.OfflineTts(config); | ||
} | ||
|
||
const tts = createOfflineTts(); | ||
|
||
const text = | ||
'当夜幕降临,星光点点,伴随着微风拂面,我在静谧中感受着时光的流转,思念如涟漪荡漾,梦境如画卷展开,我与自然融为一体,沉静在这片宁静的美丽之中,感受着生命的奇迹与温柔. 某某银行的副行长和一些行政领导表示,他们去过长江和长白山; 经济不断增长。2024年12月31号,拨打110或者18920240511。123456块钱。' | ||
|
||
|
||
let start = Date.now(); | ||
const audio = tts.generate({text: text, sid: 0, 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-matcha-zh.wav'; | ||
sherpa_onnx.writeWave( | ||
filename, {samples: audio.samples, sampleRate: audio.sampleRate}); | ||
|
||
console.log(`Saved to ${filename}`); |
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