TTS: VITS (Chinese, zh-ll)
Generate speech with the VITS Chinese (sherpa-onnx-vits-zh-ll) model. This model uses a lexicon and rule FSTs for Chinese text normalization. It supports both synchronous and asynchronous generation.
For model documentation, see VITS zh-ll.
Source files
Synchronous generation
1// Copyright (c) 2024 Xiaomi Corporation
2//
3// Text-to-speech with the VITS Chinese (sherpa-onnx-vits-zh-ll) model.
4// Uses lexicon and rule FSTs for Chinese text normalization.
5//
6// Usage:
7// node tts_vits_zh_ll.js
8//
9const sherpa_onnx = require('sherpa-onnx-node');
10
11function createOfflineTts() {
12 const config = {
13 model: {
14 vits: {
15 model: './sherpa-onnx-vits-zh-ll/model.onnx',
16 tokens: './sherpa-onnx-vits-zh-ll/tokens.txt',
17 lexicon: './sherpa-onnx-vits-zh-ll/lexicon.txt',
18 },
19 debug: true,
20 numThreads: 1,
21 provider: 'cpu',
22 },
23 maxNumSentences: 1,
24 ruleFsts:
25 './sherpa-onnx-vits-zh-ll/date.fst,./sherpa-onnx-vits-zh-ll/phone.fst,./sherpa-onnx-vits-zh-ll/number.fst',
26 };
27 return new sherpa_onnx.OfflineTts(config);
28}
29
30const tts = createOfflineTts();
31
32const text =
33 '当夜幕降临,星光点点,伴随着微风拂面,我在静谧中感受着时光的流转,思念如涟漪荡漾,梦境如画卷展开,我与自然融为一体,沉静在这片宁静的美丽之中,感受着生命的奇迹与温柔。2024年5月13号,拨打110或者18920240513。123456块钱。';
34
35const generationConfig = new sherpa_onnx.GenerationConfig({
36 sid: 2,
37 speed: 1.0,
38 silenceScale: 0.2,
39});
40
41let start = Date.now();
42const audio = tts.generate({text: text, generationConfig});
43let stop = Date.now();
44const elapsed_seconds = (stop - start) / 1000;
45const duration = audio.samples.length / audio.sampleRate;
46const real_time_factor = elapsed_seconds / duration;
47console.log('Wave duration', duration.toFixed(3), 'seconds');
48console.log('Elapsed', elapsed_seconds.toFixed(3), 'seconds');
49console.log(
50 `RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
51 real_time_factor.toFixed(3));
52
53const filename = 'test-vits-zh-ll.wav';
54sherpa_onnx.writeWave(
55 filename, {samples: audio.samples, sampleRate: audio.sampleRate});
56
57console.log(`Saved to ${filename}`);
Asynchronous generation
1// Copyright (c) 2026 Xiaomi Corporation
2//
3// Asynchronous text-to-speech with the VITS Chinese (zh-ll) model.
4//
5// Usage:
6// node tts_vits_zh_ll_async.js
7//
8const sherpa_onnx = require('sherpa-onnx-node');
9
10async function createOfflineTts() {
11 const config = {
12 model: {
13 vits: {
14 model: './sherpa-onnx-vits-zh-ll/model.onnx',
15 tokens: './sherpa-onnx-vits-zh-ll/tokens.txt',
16 lexicon: './sherpa-onnx-vits-zh-ll/lexicon.txt',
17 },
18 debug: false,
19 numThreads: 1,
20 provider: 'cpu',
21 },
22 maxNumSentences: 1,
23 ruleFsts:
24 './sherpa-onnx-vits-zh-ll/date.fst,./sherpa-onnx-vits-zh-ll/phone.fst,./sherpa-onnx-vits-zh-ll/number.fst',
25 };
26 return await sherpa_onnx.OfflineTts.createAsync(config);
27}
28
29async function main() {
30 const tts = await createOfflineTts();
31
32 const text =
33 '当夜幕降临,星光点点,伴随着微风拂面,我在静谧中感受着时光的流转,思念如涟漪荡漾,梦境如画卷展开,我与自然融为一体,沉静在这片宁静的美丽之中,感受着生命的奇迹与温柔。2024年5月13号,拨打110或者18920240513。123456块钱。';
34
35 const generationConfig = new sherpa_onnx.GenerationConfig({
36 sid: 2,
37 speed: 1.0,
38 silenceScale: 0.2,
39 });
40
41 const start = Date.now();
42 const audio = await tts.generateAsync({
43 text,
44 enableExternalBuffer: true,
45 generationConfig,
46 onProgress: ({samples, progress}) => {
47 process.stdout.write(
48 `Progress: ${(progress * 100).toFixed(1)}%, ` +
49 `Samples: ${samples.length}\r`);
50 return 1;
51 },
52 });
53
54 console.log('');
55 const stop = Date.now();
56 const elapsed_seconds = (stop - start) / 1000;
57 const duration = audio.samples.length / audio.sampleRate;
58 const real_time_factor = elapsed_seconds / duration;
59 console.log('Wave duration', duration.toFixed(3), 'seconds');
60 console.log('Elapsed', elapsed_seconds.toFixed(3), 'seconds');
61 console.log(
62 `RTF = ${elapsed_seconds.toFixed(3)}/${duration.toFixed(3)} =`,
63 real_time_factor.toFixed(3));
64
65 const filename = 'test-vits-zh-ll-async.wav';
66 sherpa_onnx.writeWave(
67 filename, {samples: audio.samples, sampleRate: audio.sampleRate});
68 console.log(`Saved to ${filename}`);
69}
70
71main().catch((err) => {
72 console.error('Error:', err);
73});
How to run
Install the package:
npm install sherpa-onnx-node
Download the model:
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/sherpa-onnx-vits-zh-ll.tar.bz2 tar xvf sherpa-onnx-vits-zh-ll.tar.bz2
Set the library path and run:
# macOS export DYLD_LIBRARY_PATH=$(npm root)/sherpa-onnx-node/lib:$DYLD_LIBRARY_PATH # Linux export LD_LIBRARY_PATH=$(npm root)/sherpa-onnx-node/lib:$LD_LIBRARY_PATH # Choose one: node tts_vits_zh_ll.js node tts_vits_zh_ll_async.js
Notes
The config key is
vitswith fields:model,tokens,lexicon.ruleFstscontains comma-separated FST files:date.fst,phone.fst,number.fst.The example text contains dates, phone numbers, and monetary amounts that are normalized by the rule FSTs.
The sync API uses
new sherpa_onnx.OfflineTts(config)andtts.generate({text, generationConfig}).The async API uses
OfflineTts.createAsync()andtts.generateAsync()with anonProgresscallback.