2024-05-13 19:24:09 +08:00
// Copyright (c) 2024 Xiaomi Corporation
const sherpa _onnx = require ( 'sherpa-onnx-node' ) ;
// please download model files from
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
function createOfflineTts ( ) {
const config = {
model : {
vits : {
model : './vits-piper-en_GB-cori-medium/en_GB-cori-medium.onnx' ,
tokens : './vits-piper-en_GB-cori-medium/tokens.txt' ,
dataDir : './vits-piper-en_GB-cori-medium/espeak-ng-data' ,
} ,
debug : true ,
numThreads : 1 ,
provider : 'cpu' ,
} ,
2024-12-16 16:37:59 +02:00
maxNumSentences : 1 ,
2024-05-13 19:24:09 +08:00
} ;
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.'
2024-05-15 14:32:30 +08:00
let start = Date . now ( ) ;
2024-05-13 19:24:09 +08:00
const audio = tts . generate ( { text : text , sid : 0 , speed : 1.0 } ) ;
2024-05-15 14:32:30 +08:00
let stop = Date . now ( ) ;
2024-05-13 19:24:09 +08:00
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-piper-en.wav' ;
sherpa _onnx . writeWave (
filename , { samples : audio . samples , sampleRate : audio . sampleRate } ) ;
console . log ( ` Saved to ${ filename } ` ) ;