@micdrop/piper 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
@@ -1,6 +1,6 @@
1
1
  # @micdrop/piper
2
2
 
3
- [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/ai-integration/provided-integrations/piper)
3
+ [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/text-to-speech/piper)
4
4
 
5
5
  Local Piper text to speech for [@micdrop/server](https://micdrop.dev/docs/server).
6
6
 
@@ -36,7 +36,7 @@ Piper ships a `download_voices` module, but calling it means finding the exact
36
36
  interpreter `pip` installed it under, which is rarely the `python3` in your
37
37
  path. Downloading the two files is shorter and works the same.
38
38
 
39
- ## Usage
39
+ ## Usage with MicdropServer
40
40
 
41
41
  ```typescript
42
42
  import { PiperTTS } from '@micdrop/piper'
@@ -53,9 +53,35 @@ new MicdropServer(socket, {
53
53
  })
54
54
  ```
55
55
 
56
+ ## Usage without MicdropServer
57
+
58
+ ```typescript
59
+ import { PiperTTS } from '@micdrop/piper'
60
+ import { Readable } from 'stream'
61
+
62
+ const tts = new PiperTTS({
63
+ modelPath: './voices/fr_FR-siwis-medium.onnx',
64
+ })
65
+
66
+ // Audio is raw PCM, 16 bits, mono, at tts.sampleRate
67
+ tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))
68
+ tts.on('Failed', (texts) => console.error('Failed:', texts))
69
+
70
+ tts.speak(Readable.from(['Hello! ', 'What can I do for you?']))
71
+ ```
72
+
73
+ ## Events
74
+
75
+ | Event | Payload | Description |
76
+ | -------- | ---------- | ---------------------------------------------------------------------------------------------------------------------- |
77
+ | `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, mono, at `tts.sampleRate` (the rate of the voice, 16 or 22.05 kHz), ready to be played. |
78
+ | `Failed` | `string[]` | The Piper process exited with an error, or the binary cannot be run (reported once). The payload stays empty. |
79
+
80
+ See the [TTS interface](https://micdrop.dev/docs/text-to-speech/custom) for the full contract.
81
+
56
82
  ## Documentation
57
83
 
58
- Read full [documentation of the Piper integration for Micdrop](https://micdrop.dev/docs/ai-integration/provided-integrations/piper) on the [website](https://micdrop.dev), and the [guide on running Micdrop with local models](https://micdrop.dev/docs/ai-integration/local-models).
84
+ Read full [documentation of the Piper integration for Micdrop](https://micdrop.dev/docs/text-to-speech/piper) on the [website](https://micdrop.dev), and the [guide on running Micdrop with local models](https://micdrop.dev/docs/local-models).
59
85
 
60
86
  ## License
61
87
 
package/dist/index.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { TTS } from '@micdrop/server';
1
+ import { TTS, MicdropVoice } from '@micdrop/server';
2
2
 
3
3
  /**
4
4
  * The part of a Piper voice configuration we read.
@@ -13,6 +13,7 @@ interface PiperVoiceConfig {
13
13
  speakerCount: number;
14
14
  language?: string;
15
15
  }
16
+ declare const DEFAULT_SAMPLE_RATE = 22050;
16
17
  declare function readVoiceConfig(configPath: string): Promise<PiperVoiceConfig>;
17
18
  /** Piper expects its configuration next to the model, with a .json suffix. */
18
19
  declare function defaultConfigPath(modelPath: string): string;
@@ -29,10 +30,10 @@ declare function defaultConfigPath(modelPath: string): string;
29
30
  * second while synthesizing takes almost no time on top of it, so spawning one
30
31
  * process per sentence would pay that second again on every sentence. Keeping
31
32
  * one process means the audio of consecutive sentences arrives as a single
32
- * stream, with no way to tell where one ends, so an interruption is handled by
33
- * killing the process outright and starting the replacement immediately: the
34
- * new one loads its voice while the user is still speaking, and is ready by the
35
- * time the next answer starts.
33
+ * stream, with no way to tell where one ends, so an interruption arriving while
34
+ * it is still writing audio is handled by killing the process outright and
35
+ * starting the replacement immediately: the new one loads its voice while the
36
+ * user is still speaking, and is ready by the time the next answer starts.
36
37
  *
37
38
  * The binary and the voice files are installed separately, see the README.
38
39
  *
@@ -60,21 +61,40 @@ interface PiperTTSOptions {
60
61
  }
61
62
  declare class PiperTTS extends TTS {
62
63
  private readonly options;
64
+ /**
65
+ * Lists the voices downloaded in a folder, by the path `modelPath` takes.
66
+ *
67
+ * A voice is a pair of files, `fr_FR-siwis-medium.onnx` and its `.json`
68
+ * configuration, which gives its language and its number of speakers. The
69
+ * name of the file stands in for a configuration that cannot be read.
70
+ * `language` keeps the voices that speak it.
71
+ */
72
+ static listVoices(options: {
73
+ voicesDir: string;
74
+ language?: string;
75
+ }): Promise<MicdropVoice[]>;
63
76
  private config;
64
77
  private splitter;
65
- private child?;
66
- private dirty;
78
+ private state;
67
79
  private generation;
68
80
  private counter;
69
- private destroyed;
81
+ private rate;
82
+ get sampleRate(): number;
70
83
  constructor(options: PiperTTSOptions);
71
84
  speak(textStream: NodeJS.ReadableStream): void;
72
85
  cancel(): void;
73
86
  destroy(): void;
74
87
  private write;
88
+ /** Returns the running process, starting one if there is none. */
75
89
  private spawnChild;
90
+ private isCurrent;
91
+ /** The process was given text, it holds audio back until it went quiet. */
92
+ private setDirty;
93
+ /** Audio came out: the process is clean once it stays quiet a moment. */
94
+ private handleOutput;
95
+ private clearQuietTimer;
76
96
  private killChild;
77
97
  private getArguments;
78
98
  }
79
99
 
80
- export { PiperTTS, type PiperTTSOptions, type PiperVoiceConfig, defaultConfigPath, readVoiceConfig };
100
+ export { DEFAULT_SAMPLE_RATE, PiperTTS, type PiperTTSOptions, type PiperVoiceConfig, defaultConfigPath, readVoiceConfig };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { TTS } from '@micdrop/server';
1
+ import { TTS, MicdropVoice } from '@micdrop/server';
2
2
 
3
3
  /**
4
4
  * The part of a Piper voice configuration we read.
@@ -13,6 +13,7 @@ interface PiperVoiceConfig {
13
13
  speakerCount: number;
14
14
  language?: string;
15
15
  }
16
+ declare const DEFAULT_SAMPLE_RATE = 22050;
16
17
  declare function readVoiceConfig(configPath: string): Promise<PiperVoiceConfig>;
17
18
  /** Piper expects its configuration next to the model, with a .json suffix. */
18
19
  declare function defaultConfigPath(modelPath: string): string;
@@ -29,10 +30,10 @@ declare function defaultConfigPath(modelPath: string): string;
29
30
  * second while synthesizing takes almost no time on top of it, so spawning one
30
31
  * process per sentence would pay that second again on every sentence. Keeping
31
32
  * one process means the audio of consecutive sentences arrives as a single
32
- * stream, with no way to tell where one ends, so an interruption is handled by
33
- * killing the process outright and starting the replacement immediately: the
34
- * new one loads its voice while the user is still speaking, and is ready by the
35
- * time the next answer starts.
33
+ * stream, with no way to tell where one ends, so an interruption arriving while
34
+ * it is still writing audio is handled by killing the process outright and
35
+ * starting the replacement immediately: the new one loads its voice while the
36
+ * user is still speaking, and is ready by the time the next answer starts.
36
37
  *
37
38
  * The binary and the voice files are installed separately, see the README.
38
39
  *
@@ -60,21 +61,40 @@ interface PiperTTSOptions {
60
61
  }
61
62
  declare class PiperTTS extends TTS {
62
63
  private readonly options;
64
+ /**
65
+ * Lists the voices downloaded in a folder, by the path `modelPath` takes.
66
+ *
67
+ * A voice is a pair of files, `fr_FR-siwis-medium.onnx` and its `.json`
68
+ * configuration, which gives its language and its number of speakers. The
69
+ * name of the file stands in for a configuration that cannot be read.
70
+ * `language` keeps the voices that speak it.
71
+ */
72
+ static listVoices(options: {
73
+ voicesDir: string;
74
+ language?: string;
75
+ }): Promise<MicdropVoice[]>;
63
76
  private config;
64
77
  private splitter;
65
- private child?;
66
- private dirty;
78
+ private state;
67
79
  private generation;
68
80
  private counter;
69
- private destroyed;
81
+ private rate;
82
+ get sampleRate(): number;
70
83
  constructor(options: PiperTTSOptions);
71
84
  speak(textStream: NodeJS.ReadableStream): void;
72
85
  cancel(): void;
73
86
  destroy(): void;
74
87
  private write;
88
+ /** Returns the running process, starting one if there is none. */
75
89
  private spawnChild;
90
+ private isCurrent;
91
+ /** The process was given text, it holds audio back until it went quiet. */
92
+ private setDirty;
93
+ /** Audio came out: the process is clean once it stays quiet a moment. */
94
+ private handleOutput;
95
+ private clearQuietTimer;
76
96
  private killChild;
77
97
  private getArguments;
78
98
  }
79
99
 
80
- export { PiperTTS, type PiperTTSOptions, type PiperVoiceConfig, defaultConfigPath, readVoiceConfig };
100
+ export { DEFAULT_SAMPLE_RATE, PiperTTS, type PiperTTSOptions, type PiperVoiceConfig, defaultConfigPath, readVoiceConfig };
package/dist/index.js CHANGED
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
20
20
  // src/index.ts
21
21
  var index_exports = {};
22
22
  __export(index_exports, {
23
+ DEFAULT_SAMPLE_RATE: () => DEFAULT_SAMPLE_RATE,
23
24
  PiperTTS: () => PiperTTS,
24
25
  defaultConfigPath: () => defaultConfigPath,
25
26
  readVoiceConfig: () => readVoiceConfig
@@ -43,34 +44,73 @@ function defaultConfigPath(modelPath) {
43
44
 
44
45
  // src/PiperTTS.ts
45
46
  var import_server = require("@micdrop/server");
47
+ var import_promises2 = require("fs/promises");
48
+ var import_path = require("path");
46
49
  var import_child_process = require("child_process");
47
50
  var DEFAULT_BINARY = "piper";
48
- var OUTPUT_SAMPLE_RATE = 16e3;
51
+ var QUIET_DELAY = 1e3;
52
+ var FATAL_SPAWN_ERRORS = ["ENOENT", "EACCES", "ENOTDIR"];
49
53
  var PiperTTS = class extends import_server.TTS {
50
54
  constructor(options) {
51
55
  super();
52
56
  this.options = options;
53
57
  this.splitter = new import_server.SentenceSplitter();
54
- // Whether the running process has been given text, and so may still be
55
- // writing the audio of an utterance we no longer want to be heard
56
- this.dirty = false;
58
+ this.state = { status: "none" };
57
59
  // Bumped by every speak() and every cancel(), so a call claimed late can tell
58
60
  // whether it is still the one that should be heard
59
61
  this.generation = 0;
60
62
  this.counter = 0;
61
63
  // Identifies the current speak() call
62
- this.destroyed = false;
64
+ // Rate of the voice, read from its config, which the audio is sent at
65
+ this.rate = DEFAULT_SAMPLE_RATE;
63
66
  this.config = readVoiceConfig(
64
67
  options.configPath ?? defaultConfigPath(options.modelPath)
65
68
  );
66
69
  this.config.catch((error) => {
67
70
  console.error("[PiperTTS] Failed to read voice config:", error);
68
71
  });
69
- this.config.then(() => {
70
- if (!this.child) this.spawnChild();
72
+ this.config.then(({ sampleRate }) => {
73
+ this.rate = sampleRate;
74
+ if (this.state.status === "none") this.spawnChild();
71
75
  }).catch(() => {
72
76
  });
73
77
  }
78
+ /**
79
+ * Lists the voices downloaded in a folder, by the path `modelPath` takes.
80
+ *
81
+ * A voice is a pair of files, `fr_FR-siwis-medium.onnx` and its `.json`
82
+ * configuration, which gives its language and its number of speakers. The
83
+ * name of the file stands in for a configuration that cannot be read.
84
+ * `language` keeps the voices that speak it.
85
+ */
86
+ static async listVoices(options) {
87
+ let files;
88
+ try {
89
+ files = await (0, import_promises2.readdir)(options.voicesDir);
90
+ } catch {
91
+ return [];
92
+ }
93
+ const voices = await Promise.all(
94
+ files.filter((file) => file.endsWith(".onnx")).sort().map(async (file) => {
95
+ const modelPath = (0, import_path.join)(options.voicesDir, file);
96
+ const name = file.replace(/\.onnx$/, "");
97
+ const config = await readVoiceConfig(
98
+ defaultConfigPath(modelPath)
99
+ ).catch(() => void 0);
100
+ const code = config?.language ?? name.split("-")[0];
101
+ return {
102
+ id: modelPath,
103
+ name,
104
+ languages: code ? [code.replace("_", "-")] : void 0,
105
+ description: config && config.speakerCount > 1 ? `${config.speakerCount} speakers, picked with \`speaker\`` : void 0
106
+ };
107
+ })
108
+ );
109
+ return (0, import_server.voicesForLanguage)(voices, options.language);
110
+ }
111
+ get sampleRate() {
112
+ return this.rate;
113
+ }
74
114
  speak(textStream) {
75
115
  const generation = ++this.generation;
76
116
  let counter = 0;
@@ -100,30 +140,37 @@ var PiperTTS = class extends import_server.TTS {
100
140
  this.generation++;
101
141
  this.counter++;
102
142
  this.splitter.reset();
103
- if (!this.dirty) return;
143
+ if (this.state.status === "clean") {
144
+ this.state.muted = true;
145
+ return;
146
+ }
147
+ if (this.state.status !== "dirty") return;
104
148
  this.killChild();
105
149
  this.spawnChild();
106
150
  }
107
151
  destroy() {
108
- this.destroyed = true;
109
152
  super.destroy();
110
153
  this.killChild();
154
+ this.state = { status: "destroyed" };
111
155
  }
112
156
  write(counter, sentences) {
113
157
  if (sentences.length === 0) return;
114
158
  if (counter !== this.counter) return;
115
- const child = this.child ?? this.spawnChild();
159
+ if (this.state.status === "clean" && this.state.stale) this.killChild();
160
+ const child = this.spawnChild();
116
161
  if (!child) return;
117
162
  for (const sentence of sentences) {
118
163
  this.log(`Synthesizing: "${sentence}"`);
119
164
  child.stdin.write(`${sentence.replace(/\s+/g, " ").trim()}
120
165
  `);
121
- this.dirty = true;
122
166
  }
167
+ this.setDirty(child);
123
168
  }
169
+ /** Returns the running process, starting one if there is none. */
124
170
  spawnChild() {
125
- if (this.destroyed) return void 0;
126
- if (this.child) return this.child;
171
+ const { state } = this;
172
+ if (state.status === "clean" || state.status === "dirty") return state.child;
173
+ if (state.status !== "none") return void 0;
127
174
  const binary = this.options.binaryPath ?? DEFAULT_BINARY;
128
175
  let child;
129
176
  try {
@@ -134,30 +181,48 @@ var PiperTTS = class extends import_server.TTS {
134
181
  console.error(`[PiperTTS] Cannot run "${binary}":`, error);
135
182
  return void 0;
136
183
  }
137
- this.child = child;
138
- this.dirty = false;
139
- const resampler = this.config.then(
140
- ({ sampleRate }) => new import_server.Pcm16Resampler(sampleRate, OUTPUT_SAMPLE_RATE)
141
- );
184
+ this.state = { status: "clean", child };
185
+ const aligner = new import_server.Pcm16Aligner();
186
+ const ready = this.config.then(({ sampleRate }) => {
187
+ this.rate = sampleRate;
188
+ });
142
189
  let pending = Promise.resolve();
143
190
  child.stdout.on("data", (chunk) => {
191
+ const { state: state2 } = this;
192
+ if (state2.status === "clean" && state2.child === child && state2.muted) {
193
+ state2.stale = true;
194
+ return;
195
+ }
196
+ this.handleOutput(child);
144
197
  pending = pending.then(async () => {
145
- const output = (await resampler).process(chunk);
146
- if (this.child !== child || output.length === 0) return;
198
+ await ready;
199
+ const output = aligner.process(chunk);
200
+ if (!this.isCurrent(child) || output.length === 0) return;
201
+ if (this.state.status === "clean" && this.state.muted) return;
147
202
  this.emit("Audio", output);
148
203
  });
149
204
  });
205
+ child.stdin.on("error", (error) => {
206
+ if (this.isCurrent(child)) this.log("Piper stdin error:", error.message);
207
+ });
150
208
  const errors = [];
151
209
  child.stderr.on("data", (chunk) => errors.push(chunk.toString()));
152
210
  child.on("error", (error) => {
153
- if (this.child !== child) return;
211
+ if (!this.isCurrent(child)) return;
154
212
  console.error(`[PiperTTS] Cannot run "${binary}":`, error.message);
155
- this.child = void 0;
213
+ this.clearQuietTimer();
214
+ if (error.code && FATAL_SPAWN_ERRORS.includes(error.code)) {
215
+ this.state = { status: "failed" };
216
+ this.emit("Failed", []);
217
+ } else {
218
+ this.state = { status: "none" };
219
+ }
156
220
  });
157
221
  child.on("exit", (code) => {
158
- if (this.child !== child) return;
159
- this.child = void 0;
160
- if (code === 0 || this.destroyed) return;
222
+ if (!this.isCurrent(child)) return;
223
+ this.clearQuietTimer();
224
+ this.state = { status: "none" };
225
+ if (code === 0) return;
161
226
  console.error(
162
227
  `[PiperTTS] Piper exited with code ${code}`,
163
228
  errors.join("")
@@ -166,13 +231,36 @@ var PiperTTS = class extends import_server.TTS {
166
231
  });
167
232
  return child;
168
233
  }
234
+ isCurrent(child) {
235
+ const { state } = this;
236
+ return (state.status === "clean" || state.status === "dirty") && state.child === child;
237
+ }
238
+ /** The process was given text, it holds audio back until it went quiet. */
239
+ setDirty(child) {
240
+ this.clearQuietTimer();
241
+ this.state = { status: "dirty", child };
242
+ }
243
+ /** Audio came out: the process is clean once it stays quiet a moment. */
244
+ handleOutput(child) {
245
+ const { state } = this;
246
+ if (state.status !== "dirty" || state.child !== child) return;
247
+ if (state.quietTimer) clearTimeout(state.quietTimer);
248
+ state.quietTimer = setTimeout(() => {
249
+ if (this.state === state) this.state = { status: "clean", child };
250
+ }, QUIET_DELAY);
251
+ }
252
+ clearQuietTimer() {
253
+ if (this.state.status === "dirty" && this.state.quietTimer) {
254
+ clearTimeout(this.state.quietTimer);
255
+ }
256
+ }
169
257
  killChild() {
170
- const child = this.child;
171
- if (!child) return;
172
- this.child = void 0;
173
- this.dirty = false;
174
- child.stdout.removeAllListeners();
175
- child.kill("SIGKILL");
258
+ const { state } = this;
259
+ if (state.status !== "clean" && state.status !== "dirty") return;
260
+ this.clearQuietTimer();
261
+ this.state = { status: "none" };
262
+ state.child.stdout.removeAllListeners();
263
+ state.child.kill("SIGKILL");
176
264
  }
177
265
  getArguments() {
178
266
  const { options } = this;
@@ -201,6 +289,7 @@ var PiperTTS = class extends import_server.TTS {
201
289
  };
202
290
  // Annotate the CommonJS export names for ESM import in node:
203
291
  0 && (module.exports = {
292
+ DEFAULT_SAMPLE_RATE,
204
293
  PiperTTS,
205
294
  defaultConfigPath,
206
295
  readVoiceConfig
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/config.ts","../src/PiperTTS.ts"],"sourcesContent":["export * from './config'\nexport * from './PiperTTS'\n","import { readFile } from 'fs/promises'\n\n/**\n * The part of a Piper voice configuration we read.\n *\n * Every voice ships as a pair of files, `<voice>.onnx` and `<voice>.onnx.json`.\n * The JSON one carries the sample rate the model generates at, which varies\n * between voices (16000 for the low quality ones, 22050 for the others), and\n * the number of speakers a multi-speaker voice holds.\n */\nexport interface PiperVoiceConfig {\n sampleRate: number\n speakerCount: number\n language?: string\n}\n\nconst DEFAULT_SAMPLE_RATE = 22050\n\nexport async function readVoiceConfig(\n configPath: string\n): Promise<PiperVoiceConfig> {\n const raw = JSON.parse(await readFile(configPath, 'utf-8'))\n return {\n sampleRate: raw?.audio?.sample_rate ?? DEFAULT_SAMPLE_RATE,\n speakerCount: raw?.num_speakers ?? 1,\n language: raw?.language?.code,\n }\n}\n\n/** Piper expects its configuration next to the model, with a .json suffix. */\nexport function defaultConfigPath(modelPath: string): string {\n return `${modelPath}.json`\n}\n","import { Pcm16Resampler, SentenceSplitter, TTS } from '@micdrop/server'\nimport { ChildProcessWithoutNullStreams, spawn } from 'child_process'\nimport { defaultConfigPath, PiperVoiceConfig, readVoiceConfig } from './config'\n\n/**\n * Local Piper text to speech, driving the piper binary as a subprocess.\n *\n * Piper reads one utterance per line on its standard input and writes raw\n * PCM16 on its standard output. Voices are small VITS models, a few dozen\n * megabytes each, covering around forty languages, which is what makes this\n * the option to reach for outside English.\n *\n * The process is kept alive across sentences. Loading a voice costs about a\n * second while synthesizing takes almost no time on top of it, so spawning one\n * process per sentence would pay that second again on every sentence. Keeping\n * one process means the audio of consecutive sentences arrives as a single\n * stream, with no way to tell where one ends, so an interruption is handled by\n * killing the process outright and starting the replacement immediately: the\n * new one loads its voice while the user is still speaking, and is ready by the\n * time the next answer starts.\n *\n * The binary and the voice files are installed separately, see the README.\n *\n * @see https://github.com/OHF-Voice/piper1-gpl\n */\n\nexport interface PiperTTSOptions {\n /** Path to the `.onnx` voice file, for instance `fr_FR-siwis-medium.onnx`. */\n modelPath: string\n\n /** Voice configuration, `<modelPath>.json` by default. */\n configPath?: string\n\n /** Piper executable, looked up in the PATH by default. */\n binaryPath?: string\n\n /** Speaker index, for the voices that hold several. */\n speaker?: number\n\n /** Duration multiplier, above 1 to slow the voice down. */\n lengthScale?: number\n\n /** Variability of the generated speech. */\n noiseScale?: number\n\n /** Variability of the phoneme durations. */\n noiseWidth?: number\n\n /** Silence added after each sentence, in seconds. */\n sentenceSilence?: number\n\n /** Volume multiplier, 1 being the level the voice was trained at. */\n volume?: number\n}\n\nconst DEFAULT_BINARY = 'piper'\nconst OUTPUT_SAMPLE_RATE = 16000 // Rate expected by the Micdrop client\n\nexport class PiperTTS extends TTS {\n private config: Promise<PiperVoiceConfig>\n private splitter = new SentenceSplitter()\n private child?: ChildProcessWithoutNullStreams\n // Whether the running process has been given text, and so may still be\n // writing the audio of an utterance we no longer want to be heard\n private dirty = false\n // Bumped by every speak() and every cancel(), so a call claimed late can tell\n // whether it is still the one that should be heard\n private generation = 0\n private counter = 0 // Identifies the current speak() call\n private destroyed = false\n\n constructor(private readonly options: PiperTTSOptions) {\n super()\n this.config = readVoiceConfig(\n options.configPath ?? defaultConfigPath(options.modelPath)\n )\n this.config.catch((error) => {\n console.error('[PiperTTS] Failed to read voice config:', error)\n })\n // Load the voice now rather than on the first sentence of the call. A\n // sentence arriving before the config is read spawns the process itself,\n // hence the guard: replacing it here would orphan the text it was given.\n this.config\n .then(() => {\n if (!this.child) this.spawnChild()\n })\n .catch(() => {})\n }\n\n speak(textStream: NodeJS.ReadableStream) {\n const generation = ++this.generation\n let counter = 0\n\n // Claiming the call is deferred until there is something to say, so an\n // answer skipped by a tool or by onBeforeAnswer leaves the sentences still\n // being spoken alone instead of cutting the assistant off.\n const claimCall = () => {\n if (counter) return true\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n this.splitter.reset()\n return true\n }\n\n textStream.on('data', (chunk: Buffer) => {\n if (!claimCall()) return\n if (counter !== this.counter) return\n this.write(counter, this.splitter.push(chunk.toString('utf-8')))\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream', error)\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is nothing left to flush\n if (!counter || counter !== this.counter) return\n this.write(counter, this.splitter.flush())\n })\n }\n\n cancel() {\n this.log('Cancel')\n this.generation++\n this.counter++\n this.splitter.reset()\n\n // A process that was never given text cannot be holding audio back, so it\n // is kept as it is rather than paying for another voice load\n if (!this.dirty) return\n this.killChild()\n this.spawnChild()\n }\n\n destroy() {\n this.destroyed = true\n super.destroy()\n this.killChild()\n }\n\n private write(counter: number, sentences: string[]) {\n if (sentences.length === 0) return\n if (counter !== this.counter) return\n\n const child = this.child ?? this.spawnChild()\n if (!child) return\n\n for (const sentence of sentences) {\n this.log(`Synthesizing: \"${sentence}\"`)\n // Piper reads one utterance per line, so the sentence has to be flat\n child.stdin.write(`${sentence.replace(/\\s+/g, ' ').trim()}\\n`)\n this.dirty = true\n }\n }\n\n private spawnChild(): ChildProcessWithoutNullStreams | undefined {\n if (this.destroyed) return undefined\n if (this.child) return this.child\n\n const binary = this.options.binaryPath ?? DEFAULT_BINARY\n let child: ChildProcessWithoutNullStreams\n try {\n child = spawn(binary, this.getArguments(), {\n stdio: ['pipe', 'pipe', 'pipe'],\n })\n } catch (error) {\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error)\n return undefined\n }\n\n this.child = child\n this.dirty = false\n\n // One resampler per process: its audio is a single continuous stream, and\n // a new process starts a new one\n const resampler = this.config.then(\n ({ sampleRate }) => new Pcm16Resampler(sampleRate, OUTPUT_SAMPLE_RATE)\n )\n\n let pending: Promise<void> = Promise.resolve()\n child.stdout.on('data', (chunk: Buffer) => {\n // Chunks are resampled in the order they arrive, the resampler carries\n // its fractional position from one to the next\n pending = pending.then(async () => {\n const output = (await resampler).process(chunk)\n // The process may have been replaced while this chunk was waiting\n if (this.child !== child || output.length === 0) return\n this.emit('Audio', output)\n })\n })\n\n const errors: string[] = []\n child.stderr.on('data', (chunk: Buffer) => errors.push(chunk.toString()))\n\n child.on('error', (error) => {\n if (this.child !== child) return\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error.message)\n this.child = undefined\n })\n\n child.on('exit', (code) => {\n // A process we replaced ourselves exits on purpose\n if (this.child !== child) return\n this.child = undefined\n if (code === 0 || this.destroyed) return\n console.error(\n `[PiperTTS] Piper exited with code ${code}`,\n errors.join('')\n )\n this.emit('Failed', [])\n })\n\n return child\n }\n\n private killChild() {\n const child = this.child\n if (!child) return\n this.child = undefined\n this.dirty = false\n child.stdout.removeAllListeners()\n child.kill('SIGKILL')\n }\n\n private getArguments(): string[] {\n const { options } = this\n const args = ['--model', options.modelPath, '--output_raw']\n\n if (options.configPath) args.push('--config', options.configPath)\n if (options.speaker !== undefined) {\n args.push('--speaker', String(options.speaker))\n }\n if (options.lengthScale !== undefined) {\n args.push('--length_scale', String(options.lengthScale))\n }\n if (options.noiseScale !== undefined) {\n args.push('--noise_scale', String(options.noiseScale))\n }\n if (options.noiseWidth !== undefined) {\n args.push('--noise_w', String(options.noiseWidth))\n }\n if (options.sentenceSilence !== undefined) {\n args.push('--sentence_silence', String(options.senten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,CAAC,MAAO;AACZ,SAAK,QAAQ;AACb,SAAK,QAAQ;AACb,UAAM,OAAO,mBAAmB;AAChC,UAAM,KAAK,SAAS;AAAA,EACtB;AAAA,EAEQ,eAAyB;AAC/B,UAAM,EAAE,QAAQ,IAAI;AACpB,UAAM,OAAO,CAAC,WAAW,QAAQ,WAAW,cAAc;AAE1D,QAAI,QAAQ,WAAY,MAAK,KAAK,YAAY,QAAQ,UAAU;AAChE,QAAI,QAAQ,YAAY,QAAW;AACjC,WAAK,KAAK,aAAa,OAAO,QAAQ,OAAO,CAAC;AAAA,IAChD;AACA,QAAI,QAAQ,gBAAgB,QAAW;AACrC,WAAK,KAAK,kBAAkB,OAAO,QAAQ,WAAW,CAAC;AAAA,IACzD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,iBAAiB,OAAO,QAAQ,UAAU,CAAC;AAAA,IACvD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,aAAa,OAAO,QAAQ,UAAU,CAAC;AAAA,IACnD;AACA,QAAI,QAAQ,oBAAoB,QAAW;AACzC,WAAK,KAAK,sBAAsB,OAAO,QAAQ,eAAe,CAAC;AAAA,IACjE;AACA,QAAI,QAAQ,WAAW,QAAW;AAChC,WAAK,KAAK,YAAY,OAAO,QAAQ,MAAM,CAAC;AAAA,IAC9C;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/config.ts","../src/PiperTTS.ts"],"sourcesContent":["export * from './config'\nexport * from './PiperTTS'\n","import { readFile } from 'fs/promises'\n\n/**\n * The part of a Piper voice configuration we read.\n *\n * Every voice ships as a pair of files, `<voice>.onnx` and `<voice>.onnx.json`.\n * The JSON one carries the sample rate the model generates at, which varies\n * between voices (16000 for the low quality ones, 22050 for the others), and\n * the number of speakers a multi-speaker voice holds.\n */\nexport interface PiperVoiceConfig {\n sampleRate: number\n speakerCount: number\n language?: string\n}\n\nexport const DEFAULT_SAMPLE_RATE = 22050\n\nexport async function readVoiceConfig(\n configPath: string\n): Promise<PiperVoiceConfig> {\n const raw = JSON.parse(await readFile(configPath, 'utf-8'))\n return {\n sampleRate: raw?.audio?.sample_rate ?? DEFAULT_SAMPLE_RATE,\n speakerCount: raw?.num_speakers ?? 1,\n language: raw?.language?.code,\n }\n}\n\n/** Piper expects its configuration next to the model, with a .json suffix. */\nexport function defaultConfigPath(modelPath: string): string {\n return `${modelPath}.json`\n}\n","import {\n MicdropVoice,\n Pcm16Aligner,\n SentenceSplitter,\n TTS,\n voicesForLanguage,\n} from '@micdrop/server'\nimport { readdir } from 'fs/promises'\nimport { join } from 'path'\nimport { ChildProcessWithoutNullStreams, spawn } from 'child_process'\nimport {\n DEFAULT_SAMPLE_RATE,\n defaultConfigPath,\n PiperVoiceConfig,\n readVoiceConfig,\n} from './config'\n\n/**\n * Local Piper text to speech, driving the piper binary as a subprocess.\n *\n * Piper reads one utterance per line on its standard input and writes raw\n * PCM16 on its standard output. Voices are small VITS models, a few dozen\n * megabytes each, covering around forty languages, which is what makes this\n * the option to reach for outside English.\n *\n * The process is kept alive across sentences. Loading a voice costs about a\n * second while synthesizing takes almost no time on top of it, so spawning one\n * process per sentence would pay that second again on every sentence. Keeping\n * one process means the audio of consecutive sentences arrives as a single\n * stream, with no way to tell where one ends, so an interruption arriving while\n * it is still writing audio is handled by killing the process outright and\n * starting the replacement immediately: the new one loads its voice while the\n * user is still speaking, and is ready by the time the next answer starts.\n *\n * The binary and the voice files are installed separately, see the README.\n *\n * @see https://github.com/OHF-Voice/piper1-gpl\n */\n\nexport interface PiperTTSOptions {\n /** Path to the `.onnx` voice file, for instance `fr_FR-siwis-medium.onnx`. */\n modelPath: string\n\n /** Voice configuration, `<modelPath>.json` by default. */\n configPath?: string\n\n /** Piper executable, looked up in the PATH by default. */\n binaryPath?: string\n\n /** Speaker index, for the voices that hold several. */\n speaker?: number\n\n /** Duration multiplier, above 1 to slow the voice down. */\n lengthScale?: number\n\n /** Variability of the generated speech. */\n noiseScale?: number\n\n /** Variability of the phoneme durations. */\n noiseWidth?: number\n\n /** Silence added after each sentence, in seconds. */\n sentenceSilence?: number\n\n /** Volume multiplier, 1 being the level the voice was trained at. */\n volume?: number\n}\n\nconst DEFAULT_BINARY = 'piper'\n// Piper marks neither the end of an utterance nor of its queue on standard\n// output. Once it has written some audio after the last sentence it was given\n// and then stayed silent this long, it is taken to have said everything. It\n// writes a whole sentence in a fraction of this, faster than real time.\nconst QUIET_DELAY = 1000\n\n/**\n * Where the Piper process stands.\n *\n * - `none`: no process, the next sentence starts one.\n * - `clean`: a process holding no audio back, either never given text or done\n * writing all of it. A cancel keeps it, muted: audio coming out after all\n * was still owed to the cancelled answer, so it is dropped, and the process\n * is replaced before the next sentence.\n * - `dirty`: a process that may still be writing the audio of a sentence. A\n * cancel kills it and starts another, the only way to cut that audio.\n * - `failed`: the binary cannot be run at all (missing, not executable). The\n * failure was reported once, and nothing is tried again.\n * - `destroyed`: the instance is done for.\n */\ntype ProcessState =\n | { status: 'none' }\n | {\n status: 'clean'\n child: ChildProcessWithoutNullStreams\n // Cancelled since its last sentence, nothing it says is wanted\n muted?: boolean\n // Spoke while muted, so it still holds audio of the cancelled answer\n stale?: boolean\n }\n | {\n status: 'dirty'\n child: ChildProcessWithoutNullStreams\n // Started by the first audio after the last sentence written, so the\n // silence of a process still loading its voice does not count as done\n quietTimer?: NodeJS.Timeout\n }\n | { status: 'failed' }\n | { status: 'destroyed' }\n\n// Spawn errors that a retry cannot fix\nconst FATAL_SPAWN_ERRORS = ['ENOENT', 'EACCES', 'ENOTDIR']\n\nexport class PiperTTS extends TTS {\n /**\n * Lists the voices downloaded in a folder, by the path `modelPath` takes.\n *\n * A voice is a pair of files, `fr_FR-siwis-medium.onnx` and its `.json`\n * configuration, which gives its language and its number of speakers. The\n * name of the file stands in for a configuration that cannot be read.\n * `language` keeps the voices that speak it.\n */\n static async listVoices(options: {\n voicesDir: string\n language?: string\n }): Promise<MicdropVoice[]> {\n let files: string[]\n try {\n files = await readdir(options.voicesDir)\n } catch {\n return []\n }\n const voices = await Promise.all(\n files\n .filter((file) => file.endsWith('.onnx'))\n .sort()\n .map(async (file) => {\n const modelPath = join(options.voicesDir, file)\n const name = file.replace(/\\.onnx$/, '')\n const config = await readVoiceConfig(\n defaultConfigPath(modelPath)\n ).catch(() => undefined)\n // fr_FR-siwis-medium speaks fr-FR\n const code = config?.language ?? name.split('-')[0]\n return {\n id: modelPath,\n name,\n languages: code ? [code.replace('_', '-')] : undefined,\n description:\n config && config.speakerCount > 1\n ? `${config.speakerCount} speakers, picked with \\`speaker\\``\n : undefined,\n }\n })\n )\n return voicesForLanguage(voices, options.language)\n }\n\n private config: Promise<PiperVoiceConfig>\n private splitter = new SentenceSplitter()\n private state: ProcessState = { status: 'none' }\n // Bumped by every speak() and every cancel(), so a call claimed late can tell\n // whether it is still the one that should be heard\n private generation = 0\n private counter = 0 // Identifies the current speak() call\n // Rate of the voice, read from its config, which the audio is sent at\n private rate = DEFAULT_SAMPLE_RATE\n\n get sampleRate() {\n return this.rate\n }\n\n constructor(private readonly options: PiperTTSOptions) {\n super()\n this.config = readVoiceConfig(\n options.configPath ?? defaultConfigPath(options.modelPath)\n )\n this.config.catch((error) => {\n console.error('[PiperTTS] Failed to read voice config:', error)\n })\n // Load the voice now rather than on the first sentence of the call. A\n // sentence arriving before the config is read spawns the process itself,\n // hence the guard: replacing it here would orphan the text it was given.\n this.config\n .then(({ sampleRate }) => {\n this.rate = sampleRate\n if (this.state.status === 'none') this.spawnChild()\n })\n .catch(() => {})\n }\n\n speak(textStream: NodeJS.ReadableStream) {\n const generation = ++this.generation\n let counter = 0\n\n // Claiming the call is deferred until there is something to say, so an\n // answer skipped by a tool or by onBeforeAnswer leaves the sentences still\n // being spoken alone instead of cutting the assistant off.\n const claimCall = () => {\n if (counter) return true\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n this.splitter.reset()\n return true\n }\n\n textStream.on('data', (chunk: Buffer) => {\n if (!claimCall()) return\n if (counter !== this.counter) return\n this.write(counter, this.splitter.push(chunk.toString('utf-8')))\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream', error)\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is nothing left to flush\n if (!counter || counter !== this.counter) return\n this.write(counter, this.splitter.flush())\n })\n }\n\n cancel() {\n this.log('Cancel')\n this.generation++\n this.counter++\n this.splitter.reset()\n\n // A process holding no audio back is kept as it is rather than paying for\n // another voice load. The server cancels before every answer, so this is\n // the common case.\n if (this.state.status === 'clean') {\n this.state.muted = true\n return\n }\n if (this.state.status !== 'dirty') return\n this.killChild()\n this.spawnChild()\n }\n\n destroy() {\n super.destroy()\n this.killChild()\n this.state = { status: 'destroyed' }\n }\n\n private write(counter: number, sentences: string[]) {\n if (sentences.length === 0) return\n if (counter !== this.counter) return\n\n // It went quiet longer than a sentence takes, yet still had audio of the\n // cancelled answer: only a new process keeps it out of this one\n if (this.state.status === 'clean' && this.state.stale) this.killChild()\n const child = this.spawnChild()\n if (!child) return\n\n for (const sentence of sentences) {\n this.log(`Synthesizing: \"${sentence}\"`)\n // Piper reads one utterance per line, so the sentence has to be flat\n child.stdin.write(`${sentence.replace(/\\s+/g, ' ').trim()}\\n`)\n }\n this.setDirty(child)\n }\n\n /** Returns the running process, starting one if there is none. */\n private spawnChild(): ChildProcessWithoutNullStreams | undefined {\n const { state } = this\n if (state.status === 'clean' || state.status === 'dirty') return state.child\n if (state.status !== 'none') return undefined\n\n const binary = this.options.binaryPath ?? DEFAULT_BINARY\n let child: ChildProcessWithoutNullStreams\n try {\n child = spawn(binary, this.getArguments(), {\n stdio: ['pipe', 'pipe', 'pipe'],\n })\n } catch (error) {\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error)\n return undefined\n }\n\n this.state = { status: 'clean', child }\n\n // One aligner per process: a chunk can end in the middle of a sample, and\n // the byte held back belongs to the audio of this process only. The audio\n // waits for the config, which gives its rate.\n const aligner = new Pcm16Aligner()\n const ready = this.config.then(({ sampleRate }) => {\n this.rate = sampleRate\n })\n\n let pending: Promise<void> = Promise.resolve()\n child.stdout.on('data', (chunk: Buffer) => {\n const { state } = this\n if (state.status === 'clean' && state.child === child && state.muted) {\n state.stale = true\n return\n }\n this.handleOutput(child)\n // Chunks are aligned in the order they arrive, the aligner carries a\n // split sample from one to the next\n pending = pending.then(async () => {\n await ready\n const output = aligner.process(chunk)\n // The process may have been replaced, or muted by a cancel, while\n // this chunk was waiting\n if (!this.isCurrent(child) || output.length === 0) return\n if (this.state.status === 'clean' && this.state.muted) return\n this.emit('Audio', output)\n })\n })\n\n // Writing to a process that just died fails with EPIPE, which would\n // otherwise be thrown as an unhandled error. The exit is handled below.\n child.stdin.on('error', (error) => {\n if (this.isCurrent(child)) this.log('Piper stdin error:', error.message)\n })\n\n const errors: string[] = []\n child.stderr.on('data', (chunk: Buffer) => errors.push(chunk.toString()))\n\n child.on('error', (error: NodeJS.ErrnoException) => {\n if (!this.isCurrent(child)) return\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error.message)\n this.clearQuietTimer()\n if (error.code && FATAL_SPAWN_ERRORS.includes(error.code)) {\n // Every sentence of the call would fail the same way, so it is said\n // once and nothing is started again\n this.state = { status: 'failed' }\n this.emit('Failed', [])\n } else {\n this.state = { status: 'none' }\n }\n })\n\n child.on('exit', (code) => {\n // A process we replaced ourselves exits on purpose\n if (!this.isCurrent(child)) return\n this.clearQuietTimer()\n this.state = { status: 'none' }\n if (code === 0) return\n console.error(\n `[PiperTTS] Piper exited with code ${code}`,\n errors.join('')\n )\n this.emit('Failed', [])\n })\n\n return child\n }\n\n private isCurrent(child: ChildProcessWithoutNullStreams): boolean {\n const { state } = this\n return (\n (state.status === 'clean' || state.status === 'dirty') &&\n state.child === child\n )\n }\n\n /** The process was given text, it holds audio back until it went quiet. */\n private setDirty(child: ChildProcessWithoutNullStreams) {\n this.clearQuietTimer()\n this.state = { status: 'dirty', child }\n }\n\n /** Audio came out: the process is clean once it stays quiet a moment. */\n private handleOutput(child: ChildProcessWithoutNullStreams) {\n const { state } = this\n if (state.status !== 'dirty' || state.child !== child) return\n if (state.quietTimer) clearTimeout(state.quietTimer)\n state.quietTimer = setTimeout(() => {\n if (this.state === state) this.state = { status: 'clean', child }\n }, QUIET_DELAY)\n }\n\n private clearQuietTimer() {\n if (this.state.status === 'dirty' && this.state.quietTimer) {\n clearTimeout(this.state.quietTimer)\n }\n }\n\n private killChild() {\n const { state } = this\n if (state.status !== 'clean' && state.status !== 'dirty') return\n this.clearQuietTimer()\n this.state = { status: 'none' }\n state.child.stdout.removeAllListeners()\n state.child.kill('SIGKILL')\n }\n\n private getArguments(): string[] {\n const { options } = this\n const args = ['--model', options.modelPath, '--output_raw']\n\n if (options.configPath) args.push('--config', options.configPath)\n if (options.speaker !== undefined) {\n args.push('--speaker', String(options.speaker))\n }\n if (options.lengthScale !== undefined) {\n args.push('--length_scale', String(options.lengthScale))\n }\n if (options.noiseScale !== undefined) {\n args.push('--noise_scale', String(options.noiseScale))\n }\n if (options.noiseWidth !== undefined) {\n args.push('--noise_w', String(options.noiseWidth))\n }\n if (options.sentenceSilence !== undefined) {\n args.push('--sentence_silence', String(options.sentenceSilence))\n }\n if (options.volume !== undefined) {\n args.push('--volume', String(options.volume))\n }\n\n return args\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,sBAAyB;AAgBlB,IAAM,sBAAsB;AAEnC,eAAsB,gBACpB,YAC2B;AAC3B,QAAM,MAAM,KAAK,MAAM,UAAM,0BAAS,YAAY,OAAO,CAAC;AAC1D,SAAO;AAAA,IACL,YAAY,KAAK,OAAO,eAAe;AAAA,IACvC,cAAc,KAAK,gBAAgB;AAAA,IACnC,UAAU,KAAK,UAAU;AAAA,EAC3B;AACF;AAGO,SAAS,kBAAkB,WAA2B;AAC3D,SAAO,GAAG,SAAS;AACrB;;;AChCA,oBAMO;AACP,IAAAA,mBAA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AAO,CAAC;AAAA,IAChD;AACA,QAAI,QAAQ,gBAAgB,QAAW;AACrC,WAAK,KAAK,kBAAkB,OAAO,QAAQ,WAAW,CAAC;AAAA,IACzD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,iBAAiB,OAAO,QAAQ,UAAU,CAAC;AAAA,IACvD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,aAAa,OAAO,QAAQ,UAAU,CAAC;AAAA,IACnD;AACA,QAAI,QAAQ,oBAAoB,QAAW;AACzC,WAAK,KAAK,sBAAsB,OAAO,QAAQ,eAAe,CAAC;AAAA,IACjE;AACA,QAAI,QAAQ,WAAW,QAAW;AAChC,WAAK,KAAK,YAAY,OAAO,QAAQ,MAAM,CAAC;AAAA,IAC9C;AAEA,WAAO;AAAA,EACT;AACF;","names":["import_promises","state"]}
package/dist/index.mjs CHANGED
@@ -14,35 +14,79 @@ function defaultConfigPath(modelPath) {
14
14
  }
15
15
 
16
16
  // src/PiperTTS.ts
17
- import { Pcm16Resampler, SentenceSplitter, TTS } from "@micdrop/server";
17
+ import {
18
+ Pcm16Aligner,
19
+ SentenceSplitter,
20
+ TTS,
21
+ voicesForLanguage
22
+ } from "@micdrop/server";
23
+ import { readdir } from "fs/promises";
24
+ import { join } from "path";
18
25
  import { spawn } from "child_process";
19
26
  var DEFAULT_BINARY = "piper";
20
- var OUTPUT_SAMPLE_RATE = 16e3;
27
+ var QUIET_DELAY = 1e3;
28
+ var FATAL_SPAWN_ERRORS = ["ENOENT", "EACCES", "ENOTDIR"];
21
29
  var PiperTTS = class extends TTS {
22
30
  constructor(options) {
23
31
  super();
24
32
  this.options = options;
25
33
  this.splitter = new SentenceSplitter();
26
- // Whether the running process has been given text, and so may still be
27
- // writing the audio of an utterance we no longer want to be heard
28
- this.dirty = false;
34
+ this.state = { status: "none" };
29
35
  // Bumped by every speak() and every cancel(), so a call claimed late can tell
30
36
  // whether it is still the one that should be heard
31
37
  this.generation = 0;
32
38
  this.counter = 0;
33
39
  // Identifies the current speak() call
34
- this.destroyed = false;
40
+ // Rate of the voice, read from its config, which the audio is sent at
41
+ this.rate = DEFAULT_SAMPLE_RATE;
35
42
  this.config = readVoiceConfig(
36
43
  options.configPath ?? defaultConfigPath(options.modelPath)
37
44
  );
38
45
  this.config.catch((error) => {
39
46
  console.error("[PiperTTS] Failed to read voice config:", error);
40
47
  });
41
- this.config.then(() => {
42
- if (!this.child) this.spawnChild();
48
+ this.config.then(({ sampleRate }) => {
49
+ this.rate = sampleRate;
50
+ if (this.state.status === "none") this.spawnChild();
43
51
  }).catch(() => {
44
52
  });
45
53
  }
54
+ /**
55
+ * Lists the voices downloaded in a folder, by the path `modelPath` takes.
56
+ *
57
+ * A voice is a pair of files, `fr_FR-siwis-medium.onnx` and its `.json`
58
+ * configuration, which gives its language and its number of speakers. The
59
+ * name of the file stands in for a configuration that cannot be read.
60
+ * `language` keeps the voices that speak it.
61
+ */
62
+ static async listVoices(options) {
63
+ let files;
64
+ try {
65
+ files = await readdir(options.voicesDir);
66
+ } catch {
67
+ return [];
68
+ }
69
+ const voices = await Promise.all(
70
+ files.filter((file) => file.endsWith(".onnx")).sort().map(async (file) => {
71
+ const modelPath = join(options.voicesDir, file);
72
+ const name = file.replace(/\.onnx$/, "");
73
+ const config = await readVoiceConfig(
74
+ defaultConfigPath(modelPath)
75
+ ).catch(() => void 0);
76
+ const code = config?.language ?? name.split("-")[0];
77
+ return {
78
+ id: modelPath,
79
+ name,
80
+ languages: code ? [code.replace("_", "-")] : void 0,
81
+ description: config && config.speakerCount > 1 ? `${config.speakerCount} speakers, picked with \`speaker\`` : void 0
82
+ };
83
+ })
84
+ );
85
+ return voicesForLanguage(voices, options.language);
86
+ }
87
+ get sampleRate() {
88
+ return this.rate;
89
+ }
46
90
  speak(textStream) {
47
91
  const generation = ++this.generation;
48
92
  let counter = 0;
@@ -72,30 +116,37 @@ var PiperTTS = class extends TTS {
72
116
  this.generation++;
73
117
  this.counter++;
74
118
  this.splitter.reset();
75
- if (!this.dirty) return;
119
+ if (this.state.status === "clean") {
120
+ this.state.muted = true;
121
+ return;
122
+ }
123
+ if (this.state.status !== "dirty") return;
76
124
  this.killChild();
77
125
  this.spawnChild();
78
126
  }
79
127
  destroy() {
80
- this.destroyed = true;
81
128
  super.destroy();
82
129
  this.killChild();
130
+ this.state = { status: "destroyed" };
83
131
  }
84
132
  write(counter, sentences) {
85
133
  if (sentences.length === 0) return;
86
134
  if (counter !== this.counter) return;
87
- const child = this.child ?? this.spawnChild();
135
+ if (this.state.status === "clean" && this.state.stale) this.killChild();
136
+ const child = this.spawnChild();
88
137
  if (!child) return;
89
138
  for (const sentence of sentences) {
90
139
  this.log(`Synthesizing: "${sentence}"`);
91
140
  child.stdin.write(`${sentence.replace(/\s+/g, " ").trim()}
92
141
  `);
93
- this.dirty = true;
94
142
  }
143
+ this.setDirty(child);
95
144
  }
145
+ /** Returns the running process, starting one if there is none. */
96
146
  spawnChild() {
97
- if (this.destroyed) return void 0;
98
- if (this.child) return this.child;
147
+ const { state } = this;
148
+ if (state.status === "clean" || state.status === "dirty") return state.child;
149
+ if (state.status !== "none") return void 0;
99
150
  const binary = this.options.binaryPath ?? DEFAULT_BINARY;
100
151
  let child;
101
152
  try {
@@ -106,30 +157,48 @@ var PiperTTS = class extends TTS {
106
157
  console.error(`[PiperTTS] Cannot run "${binary}":`, error);
107
158
  return void 0;
108
159
  }
109
- this.child = child;
110
- this.dirty = false;
111
- const resampler = this.config.then(
112
- ({ sampleRate }) => new Pcm16Resampler(sampleRate, OUTPUT_SAMPLE_RATE)
113
- );
160
+ this.state = { status: "clean", child };
161
+ const aligner = new Pcm16Aligner();
162
+ const ready = this.config.then(({ sampleRate }) => {
163
+ this.rate = sampleRate;
164
+ });
114
165
  let pending = Promise.resolve();
115
166
  child.stdout.on("data", (chunk) => {
167
+ const { state: state2 } = this;
168
+ if (state2.status === "clean" && state2.child === child && state2.muted) {
169
+ state2.stale = true;
170
+ return;
171
+ }
172
+ this.handleOutput(child);
116
173
  pending = pending.then(async () => {
117
- const output = (await resampler).process(chunk);
118
- if (this.child !== child || output.length === 0) return;
174
+ await ready;
175
+ const output = aligner.process(chunk);
176
+ if (!this.isCurrent(child) || output.length === 0) return;
177
+ if (this.state.status === "clean" && this.state.muted) return;
119
178
  this.emit("Audio", output);
120
179
  });
121
180
  });
181
+ child.stdin.on("error", (error) => {
182
+ if (this.isCurrent(child)) this.log("Piper stdin error:", error.message);
183
+ });
122
184
  const errors = [];
123
185
  child.stderr.on("data", (chunk) => errors.push(chunk.toString()));
124
186
  child.on("error", (error) => {
125
- if (this.child !== child) return;
187
+ if (!this.isCurrent(child)) return;
126
188
  console.error(`[PiperTTS] Cannot run "${binary}":`, error.message);
127
- this.child = void 0;
189
+ this.clearQuietTimer();
190
+ if (error.code && FATAL_SPAWN_ERRORS.includes(error.code)) {
191
+ this.state = { status: "failed" };
192
+ this.emit("Failed", []);
193
+ } else {
194
+ this.state = { status: "none" };
195
+ }
128
196
  });
129
197
  child.on("exit", (code) => {
130
- if (this.child !== child) return;
131
- this.child = void 0;
132
- if (code === 0 || this.destroyed) return;
198
+ if (!this.isCurrent(child)) return;
199
+ this.clearQuietTimer();
200
+ this.state = { status: "none" };
201
+ if (code === 0) return;
133
202
  console.error(
134
203
  `[PiperTTS] Piper exited with code ${code}`,
135
204
  errors.join("")
@@ -138,13 +207,36 @@ var PiperTTS = class extends TTS {
138
207
  });
139
208
  return child;
140
209
  }
210
+ isCurrent(child) {
211
+ const { state } = this;
212
+ return (state.status === "clean" || state.status === "dirty") && state.child === child;
213
+ }
214
+ /** The process was given text, it holds audio back until it went quiet. */
215
+ setDirty(child) {
216
+ this.clearQuietTimer();
217
+ this.state = { status: "dirty", child };
218
+ }
219
+ /** Audio came out: the process is clean once it stays quiet a moment. */
220
+ handleOutput(child) {
221
+ const { state } = this;
222
+ if (state.status !== "dirty" || state.child !== child) return;
223
+ if (state.quietTimer) clearTimeout(state.quietTimer);
224
+ state.quietTimer = setTimeout(() => {
225
+ if (this.state === state) this.state = { status: "clean", child };
226
+ }, QUIET_DELAY);
227
+ }
228
+ clearQuietTimer() {
229
+ if (this.state.status === "dirty" && this.state.quietTimer) {
230
+ clearTimeout(this.state.quietTimer);
231
+ }
232
+ }
141
233
  killChild() {
142
- const child = this.child;
143
- if (!child) return;
144
- this.child = void 0;
145
- this.dirty = false;
146
- child.stdout.removeAllListeners();
147
- child.kill("SIGKILL");
234
+ const { state } = this;
235
+ if (state.status !== "clean" && state.status !== "dirty") return;
236
+ this.clearQuietTimer();
237
+ this.state = { status: "none" };
238
+ state.child.stdout.removeAllListeners();
239
+ state.child.kill("SIGKILL");
148
240
  }
149
241
  getArguments() {
150
242
  const { options } = this;
@@ -172,6 +264,7 @@ var PiperTTS = class extends TTS {
172
264
  }
173
265
  };
174
266
  export {
267
+ DEFAULT_SAMPLE_RATE,
175
268
  PiperTTS,
176
269
  defaultConfigPath,
177
270
  readVoiceConfig
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/config.ts","../src/PiperTTS.ts"],"sourcesContent":["import { readFile } from 'fs/promises'\n\n/**\n * The part of a Piper voice configuration we read.\n *\n * Every voice ships as a pair of files, `<voice>.onnx` and `<voice>.onnx.json`.\n * The JSON one carries the sample rate the model generates at, which varies\n * between voices (16000 for the low quality ones, 22050 for the others), and\n * the number of speakers a multi-speaker voice holds.\n */\nexport interface PiperVoiceConfig {\n sampleRate: number\n speakerCount: number\n language?: string\n}\n\nconst DEFAULT_SAMPLE_RATE = 22050\n\nexport async function readVoiceConfig(\n configPath: string\n): Promise<PiperVoiceConfig> {\n const raw = JSON.parse(await readFile(configPath, 'utf-8'))\n return {\n sampleRate: raw?.audio?.sample_rate ?? DEFAULT_SAMPLE_RATE,\n speakerCount: raw?.num_speakers ?? 1,\n language: raw?.language?.code,\n }\n}\n\n/** Piper expects its configuration next to the model, with a .json suffix. */\nexport function defaultConfigPath(modelPath: string): string {\n return `${modelPath}.json`\n}\n","import { Pcm16Resampler, SentenceSplitter, TTS } from '@micdrop/server'\nimport { ChildProcessWithoutNullStreams, spawn } from 'child_process'\nimport { defaultConfigPath, PiperVoiceConfig, readVoiceConfig } from './config'\n\n/**\n * Local Piper text to speech, driving the piper binary as a subprocess.\n *\n * Piper reads one utterance per line on its standard input and writes raw\n * PCM16 on its standard output. Voices are small VITS models, a few dozen\n * megabytes each, covering around forty languages, which is what makes this\n * the option to reach for outside English.\n *\n * The process is kept alive across sentences. Loading a voice costs about a\n * second while synthesizing takes almost no time on top of it, so spawning one\n * process per sentence would pay that second again on every sentence. Keeping\n * one process means the audio of consecutive sentences arrives as a single\n * stream, with no way to tell where one ends, so an interruption is handled by\n * killing the process outright and starting the replacement immediately: the\n * new one loads its voice while the user is still speaking, and is ready by the\n * time the next answer starts.\n *\n * The binary and the voice files are installed separately, see the README.\n *\n * @see https://github.com/OHF-Voice/piper1-gpl\n */\n\nexport interface PiperTTSOptions {\n /** Path to the `.onnx` voice file, for instance `fr_FR-siwis-medium.onnx`. */\n modelPath: string\n\n /** Voice configuration, `<modelPath>.json` by default. */\n configPath?: string\n\n /** Piper executable, looked up in the PATH by default. */\n binaryPath?: string\n\n /** Speaker index, for the voices that hold several. */\n speaker?: number\n\n /** Duration multiplier, above 1 to slow the voice down. */\n lengthScale?: number\n\n /** Variability of the generated speech. */\n noiseScale?: number\n\n /** Variability of the phoneme durations. */\n noiseWidth?: number\n\n /** Silence added after each sentence, in seconds. */\n sentenceSilence?: number\n\n /** Volume multiplier, 1 being the level the voice was trained at. */\n volume?: number\n}\n\nconst DEFAULT_BINARY = 'piper'\nconst OUTPUT_SAMPLE_RATE = 16000 // Rate expected by the Micdrop client\n\nexport class PiperTTS extends TTS {\n private config: Promise<PiperVoiceConfig>\n private splitter = new SentenceSplitter()\n private child?: ChildProcessWithoutNullStreams\n // Whether the running process has been given text, and so may still be\n // writing the audio of an utterance we no longer want to be heard\n private dirty = false\n // Bumped by every speak() and every cancel(), so a call claimed late can tell\n // whether it is still the one that should be heard\n private generation = 0\n private counter = 0 // Identifies the current speak() call\n private destroyed = false\n\n constructor(private readonly options: PiperTTSOptions) {\n super()\n this.config = readVoiceConfig(\n options.configPath ?? defaultConfigPath(options.modelPath)\n )\n this.config.catch((error) => {\n console.error('[PiperTTS] Failed to read voice config:', error)\n })\n // Load the voice now rather than on the first sentence of the call. A\n // sentence arriving before the config is read spawns the process itself,\n // hence the guard: replacing it here would orphan the text it was given.\n this.config\n .then(() => {\n if (!this.child) this.spawnChild()\n })\n .catch(() => {})\n }\n\n speak(textStream: NodeJS.ReadableStream) {\n const generation = ++this.generation\n let counter = 0\n\n // Claiming the call is deferred until there is something to say, so an\n // answer skipped by a tool or by onBeforeAnswer leaves the sentences still\n // being spoken alone instead of cutting the assistant off.\n const claimCall = () => {\n if (counter) return true\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n this.splitter.reset()\n return true\n }\n\n textStream.on('data', (chunk: Buffer) => {\n if (!claimCall()) return\n if (counter !== this.counter) return\n this.write(counter, this.splitter.push(chunk.toString('utf-8')))\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream', error)\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is nothing left to flush\n if (!counter || counter !== this.counter) return\n this.write(counter, this.splitter.flush())\n })\n }\n\n cancel() {\n this.log('Cancel')\n this.generation++\n this.counter++\n this.splitter.reset()\n\n // A process that was never given text cannot be holding audio back, so it\n // is kept as it is rather than paying for another voice load\n if (!this.dirty) return\n this.killChild()\n this.spawnChild()\n }\n\n destroy() {\n this.destroyed = true\n super.destroy()\n this.killChild()\n }\n\n private write(counter: number, sentences: string[]) {\n if (sentences.length === 0) return\n if (counter !== this.counter) return\n\n const child = this.child ?? this.spawnChild()\n if (!child) return\n\n for (const sentence of sentences) {\n this.log(`Synthesizing: \"${sentence}\"`)\n // Piper reads one utterance per line, so the sentence has to be flat\n child.stdin.write(`${sentence.replace(/\\s+/g, ' ').trim()}\\n`)\n this.dirty = true\n }\n }\n\n private spawnChild(): ChildProcessWithoutNullStreams | undefined {\n if (this.destroyed) return undefined\n if (this.child) return this.child\n\n const binary = this.options.binaryPath ?? DEFAULT_BINARY\n let child: ChildProcessWithoutNullStreams\n try {\n child = spawn(binary, this.getArguments(), {\n stdio: ['pipe', 'pipe', 'pipe'],\n })\n } catch (error) {\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error)\n return undefined\n }\n\n this.child = child\n this.dirty = false\n\n // One resampler per process: its audio is a single continuous stream, and\n // a new process starts a new one\n const resampler = this.config.then(\n ({ sampleRate }) => new Pcm16Resampler(sampleRate, OUTPUT_SAMPLE_RATE)\n )\n\n let pending: Promise<void> = Promise.resolve()\n child.stdout.on('data', (chunk: Buffer) => {\n // Chunks are resampled in the order they arrive, the resampler carries\n // its fractional position from one to the next\n pending = pending.then(async () => {\n const output = (await resampler).process(chunk)\n // The process may have been replaced while this chunk was waiting\n if (this.child !== child || output.length === 0) return\n this.emit('Audio', output)\n })\n })\n\n const errors: string[] = []\n child.stderr.on('data', (chunk: Buffer) => errors.push(chunk.toString()))\n\n child.on('error', (error) => {\n if (this.child !== child) return\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error.message)\n this.child = undefined\n })\n\n child.on('exit', (code) => {\n // A process we replaced ourselves exits on purpose\n if (this.child !== child) return\n this.child = undefined\n if (code === 0 || this.destroyed) return\n console.error(\n `[PiperTTS] Piper exited with code ${code}`,\n errors.join('')\n )\n this.emit('Failed', [])\n })\n\n return child\n }\n\n private killChild() {\n const child = this.child\n if (!child) return\n this.child = undefined\n this.dirty = false\n child.stdout.removeAllListeners()\n child.kill('SIGKILL')\n }\n\n private getArguments(): string[] {\n const { options } = this\n const args = ['--model', options.modelPath, '--output_raw']\n\n if (options.configPath) args.push('--config', options.configPath)\n if (options.speaker !== undefined) {\n args.push('--speaker', String(options.speaker))\n }\n if (options.lengthScale !== undefined) {\n args.push('--length_scale', String(options.lengthScale))\n }\n if (options.noiseScale !== undefined) {\n args.push('--noise_scale', String(options.noiseScale))\n }\n if (options.noiseWidth !== undefined) {\n args.push('--noise_w', String(options.noiseWidth))\n }\n if (options.sentenceSilence !== undefined) {\n args.push('--sentence_silence', String(options.sentenceSilence))\n }\n if (options.volume !== undefined) {\n args.push('--volume', String(options.volume))\n }\n\n return args\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AAgBzB,IAAM,sBAAsB;AAE5B,eAAsB,gBACpB,YAC2B;AAC3B,QAAM,MAAM,KAAK,MAAM,MAAM,SAAS,YAAY,OAAO,CAAC;AAC1D,SAAO;AAAA,IACL,YAAY,KAAK,OAAO,eAAe;AAAA,IACvC,cAAc,KAAK,gBAAgB;AAAA,IACnC,UAAU,KAAK,UAAU;AAAA,EAC3B;AACF;AAGO,SAAS,kBAAkB,WAA2B;AAC3D,SAAO,GAAG,SAAS;AACrB;;;AChCA,SAAS,gBAAgB,kBAAkB,WAAW;AACtD,SAAyC,aAAa;AAsDtD,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAEpB,IAAM,WAAN,cAAuB,IAAI;AAAA,EAahC,YAA6B,SAA0B;AACrD,UAAM;AADqB;AAX7B,SAAQ,WAAW,IAAI,iBAAiB;AAIxC;AAAA;AAAA,SAAQ,QAAQ;AAGhB;AAAA;AAAA,SAAQ,aAAa;AACrB,SAAQ,UAAU;AAClB;AAAA,SAAQ,YAAY;AAIlB,SAAK,SAAS;AAAA,MACZ,QAAQ,cAAc,kBAAkB,QAAQ,SAAS;AAAA,IAC3D;AACA,SAAK,OAAO,MAAM,CAAC,UAAU;AAC3B,cAAQ,MAAM,2CAA2C,KAAK;AAAA,IAChE,CAAC;AAID,SAAK,OACF,KAAK,MAAM;AACV,UAAI,CAAC,KAAK,MAAO,MAAK,WAAW;AAAA,IACnC,CAAC,EACA,MAAM,MAAM;AAAA,IAAC,CAAC;AAAA,EACnB;AAAA,EAEA,MAAM,YAAmC;AACvC,UAAM,aAAa,EAAE,KAAK;AAC1B,QAAI,UAAU;AAKd,UAAM,YAAY,MAAM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AO;AAAA,EACT;AAAA,EAEQ,YAAY;AAClB,UAAM,QAAQ,KAAK;AACnB,QAAI,CAAC,MAAO;AACZ,SAAK,QAAQ;AACb,SAAK,QAAQ;AACb,UAAM,OAAO,mBAAmB;AAChC,UAAM,KAAK,SAAS;AAAA,EACtB;AAAA,EAEQ,eAAyB;AAC/B,UAAM,EAAE,QAAQ,IAAI;AACpB,UAAM,OAAO,CAAC,WAAW,QAAQ,WAAW,cAAc;AAE1D,QAAI,QAAQ,WAAY,MAAK,KAAK,YAAY,QAAQ,UAAU;AAChE,QAAI,QAAQ,YAAY,QAAW;AACjC,WAAK,KAAK,aAAa,OAAO,QAAQ,OAAO,CAAC;AAAA,IAChD;AACA,QAAI,QAAQ,gBAAgB,QAAW;AACrC,WAAK,KAAK,kBAAkB,OAAO,QAAQ,WAAW,CAAC;AAAA,IACzD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,iBAAiB,OAAO,QAAQ,UAAU,CAAC;AAAA,IACvD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,aAAa,OAAO,QAAQ,UAAU,CAAC;AAAA,IACnD;AACA,QAAI,QAAQ,oBAAoB,QAAW;AACzC,WAAK,KAAK,sBAAsB,OAAO,QAAQ,eAAe,CAAC;AAAA,IACjE;AACA,QAAI,QAAQ,WAAW,QAAW;AAChC,WAAK,KAAK,YAAY,OAAO,QAAQ,MAAM,CAAC;AAAA,IAC9C;AAEA,WAAO;AAAA,EACT;AACF;","names":[]}
1
+ {"version":3,"sources":["../src/config.ts","../src/PiperTTS.ts"],"sourcesContent":["import { readFile } from 'fs/promises'\n\n/**\n * The part of a Piper voice configuration we read.\n *\n * Every voice ships as a pair of files, `<voice>.onnx` and `<voice>.onnx.json`.\n * The JSON one carries the sample rate the model generates at, which varies\n * between voices (16000 for the low quality ones, 22050 for the others), and\n * the number of speakers a multi-speaker voice holds.\n */\nexport interface PiperVoiceConfig {\n sampleRate: number\n speakerCount: number\n language?: string\n}\n\nexport const DEFAULT_SAMPLE_RATE = 22050\n\nexport async function readVoiceConfig(\n configPath: string\n): Promise<PiperVoiceConfig> {\n const raw = JSON.parse(await readFile(configPath, 'utf-8'))\n return {\n sampleRate: raw?.audio?.sample_rate ?? DEFAULT_SAMPLE_RATE,\n speakerCount: raw?.num_speakers ?? 1,\n language: raw?.language?.code,\n }\n}\n\n/** Piper expects its configuration next to the model, with a .json suffix. */\nexport function defaultConfigPath(modelPath: string): string {\n return `${modelPath}.json`\n}\n","import {\n MicdropVoice,\n Pcm16Aligner,\n SentenceSplitter,\n TTS,\n voicesForLanguage,\n} from '@micdrop/server'\nimport { readdir } from 'fs/promises'\nimport { join } from 'path'\nimport { ChildProcessWithoutNullStreams, spawn } from 'child_process'\nimport {\n DEFAULT_SAMPLE_RATE,\n defaultConfigPath,\n PiperVoiceConfig,\n readVoiceConfig,\n} from './config'\n\n/**\n * Local Piper text to speech, driving the piper binary as a subprocess.\n *\n * Piper reads one utterance per line on its standard input and writes raw\n * PCM16 on its standard output. Voices are small VITS models, a few dozen\n * megabytes each, covering around forty languages, which is what makes this\n * the option to reach for outside English.\n *\n * The process is kept alive across sentences. Loading a voice costs about a\n * second while synthesizing takes almost no time on top of it, so spawning one\n * process per sentence would pay that second again on every sentence. Keeping\n * one process means the audio of consecutive sentences arrives as a single\n * stream, with no way to tell where one ends, so an interruption arriving while\n * it is still writing audio is handled by killing the process outright and\n * starting the replacement immediately: the new one loads its voice while the\n * user is still speaking, and is ready by the time the next answer starts.\n *\n * The binary and the voice files are installed separately, see the README.\n *\n * @see https://github.com/OHF-Voice/piper1-gpl\n */\n\nexport interface PiperTTSOptions {\n /** Path to the `.onnx` voice file, for instance `fr_FR-siwis-medium.onnx`. */\n modelPath: string\n\n /** Voice configuration, `<modelPath>.json` by default. */\n configPath?: string\n\n /** Piper executable, looked up in the PATH by default. */\n binaryPath?: string\n\n /** Speaker index, for the voices that hold several. */\n speaker?: number\n\n /** Duration multiplier, above 1 to slow the voice down. */\n lengthScale?: number\n\n /** Variability of the generated speech. */\n noiseScale?: number\n\n /** Variability of the phoneme durations. */\n noiseWidth?: number\n\n /** Silence added after each sentence, in seconds. */\n sentenceSilence?: number\n\n /** Volume multiplier, 1 being the level the voice was trained at. */\n volume?: number\n}\n\nconst DEFAULT_BINARY = 'piper'\n// Piper marks neither the end of an utterance nor of its queue on standard\n// output. Once it has written some audio after the last sentence it was given\n// and then stayed silent this long, it is taken to have said everything. It\n// writes a whole sentence in a fraction of this, faster than real time.\nconst QUIET_DELAY = 1000\n\n/**\n * Where the Piper process stands.\n *\n * - `none`: no process, the next sentence starts one.\n * - `clean`: a process holding no audio back, either never given text or done\n * writing all of it. A cancel keeps it, muted: audio coming out after all\n * was still owed to the cancelled answer, so it is dropped, and the process\n * is replaced before the next sentence.\n * - `dirty`: a process that may still be writing the audio of a sentence. A\n * cancel kills it and starts another, the only way to cut that audio.\n * - `failed`: the binary cannot be run at all (missing, not executable). The\n * failure was reported once, and nothing is tried again.\n * - `destroyed`: the instance is done for.\n */\ntype ProcessState =\n | { status: 'none' }\n | {\n status: 'clean'\n child: ChildProcessWithoutNullStreams\n // Cancelled since its last sentence, nothing it says is wanted\n muted?: boolean\n // Spoke while muted, so it still holds audio of the cancelled answer\n stale?: boolean\n }\n | {\n status: 'dirty'\n child: ChildProcessWithoutNullStreams\n // Started by the first audio after the last sentence written, so the\n // silence of a process still loading its voice does not count as done\n quietTimer?: NodeJS.Timeout\n }\n | { status: 'failed' }\n | { status: 'destroyed' }\n\n// Spawn errors that a retry cannot fix\nconst FATAL_SPAWN_ERRORS = ['ENOENT', 'EACCES', 'ENOTDIR']\n\nexport class PiperTTS extends TTS {\n /**\n * Lists the voices downloaded in a folder, by the path `modelPath` takes.\n *\n * A voice is a pair of files, `fr_FR-siwis-medium.onnx` and its `.json`\n * configuration, which gives its language and its number of speakers. The\n * name of the file stands in for a configuration that cannot be read.\n * `language` keeps the voices that speak it.\n */\n static async listVoices(options: {\n voicesDir: string\n language?: string\n }): Promise<MicdropVoice[]> {\n let files: string[]\n try {\n files = await readdir(options.voicesDir)\n } catch {\n return []\n }\n const voices = await Promise.all(\n files\n .filter((file) => file.endsWith('.onnx'))\n .sort()\n .map(async (file) => {\n const modelPath = join(options.voicesDir, file)\n const name = file.replace(/\\.onnx$/, '')\n const config = await readVoiceConfig(\n defaultConfigPath(modelPath)\n ).catch(() => undefined)\n // fr_FR-siwis-medium speaks fr-FR\n const code = config?.language ?? name.split('-')[0]\n return {\n id: modelPath,\n name,\n languages: code ? [code.replace('_', '-')] : undefined,\n description:\n config && config.speakerCount > 1\n ? `${config.speakerCount} speakers, picked with \\`speaker\\``\n : undefined,\n }\n })\n )\n return voicesForLanguage(voices, options.language)\n }\n\n private config: Promise<PiperVoiceConfig>\n private splitter = new SentenceSplitter()\n private state: ProcessState = { status: 'none' }\n // Bumped by every speak() and every cancel(), so a call claimed late can tell\n // whether it is still the one that should be heard\n private generation = 0\n private counter = 0 // Identifies the current speak() call\n // Rate of the voice, read from its config, which the audio is sent at\n private rate = DEFAULT_SAMPLE_RATE\n\n get sampleRate() {\n return this.rate\n }\n\n constructor(private readonly options: PiperTTSOptions) {\n super()\n this.config = readVoiceConfig(\n options.configPath ?? defaultConfigPath(options.modelPath)\n )\n this.config.catch((error) => {\n console.error('[PiperTTS] Failed to read voice config:', error)\n })\n // Load the voice now rather than on the first sentence of the call. A\n // sentence arriving before the config is read spawns the process itself,\n // hence the guard: replacing it here would orphan the text it was given.\n this.config\n .then(({ sampleRate }) => {\n this.rate = sampleRate\n if (this.state.status === 'none') this.spawnChild()\n })\n .catch(() => {})\n }\n\n speak(textStream: NodeJS.ReadableStream) {\n const generation = ++this.generation\n let counter = 0\n\n // Claiming the call is deferred until there is something to say, so an\n // answer skipped by a tool or by onBeforeAnswer leaves the sentences still\n // being spoken alone instead of cutting the assistant off.\n const claimCall = () => {\n if (counter) return true\n if (this.generation !== generation) return false\n this.counter++\n counter = this.counter\n this.splitter.reset()\n return true\n }\n\n textStream.on('data', (chunk: Buffer) => {\n if (!claimCall()) return\n if (counter !== this.counter) return\n this.write(counter, this.splitter.push(chunk.toString('utf-8')))\n })\n\n textStream.on('error', (error) => {\n this.log('Error in text stream', error)\n })\n\n textStream.on('end', () => {\n // Nothing was ever said, so there is nothing left to flush\n if (!counter || counter !== this.counter) return\n this.write(counter, this.splitter.flush())\n })\n }\n\n cancel() {\n this.log('Cancel')\n this.generation++\n this.counter++\n this.splitter.reset()\n\n // A process holding no audio back is kept as it is rather than paying for\n // another voice load. The server cancels before every answer, so this is\n // the common case.\n if (this.state.status === 'clean') {\n this.state.muted = true\n return\n }\n if (this.state.status !== 'dirty') return\n this.killChild()\n this.spawnChild()\n }\n\n destroy() {\n super.destroy()\n this.killChild()\n this.state = { status: 'destroyed' }\n }\n\n private write(counter: number, sentences: string[]) {\n if (sentences.length === 0) return\n if (counter !== this.counter) return\n\n // It went quiet longer than a sentence takes, yet still had audio of the\n // cancelled answer: only a new process keeps it out of this one\n if (this.state.status === 'clean' && this.state.stale) this.killChild()\n const child = this.spawnChild()\n if (!child) return\n\n for (const sentence of sentences) {\n this.log(`Synthesizing: \"${sentence}\"`)\n // Piper reads one utterance per line, so the sentence has to be flat\n child.stdin.write(`${sentence.replace(/\\s+/g, ' ').trim()}\\n`)\n }\n this.setDirty(child)\n }\n\n /** Returns the running process, starting one if there is none. */\n private spawnChild(): ChildProcessWithoutNullStreams | undefined {\n const { state } = this\n if (state.status === 'clean' || state.status === 'dirty') return state.child\n if (state.status !== 'none') return undefined\n\n const binary = this.options.binaryPath ?? DEFAULT_BINARY\n let child: ChildProcessWithoutNullStreams\n try {\n child = spawn(binary, this.getArguments(), {\n stdio: ['pipe', 'pipe', 'pipe'],\n })\n } catch (error) {\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error)\n return undefined\n }\n\n this.state = { status: 'clean', child }\n\n // One aligner per process: a chunk can end in the middle of a sample, and\n // the byte held back belongs to the audio of this process only. The audio\n // waits for the config, which gives its rate.\n const aligner = new Pcm16Aligner()\n const ready = this.config.then(({ sampleRate }) => {\n this.rate = sampleRate\n })\n\n let pending: Promise<void> = Promise.resolve()\n child.stdout.on('data', (chunk: Buffer) => {\n const { state } = this\n if (state.status === 'clean' && state.child === child && state.muted) {\n state.stale = true\n return\n }\n this.handleOutput(child)\n // Chunks are aligned in the order they arrive, the aligner carries a\n // split sample from one to the next\n pending = pending.then(async () => {\n await ready\n const output = aligner.process(chunk)\n // The process may have been replaced, or muted by a cancel, while\n // this chunk was waiting\n if (!this.isCurrent(child) || output.length === 0) return\n if (this.state.status === 'clean' && this.state.muted) return\n this.emit('Audio', output)\n })\n })\n\n // Writing to a process that just died fails with EPIPE, which would\n // otherwise be thrown as an unhandled error. The exit is handled below.\n child.stdin.on('error', (error) => {\n if (this.isCurrent(child)) this.log('Piper stdin error:', error.message)\n })\n\n const errors: string[] = []\n child.stderr.on('data', (chunk: Buffer) => errors.push(chunk.toString()))\n\n child.on('error', (error: NodeJS.ErrnoException) => {\n if (!this.isCurrent(child)) return\n console.error(`[PiperTTS] Cannot run \"${binary}\":`, error.message)\n this.clearQuietTimer()\n if (error.code && FATAL_SPAWN_ERRORS.includes(error.code)) {\n // Every sentence of the call would fail the same way, so it is said\n // once and nothing is started again\n this.state = { status: 'failed' }\n this.emit('Failed', [])\n } else {\n this.state = { status: 'none' }\n }\n })\n\n child.on('exit', (code) => {\n // A process we replaced ourselves exits on purpose\n if (!this.isCurrent(child)) return\n this.clearQuietTimer()\n this.state = { status: 'none' }\n if (code === 0) return\n console.error(\n `[PiperTTS] Piper exited with code ${code}`,\n errors.join('')\n )\n this.emit('Failed', [])\n })\n\n return child\n }\n\n private isCurrent(child: ChildProcessWithoutNullStreams): boolean {\n const { state } = this\n return (\n (state.status === 'clean' || state.status === 'dirty') &&\n state.child === child\n )\n }\n\n /** The process was given text, it holds audio back until it went quiet. */\n private setDirty(child: ChildProcessWithoutNullStreams) {\n this.clearQuietTimer()\n this.state = { status: 'dirty', child }\n }\n\n /** Audio came out: the process is clean once it stays quiet a moment. */\n private handleOutput(child: ChildProcessWithoutNullStreams) {\n const { state } = this\n if (state.status !== 'dirty' || state.child !== child) return\n if (state.quietTimer) clearTimeout(state.quietTimer)\n state.quietTimer = setTimeout(() => {\n if (this.state === state) this.state = { status: 'clean', child }\n }, QUIET_DELAY)\n }\n\n private clearQuietTimer() {\n if (this.state.status === 'dirty' && this.state.quietTimer) {\n clearTimeout(this.state.quietTimer)\n }\n }\n\n private killChild() {\n const { state } = this\n if (state.status !== 'clean' && state.status !== 'dirty') return\n this.clearQuietTimer()\n this.state = { status: 'none' }\n state.child.stdout.removeAllListeners()\n state.child.kill('SIGKILL')\n }\n\n private getArguments(): string[] {\n const { options } = this\n const args = ['--model', options.modelPath, '--output_raw']\n\n if (options.configPath) args.push('--config', options.configPath)\n if (options.speaker !== undefined) {\n args.push('--speaker', String(options.speaker))\n }\n if (options.lengthScale !== undefined) {\n args.push('--length_scale', String(options.lengthScale))\n }\n if (options.noiseScale !== undefined) {\n args.push('--noise_scale', String(options.noiseScale))\n }\n if (options.noiseWidth !== undefined) {\n args.push('--noise_w', String(options.noiseWidth))\n }\n if (options.sentenceSilence !== undefined) {\n args.push('--sentence_silence', String(options.sentenceSilence))\n }\n if (options.volume !== undefined) {\n args.push('--volume', String(options.volume))\n }\n\n return args\n }\n}\n"],"mappings":";AAAA,SAAS,gBAAgB;AAgBlB,IAAM,sBAAsB;AAEnC,eAAsB,gBACpB,YAC2B;AAC3B,QAAM,MAAM,KAAK,MAAM,MAAM,SAAS,YAAY,OAAO,CAAC;AAC1D,SAAO;AAAA,IACL,YAAY,KAAK,OAAO,eAAe;AAAA,IACvC,cAAc,KAAK,gBAAgB;AAAA,IACnC,UAAU,KAAK,UAAU;AAAA,EAC3B;AACF;AAGO,SAAS,kBAAkB,WAA2B;AAC3D,SAAO,GAAG,SAAS;AACrB;;;AChCA;AAAA,EAEE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;AACP,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QAAQ,OAAO,CAAC;AAAA,IAChD;AACA,QAAI,QAAQ,gBAAgB,QAAW;AACrC,WAAK,KAAK,kBAAkB,OAAO,QAAQ,WAAW,CAAC;AAAA,IACzD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,iBAAiB,OAAO,QAAQ,UAAU,CAAC;AAAA,IACvD;AACA,QAAI,QAAQ,eAAe,QAAW;AACpC,WAAK,KAAK,aAAa,OAAO,QAAQ,UAAU,CAAC;AAAA,IACnD;AACA,QAAI,QAAQ,oBAAoB,QAAW;AACzC,WAAK,KAAK,sBAAsB,OAAO,QAAQ,eAAe,CAAC;AAAA,IACjE;AACA,QAAI,QAAQ,WAAW,QAAW;AAChC,WAAK,KAAK,YAAY,OAAO,QAAQ,MAAM,CAAC;AAAA,IAC9C;AAEA,WAAO;AAAA,EACT;AACF;","names":["state"]}
package/package.json CHANGED
@@ -1,12 +1,19 @@
1
1
  {
2
2
  "name": "@micdrop/piper",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Local Piper text to speech for @micdrop/server",
5
5
  "author": "Godefroy de Compreignac",
6
6
  "license": "MIT",
7
7
  "main": "./dist/index.js",
8
8
  "module": "./dist/index.mjs",
9
9
  "types": "./dist/index.d.ts",
10
+ "scripts": {
11
+ "build": "tsup",
12
+ "dev": "tsup --watch",
13
+ "clean": "rm -rf dist",
14
+ "typecheck": "tsc --noEmit",
15
+ "prepublishOnly": "npm run build"
16
+ },
10
17
  "files": [
11
18
  "dist"
12
19
  ],
@@ -29,17 +36,11 @@
29
36
  "tts"
30
37
  ],
31
38
  "peerDependencies": {
32
- "@micdrop/server": ">=2.3.0"
39
+ "@micdrop/server": ">=2.11.0"
33
40
  },
34
41
  "devDependencies": {
35
42
  "@types/node": "^22.19.13",
36
43
  "tsup": "^8.5.1",
37
44
  "typescript": "^5.9.3"
38
- },
39
- "scripts": {
40
- "build": "tsup",
41
- "dev": "tsup --watch",
42
- "clean": "rm -rf dist",
43
- "typecheck": "tsc --noEmit"
44
45
  }
45
- }
46
+ }
package/LICENSE DELETED
@@ -1,9 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2022 Rolebase
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
-
7
- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
-
9
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.