pi-reply-reader 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +57 -0
- package/LICENSE +21 -0
- package/README.md +347 -2
- package/dist/bin/reply-reader.js +49 -0
- package/dist/src/cli.js +202 -0
- package/dist/src/outputs.js +61 -0
- package/dist/src/playback.js +245 -0
- package/dist/src/reading-lock.js +143 -0
- package/dist/src/settings.js +97 -0
- package/dist/src/speech-text.js +511 -0
- package/dist/src/voice.js +173 -0
- package/examples/reply-reader-desktop-audio.service +44 -0
- package/extensions/reply-reader.ts +464 -0
- package/package.json +70 -4
- package/src/cli.ts +225 -0
- package/src/outputs.ts +73 -0
- package/src/playback.ts +292 -0
- package/src/questions.ts +19 -0
- package/src/reader-events.ts +44 -0
- package/src/reading-lock.ts +167 -0
- package/src/replies.ts +64 -0
- package/src/sections.ts +47 -0
- package/src/settings.ts +94 -0
- package/src/sherpa-onnx-node.d.ts +29 -0
- package/src/speech-text.ts +579 -0
- package/src/voice.ts +195 -0
package/src/voice.ts
ADDED
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { accessSync, constants, createWriteStream, existsSync, mkdirSync, mkdtempSync, renameSync, rmSync, statSync } from "node:fs";
|
|
4
|
+
import { delimiter, isAbsolute, join } from "node:path";
|
|
5
|
+
import { Readable } from "node:stream";
|
|
6
|
+
import { pipeline } from "node:stream/promises";
|
|
7
|
+
import type { ReadableStream as NodeReadableStream } from "node:stream/web";
|
|
8
|
+
import { promisify } from "node:util";
|
|
9
|
+
|
|
10
|
+
/** The local engine (S3): Supertonic 3, int8, as sherpa-onnx packages it for its v2 Node binding. */
|
|
11
|
+
const VOICE = "Supertonic 3";
|
|
12
|
+
const BUNDLE = "sherpa-onnx-supertonic-3-tts-int8-2026-05-11";
|
|
13
|
+
/** sherpa-onnx's supertonic config key → file in the bundle. */
|
|
14
|
+
const MODEL_FILES = {
|
|
15
|
+
durationPredictor: "duration_predictor.int8.onnx",
|
|
16
|
+
textEncoder: "text_encoder.int8.onnx",
|
|
17
|
+
vectorEstimator: "vector_estimator.int8.onnx",
|
|
18
|
+
vocoder: "vocoder.int8.onnx",
|
|
19
|
+
ttsJson: "tts.json",
|
|
20
|
+
unicodeIndexer: "unicode_indexer.bin",
|
|
21
|
+
voiceStyle: "voice.bin",
|
|
22
|
+
} as const;
|
|
23
|
+
const BUNDLE_FILES = Object.values(MODEL_FILES);
|
|
24
|
+
|
|
25
|
+
export interface VoiceArchive {
|
|
26
|
+
url: string;
|
|
27
|
+
bytes: number;
|
|
28
|
+
sha256: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** sherpa-onnx's Supertonic 3 bundle; size and digest as published by GitHub for the release asset. */
|
|
32
|
+
export const VOICE_ARCHIVE: VoiceArchive = {
|
|
33
|
+
url: `https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/${BUNDLE}.tar.bz2`,
|
|
34
|
+
bytes: 128774318,
|
|
35
|
+
sha256: "82fa96f91c4ef8abaae3a14a3f4153facf88bed821d1f7331cec2700f432c427",
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
/** The model files, shaped as sherpa-onnx's `model.supertonic` config. */
|
|
39
|
+
export interface Voice {
|
|
40
|
+
supertonic: Record<keyof typeof MODEL_FILES, string>;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface Speaker {
|
|
44
|
+
/** Speaker ID inside the Supertonic model. */
|
|
45
|
+
sid: number;
|
|
46
|
+
/** Linear gain applied to every sample of a reading. */
|
|
47
|
+
gain: number;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* The two voices the model ships (S3, L20): male is the default, female is selectable. Each gain
|
|
52
|
+
* lifts the voice's speech to about -16 dBFS (L22). Measured on the hs bake-off WAVs, speech RMS was
|
|
53
|
+
* -22.7 dBFS for sid 6 and -23.5 dBFS for sid 0, with peaks at -2.8 and -4.8 dBFS, so the lifted
|
|
54
|
+
* peaks go past full scale and playback limits them (playback.ts).
|
|
55
|
+
*/
|
|
56
|
+
export const SPEAKERS = {
|
|
57
|
+
male: { sid: 6, gain: 10 ** ((-16 - -22.7) / 20) },
|
|
58
|
+
female: { sid: 0, gain: 10 ** ((-16 - -23.5) / 20) },
|
|
59
|
+
} as const satisfies Record<string, Speaker>;
|
|
60
|
+
|
|
61
|
+
export type SpeakerName = keyof typeof SPEAKERS;
|
|
62
|
+
|
|
63
|
+
/** The voice this package downloaded, when every model file is in place. */
|
|
64
|
+
export function findVoice(env: NodeJS.ProcessEnv): Voice | undefined {
|
|
65
|
+
const data = dataHome(env);
|
|
66
|
+
if (!data) return undefined;
|
|
67
|
+
const dir = join(data, "pi-reply-reader", "voices", BUNDLE);
|
|
68
|
+
if (!BUNDLE_FILES.every((name) => existsSync(join(dir, name)))) return undefined;
|
|
69
|
+
const files = Object.entries(MODEL_FILES).map(([key, name]) => [key, join(dir, name)]);
|
|
70
|
+
return { supertonic: Object.fromEntries(files) as Voice["supertonic"] };
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Downloads the pinned voice after `confirm` agrees. The archive is checked for size and sha256
|
|
75
|
+
* before extraction, extracted beside its destination and moved into place whole, so a failure
|
|
76
|
+
* leaves nothing behind and a half-written voice is never found.
|
|
77
|
+
*/
|
|
78
|
+
export async function downloadVoice(options: {
|
|
79
|
+
env: NodeJS.ProcessEnv;
|
|
80
|
+
confirm: (message: string) => Promise<boolean>;
|
|
81
|
+
archive?: VoiceArchive;
|
|
82
|
+
/** Give up when the server sends nothing for this long. */
|
|
83
|
+
idleTimeoutMs?: number;
|
|
84
|
+
/** Cancels the download (Ctrl+C in the reply-reader command); it then fails with "cancelled" and keeps nothing. */
|
|
85
|
+
signal?: AbortSignal;
|
|
86
|
+
}): Promise<Voice | undefined> {
|
|
87
|
+
const { env, confirm, archive = VOICE_ARCHIVE, idleTimeoutMs = 30_000, signal } = options;
|
|
88
|
+
const existing = findVoice(env);
|
|
89
|
+
if (existing) return existing;
|
|
90
|
+
|
|
91
|
+
const data = dataHome(env);
|
|
92
|
+
if (!data) {
|
|
93
|
+
throw new Error("Voice download failed: neither XDG_DATA_HOME nor HOME is an absolute path. Nothing was kept.");
|
|
94
|
+
}
|
|
95
|
+
const dir = join(data, "pi-reply-reader", "voices");
|
|
96
|
+
const megabytes = Math.round(archive.bytes / 1_000_000);
|
|
97
|
+
if (!(await confirm(`Download the Spanish voice ${VOICE} (${megabytes} MB) from ${new URL(archive.url).hostname} into ${dir}?`))) {
|
|
98
|
+
return undefined;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
mkdirSync(dir, { recursive: true });
|
|
102
|
+
const work = mkdtempSync(join(dir, ".download-"));
|
|
103
|
+
try {
|
|
104
|
+
const file = join(work, "voice.tar.bz2");
|
|
105
|
+
await fetchVerified(archive, file, idleTimeoutMs, signal);
|
|
106
|
+
await promisify(execFile)("tar", ["-xjf", file, "--no-same-owner", "-C", work]);
|
|
107
|
+
for (const name of BUNDLE_FILES) {
|
|
108
|
+
if (!existsSync(join(work, BUNDLE, name))) throw new Error(`the archive has no ${BUNDLE}/${name}`);
|
|
109
|
+
}
|
|
110
|
+
// An incomplete voice in the way (findVoice already said there is none) goes out with the work dir.
|
|
111
|
+
if (existsSync(join(dir, BUNDLE))) renameSync(join(dir, BUNDLE), join(work, "incomplete"));
|
|
112
|
+
try {
|
|
113
|
+
renameSync(join(work, BUNDLE), join(dir, BUNDLE));
|
|
114
|
+
} catch (error) {
|
|
115
|
+
// Another session finished the same download first; its copy is as good as ours.
|
|
116
|
+
if (!findVoice(env)) throw error;
|
|
117
|
+
}
|
|
118
|
+
return findVoice(env);
|
|
119
|
+
} catch (error) {
|
|
120
|
+
throw new Error(`Voice download failed: ${(error as Error).message}. Nothing was kept.`);
|
|
121
|
+
} finally {
|
|
122
|
+
rmSync(work, { recursive: true, force: true });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Streams the archive to `file`, failing on HTTP errors, silence, oversize and disk errors alike. */
|
|
127
|
+
async function fetchVerified(archive: VoiceArchive, file: string, idleTimeoutMs: number, cancel?: AbortSignal): Promise<void> {
|
|
128
|
+
const idle = new AbortController();
|
|
129
|
+
const signal = cancel ? AbortSignal.any([idle.signal, cancel]) : idle.signal;
|
|
130
|
+
let timer: NodeJS.Timeout | undefined;
|
|
131
|
+
const wait = () => {
|
|
132
|
+
clearTimeout(timer);
|
|
133
|
+
timer = setTimeout(() => idle.abort(new Error(`no data for ${idleTimeoutMs / 1000} s from ${archive.url}`)), idleTimeoutMs);
|
|
134
|
+
};
|
|
135
|
+
const hash = createHash("sha256");
|
|
136
|
+
let received = 0;
|
|
137
|
+
try {
|
|
138
|
+
wait();
|
|
139
|
+
const response = await fetch(archive.url, { signal });
|
|
140
|
+
if (!response.ok || !response.body) throw new Error(`HTTP ${response.status} from ${archive.url}`);
|
|
141
|
+
await pipeline(
|
|
142
|
+
// fetch's body is typed with the DOM stream; at runtime it is Node's web stream.
|
|
143
|
+
Readable.fromWeb(response.body as NodeReadableStream<Uint8Array>),
|
|
144
|
+
async function* (source: AsyncIterable<Buffer>) {
|
|
145
|
+
for await (const chunk of source) {
|
|
146
|
+
wait();
|
|
147
|
+
received += chunk.length;
|
|
148
|
+
if (received > archive.bytes) throw new Error(`expected ${archive.bytes} bytes, got more`);
|
|
149
|
+
hash.update(chunk);
|
|
150
|
+
yield chunk;
|
|
151
|
+
}
|
|
152
|
+
},
|
|
153
|
+
createWriteStream(file),
|
|
154
|
+
{ signal },
|
|
155
|
+
);
|
|
156
|
+
} catch (error) {
|
|
157
|
+
if (idle.signal.aborted) throw idle.signal.reason as Error;
|
|
158
|
+
throw cancel?.aborted ? new Error("cancelled") : error;
|
|
159
|
+
} finally {
|
|
160
|
+
clearTimeout(timer);
|
|
161
|
+
}
|
|
162
|
+
if (received !== archive.bytes) throw new Error(`expected ${archive.bytes} bytes, got ${received}`);
|
|
163
|
+
const actual = hash.digest("hex");
|
|
164
|
+
if (actual !== archive.sha256) throw new Error(`sha256 mismatch (expected ${archive.sha256}, got ${actual})`);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** XDG_DATA_HOME, then ~/.local/share; relative values are ignored, as the XDG spec says. */
|
|
168
|
+
function dataHome(env: NodeJS.ProcessEnv): string | undefined {
|
|
169
|
+
if (env.XDG_DATA_HOME && isAbsolute(env.XDG_DATA_HOME)) return env.XDG_DATA_HOME;
|
|
170
|
+
if (env.HOME && isAbsolute(env.HOME)) return join(env.HOME, ".local", "share");
|
|
171
|
+
return undefined;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** An executable `name` in an absolute PATH entry. */
|
|
175
|
+
export function onPath(name: string, env: NodeJS.ProcessEnv): string | undefined {
|
|
176
|
+
for (const dir of (env.PATH ?? "").split(delimiter)) {
|
|
177
|
+
if (!isAbsolute(dir)) continue;
|
|
178
|
+
const candidate = join(dir, name);
|
|
179
|
+
try {
|
|
180
|
+
accessSync(candidate, constants.X_OK);
|
|
181
|
+
if (isFile(candidate)) return candidate;
|
|
182
|
+
} catch {
|
|
183
|
+
// not here, keep looking
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
function isFile(path: string): boolean {
|
|
190
|
+
try {
|
|
191
|
+
return statSync(path).isFile();
|
|
192
|
+
} catch {
|
|
193
|
+
return false;
|
|
194
|
+
}
|
|
195
|
+
}
|