@met4citizen/headtts 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +546 -0
- package/dictionaries/en-us.txt +125829 -0
- package/dictionaries/fi.txt +9939 -0
- package/modules/headtts-node.mjs +425 -0
- package/modules/headtts.mjs +975 -0
- package/modules/language-en-us.mjs +589 -0
- package/modules/language-fi.mjs +542 -0
- package/modules/language.mjs +469 -0
- package/modules/utils.mjs +206 -0
- package/modules/worker-tts.mjs +381 -0
- package/package.json +44 -0
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import * as utils from "./utils.mjs";
|
|
2
|
+
|
|
3
|
+
// Base class for language modules
|
|
4
|
+
class LanguageBase {
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @constructor
|
|
8
|
+
*/
|
|
9
|
+
constructor( settings = null ) {
|
|
10
|
+
this.settings = Object.assign({
|
|
11
|
+
trace: false
|
|
12
|
+
}, settings || {});
|
|
13
|
+
|
|
14
|
+
// Whitespace characters
|
|
15
|
+
this.whitespaces = {
|
|
16
|
+
" ": " ", "\n": "\n", "\t": "\t", "\r": "\r", "\v": "\v", "\f": "\f",
|
|
17
|
+
"\u00A0": "\u00A0", "\u1680": "\u1680", "\u2000": "\u2000",
|
|
18
|
+
"\u2001": "\u2001", "\u2002": "\u2002", "\u2003": "\u2003",
|
|
19
|
+
"\u2004": "\u2004", "\u2005": "\u2005", "\u2006": "\u2006",
|
|
20
|
+
"\u2007": "\u2007", "\u2008": "\u2008", "\u2008": "\u2008",
|
|
21
|
+
"\u2009": "\u2009", "\u200A": "\u200A", "\u2028": "\u2028",
|
|
22
|
+
"\u2029": "\u2029", "\u202F": "\u202F", "\u205F": "\u205F"
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
// Misaki tokens to Oculus visemes
|
|
26
|
+
this.misakiToOculusViseme = {
|
|
27
|
+
"$": null, ";": null, ":": null, ",": null, ".": null, "!": null,
|
|
28
|
+
"?": null, "—": null, "…": null, "\"": null, "(": null, ")": null,
|
|
29
|
+
"“": null, "”": null, " ": null, "\u0303": null, "ʣ": "DD", "ʥ": "CH",
|
|
30
|
+
"ʦ": "CH", "ʨ": "CH", "ᵝ": null, "ꭧ": null, "A": "E", "I": "I",
|
|
31
|
+
"O": "O", "Q": "O", "S": "SS", "T": "DD", "W": "U", "Y": "I",
|
|
32
|
+
"ᵊ": null, "a": "aa", "b": "PP", "c": "kk", "d": "DD", "e": "E",
|
|
33
|
+
"f": "FF", "h": null, "i": "I", "j": "I", "k": "kk", "l": "RR",
|
|
34
|
+
"m": "PP", "n": "nn", "o": "O", "p": "PP", "q": "kk", "r": "RR",
|
|
35
|
+
"s": "SS", "t": "DD", "u": "U", "v": "FF", "w": "U", "x": "SS",
|
|
36
|
+
"y": "I", "z": "SS", "ɑ": "aa", "ɐ": "aa", "ɒ": "aa", "æ": "aa",
|
|
37
|
+
"β": "FF", "ɔ": "O", "ɕ": "SS", "ç": "SS", "ɖ": "DD", "ð": "TH", "ʤ":
|
|
38
|
+
"CH", "ə": "E", "ɚ": "RR", "ɛ": "E", "ɜ": "E", "ɟ": "DD", "ɡ": "kk",
|
|
39
|
+
"ɥ": "U", "ɨ": "I", "ɪ": "I", "ʝ": "I", "ɯ": "U", "ɰ": "U", "ŋ": "nn",
|
|
40
|
+
"ɳ": "nn", "ɲ": "nn", "ɴ": "nn", "ø": "O", "ɸ": "FF", "θ": "TH",
|
|
41
|
+
"œ": "E", "ɹ": "RR", "ɾ": "DD", "ɻ": "RR", "ʁ": "RR", "ɽ": "RR",
|
|
42
|
+
"ʂ": "SS", "ʃ": "SS", "ʈ": "DD", "ʧ": "CH", "ʊ": "U", "ʋ": "FF",
|
|
43
|
+
"ʌ": "aa", "ɣ": null, "ɤ": "O", "χ": null, "ʎ": "RR", "ʒ": "SS",
|
|
44
|
+
"ʔ": null, "ˈ": null, "ˌ": null, "ː": null, "ʰ": null, "ʲ": null,
|
|
45
|
+
"↓": null, "→": null, "↗": null, "↘": null, "ᵻ": "I"
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
// Allowed letters in upper case
|
|
49
|
+
// NOTE: Diacritics will be removed unless added to this object.
|
|
50
|
+
this.normalizedLettersUpper = {
|
|
51
|
+
'A': 'A', 'B': 'B', 'C': 'C', 'D': 'D', 'E': 'E', 'F': 'F', 'G': 'G',
|
|
52
|
+
'H': 'H', 'I': 'I', 'J': 'J', 'K': 'K', 'L': 'L', 'M': 'M', 'N': 'N',
|
|
53
|
+
'O': 'O', 'P': 'P', 'Q': 'Q', 'R': 'R', 'S': 'S', 'T': 'T', 'U': 'U',
|
|
54
|
+
'V': 'V', 'W': 'W', 'X': 'X', 'Y': 'Y', 'Z': 'Z', 'ß': 'SS', 'Ø': 'O',
|
|
55
|
+
'Æ': 'AE', 'Œ': 'OE', 'Ð': 'D', 'Þ': 'TH', 'Ł': 'L'
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// Allowed punctuations
|
|
59
|
+
this.punctuations = {
|
|
60
|
+
";": ";", ":": ":", ",": ",", ".": ".", "!": "!", "?": "?", "¡": "!",
|
|
61
|
+
"¿": "?", "—": "—", '"': '"', "…": "…", "«": '"', "»": '"', "“": '"',
|
|
62
|
+
"”": '"', "(": "(", ")": ")", "{": "(", "}": ")", "[": "(", "]": ")",
|
|
63
|
+
" ": " ", "-": "-", "'": "'"
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
// Allowed punctuations in mid-word
|
|
67
|
+
this.punctuationsMidWord = {
|
|
68
|
+
"-": "-", "'": "'", ".": ".", ",": ","
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if ( this.settings.trace ) {
|
|
72
|
+
utils.trace( 'Language base module initiated.' );
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Convert graphemes to phonemes.
|
|
78
|
+
*
|
|
79
|
+
* @param {string} s Word
|
|
80
|
+
* @return {string[]} Array of phonemes
|
|
81
|
+
*/
|
|
82
|
+
phonemizeWord(s) {
|
|
83
|
+
throw new Error("The method phonemizeWord not implemented.");
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Add one dictionary line.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} s Line
|
|
90
|
+
*/
|
|
91
|
+
addToDictionary(s) {
|
|
92
|
+
if ( s.startsWith(";;;") ) return; // Comment
|
|
93
|
+
const fields = s.split("\t");
|
|
94
|
+
if ( fields.length >= 2) {
|
|
95
|
+
const word = fields[0];
|
|
96
|
+
const phonemes = fields[1].split("");
|
|
97
|
+
this.dictionary[word] = phonemes;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Load pronouncing dictionary.
|
|
103
|
+
*
|
|
104
|
+
* @param {string} [dictionary=null] Dictionary path/url. If null, do not use dictionaries
|
|
105
|
+
* @param {boolean} [force=false] If true, re-load even if already loaded.
|
|
106
|
+
*/
|
|
107
|
+
async loadDictionary( dictionary = null, force = false ) {
|
|
108
|
+
|
|
109
|
+
if ( this.dictionary && !force ) return;
|
|
110
|
+
this.dictionary = null;
|
|
111
|
+
if ( dictionary ) {
|
|
112
|
+
|
|
113
|
+
// Use stream to read the file
|
|
114
|
+
if ( utils.isNode() ) {
|
|
115
|
+
const fs = await import('node:fs');
|
|
116
|
+
const readline = await import('node:readline');
|
|
117
|
+
let stream = fs.createReadStream(dictionary, { encoding: 'utf8' });
|
|
118
|
+
const rl = readline.createInterface({
|
|
119
|
+
input: stream,
|
|
120
|
+
crlfDelay: Infinity
|
|
121
|
+
});
|
|
122
|
+
this.dictionary = {};
|
|
123
|
+
for await (const line of rl) {
|
|
124
|
+
this.addToDictionary(line);
|
|
125
|
+
}
|
|
126
|
+
} else {
|
|
127
|
+
const response = await fetch(dictionary);
|
|
128
|
+
const reader = response.body.getReader();
|
|
129
|
+
const decoder = new TextDecoder(); // Defaults to utf-8
|
|
130
|
+
let buffer = "";
|
|
131
|
+
this.dictionary = {};
|
|
132
|
+
while (true) {
|
|
133
|
+
const { value, done } = await reader.read();
|
|
134
|
+
let lines;
|
|
135
|
+
if (done) {
|
|
136
|
+
lines = [buffer];
|
|
137
|
+
} else {
|
|
138
|
+
buffer += decoder.decode(value, { stream: true });
|
|
139
|
+
lines = buffer.split(/\r?\n/);
|
|
140
|
+
buffer = lines.pop(); // Save the incomplete line
|
|
141
|
+
}
|
|
142
|
+
for (const line of lines) {
|
|
143
|
+
this.addToDictionary(line);
|
|
144
|
+
}
|
|
145
|
+
if ( done ) break;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if ( this.settings.trace ) {
|
|
151
|
+
utils.trace( 'Language dictionary "' + dictionary + '" loaded.' );
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Split text string in parts that contain one word.
|
|
159
|
+
* Each part is split from the first letter of the word, except
|
|
160
|
+
* the first part, which starts from the beginning including
|
|
161
|
+
* leading spaces, if any.
|
|
162
|
+
*
|
|
163
|
+
* @param {string} s Text string
|
|
164
|
+
* @return {string[]} Array of parts
|
|
165
|
+
*/
|
|
166
|
+
splitText(s) {
|
|
167
|
+
|
|
168
|
+
const parts = [];
|
|
169
|
+
const chars = [...s];
|
|
170
|
+
const len = chars.length;
|
|
171
|
+
let i = 0;
|
|
172
|
+
let lastType = 0; // 0=unknown, 1=whitespace, 2=other
|
|
173
|
+
let foundWord = false;
|
|
174
|
+
let part = "";
|
|
175
|
+
while( i<len ) {
|
|
176
|
+
const isLast = i === (len-1);
|
|
177
|
+
const c = chars[i];
|
|
178
|
+
const type = this.whitespaces.hasOwnProperty(c) ? 1 : 2;
|
|
179
|
+
|
|
180
|
+
if ( isLast ) {
|
|
181
|
+
parts.push(part+c);
|
|
182
|
+
} else if ( foundWord && type === 2 && type !== lastType) {
|
|
183
|
+
parts.push(part);
|
|
184
|
+
part = c;
|
|
185
|
+
lastType = type;
|
|
186
|
+
foundWord = type === 2;
|
|
187
|
+
} else {
|
|
188
|
+
part += c;
|
|
189
|
+
lastType = type;
|
|
190
|
+
foundWord = foundWord || type === 2;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
i++;
|
|
194
|
+
}
|
|
195
|
+
return parts;
|
|
196
|
+
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
/**
|
|
200
|
+
* Normalize text and set it to uppercase.
|
|
201
|
+
*
|
|
202
|
+
* @param {string} s Text string
|
|
203
|
+
* @return {string[]} Normalized array of characters in upper case.
|
|
204
|
+
*/
|
|
205
|
+
normalizeUpper(s) {
|
|
206
|
+
const norm = [];
|
|
207
|
+
const chars = [...s.toUpperCase()];
|
|
208
|
+
const len = chars.length;
|
|
209
|
+
let i=0;
|
|
210
|
+
while( i<len ) {
|
|
211
|
+
let c = chars[i];
|
|
212
|
+
if ( this.normalizedLettersUpper.hasOwnProperty(c) ) {
|
|
213
|
+
norm.push( this.normalizedLettersUpper[c] );
|
|
214
|
+
} else if ( this.punctuations.hasOwnProperty(c) ) {
|
|
215
|
+
norm.push( this.punctuations[c] );
|
|
216
|
+
} else {
|
|
217
|
+
c = c.normalize("NFD").replace(/[\u0300-\u036f]/g, "").normalize("NFC");
|
|
218
|
+
if ( this.normalizedLettersUpper.hasOwnProperty(c) ) {
|
|
219
|
+
norm.push( this.normalizedLettersUpper[c] );
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
i++;
|
|
223
|
+
}
|
|
224
|
+
return norm;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* Set the `text` to be spoken by analysing the part content.
|
|
229
|
+
* NOTE: The language module should override this
|
|
230
|
+
* method and implement language specific conversions.
|
|
231
|
+
*
|
|
232
|
+
* @param {Object} part Current part
|
|
233
|
+
* @param {number} i Index
|
|
234
|
+
* @param {Object[]} arr All the parts.
|
|
235
|
+
*/
|
|
236
|
+
partSetText(part,i,arr) {
|
|
237
|
+
|
|
238
|
+
switch( part.type ) {
|
|
239
|
+
case "text":
|
|
240
|
+
part.text = part.value;
|
|
241
|
+
part.subtitles = part.value;
|
|
242
|
+
break;
|
|
243
|
+
|
|
244
|
+
case "speech":
|
|
245
|
+
part.text = part.value;
|
|
246
|
+
break;
|
|
247
|
+
|
|
248
|
+
case "phonetic":
|
|
249
|
+
part.phonemes = part.value;
|
|
250
|
+
break;
|
|
251
|
+
|
|
252
|
+
case "break":
|
|
253
|
+
// TODO: Figure out better way to add breaks
|
|
254
|
+
let cnt = Math.max(2, Math.min( part.value / 500, 10 ));
|
|
255
|
+
part.phonemes = Array(cnt).fill(".").join("") + " ";
|
|
256
|
+
break;
|
|
257
|
+
|
|
258
|
+
// Leave other types to language specific implementation
|
|
259
|
+
}
|
|
260
|
+
part.subtitles = part.subtitles || "";
|
|
261
|
+
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Divide the given part into punctionations and words.
|
|
267
|
+
* NOTE: Only `text` type currently supported.
|
|
268
|
+
* TODO: Add support for other types.
|
|
269
|
+
*
|
|
270
|
+
* @param {Object} part Part object
|
|
271
|
+
*/
|
|
272
|
+
partSetSpeak(part) {
|
|
273
|
+
|
|
274
|
+
part.speak = [];
|
|
275
|
+
|
|
276
|
+
if ( part.text && typeof part.text === "string" ) {
|
|
277
|
+
const chars = this.normalizeUpper(part.text);
|
|
278
|
+
const len = chars.length;
|
|
279
|
+
let i=0;
|
|
280
|
+
let word = "";
|
|
281
|
+
let lastType = 0; // 0=Unknown, 1=punctuation, 2=word
|
|
282
|
+
while( i<len ) {
|
|
283
|
+
const isLast = i === (len-1);
|
|
284
|
+
const c = chars[i];
|
|
285
|
+
const cNext = isLast ? null : chars[i+1];
|
|
286
|
+
const isPunctuation = this.punctuations.hasOwnProperty(c);
|
|
287
|
+
const isPunctuationMid = !isLast && this.punctuationsMidWord.hasOwnProperty(c) && !this.punctuations.hasOwnProperty(cNext);
|
|
288
|
+
const type = (isPunctuation && !isPunctuationMid) ? 1 : 2;
|
|
289
|
+
|
|
290
|
+
if ( type === lastType ) {
|
|
291
|
+
word += c;
|
|
292
|
+
} else {
|
|
293
|
+
if ( word ) {
|
|
294
|
+
part.speak.push({ type: lastType, word: word });
|
|
295
|
+
word = "";
|
|
296
|
+
}
|
|
297
|
+
word = c;
|
|
298
|
+
lastType = type;
|
|
299
|
+
}
|
|
300
|
+
if ( isLast && word ) {
|
|
301
|
+
part.speak.push({ type, word });
|
|
302
|
+
}
|
|
303
|
+
i++;
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Construct `phonemes` from speakable words and punctuations.
|
|
311
|
+
*
|
|
312
|
+
* @param {Object} part Part object
|
|
313
|
+
*/
|
|
314
|
+
partSetPhonemes(part) {
|
|
315
|
+
|
|
316
|
+
// Initialize
|
|
317
|
+
part.phonemes = [];
|
|
318
|
+
|
|
319
|
+
if ( part.speak && Array.isArray(part.speak) ) {
|
|
320
|
+
const len = part.speak.length;
|
|
321
|
+
let i = 0;
|
|
322
|
+
while ( i<len ) {
|
|
323
|
+
const s = part.speak[i];
|
|
324
|
+
if ( s.type === 1 ) {
|
|
325
|
+
part.phonemes.push(...s.word);
|
|
326
|
+
} else if ( s.type === 2 ) {
|
|
327
|
+
part.phonemes.push(...this.phonemizeWord(s.word));
|
|
328
|
+
}
|
|
329
|
+
i++;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Construct `visemes` from `phonemes`.
|
|
337
|
+
*
|
|
338
|
+
* @param {Object} part Part object
|
|
339
|
+
*/
|
|
340
|
+
partSetVisemes(part) {
|
|
341
|
+
|
|
342
|
+
// Initialize
|
|
343
|
+
part.visemes = [];
|
|
344
|
+
|
|
345
|
+
// Map
|
|
346
|
+
if ( part.phonemes && Array.isArray(part.phonemes)) {
|
|
347
|
+
part.phonemes.forEach( ph => {
|
|
348
|
+
if ( this.misakiToOculusViseme.hasOwnProperty(ph) ) {
|
|
349
|
+
part.visemes.push( this.misakiToOculusViseme[ph] );
|
|
350
|
+
} else {
|
|
351
|
+
// console.info('Viseme not found for "' + ph + '"');
|
|
352
|
+
part.visemes.push(null)
|
|
353
|
+
}
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Generate phonemes and TalkingHead metadata from input.
|
|
362
|
+
* NOTE: Starting times and durations in `metadata` refer to phoneme
|
|
363
|
+
* start and end indices respectively.
|
|
364
|
+
*
|
|
365
|
+
* @param {string|Object[]} input Input element
|
|
366
|
+
* @return {Object} `phonemes` array and TalkingHead `metadata` template
|
|
367
|
+
*/
|
|
368
|
+
generate(input) {
|
|
369
|
+
|
|
370
|
+
// Output data
|
|
371
|
+
let phonemes = [];
|
|
372
|
+
const metadata = {
|
|
373
|
+
words: [], wtimes: [], wdurations: [],
|
|
374
|
+
visemes: [], vtimes: [], vdurations: []
|
|
375
|
+
};
|
|
376
|
+
|
|
377
|
+
if ( this.settings.trace ) {
|
|
378
|
+
utils.trace( 'Generate start, input=', input );
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Break input into parts
|
|
382
|
+
let parts = [];
|
|
383
|
+
const inputs = Array.isArray(input) ? input : [input];
|
|
384
|
+
inputs.forEach( x => {
|
|
385
|
+
if ( typeof x === "string" ) {
|
|
386
|
+
const textParts = this.splitText(x);
|
|
387
|
+
textParts.forEach( y => {
|
|
388
|
+
const part = { type: "text", value: y };
|
|
389
|
+
parts.push( part );
|
|
390
|
+
});
|
|
391
|
+
} else {
|
|
392
|
+
parts.push(x);
|
|
393
|
+
}
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
// Set text to be spoken
|
|
397
|
+
parts.forEach( this.partSetText.bind(this) );
|
|
398
|
+
|
|
399
|
+
// Populate output
|
|
400
|
+
parts.forEach( part => {
|
|
401
|
+
|
|
402
|
+
// Phonemize and set visemes
|
|
403
|
+
if ( part.hasOwnProperty("phonemes") ) {
|
|
404
|
+
if ( typeof part.phonemes === "string" ) {
|
|
405
|
+
part.phonemes = [...part.phonemes];
|
|
406
|
+
}
|
|
407
|
+
} else {
|
|
408
|
+
this.partSetSpeak(part);
|
|
409
|
+
this.partSetPhonemes(part);
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
// Set visemes
|
|
413
|
+
this.partSetVisemes(part);
|
|
414
|
+
|
|
415
|
+
if ( this.settings.trace ) {
|
|
416
|
+
utils.trace( 'Generate, part=', part );
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Set output
|
|
420
|
+
metadata.words.push( part.subtitles );
|
|
421
|
+
metadata.wtimes.push( phonemes.length );
|
|
422
|
+
const len = part.phonemes.length
|
|
423
|
+
let i = phonemes.length; // Phoneme index
|
|
424
|
+
for( let j=0; j<len; j++ ) {
|
|
425
|
+
|
|
426
|
+
// Phonemes
|
|
427
|
+
const ph = part.phonemes[j];
|
|
428
|
+
phonemes.push( ph );
|
|
429
|
+
|
|
430
|
+
// Visemes
|
|
431
|
+
const viseme = part.visemes[j];
|
|
432
|
+
if ( viseme ) {
|
|
433
|
+
metadata.visemes.push(viseme);
|
|
434
|
+
metadata.vtimes.push(i);
|
|
435
|
+
i = phonemes.length;
|
|
436
|
+
metadata.vdurations.push(i);
|
|
437
|
+
} else {
|
|
438
|
+
|
|
439
|
+
// Custom timing
|
|
440
|
+
if ( ph === "ˈ" || ph === "ˌ" ) {
|
|
441
|
+
// Do not update startingtime
|
|
442
|
+
} else if ( ph === "ː" ) {
|
|
443
|
+
i = phonemes.length;
|
|
444
|
+
const len2 = metadata.visemes.length;
|
|
445
|
+
if ( len2 ) {
|
|
446
|
+
metadata.vdurations[len2-1] = i;
|
|
447
|
+
}
|
|
448
|
+
} else {
|
|
449
|
+
i = phonemes.length;
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
}
|
|
453
|
+
}
|
|
454
|
+
metadata.wdurations.push( phonemes.length );
|
|
455
|
+
|
|
456
|
+
});
|
|
457
|
+
|
|
458
|
+
if ( this.settings.trace ) {
|
|
459
|
+
utils.trace( 'Generate end, output=', { phonemes, metadata } );
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Output
|
|
463
|
+
return { phonemes, metadata };
|
|
464
|
+
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
export { LanguageBase };
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Test if we are in Node.js environment
|
|
5
|
+
* @return {boolean} If true, we are in Node.js
|
|
6
|
+
*/
|
|
7
|
+
export function isNode() {
|
|
8
|
+
return typeof import.meta !== 'undefined' &&
|
|
9
|
+
typeof import.meta.url === 'string' &&
|
|
10
|
+
import.meta.url.startsWith('file://');
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Test support for Module Web Workers
|
|
16
|
+
* @return {boolean} If true, we support module web workers
|
|
17
|
+
*/
|
|
18
|
+
export function isModuleWebWorkers() {
|
|
19
|
+
let support = false;
|
|
20
|
+
try {
|
|
21
|
+
const tester = {
|
|
22
|
+
get type() { support = true; }
|
|
23
|
+
}
|
|
24
|
+
const worker = new Worker('blob://', tester);
|
|
25
|
+
} finally {
|
|
26
|
+
return support;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Test support for WebGPU
|
|
32
|
+
* @return {boolean} If true, we support WebGPU
|
|
33
|
+
*/
|
|
34
|
+
export function isWebGPU() {
|
|
35
|
+
return !!(globalThis && globalThis.navigator && globalThis.navigator.gpu);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Traceable subsystems
|
|
40
|
+
*/
|
|
41
|
+
export const traceMask = {
|
|
42
|
+
connection: 1,
|
|
43
|
+
messages: 2,
|
|
44
|
+
events: 4,
|
|
45
|
+
g2p: 8,
|
|
46
|
+
language: 16
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Write console trace.
|
|
51
|
+
*
|
|
52
|
+
* @param {...any} outputs Output strings.
|
|
53
|
+
*/
|
|
54
|
+
export function trace( ...outputs ) {
|
|
55
|
+
const s = "HeadTTS [" + new Date().toISOString().slice(11, 23) + "] ";
|
|
56
|
+
console.log(s,...outputs);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* A utility class that creates a Promise along with its associated
|
|
61
|
+
* resolve and reject methods, allowing manual control over the Promise.
|
|
62
|
+
*/
|
|
63
|
+
export class Deferred {
|
|
64
|
+
constructor() {
|
|
65
|
+
this.status = "pending";
|
|
66
|
+
this.promise = new Promise((resolve, reject) => {
|
|
67
|
+
this.resolve = (value) => {
|
|
68
|
+
this.status = "resolved";
|
|
69
|
+
resolve(value);
|
|
70
|
+
};
|
|
71
|
+
this.reject = (reason) => {
|
|
72
|
+
this.status = "rejected";
|
|
73
|
+
reject(reason);
|
|
74
|
+
};
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Create a lookup table for base64 decoding
|
|
80
|
+
const b64Chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
|
81
|
+
const b64Lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
|
|
82
|
+
for (let i = 0; i < b64Chars.length; i++) b64Lookup[b64Chars.charCodeAt(i)] = i;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Convert a Base64 MP3 chunk to ArrayBuffer.
|
|
86
|
+
* @param {string} chunk Base64 encoded chunk
|
|
87
|
+
* @return {ArrayBuffer} ArrayBuffer
|
|
88
|
+
*/
|
|
89
|
+
export function b64ToArrayBuffer(chunk) {
|
|
90
|
+
|
|
91
|
+
// Calculate the needed total buffer length
|
|
92
|
+
let bufLen = 3 * chunk.length / 4;
|
|
93
|
+
if (chunk[chunk.length - 1] === '=') {
|
|
94
|
+
bufLen--;
|
|
95
|
+
if (chunk[chunk.length - 2] === '=') {
|
|
96
|
+
bufLen--;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Create the ArrayBuffer
|
|
101
|
+
const arrBuf = new ArrayBuffer(bufLen);
|
|
102
|
+
const arr = new Uint8Array(arrBuf);
|
|
103
|
+
let i, p = 0, c1, c2, c3, c4;
|
|
104
|
+
|
|
105
|
+
// Populate the buffer
|
|
106
|
+
for (i = 0; i < chunk.length; i += 4) {
|
|
107
|
+
c1 = b64Lookup[chunk.charCodeAt(i)];
|
|
108
|
+
c2 = b64Lookup[chunk.charCodeAt(i+1)];
|
|
109
|
+
c3 = b64Lookup[chunk.charCodeAt(i+2)];
|
|
110
|
+
c4 = b64Lookup[chunk.charCodeAt(i+3)];
|
|
111
|
+
arr[p++] = (c1 << 2) | (c2 >> 4);
|
|
112
|
+
arr[p++] = ((c2 & 15) << 4) | (c3 >> 2);
|
|
113
|
+
arr[p++] = ((c3 & 3) << 6) | (c4 & 63);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
return arrBuf;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Encode float32 samples to PCM 16bit LE buffer.
|
|
122
|
+
*
|
|
123
|
+
* @param {Float32Array} samples Float32 samples
|
|
124
|
+
* @param {number} sampleRate Sample rate
|
|
125
|
+
* @param {boolean} [header=true] If true, add WAV header
|
|
126
|
+
* @return {ArrayBuffer} WAV or raw PCM 16bit LE samples.
|
|
127
|
+
*/
|
|
128
|
+
export function encodeAudio(samples, sampleRate, header=true) {
|
|
129
|
+
const len = samples.length;
|
|
130
|
+
let offset = header ? 44 : 0;
|
|
131
|
+
const buffer = new ArrayBuffer(offset + len * 2);
|
|
132
|
+
const view = new DataView(buffer);
|
|
133
|
+
|
|
134
|
+
// Write WAV header
|
|
135
|
+
if ( header ) {
|
|
136
|
+
|
|
137
|
+
function writeString(view, off, string) {
|
|
138
|
+
for (let i = 0; i < string.length; i++) {
|
|
139
|
+
view.setUint8(off + i, string.charCodeAt(i));
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
writeString(view, 0, "RIFF");
|
|
144
|
+
view.setUint32(4, 32 + samples.length * 2, true);
|
|
145
|
+
writeString(view, 8, "WAVE");
|
|
146
|
+
writeString(view, 12, "fmt ");
|
|
147
|
+
view.setUint32(16, 16, true);
|
|
148
|
+
view.setUint16(20, 1, true);
|
|
149
|
+
view.setUint16(22, 1, true);
|
|
150
|
+
view.setUint32(24, sampleRate, true);
|
|
151
|
+
view.setUint32(28, sampleRate * 2, true);
|
|
152
|
+
view.setUint16(32, 2, true);
|
|
153
|
+
view.setUint16(34, 16, true);
|
|
154
|
+
writeString(view, 36, "data");
|
|
155
|
+
view.setUint32(40, samples.length * 2, true);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Write samples as PCM 16bit LE
|
|
159
|
+
for (let i = 0; i < len; i++, offset += 2) {
|
|
160
|
+
const s = Math.max(-1, Math.min(1, samples[i]));
|
|
161
|
+
view.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return buffer;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Concatenate an array of ArrayBuffers.
|
|
169
|
+
* @param {ArrayBuffer[]} bufs Array of ArrayBuffers
|
|
170
|
+
* @return {ArrayBuffer} Concatenated ArrayBuffer
|
|
171
|
+
*/
|
|
172
|
+
export function concatArrayBuffers(bufs) {
|
|
173
|
+
const len = bufs.length;
|
|
174
|
+
if ( len === 1 ) return bufs[0];
|
|
175
|
+
let byteLen = 0;
|
|
176
|
+
for( let i=0; i<len; i++ ) {
|
|
177
|
+
byteLen += bufs[i].byteLength;
|
|
178
|
+
}
|
|
179
|
+
const buf = new ArrayBuffer(byteLen);
|
|
180
|
+
const arr = new Uint8Array(buf);
|
|
181
|
+
let p = 0;
|
|
182
|
+
for( let i=0; i<len; i++ ) {
|
|
183
|
+
arr.set( new Uint8Array(bufs[i]), p);
|
|
184
|
+
p += bufs[i].byteLength;
|
|
185
|
+
}
|
|
186
|
+
return buf;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Convert PCM buffer to AudioBuffer.
|
|
191
|
+
* NOTE: Only signed 16bit little endian supported.
|
|
192
|
+
*
|
|
193
|
+
* @param {ArrayBuffer} buf PCM 16bit LE array buffer
|
|
194
|
+
* @return {AudioBuffer} AudioBuffer
|
|
195
|
+
*/
|
|
196
|
+
export function pcmToAudioBuffer(buf,samplerate,ctx) {
|
|
197
|
+
const arr = new Int16Array(buf);
|
|
198
|
+
const floats = new Float32Array(arr.length);
|
|
199
|
+
const len = arr.length;
|
|
200
|
+
for( let i=0; i<len; i++ ) {
|
|
201
|
+
floats[i] = (arr[i] >= 0x8000) ? -(0x10000 - arr[i]) / 0x8000 : arr[i] / 0x7FFF;
|
|
202
|
+
}
|
|
203
|
+
const audio = ctx.createBuffer(1, floats.length, samplerate );
|
|
204
|
+
audio.copyToChannel( floats, 0 , 0 );
|
|
205
|
+
return audio;
|
|
206
|
+
}
|