@met4citizen/headtts 0.2.0 → 0.3.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/README.md +19 -17
- package/modules/headtts.mjs +27 -23
- package/modules/language-en-us.mjs +138 -15
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -56,7 +56,7 @@ const headtts = new HeadTTS({
|
|
|
56
56
|
```
|
|
57
57
|
|
|
58
58
|
<details>
|
|
59
|
-
<summary>
|
|
59
|
+
<summary>CLICK HERE to see all the options.</summary>
|
|
60
60
|
|
|
61
61
|
Option | Description | Default value
|
|
62
62
|
--- | --- | ---
|
|
@@ -122,7 +122,7 @@ headtts.onmessage = (message) => {
|
|
|
122
122
|
```
|
|
123
123
|
|
|
124
124
|
<details>
|
|
125
|
-
<summary>
|
|
125
|
+
<summary>CLICK HERE to see all the available class events.</summary>
|
|
126
126
|
|
|
127
127
|
Event handler | Description
|
|
128
128
|
--- | ---
|
|
@@ -169,7 +169,7 @@ try {
|
|
|
169
169
|
```
|
|
170
170
|
|
|
171
171
|
<details>
|
|
172
|
-
<summary>
|
|
172
|
+
<summary>CLICK HERE to see all the class method.</summary>
|
|
173
173
|
|
|
174
174
|
Method | Description
|
|
175
175
|
--- | ---
|
|
@@ -181,21 +181,21 @@ Method | Description
|
|
|
181
181
|
</details>
|
|
182
182
|
|
|
183
183
|
<details>
|
|
184
|
-
<summary>
|
|
184
|
+
<summary>CLICK HERE to see the available input types for synthesize request.</summary>
|
|
185
185
|
|
|
186
186
|
The `input` parameter can be a string or, alternatively, an array
|
|
187
187
|
of strings or inputs items.
|
|
188
188
|
|
|
189
189
|
Type | Description | Example
|
|
190
190
|
---|---|---
|
|
191
|
-
`text` | Speak the text in `value`. This is equivalent to giving a pure string input. | <pre
|
|
192
|
-
`speech` | Speak the text in `value` with corresponding subtitles in `subtitles` (optional). This type allows the spoken words to be different that the subtitles. | <pre
|
|
193
|
-
`phonetic` | Speak the model specific phonetic alphabets in `value` with corresponding `subtitles` (optional). | <pre
|
|
194
|
-
`characters` | Speak the `value` character-by-character with corresponding `subtitles` (optional). Supports also numbers that are read digit-by-digit. | <pre
|
|
195
|
-
`number` | Speak the number in `value` with corresponding `subtitles` (optional). The number should presented as a string. | <pre
|
|
196
|
-
`date` | Speak the date in `value` with corresponding `subtitles` (optional). The date is presented as milliseconds from epoch. | <pre
|
|
197
|
-
`time` | Speak the time in `value` with corresponding `subtitles` (optional). The time is presented as milliseconds from epoch. | <pre
|
|
198
|
-
`break` | The length of the break in milliseconds in `value` with corresponding `subtitles` (optional). | <pre
|
|
191
|
+
`text` | Speak the text in `value`. This is equivalent to giving a pure string input. | <pre>{<br> type: "text",<br> value: "This is an example."<br>}</pre>
|
|
192
|
+
`speech` | Speak the text in `value` with corresponding subtitles in `subtitles` (optional). This type allows the spoken words to be different that the subtitles. | <pre>{<br> type: "text",<br> value: "One two three",<br> subtitles: "123"<br>}</pre>
|
|
193
|
+
`phonetic` | Speak the model specific phonetic alphabets in `value` with corresponding `subtitles` (optional). | <pre>{<br> type: "phonetic",<br> value: "mˈɜɹʧəndˌIz",<br> subtitles: "merchandise"<br>}</pre>
|
|
194
|
+
`characters` | Speak the `value` character-by-character with corresponding `subtitles` (optional). Supports also numbers that are read digit-by-digit. | <pre>{<br> type: "characters",<br> value: "ABC-123-8",<br> subtitles: "ABC-123-8"<br>}</pre>
|
|
195
|
+
`number` | Speak the number in `value` with corresponding `subtitles` (optional). The number should presented as a string. | <pre>{<br> type: "number",<br> value: "123.5",<br> subtitles: "123.5"<br>}</pre>
|
|
196
|
+
`date` | Speak the date in `value` with corresponding `subtitles` (optional). The date is presented as milliseconds from epoch. | <pre>{<br> type: "date",<br> value: Date.now(),<br> subtitles: "02/05/2025"<br>}</pre>
|
|
197
|
+
`time` | Speak the time in `value` with corresponding `subtitles` (optional). The time is presented as milliseconds from epoch. | <pre>{<br> type: "time",<br> value: Date.now(),<br> subtitles: "6:45 PM"<br>}</pre>
|
|
198
|
+
`break` | The length of the break in milliseconds in `value` with corresponding `subtitles` (optional). | <pre>{<br> type: "break",<br> value: 2000,<br> subtitles: "..."<br>}</pre>
|
|
199
199
|
|
|
200
200
|
*TODO: Add support for `audio` type.*
|
|
201
201
|
|
|
@@ -223,10 +223,12 @@ An example using an array of input items:
|
|
|
223
223
|
|
|
224
224
|
# NodeJS WebSocket/RESTful Server: `headtts-node.mjs`
|
|
225
225
|
|
|
226
|
-
Install
|
|
226
|
+
Install (requires Node.js v20+):
|
|
227
227
|
|
|
228
228
|
```bash
|
|
229
|
-
|
|
229
|
+
git clone https://github.com/met4citizen/HeadTTS
|
|
230
|
+
cd HeadTTS
|
|
231
|
+
npm install
|
|
230
232
|
```
|
|
231
233
|
|
|
232
234
|
Start the server:
|
|
@@ -236,7 +238,7 @@ npm start
|
|
|
236
238
|
```
|
|
237
239
|
|
|
238
240
|
<details>
|
|
239
|
-
<summary>
|
|
241
|
+
<summary>CLICK HERE to see the command line options.</summary>
|
|
240
242
|
|
|
241
243
|
Option|Description|Default
|
|
242
244
|
---|---|---
|
|
@@ -252,7 +254,7 @@ node ./modules/headtts-node.mjs --trace 16
|
|
|
252
254
|
</details>
|
|
253
255
|
|
|
254
256
|
<details>
|
|
255
|
-
<summary>
|
|
257
|
+
<summary>CLICK HERE to see the configuration file options</summary>
|
|
256
258
|
|
|
257
259
|
Property|Description|Default
|
|
258
260
|
---|---|---
|
|
@@ -521,7 +523,7 @@ solutions use streaming, some not.
|
|
|
521
523
|
audio. If RTF < 1, synthesis is faster than real-time (i.e., good).
|
|
522
524
|
|
|
523
525
|
<details>
|
|
524
|
-
<summary>
|
|
526
|
+
<summary>CLICK HERE here to see the test setup.</summary>
|
|
525
527
|
|
|
526
528
|
**Test setup**: Macbook Air M2 laptop, 8 cores, 16GB memory,
|
|
527
529
|
macOS Sequoia 15.3.2, Metal2 GPU 10 cores, 300/50 Mbit/s internet connection.
|
package/modules/headtts.mjs
CHANGED
|
@@ -109,43 +109,47 @@ class HeadTTS {
|
|
|
109
109
|
}
|
|
110
110
|
|
|
111
111
|
/**
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
divideToParts(text, initialLen=0) {
|
|
112
|
+
* Divide the given text into parts, handling emojis and complex Unicode correctly.
|
|
113
|
+
*
|
|
114
|
+
* @param {string} text Text
|
|
115
|
+
* @param {number} [initialLen=0] Initial length
|
|
116
|
+
* @return {string[]} Array of text parts.
|
|
117
|
+
*/
|
|
118
|
+
divideToParts(text, initialLen = 0) {
|
|
119
|
+
const segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
|
|
120
|
+
const letters = Array.from(segmenter.segment(text), s => s.segment);
|
|
121
|
+
const textLen = letters.length;
|
|
122
|
+
|
|
119
123
|
const parts = [];
|
|
120
|
-
const textLen = text.length;
|
|
121
|
-
const letters = [...text];
|
|
122
124
|
let lastSpace = 0;
|
|
123
125
|
let part = "";
|
|
124
|
-
|
|
126
|
+
|
|
127
|
+
for (let i = 0; i < textLen; i++) {
|
|
125
128
|
const letter = letters[i];
|
|
126
|
-
const isLast = i ===
|
|
127
|
-
const
|
|
128
|
-
const
|
|
129
|
-
|
|
130
|
-
const isMax = i >= (this.settings.splitLength - initialLen);
|
|
129
|
+
const isLast = i === textLen - 1;
|
|
130
|
+
const nextLetter = isLast ? null : letters[i + 1];
|
|
131
|
+
const letterTwo = isLast ? null : letter + nextLetter;
|
|
132
|
+
const isDivider = isLast ? false : this.dividers?.hasOwnProperty(letterTwo);
|
|
131
133
|
|
|
134
|
+
if (letter === " ") lastSpace = i;
|
|
135
|
+
|
|
136
|
+
const isMax = i >= this.settings.splitLength - initialLen;
|
|
132
137
|
part += letter;
|
|
133
138
|
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
part = part.slice(lastSpace+1);
|
|
139
|
+
if (isMax) {
|
|
140
|
+
if (lastSpace === 0) lastSpace = i;
|
|
141
|
+
parts.push(letters.slice(i - part.length + 1, lastSpace + 1).join(""));
|
|
142
|
+
part = letters.slice(lastSpace + 1, i + 1).join("");
|
|
139
143
|
lastSpace = 0;
|
|
140
144
|
initialLen = 0;
|
|
141
|
-
} else if (
|
|
142
|
-
parts.push(
|
|
145
|
+
} else if (isLast || isDivider) {
|
|
146
|
+
parts.push(part);
|
|
143
147
|
part = "";
|
|
144
148
|
lastSpace = 0;
|
|
145
149
|
initialLen = 0;
|
|
146
150
|
}
|
|
147
|
-
|
|
148
151
|
}
|
|
152
|
+
|
|
149
153
|
return parts;
|
|
150
154
|
}
|
|
151
155
|
|
|
@@ -300,6 +300,19 @@ class Language extends LanguageBase {
|
|
|
300
300
|
this.ones = ['','ONE','TWO','THREE','FOUR','FIVE','SIX','SEVEN','EIGHT','NINE'];
|
|
301
301
|
this.tens = ['','','TWENTY','THIRTY','FORTY','FIFTY','SIXTY','SEVENTY','EIGHTY','NINETY'];
|
|
302
302
|
this.teens = ['TEN','ELEVEN','TWELVE','THIRTEEN','FOURTEEN','FIFTEEN','SIXTEEN','SEVENTEEN','EIGHTEEN','NINETEEN'];
|
|
303
|
+
this.decades = {
|
|
304
|
+
20: "TWENTIES", 30: "THIRTIES", 40: "FORTIES", 50: "FIFTIES",
|
|
305
|
+
60: "SIXTIES", 70: "SEVENTIES", 80: "EIGHTIES", 90: "NINETIES"
|
|
306
|
+
};
|
|
307
|
+
this.ordinals = {
|
|
308
|
+
1: "FIRST", 2: "SECOND", 3: "THIRD", 4: "FOURTH", 5: "FIFTH",
|
|
309
|
+
6: "SIXTH", 7: "SEVENTH", 8: "EIGHTH", 9: "NINTH", 10: "TENTH",
|
|
310
|
+
11: "ELEVENTH", 12: "TWELFTH", 13: "THIRTEENTH", 14: "FOURTEENTH",
|
|
311
|
+
15: "FIFTEENTH", 16: "SIXTEENTH", 17: "SEVENTEETH", 18: "EIGHTEENTH",
|
|
312
|
+
19: "NINETEENTH", 20: "TWENTIETH", 30: "THIRTIETH", 40: "FORTIETH",
|
|
313
|
+
50: "FIFTIETH", 60: "SIXTIETH",70: "SEVENTIETH", 80: "EIGHTIETH",
|
|
314
|
+
90: "NINETIETH"
|
|
315
|
+
};
|
|
303
316
|
|
|
304
317
|
// Date & Time
|
|
305
318
|
this.months = [
|
|
@@ -421,28 +434,104 @@ class Language extends LanguageBase {
|
|
|
421
434
|
* Convert number to words. Try to decide how to read it.
|
|
422
435
|
*
|
|
423
436
|
* @param {number|string} num Number
|
|
437
|
+
* @param {boolean} [isNotSpecial=false] If true, this is not a special number (e.g. year, zip code)
|
|
424
438
|
* @return {string} String
|
|
425
439
|
*/
|
|
426
|
-
convertNumberToWords(num) {
|
|
440
|
+
convertNumberToWords(num, isNotSpecial=false) {
|
|
427
441
|
const n = parseFloat(num);
|
|
428
442
|
if (num == "0") {
|
|
429
443
|
return "ZERO";
|
|
430
|
-
} else if(num.toString().startsWith('0')){
|
|
431
|
-
return this.convertDigitByDigit(num).trim();
|
|
432
444
|
} else if (num < 0 ) {
|
|
433
|
-
return "MINUS " + this.convertNumberToWords( Math.abs(num).toString() ).trim();
|
|
445
|
+
return "MINUS " + this.convertNumberToWords( Math.abs(num).toString(), isNotSpecial ).trim();
|
|
434
446
|
} else if ( n && !Number.isInteger(n) ) {
|
|
435
447
|
const parts = n.toString().split('.');
|
|
436
|
-
return this.convertNumberToWords(parts[0]).trim() + " POINT " + this.convertDigitByDigit(parts[1]).trim();
|
|
437
|
-
} else if
|
|
448
|
+
return this.convertNumberToWords(parts[0], isNotSpecial).trim() + " POINT " + this.convertDigitByDigit(parts[1]).trim();
|
|
449
|
+
} else if(num.toString().startsWith('0')){
|
|
450
|
+
return this.convertDigitByDigit(num).trim();
|
|
451
|
+
} else if (!isNotSpecial && ((num<1000 && num>99 && (num % 100) !== 0) || (num>10000&&num<1000000))) { //read area and zip codes digit by digit
|
|
438
452
|
return this.convertDigitByDigit(num).trim();
|
|
439
|
-
} else if ((num > 1000 && num < 2000)||(num>2009&&num<3000)) { //read years as two sets of two digits
|
|
453
|
+
} else if (!isNotSpecial && ((num > 1000 && num < 2000)||(num>2009&&num<3000))) { //read years as two sets of two digits
|
|
440
454
|
return (num % 100 != 0 ? this.convertSetsOfTwo(num).trim() : this.convertTens(num.toString().substring(0, 2)).trim() + " HUNDRED");
|
|
441
455
|
} else {
|
|
442
456
|
return this.convertMillions(num).trim();
|
|
443
457
|
}
|
|
444
458
|
}
|
|
445
459
|
|
|
460
|
+
/**
|
|
461
|
+
* Expand decade to text.
|
|
462
|
+
*
|
|
463
|
+
* @param {string} decade Decade
|
|
464
|
+
* @return {string} Normalized text
|
|
465
|
+
*/
|
|
466
|
+
convertDecade(decade) {
|
|
467
|
+
const num = parseInt(decade);
|
|
468
|
+
const isShort = !isNaN(num) && decade.length === 2;
|
|
469
|
+
const isLong = !isNaN(num) && decade.length > 2 && num > 0 && num <= 3000;
|
|
470
|
+
const thousands = (isLong && (num % 1000) === 0 ) ? Math.floor(num / 1000) : null;
|
|
471
|
+
const hundreds = (isLong && !thousands) ? Math.floor(num / 100) : null;
|
|
472
|
+
const tens = (isShort || isLong) ? Math.floor((num % 100) / 10) * 10 : null;
|
|
473
|
+
|
|
474
|
+
let s = [];
|
|
475
|
+
if ( thousands ) {
|
|
476
|
+
s.push( this.convertNumberToWords(thousands).trim(), "THOUSANDS" );
|
|
477
|
+
} else {
|
|
478
|
+
if ( hundreds ) {
|
|
479
|
+
s.push( this.convertNumberToWords(hundreds).trim() );
|
|
480
|
+
}
|
|
481
|
+
if ( tens ) {
|
|
482
|
+
s.push( this.decades[tens] || (this.convertNumberToWords(tens).trim() + 'S') );
|
|
483
|
+
} else if ( hundreds ) {
|
|
484
|
+
s.push( "HUNDREDS" );
|
|
485
|
+
} else {
|
|
486
|
+
s.push( decade );
|
|
487
|
+
}
|
|
488
|
+
}
|
|
489
|
+
|
|
490
|
+
return s.join(" ");
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Convert ordinal number to text.
|
|
495
|
+
*
|
|
496
|
+
* @param {number} num Ordinal number
|
|
497
|
+
* @return {string} Normalized text
|
|
498
|
+
*/
|
|
499
|
+
convertOrdinal(num) {
|
|
500
|
+
|
|
501
|
+
// Return immediately, if we have the number in our map
|
|
502
|
+
if ( this.ordinals.hasOwnProperty(num) ) {
|
|
503
|
+
return this.ordinals[num];
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
const hundreds = Math.floor(num / 100);
|
|
507
|
+
const tens = Math.floor( (num % 100) / 10) * 10;
|
|
508
|
+
const ones = num % 10;
|
|
509
|
+
|
|
510
|
+
let s = [];
|
|
511
|
+
if ( hundreds ) {
|
|
512
|
+
s.push( this.convertNumberToWords(hundreds).trim() );
|
|
513
|
+
if ( tens || ones ) {
|
|
514
|
+
s.push( "HUNDRED" );
|
|
515
|
+
} else {
|
|
516
|
+
s.push( "HUNDREDTH" );
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
if ( tens ) {
|
|
521
|
+
if ( ones ) {
|
|
522
|
+
s.push( this.convertNumberToWords(tens).trim() );
|
|
523
|
+
} else {
|
|
524
|
+
s.push( this.ordinals[tens] );
|
|
525
|
+
}
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
if ( ones ) {
|
|
529
|
+
s.push( this.ordinals[ones] );
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
return s.join(" ");
|
|
533
|
+
}
|
|
534
|
+
|
|
446
535
|
/**
|
|
447
536
|
* Set the `text` to be spoken by analysing the part content.
|
|
448
537
|
*
|
|
@@ -459,14 +548,48 @@ class Language extends LanguageBase {
|
|
|
459
548
|
switch( part.type ) {
|
|
460
549
|
|
|
461
550
|
case "text":
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
if (
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
551
|
+
|
|
552
|
+
// Process numbers, if any
|
|
553
|
+
if ( /\d/.test(part.text) ) {
|
|
554
|
+
|
|
555
|
+
// Decades: 70s, 1970s -> SEVENTIES, NINETEEN SEVENTIES
|
|
556
|
+
part.text = part.text.replace(/\b(\d{2,4})[''']?\s?[sS](?=\s|[.,!?;:]|$)/g, (match, decade) => {
|
|
557
|
+
const result = this.convertDecade(decade);
|
|
558
|
+
return result === decade ? match : result;
|
|
559
|
+
});
|
|
560
|
+
|
|
561
|
+
// Ordinals: 1st, 22nd -> FIRST, TWENTY SECOND
|
|
562
|
+
part.text = part.text.replace(/\b(\d+)\s*(st|nd|rd|th)(?=\s|[.,!?;:]|$)/gi, (match, number) => {
|
|
563
|
+
return this.convertOrdinal(Number(number));
|
|
564
|
+
});
|
|
565
|
+
|
|
566
|
+
// Handle mixed alphanumeric sequences
|
|
567
|
+
part.text = part.text.replace(/\b(\w*?)(\d+)([A-Za-z]+)\b/g, (match, prefix, numbers, letters) => {
|
|
568
|
+
const processedNumber = this.convertNumberToWords(numbers);
|
|
569
|
+
return `${prefix}${processedNumber} ${letters}`;
|
|
570
|
+
}).replace(/\b([A-Za-z]+)(\d+)(\w*?)\b/g, (match, letters, numbers, suffix) => {
|
|
571
|
+
const processedNumber = this.convertNumberToWords(numbers);
|
|
572
|
+
return `${letters} ${processedNumber}${suffix}`;
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
// Process the remaining numbers
|
|
576
|
+
// Note: If there are thousand separators or a decimal part, we know
|
|
577
|
+
// that this is not a special number e.g. phone number, zip code or year
|
|
578
|
+
part.text = part.text.replace(/-?(?:\d{1,3}(?:,\d{3})+|\d+)(\.\d+)?/g, (match, decimal) => {
|
|
579
|
+
let s = match;
|
|
580
|
+
let isNotSpecial = false;
|
|
581
|
+
if ( /,/.test(s) ) {
|
|
582
|
+
s = s.replace( /,/g, "" );
|
|
583
|
+
isNotSpecial = true;
|
|
584
|
+
}
|
|
585
|
+
if ( decimal ) {
|
|
586
|
+
isNotSpecial = true;
|
|
587
|
+
}
|
|
588
|
+
return this.convertNumberToWords(s, isNotSpecial);
|
|
589
|
+
});
|
|
590
|
+
|
|
469
591
|
}
|
|
592
|
+
|
|
470
593
|
break;
|
|
471
594
|
|
|
472
595
|
case "characters":
|
|
@@ -485,7 +608,7 @@ class Language extends LanguageBase {
|
|
|
485
608
|
break;
|
|
486
609
|
|
|
487
610
|
case "number":
|
|
488
|
-
part.text = this.
|
|
611
|
+
part.text = this.convertRegexNumberToWords(part.value) + " ";
|
|
489
612
|
break;
|
|
490
613
|
|
|
491
614
|
case "date":
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@met4citizen/headtts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "HeadTTS: A free Javascript text-to-speech with timestamps and visemes.",
|
|
5
5
|
"main": "modules/headtts.mjs",
|
|
6
6
|
"type": "module",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"dictionaries/"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@huggingface/transformers": "^3.
|
|
15
|
+
"@huggingface/transformers": "^3.6.0",
|
|
16
16
|
"ws": "^8.18.1"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|