@micdrop/piper 1.0.0 → 1.0.1

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