NonStreamingTtsWorker.ets
11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import worker, { ThreadWorkerGlobalScope, MessageEvents, ErrorEvent } from '@ohos.worker';
import { fileIo as fs } from '@kit.CoreFileKit';
import { OfflineTtsConfig, OfflineTts, listRawfileDir, TtsInput, TtsOutput } from 'sherpa_onnx';
import { buffer } from '@kit.ArkTS';
const workerPort: ThreadWorkerGlobalScope = worker.workerPort;
let tts: OfflineTts;
let cancelled = false;
function mkdir(context: Context, parts: string[]) {
const path = parts.join('/');
if (fs.accessSync(path)) {
return;
}
const sandboxPath: string = context.getApplicationContext().filesDir;
let d = sandboxPath
for (const p of parts) {
d = d + '/' + p;
if (fs.accessSync(d)) {
continue;
}
fs.mkdirSync(d);
}
}
function copyRawFileDirToSandbox(context: Context, srcDir: string) {
let mgr = context.resourceManager;
const allFiles: string[] = listRawfileDir(mgr, srcDir);
for (const src of allFiles) {
const parts: string[] = src.split('/');
if (parts.length != 1) {
mkdir(context, parts.slice(0, -1));
}
copyRawFileToSandbox(context, src, src);
}
}
function copyRawFileToSandbox(context: Context, src: string,
dst: string) {
/* see
https://blog.csdn.net/weixin_44640245/article/details/142634846
https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/rawfile-guidelines-V5
*/
let uint8Array: Uint8Array = context.resourceManager.getRawFileContentSync(src);
// https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fs-V5#fsmkdir
let sandboxPath: string = context.getApplicationContext().filesDir;
let filepath = sandboxPath + '/' + dst;
if (fs.accessSync(filepath)) {
/* if the destination exists and has the expected file size
then we skip copying it
*/
let stat = fs.statSync(filepath);
if (stat.size == uint8Array.length) {
return;
}
}
const fp = fs.openSync(filepath, fs.OpenMode.WRITE_ONLY | fs.OpenMode.CREATE | fs.OpenMode.TRUNC);
fs.writeSync(fp.fd, buffer.from(uint8Array).buffer)
fs.close(fp.fd);
}
function initTts(context: Context): OfflineTts {
/* Such a design is to make it easier to build flutter APPs with
github actions for a variety of tts models
See https://github.com/k2-fsa/sherpa-onnx/blob/master/scripts/flutter/generate-tts.py
for details
*/
let modelDir = '';
// for VITS begin
let modelName = '';
// for VITS end
// for Matcha begin
let acousticModelName = '';
let vocoder = '';
// for Matcha end
// for Kokoro begin
let voices = '';
// for Kokoro end
let ruleFsts = '';
let ruleFars = '';
let lexicon = '';
let dataDir = '';
let dictDir = '';
/*
You can select an example below and change it according to match your
selected tts model
*/
// ============================================================
// Your change starts here
// ============================================================
// Example 1:
// modelDir = 'vits-vctk';
// modelName = 'vits-vctk.onnx';
// lexicon = 'lexicon.txt';
// Example 2:
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
// https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_US-amy-low.tar.bz2
// modelDir = 'vits-piper-en_US-amy-low';
// modelName = 'en_US-amy-low.onnx';
// dataDir = 'espeak-ng-data';
// Example 3:
// https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-icefall-zh-aishell3.tar.bz2
// modelDir = 'vits-icefall-zh-aishell3';
// modelName = 'model.onnx';
// ruleFsts = 'phone.fst,date.fst,number.fst,new_heteronym.fst';
// ruleFars = 'rule.far';
// lexicon = 'lexicon.txt';
// Example 4:
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/vits.html#csukuangfj-vits-zh-hf-fanchen-c-chinese-187-speakers
// modelDir = 'vits-zh-hf-fanchen-C';
// modelName = 'vits-zh-hf-fanchen-C.onnx';
// lexicon = 'lexicon.txt';
// dictDir = 'dict';
// Example 5:
// https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-coqui-de-css10.tar.bz2
// modelDir = 'vits-coqui-de-css10';
// modelName = 'model.onnx';
// Example 6
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
// https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-piper-en_US-libritts_r-medium.tar.bz2
// modelDir = 'vits-piper-en_US-libritts_r-medium';
// modelName = 'en_US-libritts_r-medium.onnx';
// dataDir = 'espeak-ng-data';
// Example 7
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
// https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/vits-melo-tts-zh_en.tar.bz2
// modelDir = 'vits-melo-tts-zh_en';
// modelName = 'model.onnx';
// lexicon = 'lexicon.txt';
// dictDir = 'dict';
// ruleFsts = `date.fst,phone.fst,number.fst`;
// Example 8
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/matcha.html#matcha-icefall-zh-baker-chinese-1-female-speaker
// modelDir = 'matcha-icefall-zh-baker';
// acousticModelName = 'model-steps-3.onnx';
// vocoder = 'vocos-22khz-univ.onnx';
// lexicon = 'lexicon.txt';
// dictDir = 'dict';
// ruleFsts = `date.fst,phone.fst,number.fst`;
// Example 9
// https://github.com/k2-fsa/sherpa-onnx/releases/tag/tts-models
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/matcha.html#matcha-icefall-en-us-ljspeech-american-english-1-female-speaker
// modelDir = 'matcha-icefall-en_US-ljspeech';
// acousticModelName = 'model-steps-3.onnx';
// vocoder = 'vocos-22khz-univ.onnx';
// dataDir = 'espeak-ng-data';
// Example 10
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/kokoro.html#kokoro-en-v0-19-english-11-speakers
// modelDir = 'kokoro-en-v0_19';
// modelName = 'model.onnx';
// voices = 'voices.bin'
// dataDir = 'espeak-ng-data';
// Example 11
// https://k2-fsa.github.io/sherpa/onnx/tts/pretrained_models/kokoro.html
// modelDir = 'kokoro-multi-lang-v1_0';
// modelName = 'model.onnx';
// voices = 'voices.bin'
// dataDir = 'espeak-ng-data';
// dictDir = 'dict';
// lexicon = 'lexicon-us-en.txt,lexicon-zh.txt';
// ruleFsts = `date-zh.fst,phone-zh.fst,number-zh.fst`;
// ============================================================
// Please don't change the remaining part of this function
// ============================================================
if (modelName == '' && acousticModelName == '' && vocoder == '') {
throw new Error('You are supposed to select a model by changing the code before you run the app');
}
if (modelName != '' && acousticModelName != '') {
throw new Error('Please select either VITS or Matcha, not both');
}
if (acousticModelName != '' && vocoder == '') {
throw new Error('Please provider vocoder for matcha tts models');
}
if (modelName != '') {
modelName = modelDir + '/' + modelName;
}
if (acousticModelName != '') {
acousticModelName = modelDir + '/' + acousticModelName;
}
if (voices != '') {
voices = modelDir + '/' + voices;
}
if (ruleFsts != '') {
let fsts = ruleFsts.split(',')
let tmp: string[] = [];
for (const f of fsts) {
tmp.push(modelDir + '/' + f);
}
ruleFsts = tmp.join(',');
}
if (ruleFars != '') {
let fars = ruleFars.split(',')
let tmp: string[] = [];
for (const f of fars) {
tmp.push(modelDir + '/' + f);
}
ruleFars = tmp.join(',');
}
if (lexicon.includes(",")) {
let v = lexicon.split(',')
let tmp: string[] = [];
for (const f of v) {
tmp.push(modelDir + '/' + f);
}
lexicon = tmp.join(',');
} else if (lexicon != '') {
lexicon = modelDir + '/' + lexicon;
}
if (dataDir != '') {
copyRawFileDirToSandbox(context, modelDir + '/' + dataDir)
let sandboxPath: string = context.getApplicationContext().filesDir;
dataDir = sandboxPath + '/' + modelDir + '/' + dataDir;
}
if (dictDir != '') {
copyRawFileDirToSandbox(context, modelDir + '/' + dictDir)
let sandboxPath: string = context.getApplicationContext().filesDir;
dictDir = sandboxPath + '/' + modelDir + '/' + dictDir;
}
const tokens = modelDir + '/tokens.txt';
const config: OfflineTtsConfig = new OfflineTtsConfig();
if (voices != '') {
config.model.vits.model = '';
} else {
config.model.vits.model = modelName;
}
if (voices == '') {
config.model.vits.lexicon = lexicon;
config.model.vits.tokens = tokens;
config.model.vits.dataDir = dataDir;
config.model.vits.dictDir = dictDir;
config.model.matcha.acousticModel = acousticModelName;
config.model.matcha.vocoder = vocoder;
config.model.matcha.lexicon = lexicon;
config.model.matcha.tokens = tokens;
config.model.matcha.dataDir = dataDir;
config.model.matcha.dictDir = dictDir;
}
if (voices != '') {
config.model.kokoro.model = modelName;
} else {
config.model.kokoro.model = '';
}
if (voices != '') {
config.model.kokoro.voices = voices;
config.model.kokoro.tokens = tokens;
config.model.kokoro.dataDir = dataDir;
config.model.kokoro.dictDir = dictDir;
config.model.kokoro.lexicon = lexicon;
}
config.model.numThreads = 2;
config.model.debug = true;
config.ruleFsts = ruleFsts;
config.ruleFars = ruleFars;
return new OfflineTts(config, context.resourceManager);
}
interface TtsCallbackData {
samples: Float32Array;
progress: number;
}
function callback(data: TtsCallbackData): number {
workerPort.postMessage({
'msgType': 'tts-generate-partial', samples: Float32Array.from(data.samples), progress: data.progress,
});
// 0 means to stop generating in C++
// 1 means to continue generating in C++
return cancelled ? 0 : 1;
}
/**
* Defines the event handler to be called when the worker thread receives a message sent by the host thread.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessage = (e: MessageEvents) => {
const msgType = e.data['msgType'] as string;
console.log(`msg-type: ${msgType}`);
if (msgType == 'init-tts' && !tts) {
const context = e.data['context'] as Context;
tts = initTts(context);
workerPort.postMessage({
'msgType': 'init-tts-done',
sampleRate: tts.sampleRate,
numSpeakers: tts.numSpeakers,
numThreads: tts.config.model.numThreads,
});
}
if (msgType == 'tts-generate-cancel') {
cancelled = true;
}
if (msgType == 'tts-generate') {
const text = e.data['text'] as string;
console.log(`recevied text ${text}`);
const input: TtsInput = new TtsInput();
input.text = text;
input.sid = e.data['sid'] as number;
input.speed = e.data['speed'] as number;
input.callback = callback;
cancelled = false;
if (true) {
tts.generateAsync(input).then((ttsOutput: TtsOutput) => {
console.log(`sampleRate: ${ttsOutput.sampleRate}`);
workerPort.postMessage({
'msgType': 'tts-generate-done', samples: Float32Array.from(ttsOutput.samples),
});
});
} else {
const ttsOutput: TtsOutput = tts.generate(input);
workerPort.postMessage({
'msgType': 'tts-generate-done', samples: Float32Array.from(ttsOutput.samples),
});
}
}
}
/**
* Defines the event handler to be called when the worker receives a message that cannot be deserialized.
* The event handler is executed in the worker thread.
*
* @param e message data
*/
workerPort.onmessageerror = (e: MessageEvents) => {
}
/**
* Defines the event handler to be called when an exception occurs during worker execution.
* The event handler is executed in the worker thread.
*
* @param e error message
*/
workerPort.onerror = (e: ErrorEvent) => {
}