@met4citizen/headtts 1.1.1 → 1.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 CHANGED
@@ -5,24 +5,27 @@ provides phoneme-level timestamps and Oculus visemes for lip-sync, in addition
5
5
  to audio output (WAV/PCM). It uses
6
6
  [Kokoro](https://huggingface.co/onnx-community/Kokoro-82M-v1.0-ONNX-timestamped)
7
7
  neural model and voices, and inference can run entirely in
8
- the browser (via WebGPU or WASM), or alternatively
9
- on a Node.js WebSocket/RESTful server (CPU-based).
8
+ the browser (WebGPU or WASM), or alternatively
9
+ on a Node.js WebSocket/RESTful server (WebGPU or CPU).
10
10
 
11
11
  - **Pros**: Free. Doesn't require a server in in-browser mode.
12
12
  WebGPU support. Uses neural voices with a StyleTTS 2 model.
13
13
  Great for lip-sync use cases and fully compatible with the
14
14
  [TalkingHead](https://github.com/met4citizen/TalkingHead).
15
- MIT licensed, doesn't use eSpeak NG or any other GPL-licensed
15
+ MIT licensed, doesn't use eSpeak or any other GPL-licensed
16
16
  module.
17
17
 
18
- - **Cons**: WebGPU is only supported by default in Chrome and Edge
19
- desktop browsers. Takes time to load on first load and uses
20
- a lot of memory. WebGPU support in onnxruntime-node is still experimental
21
- and not released, so server-side inference is relatively slow. English
22
- is currently the only supported language.
18
+ - **Cons**: Only the latest desktop browsers have
19
+ [WebGPU support](https://caniuse.com/webgpu) enabled by default,
20
+ the WASM fallback is much slower.
21
+ Kokoro is a lightweight model, but it still takes time to
22
+ load the first time and consumes a lot of memory.
23
+ English is currently the only supported language.
23
24
 
24
- **👉 If you're using a Chrome or Edge desktop browser, check out the
25
- [In-browser Demo](https://met4citizen.github.io/HeadTTS/)!**
25
+ **👉 If you're using a desktop browser, check out the
26
+ [IN-BROWSER DEMO](https://met4citizen.github.io/HeadTTS/)!** - If
27
+ your browser doesn't have WebGPU support enabled,
28
+ the demo app uses WASM as a fallback.
26
29
 
27
30
  The project uses [websockets/ws](https://github.com/websockets/ws) (MIT License),
28
31
  [hugginface/transformers.js (with ONNX Runtime)](https://github.com/huggingface/transformers.js/)
@@ -60,12 +63,12 @@ set the `workerModule` and `dictionaryURL` options explicitly,
60
63
  as the default relative paths will likely not work:
61
64
 
62
65
  ```javascript
63
- import { HeadTTS } from "https://cdn.jsdelivr.net/npm/@met4citizen/headtts@1.1/+esm";
66
+ import { HeadTTS } from "https://cdn.jsdelivr.net/npm/@met4citizen/headtts@1.3/+esm";
64
67
 
65
68
  const headtts = new HeadTTS({
66
69
  /* ... */
67
- workerModule: "https://cdn.jsdelivr.net/npm/@met4citizen/headtts@1.1/modules/worker-tts.mjs",
68
- dictionaryURL: "https://cdn.jsdelivr.net/npm/@met4citizen/headtts@1.1/dictionaries/"
70
+ workerModule: "https://cdn.jsdelivr.net/npm/@met4citizen/headtts@1.3/modules/worker-tts.mjs",
71
+ dictionaryURL: "https://cdn.jsdelivr.net/npm/@met4citizen/headtts@1.3/dictionaries/"
69
72
  });
70
73
  ```
71
74
 
@@ -77,7 +80,7 @@ Option | Description | Default value
77
80
  `endpoints` | List of WebSocket/RESTful servers or backends `webgpu` or `wasm`, in order of priority. If one fails, the next is used. | `["webgpu",`<br>` "wasm"]`
78
81
  `audioCtx` | Audio context for creating audio buffers. If `null`, a new one is created. | `null`
79
82
  `workerModule` | URL of the HeadTTS Web Worker module. Enables use from a CDN. If set to `null`, the relative path/file `./worker-tts.mjs` is used. | `null`
80
- `transformersModule` | URL of the `transformers.js` module to load. | `"https://cdn.jsdelivr.net/npm/`<br>`@huggingface/transformers@3.7.2`<br>`/dist/transformers.min.js"`
83
+ `transformersModule` | URL of the `transformers.js` module to load. | `"https://cdn.jsdelivr.net/npm/`<br>`@huggingface/transformers@4.0.0`<br>`/dist/transformers.min.js"`
81
84
  `model` | Kokoro text-to-speech ONNX model (timestamped) used for in-browser inference. | `"onnx-community/`<br>`Kokoro-82M-v1.0-ONNX-timestamped"`
82
85
  `dtypeWebgpu` | Data type precision for WebGPU inference: `"fp32"` (recommended), `"fp16"`, `"q8"`, `"q4"`, or `"q4f16"`. | `"fp32"`
83
86
  `dtypeWasm` | Data type precision for WASM inference: `"fp32"`, `"fp16"`, `"q8"`, `"q4"`, or `"q4f16"`. | `"q4"`
@@ -86,7 +89,7 @@ Option | Description | Default value
86
89
  `frameRate` | Frame rate in FPS for inference. | `40`
87
90
  `languages` | Language modules to be pre-loaded. | [`"en-us"`]
88
91
  `dictionaryURL` | URL to language dictionaries. Set to `null` to disable dictionaries. | `"../dictionaries"`
89
- `voiceURL` | URL for loading voices. | `"https://huggingface.co/`<br>`onnx-community/`<br>`Kokoro-82M-v1.0-ONNX/`<br>`resolve/main/voices"`
92
+ `voiceURL` | URL for loading voices. If the given value is a relative URL, it should be relative to the worker file location. | `"https://huggingface.co/`<br>`onnx-community/`<br>`Kokoro-82M-v1.0-ONNX/`<br>`resolve/main/voices"`
90
93
  `voices` | Voices to preload (e.g., `["af_bella", "am_fenrir"]`). | `[]`
91
94
  `splitSentences` | Whether to split text into sentences. | `true`
92
95
  `splitLength` | Maximum length (in characters) of each text chunk. | `500`
@@ -206,8 +209,6 @@ Type | Description | Example
206
209
  `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>
207
210
  `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>
208
211
 
209
- *TODO: Add support for `audio` type.*
210
-
211
212
  An example using an array of input items:
212
213
 
213
214
  ```javascript
@@ -307,7 +308,7 @@ Property|Description|Default
307
308
  `tts.transformersModule` | Name of the transformers.js module to use. | `"@huggingface/transformers"`
308
309
  `tts.model` | The timestamped Kokoro TTS ONNX model. | `"onnx-community/`<br>`Kokoro-82M-v1.0-ONNX-timestamped"`
309
310
  `tts.dtype` | The data type precision used for inference. Available options: `"fp32"`, `"fp16"`, `"q8"`, `"q4"`, or `"q4f16"`. | `"fp32"`
310
- `tts.device` | Computation backend to use. Currently, the only available option for Node.js server is `"cpu"`. | `"cpu"`
311
+ `tts.device` | Computation backend to use: `"webgpu"` or `"cpu"`. NOTE: Node.js WebGPU implementation in Transformers.js is not thread safe, so we can only have one thread for WebGPU. Others will be automatically started as `"cpu"`. | `"webgpu"`
311
312
  `tts.styleDim` | The embedding dimension for style. | `256`
312
313
  `tts.audioSampleRate` | Audio sample rate in Hertz (Hz). | `24000`
313
314
  `tts.frameRate` | Frame rate in frames per second (FPS). | `40`
@@ -389,12 +390,13 @@ Returns an audio object metadata that can be passed on the TalkingHead
389
390
  type: "audio",
390
391
  ref: 13,
391
392
  data: {
392
- words: ["This ","is ","an ","example."],
393
- wtimes: [443, 678, 780, 864],
394
- wdurations: [192 ,91 ,52 ,868],
395
- visemes: ["TH", "I", "SS", "I", "SS", "aa", "nn", "SS", "aa", "PP", "PP", "E", "DD"],
396
- vtimes: [443, 494, 550, 678, 729, 780, 801, 989, 1126, 1175, 1225, 1275, 1354],
397
- vdurations: [61, 66, 85, 61, 40, 31, 31, 69, 59, 60, 60, 89, 378],
393
+ words: ['This ', 'is ', 'an ', 'example.'],
394
+ wtimes: [440, 656, 876, 1050],
395
+ wdurations: [236, 240, 194, 1035],
396
+ visemes: ['TH', 'I', 'SS', 'I', 'SS', 'aa', 'nn', 'I', 'kk', 'SS', 'aa', 'PP', 'PP', 'E', 'RR'],
397
+ vtimes: [440, 472, 562, 656, 753, 876, 993, 1050, 1097, 1149, 1200, 1322, 1372, 1423, 1499],
398
+ vdurations: [52, 110, 74, 117, 75, 137, 47, 67, 72, 71, 142, 70, 71, 96, 399],
399
+ phonemes: ['ð', 'ɪ', 's', 'ɪ', 'z', 'æ', 'n', 'ɪ', 'ɡ', 'z', 'æ', 'm', 'p', 'ə', 'l'],
398
400
  audioEncoding: "wav"
399
401
  }
400
402
  }
@@ -430,13 +432,14 @@ OK response:
430
432
 
431
433
  JSON|Description
432
434
  ---|---
433
- `audio` | AudioBuffer for `"wav"` audio encoding, ArrayBuffer of raw PCM 16bit LE samples for `"pcm"` audio encoding.
435
+ `audio` | Base64 encoded WAV data for `"wav"` or raw PCM 16bit LE samples for `"pcm"` audio encoding.
434
436
  `words` | Array of words.
435
437
  `wtimes` | Array of word starting times for `words` in milliseconds.
436
438
  `wdurations` | Array of word durations for `words` in milliseconds.
437
439
  `visemes` | Array of Oculus viseme IDs: `'aa'`, `'E'`, `'I'`, `'O'`, `'U'`, `'PP'`, `'SS'`, `'TH'`, `'CH'`, `'FF'`, `'kk'`, `'nn'`, `'RR'`, `'DD'`, `'sil'`.
438
440
  `vtimes` | Array of viseme starting times for `visemes` in milliseconds.
439
441
  `vdurations` | Array of viseme durations for `visemes` in milliseconds.
442
+ `phonemes` | Array of phonemes corresponding to the array of visemes.
440
443
  `audioEncoding` | Audio encoding: `"wav"` or `"pcm"`.
441
444
 
442
445
  Error response:
@@ -474,9 +477,6 @@ and then to Misaki-compatible phonemes by applying the following mapping:
474
477
  - `oʊ` → [ `O` ], `ˈoʊ` → [ `ˈO` ], `ˌoʊ` → [ `ˌO` ]
475
478
  - `əʊ` → [ `Q` ], `ˈəʊ` → [ `ˈQ` ], `ˌəʊ` → [ `ˌQ` ]
476
479
 
477
- Note: During the dataset conversion, some vowels were reduced to reflect
478
- casual speech since HeadTTS is primarily designed for conversational use.
479
-
480
480
  The final dictionary is a plain text file with around 125,000 lines (2,8MB).
481
481
  Lines starting with `;;;` are comments. Each other line represents
482
482
  one word and its pronunciations. The word and its different possible
@@ -524,32 +524,41 @@ ALUMIINI FOLI KATT OKSI PAPE SEOS VENE VUOK
524
524
 
525
525
  # Appendix C: Latency
526
526
 
527
- In-browser TTS on WebGPU is 3x faster than
528
- real-time and approximately 10x faster than WASM. CPU inference on
529
- a Node.js server performs surprisingly well, but increasing the thread
530
- pool size worsens performance, so we need to wait for WebGPU support.
531
- Quantization makes no significant difference, so I recommend using 32-bit
532
- floating point precision (fp32) for the best audio quality unless
533
- memory consumption becomes a concern.
527
+ In-browser TTS using WebGPU runs approximately 3x faster than real time
528
+ and about 10x faster than WASM. CPU-based inference on a Node.js server
529
+ performs surprisingly well. However, increasing the thread pool size
530
+ degrades performance. WebGPU inference on a Node.js server is slightly
531
+ faster than CPU inference, but it supports only a single dedicated WebGPU
532
+ thread. On Metal, the fastest HeadTTS configuration is WebGPU with two
533
+ threads where the second thread automatically falls back to CPU execution.
534
+
535
+ I recommend using 32-bit floating point precision (fp32) for the best
536
+ audio quality unless memory consumption becomes a concern.
534
537
 
535
538
  Unofficial latency results using my own
536
539
  [latency test app](https://github.com/met4citizen/HeadTTS/blob/main/tests/latency.html):
537
540
 
538
- TTS Engine | Setup |`FIL`<sup>\[1]</sup>|`FBL`<sup>\[2]</sup>|`RTF`<sup>\[3]</sup>
539
- ---|---|---|---|---
540
- HeadTTS, in-browser | Chrome, WebGPU/fp32 | 9.4s | 958ms | 0.30
541
- HeadTTS, in-browser | Edge, WebGPU/fp32 | 8.7s | 913ms | 0.28
542
- HeadTTS, in-browser | Chrome, WASM/q4 | 88.4s | 8752ms | 2.87
543
- HeadTTS, in-browser | Edge, WASM/q4 | 44.8s | 4437ms | 1.46
544
- HeadTTS, server | WebSocket, CPU/fp32, 1 thread | 6.8s | 712ms | 0.22
545
- HeadTTS, server | WebSocket, CPU/fp32, 4 threads | 6.0s | 2341ms | 0.20
546
- HeadTTS, server | REST, CPU/fp32, 1 thread | 7.0s | 793ms | 0.23
547
- HeadTTS, server | REST, CPU/fp32, 4 threads | 6.5s | 2638ms | 0.21
548
- ElevenLabs | WebSocket | 4.8s | 977ms | 0.20
549
- ElevenLabs | REST | 11.3s | 1097ms | 0.46
550
- ElevenLabs | REST, Flash_v2_5 | 4.8s | 581ms | 0.22
551
- Microsoft Azure TTS | Speech SDK, WebSocket | 1.1s | 274ms | 0.04
552
- Google TTS | REST | 0.79s | 67ms | 0.03
541
+ TTS Engine/Setup |`FIL`<sup>\[1]</sup>|`FBL`<sup>\[2]</sup>|`RTF`<sup>\[3]</sup>
542
+ ---|---|---|---
543
+ HeadTTS, Chrome, WebGPU/fp32 | 8.6s | 852ms | **0.27**
544
+ HeadTTS, Edge, WebGPU/fp32 | 8.8s | 858ms | 0.28
545
+ HeadTTS, Safari, WebGPU/fp32 | 25.8s | 2437ms | 0.82
546
+ HeadTTS, Chrome, WASM/q4 | 45.4s | 4404ms | 1.45
547
+ HeadTTS, Edge, WASM/q4 | 45.5s | 4392ms | 1.45
548
+ HeadTTS, Safari, WASM/q4 | 45.6s | 4447ms | 1.46
549
+ HeadTTS, WebSocket, WebGPU/fp32, 1 thread | 6.6s | 719ms | 0.21
550
+ HeadTTS, WebSocket, WebGPU/fp32, 2 threads | 3.8s | 742ms | **0.12**
551
+ HeadTTS, WebSocket, CPU/fp32, 1 thread | 6.8s | 712ms | 0.22
552
+ HeadTTS, WebSocket, CPU/fp32, 4 threads | 6.0s | 2341ms | 0.20
553
+ HeadTTS, REST, WebGPU/fp32, 1 thread | 6.7s | 713ms | 0.21
554
+ HeadTTS, REST, WebGPU/fp32, 2 threads | 3.6s | 717ms | **0.11**
555
+ HeadTTS, REST, CPU/fp32, 1 thread | 7.0s | 793ms | 0.23
556
+ HeadTTS, REST, CPU/fp32, 4 threads | 6.5s | 2638ms | 0.21
557
+ ElevenLabs, WebSocket | 4.8s | 977ms | 0.20
558
+ ElevenLabs, REST | 11.3s | 1097ms | 0.46
559
+ ElevenLabs, REST, Flash_v2_5 | 4.8s | 581ms | 0.22
560
+ Microsoft TTS, WebSocket (Speech SDK) | 1.1s | 274ms | 0.04
561
+ Google TTS, REST | 0.79s | 67ms | 0.03
553
562
 
554
563
 
555
564
  <sup>\[1]</sup> *Finish latency*: Total time from sending text input to receiving
@@ -563,27 +572,9 @@ solutions use streaming, some not.
563
572
  <sup>\[3]</sup> *Real-time factor* = Time to generate full audio / Duration of the full
564
573
  audio. If RTF < 1, synthesis is faster than real-time (i.e., good).
565
574
 
566
- <details>
567
- <summary>CLICK HERE here to see the TEST SETUP.</summary>
568
-
569
575
  **Test setup**: Macbook Air M2 laptop, 8 cores, 16GB memory,
570
- macOS Sequoia 15.3.2, Metal2 GPU 10 cores, 300/50 Mbit/s internet connection.
571
- The latest Google Chrome/Edge desktop browsers.
576
+ macOS Tahoe 26.0, Metal2 GPU 10 cores, 300/50 Mbit/s internet connection.
577
+ The latest Google Chrome, Edge, Safari desktop browsers.
572
578
 
573
579
  All test cases use WAV or raw PCM 16bit LE format and the "List 1" of the
574
580
  [Harvard Sentences](https://www.cs.columbia.edu/~hgs/audio/harvard.html):
575
-
576
- ```text
577
- The birch canoe slid on the smooth planks.
578
- Glue the sheet to the dark blue background.
579
- It's easy to tell the depth of a well.
580
- These days a chicken leg is a rare dish.
581
- Rice is often served in round bowls.
582
- The juice of lemons makes fine punch.
583
- The box was thrown beside the parked truck.
584
- The hogs were fed chopped corn and garbage.
585
- Four hours of steady work faced us.
586
- A large size in stockings is hard to sell.
587
- ```
588
-
589
- </details>
@@ -48,6 +48,8 @@ const config = {
48
48
 
49
49
  tts: {
50
50
  threads: 1,
51
+ transformersModule: "@huggingface/transformers",
52
+
51
53
  model: "onnx-community/Kokoro-82M-v1.0-ONNX-timestamped",
52
54
  dtype: "fp32",
53
55
  device: "cpu",
@@ -194,6 +196,12 @@ for( let i=0; i<config.tts.threads; i++ ) {
194
196
  [ "transformersModule", "model", "dtype", "device", "styleDim", "frameRate",
195
197
  "languages", "dictionaryPath", "voicePath", "voices", "audioSampleRate",
196
198
  "deltaStart", "deltaEnd", "trace" ].forEach( x => data[x] = config.tts[x] );
199
+
200
+ // Node.js WebGPU is not thread safe, so we can only have one thread for WebGPU
201
+ if ( data.device === "webgpu" && i > 0 ) {
202
+ data.device = "cpu";
203
+ }
204
+
197
205
  const message = {
198
206
  type: "connect",
199
207
  data: data
@@ -37,7 +37,7 @@ class HeadTTS {
37
37
  audioCtx: null,
38
38
 
39
39
  workerModule: null,
40
- transformersModule: "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.2/dist/transformers.min.js",
40
+ transformersModule: "https://cdn.jsdelivr.net/npm/@huggingface/transformers@4.0.0/dist/transformers.min.js",
41
41
 
42
42
  model: "onnx-community/Kokoro-82M-v1.0-ONNX-timestamped",
43
43
  dtypeWebgpu: "fp32", // "fp32" | "fp16" | "q8" | "q4" | "q4f16"
@@ -372,6 +372,7 @@ class LanguageBase {
372
372
  // Output data
373
373
  let phonemes = [];
374
374
  const metadata = {
375
+ phonemes: [],
375
376
  words: [], wtimes: [], wdurations: [],
376
377
  visemes: [], vtimes: [], vdurations: []
377
378
  };
@@ -433,6 +434,7 @@ class LanguageBase {
433
434
  // Visemes
434
435
  const viseme = part.visemes[j];
435
436
  if ( viseme ) {
437
+ metadata.phonemes.push(ph);
436
438
  metadata.visemes.push(viseme);
437
439
  metadata.vtimes.push(i);
438
440
  i = phonemes.length;
@@ -4,6 +4,8 @@ import * as utils from "./utils.mjs";
4
4
  let StyleTextToSpeech2Model;
5
5
  let AutoTokenizer;
6
6
  let Tensor;
7
+ let env;
8
+ let LogLevel;
7
9
 
8
10
  // Status flags
9
11
  let filesProgress;
@@ -113,7 +115,11 @@ async function connect() {
113
115
 
114
116
  // Load modules dynamically
115
117
  try {
116
- ({ StyleTextToSpeech2Model, AutoTokenizer, Tensor } = await import(settings.transformersModule));
118
+ ({ StyleTextToSpeech2Model, AutoTokenizer, Tensor, env, LogLevel } = await import(settings.transformersModule));
119
+
120
+ // Set log level to ERROR
121
+ env.logLevel = LogLevel.ERROR;
122
+
117
123
  } catch(error) {
118
124
  console.error("HeadTTS Worker: Importing modules failed, error=", error);
119
125
  throw new Error("Importing modules failed.");
@@ -225,7 +231,7 @@ async function loadVoice(s) {
225
231
  buffer = response.buffer.slice(response.byteOffset, response.byteOffset + response.byteLength);
226
232
  return buffer;
227
233
  } else if ( !isNode && settings.voiceURL ) {
228
- const url = new URL(settings.voiceURL);
234
+ const url = new URL(settings.voiceURL, self.location.href);
229
235
  url.pathname += (url.pathname.endsWith("/") ? "" : "/") + s + ".bin";
230
236
  if ( isTraceConnection ) {
231
237
  utils.trace( 'Loading voice "' + url + '".' );
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@met4citizen/headtts",
3
- "version": "1.1.1",
3
+ "version": "1.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,8 +12,8 @@
12
12
  "dictionaries/"
13
13
  ],
14
14
  "dependencies": {
15
- "@huggingface/transformers": "^3.7.2",
16
- "ws": "^8.18.3"
15
+ "@huggingface/transformers": "^4.0.0",
16
+ "ws": "^8.20.0"
17
17
  },
18
18
  "scripts": {
19
19
  "start": "node ./modules/headtts-node.mjs",
@@ -39,6 +39,6 @@
39
39
  },
40
40
  "homepage": "https://github.com/met4citizen/HeadTTS#readme",
41
41
  "devDependencies": {
42
- "jest": "^29.7.0"
42
+ "jest": "^30.2.0"
43
43
  }
44
44
  }