@micdrop/piper 1.0.1 → 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 +8 -8
- package/dist/index.d.mts +17 -2
- package/dist/index.d.ts +17 -2
- package/dist/index.js +51 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +56 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
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/
|
|
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
|
|
|
@@ -63,7 +63,7 @@ const tts = new PiperTTS({
|
|
|
63
63
|
modelPath: './voices/fr_FR-siwis-medium.onnx',
|
|
64
64
|
})
|
|
65
65
|
|
|
66
|
-
// Audio is raw PCM, 16 bits,
|
|
66
|
+
// Audio is raw PCM, 16 bits, mono, at tts.sampleRate
|
|
67
67
|
tts.on('Audio', (chunk) => console.log('Audio:', chunk.length, 'bytes'))
|
|
68
68
|
tts.on('Failed', (texts) => console.error('Failed:', texts))
|
|
69
69
|
|
|
@@ -72,16 +72,16 @@ tts.speak(Readable.from(['Hello! ', 'What can I do for you?']))
|
|
|
72
72
|
|
|
73
73
|
## Events
|
|
74
74
|
|
|
75
|
-
| Event | Payload | Description
|
|
76
|
-
| -------- | ---------- |
|
|
77
|
-
| `Audio` | `Buffer` | A chunk of audio, PCM 16 bits, 16 kHz,
|
|
78
|
-
| `Failed` | `string[]` | The Piper process exited with an error, or the binary cannot be run (reported once). The payload stays empty.
|
|
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
79
|
|
|
80
|
-
See the [TTS interface](https://micdrop.dev/docs/
|
|
80
|
+
See the [TTS interface](https://micdrop.dev/docs/text-to-speech/custom) for the full contract.
|
|
81
81
|
|
|
82
82
|
## Documentation
|
|
83
83
|
|
|
84
|
-
Read full [documentation of the Piper integration for Micdrop](https://micdrop.dev/docs/
|
|
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).
|
|
85
85
|
|
|
86
86
|
## License
|
|
87
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;
|
|
@@ -60,11 +61,25 @@ 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
78
|
private state;
|
|
66
79
|
private generation;
|
|
67
80
|
private counter;
|
|
81
|
+
private rate;
|
|
82
|
+
get sampleRate(): number;
|
|
68
83
|
constructor(options: PiperTTSOptions);
|
|
69
84
|
speak(textStream: NodeJS.ReadableStream): void;
|
|
70
85
|
cancel(): void;
|
|
@@ -82,4 +97,4 @@ declare class PiperTTS extends TTS {
|
|
|
82
97
|
private getArguments;
|
|
83
98
|
}
|
|
84
99
|
|
|
85
|
-
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;
|
|
@@ -60,11 +61,25 @@ 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
78
|
private state;
|
|
66
79
|
private generation;
|
|
67
80
|
private counter;
|
|
81
|
+
private rate;
|
|
82
|
+
get sampleRate(): number;
|
|
68
83
|
constructor(options: PiperTTSOptions);
|
|
69
84
|
speak(textStream: NodeJS.ReadableStream): void;
|
|
70
85
|
cancel(): void;
|
|
@@ -82,4 +97,4 @@ declare class PiperTTS extends TTS {
|
|
|
82
97
|
private getArguments;
|
|
83
98
|
}
|
|
84
99
|
|
|
85
|
-
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,13 +44,13 @@ 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;
|
|
49
51
|
var QUIET_DELAY = 1e3;
|
|
50
52
|
var FATAL_SPAWN_ERRORS = ["ENOENT", "EACCES", "ENOTDIR"];
|
|
51
53
|
var PiperTTS = class extends import_server.TTS {
|
|
52
|
-
// Identifies the current speak() call
|
|
53
54
|
constructor(options) {
|
|
54
55
|
super();
|
|
55
56
|
this.options = options;
|
|
@@ -59,17 +60,57 @@ var PiperTTS = class extends import_server.TTS {
|
|
|
59
60
|
// whether it is still the one that should be heard
|
|
60
61
|
this.generation = 0;
|
|
61
62
|
this.counter = 0;
|
|
63
|
+
// Identifies the current speak() call
|
|
64
|
+
// Rate of the voice, read from its config, which the audio is sent at
|
|
65
|
+
this.rate = DEFAULT_SAMPLE_RATE;
|
|
62
66
|
this.config = readVoiceConfig(
|
|
63
67
|
options.configPath ?? defaultConfigPath(options.modelPath)
|
|
64
68
|
);
|
|
65
69
|
this.config.catch((error) => {
|
|
66
70
|
console.error("[PiperTTS] Failed to read voice config:", error);
|
|
67
71
|
});
|
|
68
|
-
this.config.then(() => {
|
|
72
|
+
this.config.then(({ sampleRate }) => {
|
|
73
|
+
this.rate = sampleRate;
|
|
69
74
|
if (this.state.status === "none") this.spawnChild();
|
|
70
75
|
}).catch(() => {
|
|
71
76
|
});
|
|
72
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
|
+
}
|
|
73
114
|
speak(textStream) {
|
|
74
115
|
const generation = ++this.generation;
|
|
75
116
|
let counter = 0;
|
|
@@ -141,9 +182,10 @@ var PiperTTS = class extends import_server.TTS {
|
|
|
141
182
|
return void 0;
|
|
142
183
|
}
|
|
143
184
|
this.state = { status: "clean", child };
|
|
144
|
-
const
|
|
145
|
-
|
|
146
|
-
|
|
185
|
+
const aligner = new import_server.Pcm16Aligner();
|
|
186
|
+
const ready = this.config.then(({ sampleRate }) => {
|
|
187
|
+
this.rate = sampleRate;
|
|
188
|
+
});
|
|
147
189
|
let pending = Promise.resolve();
|
|
148
190
|
child.stdout.on("data", (chunk) => {
|
|
149
191
|
const { state: state2 } = this;
|
|
@@ -153,7 +195,8 @@ var PiperTTS = class extends import_server.TTS {
|
|
|
153
195
|
}
|
|
154
196
|
this.handleOutput(child);
|
|
155
197
|
pending = pending.then(async () => {
|
|
156
|
-
|
|
198
|
+
await ready;
|
|
199
|
+
const output = aligner.process(chunk);
|
|
157
200
|
if (!this.isCurrent(child) || output.length === 0) return;
|
|
158
201
|
if (this.state.status === "clean" && this.state.muted) return;
|
|
159
202
|
this.emit("Audio", output);
|
|
@@ -246,6 +289,7 @@ var PiperTTS = class extends import_server.TTS {
|
|
|
246
289
|
};
|
|
247
290
|
// Annotate the CommonJS export names for ESM import in node:
|
|
248
291
|
0 && (module.exports = {
|
|
292
|
+
DEFAULT_SAMPLE_RATE,
|
|
249
293
|
PiperTTS,
|
|
250
294
|
defaultConfigPath,
|
|
251
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 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'\nconst OUTPUT_SAMPLE_RATE = 16000 // Rate expected by the Micdrop client\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 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\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.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 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 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 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, 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;;;ACAA,sBAAyB;AAgBzB,IAAM,sBAAsB;AAE5B,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;AAG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SAAK,QAAQ,EAAE,QAAQ,OAAO;AAC9B,UAAM,MAAM,OAAO,mBAAmB;AACtC,UAAM,MAAM,KAAK,SAAS;AAAA,EAC5B;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":["state"]}
|
|
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,IAAA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AAQ,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":["import_promises","state"]}
|
package/dist/index.mjs
CHANGED
|
@@ -14,14 +14,19 @@ function defaultConfigPath(modelPath) {
|
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
// src/PiperTTS.ts
|
|
17
|
-
import {
|
|
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;
|
|
21
27
|
var QUIET_DELAY = 1e3;
|
|
22
28
|
var FATAL_SPAWN_ERRORS = ["ENOENT", "EACCES", "ENOTDIR"];
|
|
23
29
|
var PiperTTS = class extends TTS {
|
|
24
|
-
// Identifies the current speak() call
|
|
25
30
|
constructor(options) {
|
|
26
31
|
super();
|
|
27
32
|
this.options = options;
|
|
@@ -31,17 +36,57 @@ var PiperTTS = class extends TTS {
|
|
|
31
36
|
// whether it is still the one that should be heard
|
|
32
37
|
this.generation = 0;
|
|
33
38
|
this.counter = 0;
|
|
39
|
+
// Identifies the current speak() call
|
|
40
|
+
// Rate of the voice, read from its config, which the audio is sent at
|
|
41
|
+
this.rate = DEFAULT_SAMPLE_RATE;
|
|
34
42
|
this.config = readVoiceConfig(
|
|
35
43
|
options.configPath ?? defaultConfigPath(options.modelPath)
|
|
36
44
|
);
|
|
37
45
|
this.config.catch((error) => {
|
|
38
46
|
console.error("[PiperTTS] Failed to read voice config:", error);
|
|
39
47
|
});
|
|
40
|
-
this.config.then(() => {
|
|
48
|
+
this.config.then(({ sampleRate }) => {
|
|
49
|
+
this.rate = sampleRate;
|
|
41
50
|
if (this.state.status === "none") this.spawnChild();
|
|
42
51
|
}).catch(() => {
|
|
43
52
|
});
|
|
44
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
|
+
}
|
|
45
90
|
speak(textStream) {
|
|
46
91
|
const generation = ++this.generation;
|
|
47
92
|
let counter = 0;
|
|
@@ -113,9 +158,10 @@ var PiperTTS = class extends TTS {
|
|
|
113
158
|
return void 0;
|
|
114
159
|
}
|
|
115
160
|
this.state = { status: "clean", child };
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
161
|
+
const aligner = new Pcm16Aligner();
|
|
162
|
+
const ready = this.config.then(({ sampleRate }) => {
|
|
163
|
+
this.rate = sampleRate;
|
|
164
|
+
});
|
|
119
165
|
let pending = Promise.resolve();
|
|
120
166
|
child.stdout.on("data", (chunk) => {
|
|
121
167
|
const { state: state2 } = this;
|
|
@@ -125,7 +171,8 @@ var PiperTTS = class extends TTS {
|
|
|
125
171
|
}
|
|
126
172
|
this.handleOutput(child);
|
|
127
173
|
pending = pending.then(async () => {
|
|
128
|
-
|
|
174
|
+
await ready;
|
|
175
|
+
const output = aligner.process(chunk);
|
|
129
176
|
if (!this.isCurrent(child) || output.length === 0) return;
|
|
130
177
|
if (this.state.status === "clean" && this.state.muted) return;
|
|
131
178
|
this.emit("Audio", output);
|
|
@@ -217,6 +264,7 @@ var PiperTTS = class extends TTS {
|
|
|
217
264
|
}
|
|
218
265
|
};
|
|
219
266
|
export {
|
|
267
|
+
DEFAULT_SAMPLE_RATE,
|
|
220
268
|
PiperTTS,
|
|
221
269
|
defaultConfigPath,
|
|
222
270
|
readVoiceConfig
|
package/dist/index.mjs.map
CHANGED
|
@@ -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 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'\nconst OUTPUT_SAMPLE_RATE = 16000 // Rate expected by the Micdrop client\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 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\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.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 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 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 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, 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;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;AAK3B,IAAM,cAAc;AAqCpB,IAAM,qBAAqB,CAAC,UAAU,UAAU,SAAS;AAElD,IAAM,WAAN,cAAuB,IAAI;AAAA;AAAA,EAShC,YAA6B,SAA0B;AACrD,UAAM;AADqB;AAP7B,SAAQ,WAAW,IAAI,iBAAiB;AACxC,SAAQ,QAAsB,EAAE,QAAQ,OAAO;AAG/C;AAAA;AAAA,SAAQ,aAAa;AACrB,SAAQ,UAAU;AAIhB,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,KAAK,MAAM,WAAW,OAAQ,MAAK,WAAW;AAAA,IACpD,CAAC,EACA,MAAM,MAAM;AAA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AK,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"]}
|
|
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,SAAS,eAAe;AACxB,SAAS,YAAY;AACrB,SAAyC,aAAa;AA2DtD,IAAM,iBAAiB;AAKvB,IAAM,cAAc;AAqCpB,IAAM,qBAAqB,CAAC,UAAU,UAAU,SAAS;AAElD,IAAM,WAAN,cAAuB,IAAI;AAAA,EA2DhC,YAA6B,SAA0B;AACrD,UAAM;AADqB;AAb7B,SAAQ,WAAW,IAAI,iBAAiB;AACxC,SAAQ,QAAsB,EAAE,QAAQ,OAAO;AAG/C;AAAA;AAAA,SAAQ,aAAa;AACrB,SAAQ,UAAU;AAElB;AAAA;AAAA,SAAQ,OAAO;AAQb,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,CAAC,EAAE,WAAW,MAAM;AACxB,WAAK,OAAO;AACZ,UAAI,KAAK,MAAM,WAAW,OAAQ,MAAK,WAAW;AAAA,IACpD,CAAC,EACA,MAAM,MAAM;AAAA,IAAC,CAAC;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA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AAAA,IACpC;AAAA,EACF;AAAA,EAEQ,YAAY;AAClB,UAAM,EAAE,MAAM,IAAI;AAClB,QAAI,MAAM,WAAW,WAAW,MAAM,WAAW,QAAS;AAC1D,SAAK,gBAAgB;AACrB,SAAK,QAAQ,EAAE,QAAQ,OAAO;AAC9B,UAAM,MAAM,OAAO,mBAAmB;AACtC,UAAM,MAAM,KAAK,SAAS;AAAA,EAC5B;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":["state"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@micdrop/piper",
|
|
3
|
-
"version": "1.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",
|
|
@@ -36,7 +36,7 @@
|
|
|
36
36
|
"tts"
|
|
37
37
|
],
|
|
38
38
|
"peerDependencies": {
|
|
39
|
-
"@micdrop/server": ">=2.
|
|
39
|
+
"@micdrop/server": ">=2.11.0"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
42
|
"@types/node": "^22.19.13",
|