Fangjun Kuang
Committed by GitHub

Add HarmonyOS demo for Kokoro TTS 1.0 (#1813)

@@ -34,6 +34,8 @@ export class OfflineTtsKokoroModelConfig { @@ -34,6 +34,8 @@ export class OfflineTtsKokoroModelConfig {
34 public tokens: string = ''; 34 public tokens: string = '';
35 public dataDir: string = ''; 35 public dataDir: string = '';
36 public lengthScale: number = 1.0; 36 public lengthScale: number = 1.0;
  37 + public dictDir: string = '';
  38 + public lexicon: string = '';
37 } 39 }
38 40
39 export class OfflineTtsModelConfig { 41 export class OfflineTtsModelConfig {
@@ -179,6 +179,16 @@ function initTts(context: Context): OfflineTts { @@ -179,6 +179,16 @@ function initTts(context: Context): OfflineTts {
179 // voices = 'voices.bin' 179 // voices = 'voices.bin'
180 // dataDir = 'espeak-ng-data'; 180 // dataDir = 'espeak-ng-data';
181 181
  182 + // Example 11
  183 + // https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/kokoro.html
  184 + // modelDir = 'kokoro-multi-lang-v1_0';
  185 + // modelName = 'model.onnx';
  186 + // voices = 'voices.bin'
  187 + // dataDir = 'espeak-ng-data';
  188 + // dictDir = 'dict';
  189 + // lexicon = 'lexicon-us-en.txt,lexicon-zh.txt';
  190 + // ruleFsts = `date-zh.fst,phone-zh.fst,number-zh.fst`;
  191 +
182 // ============================================================ 192 // ============================================================
183 // Please don't change the remaining part of this function 193 // Please don't change the remaining part of this function
184 // ============================================================ 194 // ============================================================
@@ -225,7 +235,14 @@ function initTts(context: Context): OfflineTts { @@ -225,7 +235,14 @@ function initTts(context: Context): OfflineTts {
225 ruleFars = tmp.join(','); 235 ruleFars = tmp.join(',');
226 } 236 }
227 237
228 - if (lexicon != '') { 238 + if (lexicon.includes(",")) {
  239 + let v = lexicon.split(',')
  240 + let tmp: string[] = [];
  241 + for (const f of v) {
  242 + tmp.push(modelDir + '/' + f);
  243 + }
  244 + lexicon = tmp.join(',');
  245 + } else if (lexicon != '') {
229 lexicon = modelDir + '/' + lexicon; 246 lexicon = modelDir + '/' + lexicon;
230 } 247 }
231 248
@@ -250,26 +267,33 @@ function initTts(context: Context): OfflineTts { @@ -250,26 +267,33 @@ function initTts(context: Context): OfflineTts {
250 config.model.vits.model = modelName; 267 config.model.vits.model = modelName;
251 } 268 }
252 269
253 - config.model.vits.lexicon = lexicon;  
254 - config.model.vits.tokens = tokens;  
255 - config.model.vits.dataDir = dataDir;  
256 - config.model.vits.dictDir = dictDir;  
257 -  
258 - config.model.matcha.acousticModel = acousticModelName;  
259 - config.model.matcha.vocoder = vocoder;  
260 - config.model.matcha.lexicon = lexicon;  
261 - config.model.matcha.tokens = tokens;  
262 - config.model.matcha.dataDir = dataDir;  
263 - config.model.matcha.dictDir = dictDir; 270 + if (voices == '') {
  271 + config.model.vits.lexicon = lexicon;
  272 + config.model.vits.tokens = tokens;
  273 + config.model.vits.dataDir = dataDir;
  274 + config.model.vits.dictDir = dictDir;
  275 +
  276 + config.model.matcha.acousticModel = acousticModelName;
  277 + config.model.matcha.vocoder = vocoder;
  278 + config.model.matcha.lexicon = lexicon;
  279 + config.model.matcha.tokens = tokens;
  280 + config.model.matcha.dataDir = dataDir;
  281 + config.model.matcha.dictDir = dictDir;
  282 + }
264 283
265 if (voices != '') { 284 if (voices != '') {
266 config.model.kokoro.model = modelName; 285 config.model.kokoro.model = modelName;
267 } else { 286 } else {
268 config.model.kokoro.model = ''; 287 config.model.kokoro.model = '';
269 } 288 }
270 - config.model.kokoro.voices = voices;  
271 - config.model.kokoro.tokens = tokens;  
272 - config.model.kokoro.dataDir = dataDir; 289 +
  290 + if (voices != '') {
  291 + config.model.kokoro.voices = voices;
  292 + config.model.kokoro.tokens = tokens;
  293 + config.model.kokoro.dataDir = dataDir;
  294 + config.model.kokoro.dictDir = dictDir;
  295 + config.model.kokoro.lexicon = lexicon;
  296 + }
273 297
274 config.model.numThreads = 2; 298 config.model.numThreads = 2;
275 config.model.debug = true; 299 config.model.debug = true;