@met4citizen/headtts 1.0.0 → 1.1.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
@@ -21,7 +21,7 @@ a lot of memory. WebGPU support in onnxruntime-node is still experimental
21
21
  and not released, so server-side inference is relatively slow. English
22
22
  is currently the only supported language.
23
23
 
24
- **If you're using a Chrome or Edge desktop browser, check out the
24
+ **👉 If you're using a Chrome or Edge desktop browser, check out the
25
25
  [In-browser Demo](https://met4citizen.github.io/HeadTTS/)!**
26
26
 
27
27
  The project uses [websockets/ws](https://github.com/websockets/ws) (MIT License),
@@ -62,7 +62,8 @@ Option | Description | Default value
62
62
  --- | --- | ---
63
63
  `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"]`
64
64
  `audioCtx` | Audio context for creating audio buffers. If `null`, a new one is created. | `null`
65
- `transformersModule` | URL of the `transformers.js` module to load. | `"https://cdn.jsdelivr.net/npm/`<br>`@huggingface/transformers@3.4.2`<br>`/dist/transformers.min.js"`
65
+ `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`
66
+ `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"`
66
67
  `model` | Kokoro text-to-speech ONNX model (timestamped) used for in-browser inference. | `"onnx-community/`<br>`Kokoro-82M-v1.0-ONNX-timestamped"`
67
68
  `dtypeWebgpu` | Data type precision for WebGPU inference: `"fp32"` (recommended), `"fp16"`, `"q8"`, `"q4"`, or `"q4f16"`. | `"fp32"`
68
69
  `dtypeWasm` | Data type precision for WASM inference: `"fp32"`, `"fp16"`, `"q8"`, `"q4"`, or `"q4f16"`. | `"q4"`
@@ -235,7 +236,7 @@ Method | Description
235
236
  `connect( settings=null, onprogress=null, onerror=null )` | Connects to the specified set of `endpoints` set in constructor or within the optinal `settings` object. If the `settings` parameter is provided, it forces a reconnection. The `onprogress` callback handles `ProgressEvent` events, while the `onerror` callback handles system-level error events. Returns a promise. **Note:** When connecting to a RESTful server, the method sends a hello message and considers the connection established only if a text response starting with `HeadTTS` is received.
236
237
  `clear()` | Clears all work queues and resolves all promises.
237
238
  `setup( data, onerror=null )` | Adds a new setup request to the work queue. See the API section for the supported `data` properties. Returns a promise.
238
- `synthesize( data, onmessage=null, onerror=null )` | Adds a new synthesize request to the work queue. If event handlers are provided, they override other handlers. Returns a promise that resolves with a sorted array of related messages of type `"audio"` or `"error"`.
239
+ `synthesize( data, onmessage=null, onerror=null )` | Adds a new synthesis request to the work queue. The `data` object supports the `input` and `userData` properties. The `userData` property is returned in the output as `message.userData` unchanged. If event handlers are provided, they override the default handlers. Returns a promise that resolves with a sorted array of related messages of type `"audio"` or `"error"`.
239
240
  `custom( data, onmessage=null, onerror=null )` | Adds a new custom message to the work queue. If event handlers are provided, they override other handlers. Returns a promise that resolves with the related message of the type `"custom"`.
240
241
 
241
242
  </details>
@@ -274,6 +275,8 @@ node ./modules/headtts-node.mjs --trace 16
274
275
 
275
276
  </details>
276
277
 
278
+ By default, the server uses the `./headtts-node.json` configuration file.
279
+
277
280
  <details>
278
281
  <summary>CLICK HERE to see the configurable PROPERTIES.</summary>
279
282
 
@@ -507,7 +510,7 @@ ALUMIINI FOLI KATT OKSI PAPE SEOS VENE VUOK
507
510
 
508
511
  # Appendix C: Latency
509
512
 
510
- In-browser TTS on WebGPU is 3x times faster than
513
+ In-browser TTS on WebGPU is 3x faster than
511
514
  real-time and approximately 10x faster than WASM. CPU inference on
512
515
  a Node.js server performs surprisingly well, but increasing the thread
513
516
  pool size worsens performance, so we need to wait for WebGPU support.
@@ -36,7 +36,8 @@ class HeadTTS {
36
36
  endpoints: ["webgpu", "wasm"],
37
37
  audioCtx: null,
38
38
 
39
- transformersModule: "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.4.2/dist/transformers.min.js",
39
+ workerModule: null,
40
+ transformersModule: "https://cdn.jsdelivr.net/npm/@huggingface/transformers@3.7.2/dist/transformers.min.js",
40
41
 
41
42
  model: "onnx-community/Kokoro-82M-v1.0-ONNX-timestamped",
42
43
  dtypeWebgpu: "fp32", // "fp32" | "fp16" | "q8" | "q4" | "q4f16"
@@ -265,8 +266,20 @@ class HeadTTS {
265
266
  await new Promise((resolve, reject) => {
266
267
 
267
268
  // Start the TTS web worker
268
- const url = new URL("./worker-tts.mjs", import.meta.url);
269
- this.ww = new Worker(url, { type: 'module' });
269
+ if ( this.settings.workerModule ) {
270
+ const code = `import "${this.settings.workerModule}";`;
271
+ const blob = new Blob([code], { type: "application/javascript" });
272
+ const url = URL.createObjectURL(blob);
273
+ this.ww = new Worker(url, { type: "module" });
274
+ const origTerminate = this.ww.terminate;
275
+ this.ww.terminate = function () {
276
+ URL.revokeObjectURL(url);
277
+ origTerminate.call(this);
278
+ };
279
+ } else {
280
+ const url = new URL("./worker-tts.mjs", import.meta.url);
281
+ this.ww = new Worker(url, { type: 'module' });
282
+ }
270
283
 
271
284
  // Handle progress reports and wait for ready message
272
285
  const rejectedTimeout = () => reject('Connection timed out.');
@@ -516,7 +529,7 @@ class HeadTTS {
516
529
  while( this.queueOut.length ) {
517
530
  const item = this.queueOut[0];
518
531
  this.queueOut.shift();
519
- this.items.delete( item.message.id );
532
+ this.items.delete( item.message.ref );
520
533
  clearItem(item);
521
534
  }
522
535
 
@@ -732,10 +745,11 @@ class HeadTTS {
732
745
  onerror: onerror,
733
746
  started: performance.now(),
734
747
  deferred: new utils.Deferred(),
735
- metadata: {
748
+ metaData: {
736
749
  part: i,
737
750
  partsTotal: messages.length
738
- }
751
+ },
752
+ userData: data.userData
739
753
  };
740
754
  deferredPromises.push( item.deferred.promise );
741
755
  this.queueIn.push(item);
@@ -824,6 +838,7 @@ class HeadTTS {
824
838
  // Marked ready and add to output queue
825
839
  item.status = 2;
826
840
  this.items.set( item.message.id, item );
841
+ item.message.ref = item.message.id;
827
842
  this.queueOut.push(item);
828
843
 
829
844
  // Emit start event, if out queue was empty
@@ -837,6 +852,7 @@ class HeadTTS {
837
852
 
838
853
  // Add to output queue to wait for response
839
854
  this.items.set( item.message.id, item );
855
+ item.message.ref = item.message.id;
840
856
  this.queueOut.push(item);
841
857
 
842
858
 
@@ -902,7 +918,7 @@ class HeadTTS {
902
918
  console.error("HeadTTS: REST error, error=", error);
903
919
  this.processData({
904
920
  type: "error",
905
- ref: item.id,
921
+ ref: item.message.id,
906
922
  data: {
907
923
  error: "HeadTTS: REST error, message=" + error.message
908
924
  }
@@ -1004,10 +1020,11 @@ class HeadTTS {
1004
1020
  const item = this.queueOut[0];
1005
1021
  if ( item.status < 2 ) break; // First item not yet ready
1006
1022
  this.queueOut.shift();
1007
- this.items.delete( item.message.id );
1023
+ this.items.delete( item.message.ref );
1008
1024
 
1009
1025
  const message = item.message;
1010
- Object.assign(message.data, item.metadata || {});
1026
+ message.metaData = item.metaData;
1027
+ message.userData = item.userData;
1011
1028
  const type = message.type;
1012
1029
 
1013
1030
  if ( type === "audio" || type === "custom" ) {
@@ -68,6 +68,9 @@ class LanguageBase {
68
68
  "-": "-", "'": "'", ".": ".", ",": ","
69
69
  }
70
70
 
71
+ // Grapheme segmenter
72
+ this.segmenter = new Intl.Segmenter('en', { granularity: 'grapheme' });
73
+
71
74
  if ( this.settings.trace ) {
72
75
  utils.trace( 'Language base module initiated.' );
73
76
  }
@@ -166,7 +169,7 @@ class LanguageBase {
166
169
  splitText(s) {
167
170
 
168
171
  const parts = [];
169
- const chars = [...s];
172
+ const chars = Array.from(this.segmenter.segment(s), seg => seg.segment);
170
173
  const len = chars.length;
171
174
  let i = 0;
172
175
  let lastType = 0; // 0=unknown, 1=whitespace, 2=other
@@ -204,7 +207,7 @@ class LanguageBase {
204
207
  */
205
208
  normalizeUpper(s) {
206
209
  const norm = [];
207
- const chars = [...s.toUpperCase()];
210
+ const chars = Array.from(this.segmenter.segment(s.toUpperCase()), seg => seg.segment);
208
211
  const len = chars.length;
209
212
  let i=0;
210
213
  while( i<len ) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@met4citizen/headtts",
3
- "version": "1.0.0",
3
+ "version": "1.1.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.6.0",
16
- "ws": "^8.18.1"
15
+ "@huggingface/transformers": "^3.7.2",
16
+ "ws": "^8.18.3"
17
17
  },
18
18
  "scripts": {
19
19
  "start": "node ./modules/headtts-node.mjs",