|
| 1 | +// Copyright (c) 2026 Xiaomi Corporation (authors: Fangjun Kuang) |
| 2 | + |
| 3 | +const sherpa_onnx = require('sherpa-onnx'); |
| 4 | + |
| 5 | +function createOfflineTts() { |
| 6 | + const zipvoice = { |
| 7 | + encoder: './sherpa-onnx-zipvoice-distill-int8-zh-en-emilia/encoder.int8.onnx', |
| 8 | + decoder: './sherpa-onnx-zipvoice-distill-int8-zh-en-emilia/decoder.int8.onnx', |
| 9 | + tokens: './sherpa-onnx-zipvoice-distill-int8-zh-en-emilia/tokens.txt', |
| 10 | + lexicon: './sherpa-onnx-zipvoice-distill-int8-zh-en-emilia/lexicon.txt', |
| 11 | + dataDir: './sherpa-onnx-zipvoice-distill-int8-zh-en-emilia/espeak-ng-data', |
| 12 | + vocoder: './vocos_24khz.onnx', |
| 13 | + }; |
| 14 | + |
| 15 | + const offlineTtsModelConfig = { |
| 16 | + offlineTtsZipVoiceModelConfig: zipvoice, |
| 17 | + numThreads: 1, |
| 18 | + debug: 1, // set it to 1 to see verbose logs; 0 to disable logs |
| 19 | + provider: 'cpu', |
| 20 | + }; |
| 21 | + |
| 22 | + const offlineTtsConfig = { |
| 23 | + offlineTtsModelConfig: offlineTtsModelConfig, |
| 24 | + maxNumSentences: 1, |
| 25 | + }; |
| 26 | + |
| 27 | + return sherpa_onnx.createOfflineTts(offlineTtsConfig); |
| 28 | +} |
| 29 | + |
| 30 | +const referenceWaveFilename = |
| 31 | + './sherpa-onnx-zipvoice-distill-int8-zh-en-emilia/test_wavs/leijun-1.wav'; |
| 32 | +const wave = sherpa_onnx.readWave(referenceWaveFilename); |
| 33 | + |
| 34 | +const referenceText = |
| 35 | + '那还是三十六年前, 一九八七年. 我呢考上了武汉大学的计算机系.'; |
| 36 | +const text = |
| 37 | + '小米的价值观是真诚, 热爱. 真诚,就是不欺人也不自欺. 热爱, 就是全心投入并享受其中.'; |
| 38 | + |
| 39 | +const generationConfig = { |
| 40 | + referenceAudio: wave.samples, |
| 41 | + referenceSampleRate: wave.sampleRate, |
| 42 | + // It must match the transcript of the reference audio above. |
| 43 | + referenceText: referenceText, |
| 44 | + numSteps: 4, |
| 45 | + extra: {min_char_in_sentence: 10}, |
| 46 | +}; |
| 47 | + |
| 48 | +const tts = createOfflineTts(); |
| 49 | +const audio = tts.generateWithConfig(text, generationConfig); |
| 50 | +tts.save('./test-zipvoice-zh-en.wav', audio); |
| 51 | +console.log('Saved to test-zipvoice-zh-en.wav successfully.'); |
| 52 | +tts.free(); |
0 commit comments