@micdrop/piper 1.0.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/LICENSE ADDED
@@ -0,0 +1,9 @@
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.
package/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # @micdrop/piper
2
+
3
+ [Micdrop website](https://micdrop.dev) | [Documentation](https://micdrop.dev/docs/ai-integration/provided-integrations/piper)
4
+
5
+ Local Piper text to speech for [@micdrop/server](https://micdrop.dev/docs/server).
6
+
7
+ Drives the Piper binary as a subprocess, so no text leaves the machine. Piper
8
+ voices are small VITS models covering around forty languages including French,
9
+ which makes this the option to reach for outside English.
10
+
11
+ ## Installation
12
+
13
+ Install the package:
14
+
15
+ ```bash
16
+ npm install @micdrop/piper
17
+ ```
18
+
19
+ Install the binary:
20
+
21
+ ```bash
22
+ pip install piper-tts
23
+ ```
24
+
25
+ Download a voice, which comes as a pair of files that have to sit next to each
26
+ other:
27
+
28
+ ```bash
29
+ mkdir -p voices && cd voices
30
+ BASE=https://huggingface.co/rhasspy/piper-voices/resolve/main/fr/fr_FR/siwis/medium
31
+ curl -LO $BASE/fr_FR-siwis-medium.onnx
32
+ curl -LO $BASE/fr_FR-siwis-medium.onnx.json
33
+ ```
34
+
35
+ Piper ships a `download_voices` module, but calling it means finding the exact
36
+ interpreter `pip` installed it under, which is rarely the `python3` in your
37
+ path. Downloading the two files is shorter and works the same.
38
+
39
+ ## Usage
40
+
41
+ ```typescript
42
+ import { PiperTTS } from '@micdrop/piper'
43
+ import { MicdropServer } from '@micdrop/server'
44
+
45
+ const tts = new PiperTTS({
46
+ modelPath: './voices/fr_FR-siwis-medium.onnx',
47
+ })
48
+
49
+ // Use with MicdropServer
50
+ new MicdropServer(socket, {
51
+ tts,
52
+ // ... other options
53
+ })
54
+ ```
55
+
56
+ ## Documentation
57
+
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).
59
+
60
+ ## License
61
+
62
+ MIT
63
+
64
+ ## Author
65
+
66
+ Originally developed for [Raconte.ai](https://www.raconte.ai), created and open sourced by [Godefroy de Compreignac](https://github.com/Godefroy)
@@ -0,0 +1,80 @@
1
+ import { TTS } from '@micdrop/server';
2
+
3
+ /**
4
+ * The part of a Piper voice configuration we read.
5
+ *
6
+ * Every voice ships as a pair of files, `<voice>.onnx` and `<voice>.onnx.json`.
7
+ * The JSON one carries the sample rate the model generates at, which varies
8
+ * between voices (16000 for the low quality ones, 22050 for the others), and
9
+ * the number of speakers a multi-speaker voice holds.
10
+ */
11
+ interface PiperVoiceConfig {
12
+ sampleRate: number;
13
+ speakerCount: number;
14
+ language?: string;
15
+ }
16
+ declare function readVoiceConfig(configPath: string): Promise<PiperVoiceConfig>;
17
+ /** Piper expects its configuration next to the model, with a .json suffix. */
18
+ declare function defaultConfigPath(modelPath: string): string;
19
+
20
+ /**
21
+ * Local Piper text to speech, driving the piper binary as a subprocess.
22
+ *
23
+ * Piper reads one utterance per line on its standard input and writes raw
24
+ * PCM16 on its standard output. Voices are small VITS models, a few dozen
25
+ * megabytes each, covering around forty languages, which is what makes this
26
+ * the option to reach for outside English.
27
+ *
28
+ * The process is kept alive across sentences. Loading a voice costs about a
29
+ * second while synthesizing takes almost no time on top of it, so spawning one
30
+ * process per sentence would pay that second again on every sentence. Keeping
31
+ * 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.
36
+ *
37
+ * The binary and the voice files are installed separately, see the README.
38
+ *
39
+ * @see https://github.com/OHF-Voice/piper1-gpl
40
+ */
41
+ interface PiperTTSOptions {
42
+ /** Path to the `.onnx` voice file, for instance `fr_FR-siwis-medium.onnx`. */
43
+ modelPath: string;
44
+ /** Voice configuration, `<modelPath>.json` by default. */
45
+ configPath?: string;
46
+ /** Piper executable, looked up in the PATH by default. */
47
+ binaryPath?: string;
48
+ /** Speaker index, for the voices that hold several. */
49
+ speaker?: number;
50
+ /** Duration multiplier, above 1 to slow the voice down. */
51
+ lengthScale?: number;
52
+ /** Variability of the generated speech. */
53
+ noiseScale?: number;
54
+ /** Variability of the phoneme durations. */
55
+ noiseWidth?: number;
56
+ /** Silence added after each sentence, in seconds. */
57
+ sentenceSilence?: number;
58
+ /** Volume multiplier, 1 being the level the voice was trained at. */
59
+ volume?: number;
60
+ }
61
+ declare class PiperTTS extends TTS {
62
+ private readonly options;
63
+ private config;
64
+ private splitter;
65
+ private child?;
66
+ private dirty;
67
+ private generation;
68
+ private counter;
69
+ private destroyed;
70
+ constructor(options: PiperTTSOptions);
71
+ speak(textStream: NodeJS.ReadableStream): void;
72
+ cancel(): void;
73
+ destroy(): void;
74
+ private write;
75
+ private spawnChild;
76
+ private killChild;
77
+ private getArguments;
78
+ }
79
+
80
+ export { PiperTTS, type PiperTTSOptions, type PiperVoiceConfig, defaultConfigPath, readVoiceConfig };
@@ -0,0 +1,80 @@
1
+ import { TTS } from '@micdrop/server';
2
+
3
+ /**
4
+ * The part of a Piper voice configuration we read.
5
+ *
6
+ * Every voice ships as a pair of files, `<voice>.onnx` and `<voice>.onnx.json`.
7
+ * The JSON one carries the sample rate the model generates at, which varies
8
+ * between voices (16000 for the low quality ones, 22050 for the others), and
9
+ * the number of speakers a multi-speaker voice holds.
10
+ */
11
+ interface PiperVoiceConfig {
12
+ sampleRate: number;
13
+ speakerCount: number;
14
+ language?: string;
15
+ }
16
+ declare function readVoiceConfig(configPath: string): Promise<PiperVoiceConfig>;
17
+ /** Piper expects its configuration next to the model, with a .json suffix. */
18
+ declare function defaultConfigPath(modelPath: string): string;
19
+
20
+ /**
21
+ * Local Piper text to speech, driving the piper binary as a subprocess.
22
+ *
23
+ * Piper reads one utterance per line on its standard input and writes raw
24
+ * PCM16 on its standard output. Voices are small VITS models, a few dozen
25
+ * megabytes each, covering around forty languages, which is what makes this
26
+ * the option to reach for outside English.
27
+ *
28
+ * The process is kept alive across sentences. Loading a voice costs about a
29
+ * second while synthesizing takes almost no time on top of it, so spawning one
30
+ * process per sentence would pay that second again on every sentence. Keeping
31
+ * 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.
36
+ *
37
+ * The binary and the voice files are installed separately, see the README.
38
+ *
39
+ * @see https://github.com/OHF-Voice/piper1-gpl
40
+ */
41
+ interface PiperTTSOptions {
42
+ /** Path to the `.onnx` voice file, for instance `fr_FR-siwis-medium.onnx`. */
43
+ modelPath: string;
44
+ /** Voice configuration, `<modelPath>.json` by default. */
45
+ configPath?: string;
46
+ /** Piper executable, looked up in the PATH by default. */
47
+ binaryPath?: string;
48
+ /** Speaker index, for the voices that hold several. */
49
+ speaker?: number;
50
+ /** Duration multiplier, above 1 to slow the voice down. */
51
+ lengthScale?: number;
52
+ /** Variability of the generated speech. */
53
+ noiseScale?: number;
54
+ /** Variability of the phoneme durations. */
55
+ noiseWidth?: number;
56
+ /** Silence added after each sentence, in seconds. */
57
+ sentenceSilence?: number;
58
+ /** Volume multiplier, 1 being the level the voice was trained at. */
59
+ volume?: number;
60
+ }
61
+ declare class PiperTTS extends TTS {
62
+ private readonly options;
63
+ private config;
64
+ private splitter;
65
+ private child?;
66
+ private dirty;
67
+ private generation;
68
+ private counter;
69
+ private destroyed;
70
+ constructor(options: PiperTTSOptions);
71
+ speak(textStream: NodeJS.ReadableStream): void;
72
+ cancel(): void;
73
+ destroy(): void;
74
+ private write;
75
+ private spawnChild;
76
+ private killChild;
77
+ private getArguments;
78
+ }
79
+
80
+ export { PiperTTS, type PiperTTSOptions, type PiperVoiceConfig, defaultConfigPath, readVoiceConfig };
package/dist/index.js ADDED
@@ -0,0 +1,208 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var index_exports = {};
22
+ __export(index_exports, {
23
+ PiperTTS: () => PiperTTS,
24
+ defaultConfigPath: () => defaultConfigPath,
25
+ readVoiceConfig: () => readVoiceConfig
26
+ });
27
+ module.exports = __toCommonJS(index_exports);
28
+
29
+ // src/config.ts
30
+ var import_promises = require("fs/promises");
31
+ var DEFAULT_SAMPLE_RATE = 22050;
32
+ async function readVoiceConfig(configPath) {
33
+ const raw = JSON.parse(await (0, import_promises.readFile)(configPath, "utf-8"));
34
+ return {
35
+ sampleRate: raw?.audio?.sample_rate ?? DEFAULT_SAMPLE_RATE,
36
+ speakerCount: raw?.num_speakers ?? 1,
37
+ language: raw?.language?.code
38
+ };
39
+ }
40
+ function defaultConfigPath(modelPath) {
41
+ return `${modelPath}.json`;
42
+ }
43
+
44
+ // src/PiperTTS.ts
45
+ var import_server = require("@micdrop/server");
46
+ var import_child_process = require("child_process");
47
+ var DEFAULT_BINARY = "piper";
48
+ var OUTPUT_SAMPLE_RATE = 16e3;
49
+ var PiperTTS = class extends import_server.TTS {
50
+ constructor(options) {
51
+ super();
52
+ this.options = options;
53
+ 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;
57
+ // Bumped by every speak() and every cancel(), so a call claimed late can tell
58
+ // whether it is still the one that should be heard
59
+ this.generation = 0;
60
+ this.counter = 0;
61
+ // Identifies the current speak() call
62
+ this.destroyed = false;
63
+ this.config = readVoiceConfig(
64
+ options.configPath ?? defaultConfigPath(options.modelPath)
65
+ );
66
+ this.config.catch((error) => {
67
+ console.error("[PiperTTS] Failed to read voice config:", error);
68
+ });
69
+ this.config.then(() => {
70
+ if (!this.child) this.spawnChild();
71
+ }).catch(() => {
72
+ });
73
+ }
74
+ speak(textStream) {
75
+ const generation = ++this.generation;
76
+ let counter = 0;
77
+ const claimCall = () => {
78
+ if (counter) return true;
79
+ if (this.generation !== generation) return false;
80
+ this.counter++;
81
+ counter = this.counter;
82
+ this.splitter.reset();
83
+ return true;
84
+ };
85
+ textStream.on("data", (chunk) => {
86
+ if (!claimCall()) return;
87
+ if (counter !== this.counter) return;
88
+ this.write(counter, this.splitter.push(chunk.toString("utf-8")));
89
+ });
90
+ textStream.on("error", (error) => {
91
+ this.log("Error in text stream", error);
92
+ });
93
+ textStream.on("end", () => {
94
+ if (!counter || counter !== this.counter) return;
95
+ this.write(counter, this.splitter.flush());
96
+ });
97
+ }
98
+ cancel() {
99
+ this.log("Cancel");
100
+ this.generation++;
101
+ this.counter++;
102
+ this.splitter.reset();
103
+ if (!this.dirty) return;
104
+ this.killChild();
105
+ this.spawnChild();
106
+ }
107
+ destroy() {
108
+ this.destroyed = true;
109
+ super.destroy();
110
+ this.killChild();
111
+ }
112
+ write(counter, sentences) {
113
+ if (sentences.length === 0) return;
114
+ if (counter !== this.counter) return;
115
+ const child = this.child ?? this.spawnChild();
116
+ if (!child) return;
117
+ for (const sentence of sentences) {
118
+ this.log(`Synthesizing: "${sentence}"`);
119
+ child.stdin.write(`${sentence.replace(/\s+/g, " ").trim()}
120
+ `);
121
+ this.dirty = true;
122
+ }
123
+ }
124
+ spawnChild() {
125
+ if (this.destroyed) return void 0;
126
+ if (this.child) return this.child;
127
+ const binary = this.options.binaryPath ?? DEFAULT_BINARY;
128
+ let child;
129
+ try {
130
+ child = (0, import_child_process.spawn)(binary, this.getArguments(), {
131
+ stdio: ["pipe", "pipe", "pipe"]
132
+ });
133
+ } catch (error) {
134
+ console.error(`[PiperTTS] Cannot run "${binary}":`, error);
135
+ return void 0;
136
+ }
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
+ );
142
+ let pending = Promise.resolve();
143
+ child.stdout.on("data", (chunk) => {
144
+ pending = pending.then(async () => {
145
+ const output = (await resampler).process(chunk);
146
+ if (this.child !== child || output.length === 0) return;
147
+ this.emit("Audio", output);
148
+ });
149
+ });
150
+ const errors = [];
151
+ child.stderr.on("data", (chunk) => errors.push(chunk.toString()));
152
+ child.on("error", (error) => {
153
+ if (this.child !== child) return;
154
+ console.error(`[PiperTTS] Cannot run "${binary}":`, error.message);
155
+ this.child = void 0;
156
+ });
157
+ child.on("exit", (code) => {
158
+ if (this.child !== child) return;
159
+ this.child = void 0;
160
+ if (code === 0 || this.destroyed) return;
161
+ console.error(
162
+ `[PiperTTS] Piper exited with code ${code}`,
163
+ errors.join("")
164
+ );
165
+ this.emit("Failed", []);
166
+ });
167
+ return child;
168
+ }
169
+ 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");
176
+ }
177
+ getArguments() {
178
+ const { options } = this;
179
+ const args = ["--model", options.modelPath, "--output_raw"];
180
+ if (options.configPath) args.push("--config", options.configPath);
181
+ if (options.speaker !== void 0) {
182
+ args.push("--speaker", String(options.speaker));
183
+ }
184
+ if (options.lengthScale !== void 0) {
185
+ args.push("--length_scale", String(options.lengthScale));
186
+ }
187
+ if (options.noiseScale !== void 0) {
188
+ args.push("--noise_scale", String(options.noiseScale));
189
+ }
190
+ if (options.noiseWidth !== void 0) {
191
+ args.push("--noise_w", String(options.noiseWidth));
192
+ }
193
+ if (options.sentenceSilence !== void 0) {
194
+ args.push("--sentence_silence", String(options.sentenceSilence));
195
+ }
196
+ if (options.volume !== void 0) {
197
+ args.push("--volume", String(options.volume));
198
+ }
199
+ return args;
200
+ }
201
+ };
202
+ // Annotate the CommonJS export names for ESM import in node:
203
+ 0 && (module.exports = {
204
+ PiperTTS,
205
+ defaultConfigPath,
206
+ readVoiceConfig
207
+ });
208
+ //# sourceMappingURL=index.js.map
@@ -0,0 +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.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;AAGO,SAAS,kBAAkB,WAA2B;AAC3D,SAAO,GAAG,SAAS;AACrB;;;AChCA,oBAAsD;AACtD,2BAAsD;AAsDtD,IAAM,iBAAiB;AACvB,IAAM,qBAAqB;AAEpB,IAAM,WAAN,cAAuB,kBAAI;AAAA,EAahC,YAA6B,SAA0B;AACrD,UAAM;AADqB;AAX7B,SAAQ,WAAW,IAAI,+BAAiB;AAIxC;AAA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Q,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":[]}
package/dist/index.mjs ADDED
@@ -0,0 +1,179 @@
1
+ // src/config.ts
2
+ import { readFile } from "fs/promises";
3
+ var DEFAULT_SAMPLE_RATE = 22050;
4
+ async function readVoiceConfig(configPath) {
5
+ const raw = JSON.parse(await readFile(configPath, "utf-8"));
6
+ return {
7
+ sampleRate: raw?.audio?.sample_rate ?? DEFAULT_SAMPLE_RATE,
8
+ speakerCount: raw?.num_speakers ?? 1,
9
+ language: raw?.language?.code
10
+ };
11
+ }
12
+ function defaultConfigPath(modelPath) {
13
+ return `${modelPath}.json`;
14
+ }
15
+
16
+ // src/PiperTTS.ts
17
+ import { Pcm16Resampler, SentenceSplitter, TTS } from "@micdrop/server";
18
+ import { spawn } from "child_process";
19
+ var DEFAULT_BINARY = "piper";
20
+ var OUTPUT_SAMPLE_RATE = 16e3;
21
+ var PiperTTS = class extends TTS {
22
+ constructor(options) {
23
+ super();
24
+ this.options = options;
25
+ 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;
29
+ // Bumped by every speak() and every cancel(), so a call claimed late can tell
30
+ // whether it is still the one that should be heard
31
+ this.generation = 0;
32
+ this.counter = 0;
33
+ // Identifies the current speak() call
34
+ this.destroyed = false;
35
+ this.config = readVoiceConfig(
36
+ options.configPath ?? defaultConfigPath(options.modelPath)
37
+ );
38
+ this.config.catch((error) => {
39
+ console.error("[PiperTTS] Failed to read voice config:", error);
40
+ });
41
+ this.config.then(() => {
42
+ if (!this.child) this.spawnChild();
43
+ }).catch(() => {
44
+ });
45
+ }
46
+ speak(textStream) {
47
+ const generation = ++this.generation;
48
+ let counter = 0;
49
+ const claimCall = () => {
50
+ if (counter) return true;
51
+ if (this.generation !== generation) return false;
52
+ this.counter++;
53
+ counter = this.counter;
54
+ this.splitter.reset();
55
+ return true;
56
+ };
57
+ textStream.on("data", (chunk) => {
58
+ if (!claimCall()) return;
59
+ if (counter !== this.counter) return;
60
+ this.write(counter, this.splitter.push(chunk.toString("utf-8")));
61
+ });
62
+ textStream.on("error", (error) => {
63
+ this.log("Error in text stream", error);
64
+ });
65
+ textStream.on("end", () => {
66
+ if (!counter || counter !== this.counter) return;
67
+ this.write(counter, this.splitter.flush());
68
+ });
69
+ }
70
+ cancel() {
71
+ this.log("Cancel");
72
+ this.generation++;
73
+ this.counter++;
74
+ this.splitter.reset();
75
+ if (!this.dirty) return;
76
+ this.killChild();
77
+ this.spawnChild();
78
+ }
79
+ destroy() {
80
+ this.destroyed = true;
81
+ super.destroy();
82
+ this.killChild();
83
+ }
84
+ write(counter, sentences) {
85
+ if (sentences.length === 0) return;
86
+ if (counter !== this.counter) return;
87
+ const child = this.child ?? this.spawnChild();
88
+ if (!child) return;
89
+ for (const sentence of sentences) {
90
+ this.log(`Synthesizing: "${sentence}"`);
91
+ child.stdin.write(`${sentence.replace(/\s+/g, " ").trim()}
92
+ `);
93
+ this.dirty = true;
94
+ }
95
+ }
96
+ spawnChild() {
97
+ if (this.destroyed) return void 0;
98
+ if (this.child) return this.child;
99
+ const binary = this.options.binaryPath ?? DEFAULT_BINARY;
100
+ let child;
101
+ try {
102
+ child = spawn(binary, this.getArguments(), {
103
+ stdio: ["pipe", "pipe", "pipe"]
104
+ });
105
+ } catch (error) {
106
+ console.error(`[PiperTTS] Cannot run "${binary}":`, error);
107
+ return void 0;
108
+ }
109
+ this.child = child;
110
+ this.dirty = false;
111
+ const resampler = this.config.then(
112
+ ({ sampleRate }) => new Pcm16Resampler(sampleRate, OUTPUT_SAMPLE_RATE)
113
+ );
114
+ let pending = Promise.resolve();
115
+ child.stdout.on("data", (chunk) => {
116
+ pending = pending.then(async () => {
117
+ const output = (await resampler).process(chunk);
118
+ if (this.child !== child || output.length === 0) return;
119
+ this.emit("Audio", output);
120
+ });
121
+ });
122
+ const errors = [];
123
+ child.stderr.on("data", (chunk) => errors.push(chunk.toString()));
124
+ child.on("error", (error) => {
125
+ if (this.child !== child) return;
126
+ console.error(`[PiperTTS] Cannot run "${binary}":`, error.message);
127
+ this.child = void 0;
128
+ });
129
+ child.on("exit", (code) => {
130
+ if (this.child !== child) return;
131
+ this.child = void 0;
132
+ if (code === 0 || this.destroyed) return;
133
+ console.error(
134
+ `[PiperTTS] Piper exited with code ${code}`,
135
+ errors.join("")
136
+ );
137
+ this.emit("Failed", []);
138
+ });
139
+ return child;
140
+ }
141
+ 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");
148
+ }
149
+ getArguments() {
150
+ const { options } = this;
151
+ const args = ["--model", options.modelPath, "--output_raw"];
152
+ if (options.configPath) args.push("--config", options.configPath);
153
+ if (options.speaker !== void 0) {
154
+ args.push("--speaker", String(options.speaker));
155
+ }
156
+ if (options.lengthScale !== void 0) {
157
+ args.push("--length_scale", String(options.lengthScale));
158
+ }
159
+ if (options.noiseScale !== void 0) {
160
+ args.push("--noise_scale", String(options.noiseScale));
161
+ }
162
+ if (options.noiseWidth !== void 0) {
163
+ args.push("--noise_w", String(options.noiseWidth));
164
+ }
165
+ if (options.sentenceSilence !== void 0) {
166
+ args.push("--sentence_silence", String(options.sentenceSilence));
167
+ }
168
+ if (options.volume !== void 0) {
169
+ args.push("--volume", String(options.volume));
170
+ }
171
+ return args;
172
+ }
173
+ };
174
+ export {
175
+ PiperTTS,
176
+ defaultConfigPath,
177
+ readVoiceConfig
178
+ };
179
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +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;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O,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":[]}
package/package.json ADDED
@@ -0,0 +1,45 @@
1
+ {
2
+ "name": "@micdrop/piper",
3
+ "version": "1.0.0",
4
+ "description": "Local Piper text to speech for @micdrop/server",
5
+ "author": "Godefroy de Compreignac",
6
+ "license": "MIT",
7
+ "main": "./dist/index.js",
8
+ "module": "./dist/index.mjs",
9
+ "types": "./dist/index.d.ts",
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/Godefroy/micdrop.git",
16
+ "directory": "packages/piper"
17
+ },
18
+ "publishConfig": {
19
+ "access": "public"
20
+ },
21
+ "keywords": [
22
+ "microphone",
23
+ "audio",
24
+ "voice",
25
+ "conversation",
26
+ "piper",
27
+ "local",
28
+ "offline",
29
+ "tts"
30
+ ],
31
+ "peerDependencies": {
32
+ "@micdrop/server": ">=2.3.0"
33
+ },
34
+ "devDependencies": {
35
+ "@types/node": "^22.19.13",
36
+ "tsup": "^8.5.1",
37
+ "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
+ }