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/package.json
CHANGED
|
@@ -1,6 +1,72 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-reply-reader",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Read a Pi or Gentle Shell reply aloud on request, with local text-to-speech and Markdown cleaned up for listening.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Daniel Li",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/daniel2010001/pi-reply-reader.git"
|
|
11
|
+
},
|
|
12
|
+
"homepage": "https://github.com/daniel2010001/pi-reply-reader#readme",
|
|
13
|
+
"bugs": {
|
|
14
|
+
"url": "https://github.com/daniel2010001/pi-reply-reader/issues"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"pi-package",
|
|
18
|
+
"pi-extension",
|
|
19
|
+
"pi-coding-agent",
|
|
20
|
+
"gentle-shell",
|
|
21
|
+
"tts"
|
|
22
|
+
],
|
|
23
|
+
"pi": {
|
|
24
|
+
"extensions": [
|
|
25
|
+
"./extensions/reply-reader.ts"
|
|
26
|
+
]
|
|
27
|
+
},
|
|
28
|
+
"bin": {
|
|
29
|
+
"reply-reader": "./dist/bin/reply-reader.js"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist",
|
|
33
|
+
"extensions",
|
|
34
|
+
"src",
|
|
35
|
+
"examples",
|
|
36
|
+
"README.md",
|
|
37
|
+
"CHANGELOG.md",
|
|
38
|
+
"LICENSE"
|
|
39
|
+
],
|
|
40
|
+
"engines": {
|
|
41
|
+
"node": ">=22.19.0"
|
|
42
|
+
},
|
|
43
|
+
"scripts": {
|
|
44
|
+
"build": "tsc -p tsconfig.build.json",
|
|
45
|
+
"prepack": "npm run build",
|
|
46
|
+
"test": "vitest --run",
|
|
47
|
+
"typecheck": "tsc --noEmit"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"@earendil-works/pi-coding-agent": ">=1.2.0 <2",
|
|
51
|
+
"@earendil-works/pi-tui": ">=1.2.0 <2"
|
|
52
|
+
},
|
|
53
|
+
"peerDependenciesMeta": {
|
|
54
|
+
"@earendil-works/pi-coding-agent": {
|
|
55
|
+
"optional": true
|
|
56
|
+
},
|
|
57
|
+
"@earendil-works/pi-tui": {
|
|
58
|
+
"optional": true
|
|
59
|
+
}
|
|
60
|
+
},
|
|
61
|
+
"devDependencies": {
|
|
62
|
+
"@earendil-works/pi-ai": "1.4.0",
|
|
63
|
+
"@earendil-works/pi-coding-agent": "1.4.0",
|
|
64
|
+
"@earendil-works/pi-tui": "1.4.0",
|
|
65
|
+
"@types/node": "^26.6.5",
|
|
66
|
+
"typescript": "^6.0.2",
|
|
67
|
+
"vitest": "^5.0.5"
|
|
68
|
+
},
|
|
69
|
+
"dependencies": {
|
|
70
|
+
"sherpa-onnx-node": "2.0.1"
|
|
71
|
+
}
|
|
72
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { hostname as osHostname } from "node:os";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { loadOutputs } from "./outputs.ts";
|
|
5
|
+
import { type SherpaTts, startReading } from "./playback.ts";
|
|
6
|
+
import { openReadingLock } from "./reading-lock.ts";
|
|
7
|
+
import { loadSpeed, parseSpeed, SPEED_HINT } from "./settings.ts";
|
|
8
|
+
import { speechText } from "./speech-text.ts";
|
|
9
|
+
import { downloadVoice, findVoice, SPEAKERS, type SpeakerName, type Voice, VOICE_ARCHIVE, type VoiceArchive } from "./voice.ts";
|
|
10
|
+
|
|
11
|
+
/** What `reply-reader --help` prints (S6). */
|
|
12
|
+
export const USAGE = `Usage: reply-reader [options] <file.md | ->
|
|
13
|
+
|
|
14
|
+
Reads a Markdown file aloud with local text-to-speech; "-" reads standard input.
|
|
15
|
+
|
|
16
|
+
Options:
|
|
17
|
+
--output <name> where it sounds: local (default) or a name from outputs.json
|
|
18
|
+
--speed <n> voice speed from 0.5 to 2.0 for this run (default: the saved speed, else 1.1)
|
|
19
|
+
--voice male|female the voice (default: male)
|
|
20
|
+
-h, --help show this help
|
|
21
|
+
|
|
22
|
+
Ctrl+C stops the reading.
|
|
23
|
+
Exit codes: 0 read (or nothing to read), 1 cannot read, download or play, 2 bad arguments, 130 stopped with Ctrl+C.
|
|
24
|
+
`;
|
|
25
|
+
|
|
26
|
+
/** Exit codes of the command. */
|
|
27
|
+
const OK = 0;
|
|
28
|
+
const FAILED = 1;
|
|
29
|
+
const USAGE_ERROR = 2;
|
|
30
|
+
const INTERRUPTED = 130;
|
|
31
|
+
|
|
32
|
+
/** How often a reading checks whether a newer one, or /read-stop, in another session took over (as /read does). */
|
|
33
|
+
const WATCH_MS = 250;
|
|
34
|
+
const SPEAKER_NAMES = Object.keys(SPEAKERS) as SpeakerName[];
|
|
35
|
+
|
|
36
|
+
export interface CliDeps {
|
|
37
|
+
env: NodeJS.ProcessEnv;
|
|
38
|
+
/** Where a relative file path starts. */
|
|
39
|
+
cwd?: string;
|
|
40
|
+
hostname?: string;
|
|
41
|
+
stdin: AsyncIterable<Buffer | string>;
|
|
42
|
+
stdout: { write(text: string): unknown };
|
|
43
|
+
stderr: { write(text: string): unknown };
|
|
44
|
+
/** Whether a person can answer the download confirm (standard input and error are a terminal). */
|
|
45
|
+
isTTY: boolean;
|
|
46
|
+
/** Asks the y/N download question; only called when `isTTY`. */
|
|
47
|
+
confirm: (message: string) => Promise<boolean>;
|
|
48
|
+
loadSherpa?: (voice: Voice) => Promise<SherpaTts>;
|
|
49
|
+
/** Aborted on Ctrl+C: the reading or download stops and the command exits 130. */
|
|
50
|
+
signal?: AbortSignal;
|
|
51
|
+
/** The voice archive to download; the pinned one unless a test serves its own. */
|
|
52
|
+
archive?: VoiceArchive;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface Options {
|
|
56
|
+
file: string;
|
|
57
|
+
output: string;
|
|
58
|
+
speed?: string;
|
|
59
|
+
voice: string;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Why a Markdown file could not be read, in the user's words (the same reasons as `/read <file.md>`). */
|
|
63
|
+
export function readProblem(error: NodeJS.ErrnoException): string {
|
|
64
|
+
if (error.code === "ENOENT") return "file not found";
|
|
65
|
+
if (error.code === "EISDIR") return "it is a folder";
|
|
66
|
+
if (error.code === "EACCES" || error.code === "EPERM") return "permission denied";
|
|
67
|
+
return error.message;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** The options, `"help"`, or the problem with the arguments. */
|
|
71
|
+
function parse(argv: string[]): Options | "help" | { problem: string } {
|
|
72
|
+
const files: string[] = [];
|
|
73
|
+
const values: Record<"output" | "speed" | "voice", string | undefined> = { output: undefined, speed: undefined, voice: undefined };
|
|
74
|
+
let rest = false;
|
|
75
|
+
for (let i = 0; i < argv.length; i++) {
|
|
76
|
+
const arg = argv[i];
|
|
77
|
+
if (rest || arg === "-" || !arg.startsWith("-")) {
|
|
78
|
+
files.push(arg);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
if (arg === "--") {
|
|
82
|
+
rest = true;
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (arg === "-h" || arg === "--help") return "help";
|
|
86
|
+
const name = arg.slice(2);
|
|
87
|
+
if (arg !== `--${name}` || !Object.hasOwn(values, name)) return { problem: `unknown option "${arg}"` };
|
|
88
|
+
if (i + 1 >= argv.length) return { problem: `${arg} needs a value` };
|
|
89
|
+
values[name as keyof typeof values] = argv[++i];
|
|
90
|
+
}
|
|
91
|
+
if (files.length === 0) return { problem: "no file given" };
|
|
92
|
+
if (files.length > 1) return { problem: "only one file can be read" };
|
|
93
|
+
return { file: files[0], output: values.output ?? "local", speed: values.speed, voice: values.voice ?? "male" };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* `reply-reader <file.md>` (S6): reads a Markdown file, or standard input with "-", aloud from a terminal without Pi,
|
|
98
|
+
* through the same cleanup (S2, S7), voice and download confirm, saved speed (S3), named outputs (S4) and one reading
|
|
99
|
+
* per machine (S1) as `/read`. Resolves with the exit code; nothing throws.
|
|
100
|
+
*/
|
|
101
|
+
export async function main(argv: string[], deps: CliDeps): Promise<number> {
|
|
102
|
+
const { env, stdout, stderr, signal } = deps;
|
|
103
|
+
const say = (message: string): void => void stderr.write(`${message}\n`);
|
|
104
|
+
|
|
105
|
+
const parsed = parse(argv);
|
|
106
|
+
if (parsed === "help") {
|
|
107
|
+
stdout.write(USAGE);
|
|
108
|
+
return OK;
|
|
109
|
+
}
|
|
110
|
+
if ("problem" in parsed) {
|
|
111
|
+
stderr.write(`reply-reader: ${parsed.problem}.\n\n${USAGE}`);
|
|
112
|
+
return USAGE_ERROR;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
let speed: number | undefined;
|
|
116
|
+
if (parsed.speed !== undefined) {
|
|
117
|
+
speed = parseSpeed(parsed.speed);
|
|
118
|
+
if (speed === undefined) {
|
|
119
|
+
say(SPEED_HINT);
|
|
120
|
+
return USAGE_ERROR;
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
if (!SPEAKER_NAMES.includes(parsed.voice as SpeakerName)) {
|
|
124
|
+
say(`No voice named "${parsed.voice}". Voices: ${SPEAKER_NAMES.join(", ")}.`);
|
|
125
|
+
return USAGE_ERROR;
|
|
126
|
+
}
|
|
127
|
+
const speaker = SPEAKERS[parsed.voice as SpeakerName];
|
|
128
|
+
const { outputs, problems } = loadOutputs(env, deps.hostname ?? osHostname());
|
|
129
|
+
const output = outputs.find((candidate) => candidate.name === parsed.output);
|
|
130
|
+
if (!output) {
|
|
131
|
+
say(`No output named "${parsed.output}". Outputs: ${outputs.map((o) => o.name).join(", ")}.`);
|
|
132
|
+
return USAGE_ERROR;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const fromStdin = parsed.file === "-";
|
|
136
|
+
let markdown: string;
|
|
137
|
+
try {
|
|
138
|
+
markdown = fromStdin ? await readAll(deps.stdin) : readFileSync(resolve(deps.cwd ?? process.cwd(), parsed.file), "utf8");
|
|
139
|
+
} catch (error) {
|
|
140
|
+
say(`Cannot read "${parsed.file}": ${readProblem(error as NodeJS.ErrnoException)}.`);
|
|
141
|
+
return FAILED;
|
|
142
|
+
}
|
|
143
|
+
const text = speechText(markdown);
|
|
144
|
+
if (!text) {
|
|
145
|
+
say(`${fromStdin ? "Standard input" : parsed.file} has nothing to read.`);
|
|
146
|
+
return OK;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// A typo in outputs.json must not silently send the reading elsewhere; it is reported as /read does.
|
|
150
|
+
for (const problem of problems) say(problem);
|
|
151
|
+
|
|
152
|
+
let voice = findVoice(env);
|
|
153
|
+
if (!voice) {
|
|
154
|
+
// Standard input is the text, so it cannot also answer the question.
|
|
155
|
+
if (!deps.isTTY || fromStdin) {
|
|
156
|
+
const megabytes = Math.round((deps.archive ?? VOICE_ARCHIVE).bytes / 1_000_000);
|
|
157
|
+
say(
|
|
158
|
+
`The voice is not installed and there is no terminal to confirm its download (${megabytes} MB). Run reply-reader from a terminal once to download it.`,
|
|
159
|
+
);
|
|
160
|
+
return FAILED;
|
|
161
|
+
}
|
|
162
|
+
try {
|
|
163
|
+
voice = await downloadVoice({ env, confirm: deps.confirm, archive: deps.archive, signal });
|
|
164
|
+
} catch (error) {
|
|
165
|
+
if (signal?.aborted) return INTERRUPTED;
|
|
166
|
+
say((error as Error).message);
|
|
167
|
+
return FAILED;
|
|
168
|
+
}
|
|
169
|
+
if (signal?.aborted) return INTERRUPTED;
|
|
170
|
+
if (!voice) {
|
|
171
|
+
say("Reading cancelled: the voice was not downloaded.");
|
|
172
|
+
return FAILED;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
if (speed === undefined) {
|
|
177
|
+
const saved = loadSpeed(env);
|
|
178
|
+
if (saved.problem) say(saved.problem);
|
|
179
|
+
speed = saved.speed;
|
|
180
|
+
}
|
|
181
|
+
if (signal?.aborted) return INTERRUPTED;
|
|
182
|
+
|
|
183
|
+
// Without a usable XDG_RUNTIME_DIR there is no claim, and only Ctrl+C stops this reading.
|
|
184
|
+
const claim = openReadingLock(env)?.claim();
|
|
185
|
+
let interrupted = false;
|
|
186
|
+
const reading = startReading({
|
|
187
|
+
voice,
|
|
188
|
+
speaker,
|
|
189
|
+
text,
|
|
190
|
+
output,
|
|
191
|
+
env,
|
|
192
|
+
loadSherpa: deps.loadSherpa,
|
|
193
|
+
speed,
|
|
194
|
+
onPlayer: (pid) => {
|
|
195
|
+
if (claim && !claim.setPlayer(pid)) reading.stop();
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
const interrupt = (): void => {
|
|
199
|
+
interrupted = true;
|
|
200
|
+
reading.stop();
|
|
201
|
+
};
|
|
202
|
+
signal?.addEventListener("abort", interrupt, { once: true });
|
|
203
|
+
// Stopped by a newer reading or /read-stop elsewhere: quietly, as a Pi session is.
|
|
204
|
+
const watch = claim && setInterval(() => claim.lost() && reading.stop(), WATCH_MS);
|
|
205
|
+
try {
|
|
206
|
+
await reading.done;
|
|
207
|
+
} catch (error) {
|
|
208
|
+
// A player killed by a newer reading elsewhere fails; that is not an error here.
|
|
209
|
+
if (!interrupted && !claim?.lost()) {
|
|
210
|
+
say((error as Error).message);
|
|
211
|
+
return FAILED;
|
|
212
|
+
}
|
|
213
|
+
} finally {
|
|
214
|
+
clearInterval(watch || undefined);
|
|
215
|
+
signal?.removeEventListener("abort", interrupt);
|
|
216
|
+
claim?.release();
|
|
217
|
+
}
|
|
218
|
+
return interrupted ? INTERRUPTED : OK;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
async function readAll(input: AsyncIterable<Buffer | string>): Promise<string> {
|
|
222
|
+
const chunks: Buffer[] = [];
|
|
223
|
+
for await (const chunk of input) chunks.push(typeof chunk === "string" ? Buffer.from(chunk) : chunk);
|
|
224
|
+
return Buffer.concat(chunks).toString("utf8");
|
|
225
|
+
}
|
package/src/outputs.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { isAbsolute, join } from "node:path";
|
|
3
|
+
|
|
4
|
+
/** Where a reading sounds: this machine's default sink, or a PulseAudio/PipeWire server elsewhere. */
|
|
5
|
+
export interface Output {
|
|
6
|
+
name: string;
|
|
7
|
+
label: string;
|
|
8
|
+
/** PULSE_SERVER for the player; absent for this machine. */
|
|
9
|
+
pulseServer?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const NAME = /^[A-Za-z0-9][A-Za-z0-9-]*$/;
|
|
13
|
+
const VALUE_HINT = 'the value must be a PULSE_SERVER string such as "tcp:192.168.1.50:4713"';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* `local` plus the outputs in `$XDG_CONFIG_HOME/pi-reply-reader/outputs.json`, an object mapping each
|
|
17
|
+
* name to its PULSE_SERVER. Bad entries are skipped and reported in `problems`; nothing throws.
|
|
18
|
+
*/
|
|
19
|
+
export function loadOutputs(env: NodeJS.ProcessEnv, hostname: string): { outputs: Output[]; problems: string[] } {
|
|
20
|
+
const outputs: Output[] = [{ name: "local", label: `local (${hostname})` }];
|
|
21
|
+
const problems: string[] = [];
|
|
22
|
+
const path = configFile(env, "outputs.json");
|
|
23
|
+
if (!path) return { outputs, problems };
|
|
24
|
+
|
|
25
|
+
let raw: string;
|
|
26
|
+
try {
|
|
27
|
+
raw = readFileSync(path, "utf8");
|
|
28
|
+
} catch {
|
|
29
|
+
return { outputs, problems };
|
|
30
|
+
}
|
|
31
|
+
let parsed: unknown;
|
|
32
|
+
try {
|
|
33
|
+
parsed = JSON.parse(raw);
|
|
34
|
+
} catch (error) {
|
|
35
|
+
problems.push(`Ignored ${path}: not valid JSON (${(error as Error).message}).`);
|
|
36
|
+
return { outputs, problems };
|
|
37
|
+
}
|
|
38
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) {
|
|
39
|
+
problems.push(`Ignored ${path}: expected an object mapping output names to PULSE_SERVER values.`);
|
|
40
|
+
return { outputs, problems };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
for (const [name, value] of Object.entries(parsed)) {
|
|
44
|
+
const reason =
|
|
45
|
+
name === "local"
|
|
46
|
+
? "that name is built in"
|
|
47
|
+
: !NAME.test(name)
|
|
48
|
+
? 'names use letters, digits and "-"'
|
|
49
|
+
: typeof value !== "string" || value.trim() === ""
|
|
50
|
+
? VALUE_HINT
|
|
51
|
+
: undefined;
|
|
52
|
+
if (reason) {
|
|
53
|
+
problems.push(`Ignored output "${name}" in ${path}: ${reason}.`);
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
outputs.push({ name, label: `${name} (${value})`, pulseServer: value as string });
|
|
57
|
+
}
|
|
58
|
+
return { outputs, problems };
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* `name` in this package's config folder: XDG_CONFIG_HOME, then ~/.config; relative values are ignored,
|
|
63
|
+
* as the XDG spec says. Undefined when neither is usable.
|
|
64
|
+
*/
|
|
65
|
+
export function configFile(env: NodeJS.ProcessEnv, name: string): string | undefined {
|
|
66
|
+
const base =
|
|
67
|
+
env.XDG_CONFIG_HOME && isAbsolute(env.XDG_CONFIG_HOME)
|
|
68
|
+
? env.XDG_CONFIG_HOME
|
|
69
|
+
: env.HOME && isAbsolute(env.HOME)
|
|
70
|
+
? join(env.HOME, ".config")
|
|
71
|
+
: undefined;
|
|
72
|
+
return base && join(base, "pi-reply-reader", name);
|
|
73
|
+
}
|
package/src/playback.ts
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { type ChildProcess, spawn } from "node:child_process";
|
|
2
|
+
import type { Output } from "./outputs.ts";
|
|
3
|
+
import { PLAYER_MARK } from "./reading-lock.ts";
|
|
4
|
+
import { DEFAULT_SPEED } from "./settings.ts";
|
|
5
|
+
import { onPath, type Speaker, type Voice } from "./voice.ts";
|
|
6
|
+
|
|
7
|
+
/** What one segment asks of Supertonic; the real loader wraps `generationConfig` in sherpa's GenerationConfig. */
|
|
8
|
+
export interface SherpaRequest {
|
|
9
|
+
text: string;
|
|
10
|
+
generationConfig: { sid: number; speed: number; numSteps: number; extra: { lang: string; seed: number } };
|
|
11
|
+
/**
|
|
12
|
+
* Called with each synthesized chunk; returning false stops the generation. Always passed: on Bun,
|
|
13
|
+
* which runs Pi, sherpa's generateAsync without it fails with "Invalid argument".
|
|
14
|
+
*/
|
|
15
|
+
onProgress: (info: { samples: Float32Array; progress: number }) => boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** The slice of sherpa-onnx-node's OfflineTts a reading uses. */
|
|
19
|
+
export interface SherpaTts {
|
|
20
|
+
sampleRate: number;
|
|
21
|
+
generateAsync(request: SherpaRequest): Promise<{ samples: Float32Array }>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface Reading {
|
|
25
|
+
/** Settles when the audio ends or the reading is stopped; rejects with a message for the user. */
|
|
26
|
+
readonly done: Promise<void>;
|
|
27
|
+
stop(): void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const PAPLAY_MISSING =
|
|
31
|
+
"Cannot play: paplay was not found on PATH (it comes with libpulse on Arch, pulseaudio-utils on Debian and Ubuntu).";
|
|
32
|
+
|
|
33
|
+
/** Supertonic's default seed is random; a fixed one makes the same text always sound the same. */
|
|
34
|
+
const SEED = 42;
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Speaks `text` with `speaker` at `speed` (Supertonic's voice speed, 1.1 by default; our pauses do not
|
|
38
|
+
* change with it) on `output`. Supertonic synthesizes one sentence at a time, so the first
|
|
39
|
+
* audio comes after one sentence instead of the whole reply, and the next sentence is synthesized while the
|
|
40
|
+
* current one plays (at most LOOKAHEAD wait for the player); each sentence loses its own leading and
|
|
41
|
+
* trailing silence and is followed right away by our pause for its boundary, then goes to `paplay` as raw 16-bit
|
|
42
|
+
* mono PCM, on this machine's default sink or, for a named output, on its PULSE_SERVER. Nothing here
|
|
43
|
+
* throws: every failure ends in `done`, and `stop()` ends the player and settles `done` quietly.
|
|
44
|
+
*/
|
|
45
|
+
export function startReading(options: {
|
|
46
|
+
voice: Voice;
|
|
47
|
+
speaker: Speaker;
|
|
48
|
+
text: string;
|
|
49
|
+
output: Output;
|
|
50
|
+
env: NodeJS.ProcessEnv;
|
|
51
|
+
loadSherpa?: (voice: Voice) => Promise<SherpaTts>;
|
|
52
|
+
speed?: number;
|
|
53
|
+
/** Told the player's pid once it starts, so another session can stop this reading (T21). */
|
|
54
|
+
onPlayer?: (pid: number) => void;
|
|
55
|
+
}): Reading {
|
|
56
|
+
const { voice, speaker, text, output, env, loadSherpa = loadSherpaTts, speed = DEFAULT_SPEED, onPlayer } = options;
|
|
57
|
+
let player: ChildProcess | undefined;
|
|
58
|
+
let stopped = false;
|
|
59
|
+
|
|
60
|
+
const run = async (): Promise<void> => {
|
|
61
|
+
const paplay = onPath("paplay", env);
|
|
62
|
+
if (!paplay) throw new Error(PAPLAY_MISSING);
|
|
63
|
+
|
|
64
|
+
let tts: SherpaTts;
|
|
65
|
+
try {
|
|
66
|
+
tts = await loadSherpa(voice);
|
|
67
|
+
} catch (error) {
|
|
68
|
+
throw new Error(`Speech synthesis failed: ${(error as Error).message}`);
|
|
69
|
+
}
|
|
70
|
+
if (stopped) return;
|
|
71
|
+
|
|
72
|
+
const playerEnv = output.pulseServer ? { ...env, PULSE_SERVER: output.pulseServer } : env;
|
|
73
|
+
const args = ["--raw", "--format=s16le", `--rate=${tts.sampleRate}`, "--channels=1", PLAYER_MARK, "--stream-name=reply"];
|
|
74
|
+
const child = spawn(paplay, args, { env: playerEnv, stdio: ["pipe", "ignore", "pipe"] });
|
|
75
|
+
player = child;
|
|
76
|
+
// A player that quits early closes its stdin; writing to it must not crash Pi.
|
|
77
|
+
child.stdin?.on("error", () => {});
|
|
78
|
+
const playback = finished(child, `Playback on "${output.name}" failed`);
|
|
79
|
+
if (child.pid !== undefined) onPlayer?.(child.pid);
|
|
80
|
+
let playerGone = false;
|
|
81
|
+
const gone = playback.then(
|
|
82
|
+
() => {},
|
|
83
|
+
() => {},
|
|
84
|
+
);
|
|
85
|
+
gone.then(() => (playerGone = true));
|
|
86
|
+
// A player that exited takes nothing more, even before its pipes close.
|
|
87
|
+
child.once("exit", () => (playerGone = true));
|
|
88
|
+
|
|
89
|
+
// Fragments handed to stdin that the pipe has not taken yet; each write's callback fires once it has.
|
|
90
|
+
let queued = 0;
|
|
91
|
+
let taken: (() => void) | undefined;
|
|
92
|
+
const send = (audio: Float32Array): void => {
|
|
93
|
+
queued++;
|
|
94
|
+
child.stdin?.write(pcm16(audio, speaker.gain), (error) => {
|
|
95
|
+
// A write that fails means the player closed its input: it will take nothing more.
|
|
96
|
+
if (error) playerGone = true;
|
|
97
|
+
queued--;
|
|
98
|
+
taken?.();
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
const list = fragments(text);
|
|
103
|
+
// Samples of pause already written after the last audible fragment; undefined before the first one.
|
|
104
|
+
let paused: number | undefined;
|
|
105
|
+
for (const [index, fragment] of list.entries()) {
|
|
106
|
+
// Synthesize ahead, but stop while LOOKAHEAD fragments still wait for the player to take them.
|
|
107
|
+
while (queued >= LOOKAHEAD && !stopped && !playerGone) {
|
|
108
|
+
await Promise.race([new Promise<void>((resolve) => (taken = resolve)), gone]);
|
|
109
|
+
}
|
|
110
|
+
if (stopped || playerGone) break;
|
|
111
|
+
let samples: Float32Array;
|
|
112
|
+
try {
|
|
113
|
+
({ samples } = await tts.generateAsync({
|
|
114
|
+
text: fragment.text,
|
|
115
|
+
generationConfig: { sid: speaker.sid, speed, numSteps: 8, extra: { lang: "es", seed: SEED } },
|
|
116
|
+
onProgress: () => !stopped && !playerGone,
|
|
117
|
+
}));
|
|
118
|
+
} catch (error) {
|
|
119
|
+
if (stopped || playerGone) break;
|
|
120
|
+
throw new Error(`Speech synthesis failed: ${(error as Error).message}`);
|
|
121
|
+
}
|
|
122
|
+
if (stopped || playerGone) break;
|
|
123
|
+
const speech = trimSilence(samples, speaker.gain, tts.sampleRate);
|
|
124
|
+
// The last fragment's pause is never written.
|
|
125
|
+
const after = index === list.length - 1 ? 0 : Math.round(PAUSES[fragment.boundary] * tts.sampleRate);
|
|
126
|
+
// A silent fragment is dropped, but a stronger boundary it ends still counts: top up the pause written.
|
|
127
|
+
if (!speech) {
|
|
128
|
+
if (paused !== undefined && after > paused) {
|
|
129
|
+
send(new Float32Array(after - paused));
|
|
130
|
+
paused = after;
|
|
131
|
+
}
|
|
132
|
+
continue;
|
|
133
|
+
}
|
|
134
|
+
// Each pause goes right after its own fragment, so the next synthesis overlaps it too.
|
|
135
|
+
send(after ? concat(speech, new Float32Array(after)) : speech);
|
|
136
|
+
paused = after;
|
|
137
|
+
}
|
|
138
|
+
child.stdin?.end();
|
|
139
|
+
await playback;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const done = run().then(
|
|
143
|
+
() => undefined,
|
|
144
|
+
(error: Error) => {
|
|
145
|
+
player?.kill();
|
|
146
|
+
if (!stopped) throw error;
|
|
147
|
+
},
|
|
148
|
+
);
|
|
149
|
+
return {
|
|
150
|
+
done,
|
|
151
|
+
stop() {
|
|
152
|
+
stopped = true;
|
|
153
|
+
player?.kill();
|
|
154
|
+
},
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/** Resolves when `child` exits cleanly or was stopped; rejects with `prefix` and its last error line. */
|
|
158
|
+
function finished(child: ChildProcess, prefix: string): Promise<void> {
|
|
159
|
+
let stderr = "";
|
|
160
|
+
child.stderr?.on("data", (chunk: Buffer) => (stderr = (stderr + chunk.toString()).slice(-4096)));
|
|
161
|
+
return new Promise((resolve, reject) => {
|
|
162
|
+
child.on("error", (error) => reject(new Error(`${prefix}: ${error.message}`)));
|
|
163
|
+
// After stop, do not wait for pipes a grandchild may still hold open.
|
|
164
|
+
child.on("exit", () => stopped && resolve());
|
|
165
|
+
child.on("close", (code, signal) => {
|
|
166
|
+
if (code === 0 || stopped) return resolve();
|
|
167
|
+
const last = stderr.trim().split("\n").at(-1)?.trim();
|
|
168
|
+
reject(new Error(`${prefix}: ${last || (signal ? `killed by ${signal}` : `exit code ${code}`)}`));
|
|
169
|
+
});
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Synthesized fragments that may wait for the player at once (T26): the one playing and the next one, ready.
|
|
176
|
+
* Synthesis is one call at a time and, under load, slower than real time (L70), so a deeper queue would rarely
|
|
177
|
+
* fill; when it is faster, one ready fragment already hides the next synthesis. Two keeps a long reply out of
|
|
178
|
+
* memory (a fragment is about 0.3-1 MB of PCM) and a stuck player stops synthesis after two fragments.
|
|
179
|
+
*/
|
|
180
|
+
const LOOKAHEAD = 2;
|
|
181
|
+
|
|
182
|
+
type Boundary = "sentence" | "line" | "paragraph";
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Seconds of silence after a fragment, by what follows it: a sentence on the same line, a line, a blank line
|
|
186
|
+
* (also after a heading or bold-only line). Longer than the voice's own 0.08-0.26 s at `,`, `;` and `:` (L39).
|
|
187
|
+
*/
|
|
188
|
+
const PAUSES: Record<Boundary, number> = { sentence: 0.35, line: 0.5, paragraph: 0.75 };
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Sentences and lines, in order, each with the boundary after it; Supertonic only calls back once per
|
|
192
|
+
* 300 characters, so we split first. Never at ":" or ";", where a pause would sound like a full stop.
|
|
193
|
+
*/
|
|
194
|
+
function fragments(text: string): { text: string; boundary: Boundary }[] {
|
|
195
|
+
const result: { text: string; boundary: Boundary }[] = [];
|
|
196
|
+
let blank = false;
|
|
197
|
+
for (const line of text.split("\n")) {
|
|
198
|
+
const sentences = line
|
|
199
|
+
.split(/(?<=[.!?…])\s+/)
|
|
200
|
+
.map((sentence) => sentence.trim())
|
|
201
|
+
.filter((sentence) => sentence !== "");
|
|
202
|
+
if (sentences.length === 0) {
|
|
203
|
+
blank = true;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const previous = result.at(-1);
|
|
207
|
+
if (previous) previous.boundary = blank ? "paragraph" : "line";
|
|
208
|
+
blank = false;
|
|
209
|
+
for (const sentence of sentences) result.push({ text: sentence, boundary: "sentence" });
|
|
210
|
+
}
|
|
211
|
+
return result;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/** Windows quieter than this, after the speaker's gain, count as silence (-45 dBFS RMS over 10 ms). */
|
|
215
|
+
const SILENCE = 10 ** (-45 / 20);
|
|
216
|
+
const WINDOW_SECONDS = 0.01;
|
|
217
|
+
/** Kept on each side of the first and last sound, so soft speech edges are not cut. */
|
|
218
|
+
const MARGIN_SECONDS = 0.04;
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Supertonic pads every fragment with about 0.4-0.6 s of silence before and 0.6-0.7 s after (L36);
|
|
222
|
+
* this cuts it to the margin. Undefined when no window is above the threshold.
|
|
223
|
+
*/
|
|
224
|
+
function trimSilence(samples: Float32Array, gain: number, sampleRate: number): Float32Array | undefined {
|
|
225
|
+
const window = Math.max(1, Math.round(sampleRate * WINDOW_SECONDS));
|
|
226
|
+
let first = -1;
|
|
227
|
+
let last = -1;
|
|
228
|
+
for (let start = 0; start < samples.length; start += window) {
|
|
229
|
+
const end = Math.min(start + window, samples.length);
|
|
230
|
+
let sum = 0;
|
|
231
|
+
for (let i = start; i < end; i++) sum += (samples[i] * gain) ** 2;
|
|
232
|
+
if (Math.sqrt(sum / (end - start)) <= SILENCE) continue;
|
|
233
|
+
if (first < 0) first = start;
|
|
234
|
+
last = end;
|
|
235
|
+
}
|
|
236
|
+
if (first < 0) return undefined;
|
|
237
|
+
const margin = Math.round(sampleRate * MARGIN_SECONDS);
|
|
238
|
+
return samples.subarray(Math.max(0, first - margin), Math.min(samples.length, last + margin));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function concat(a: Float32Array, b: Float32Array): Float32Array {
|
|
242
|
+
const out = new Float32Array(a.length + b.length);
|
|
243
|
+
out.set(a);
|
|
244
|
+
out.set(b, a.length);
|
|
245
|
+
return out;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
/** Samples below this level pass through the limiter unchanged (-6 dBFS). */
|
|
249
|
+
const KNEE = 0.5;
|
|
250
|
+
/** No sample leaves the limiter above -1 dBFS, rounded down to a whole PCM step so rounding cannot pass it. */
|
|
251
|
+
const CEILING = Math.floor(10 ** (-1 / 20) * 32767) / 32767;
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Float samples to 16-bit little-endian PCM with the speaker's fixed gain. Above the knee a tanh curve
|
|
255
|
+
* bends peaks smoothly toward the ceiling, so they never clip or wrap around.
|
|
256
|
+
*/
|
|
257
|
+
function pcm16(samples: Float32Array, gain: number): Buffer {
|
|
258
|
+
const out = Buffer.alloc(samples.length * 2);
|
|
259
|
+
for (let i = 0; i < samples.length; i++) {
|
|
260
|
+
const x = samples[i] * gain;
|
|
261
|
+
const level = Math.abs(x);
|
|
262
|
+
const limited = level <= KNEE ? x : Math.sign(x) * (KNEE + (CEILING - KNEE) * Math.tanh((level - KNEE) / (CEILING - KNEE)));
|
|
263
|
+
out.writeInt16LE(Math.round(limited * 32767), i * 2);
|
|
264
|
+
}
|
|
265
|
+
return out;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const loaded = new Map<string, Promise<SherpaTts>>();
|
|
269
|
+
|
|
270
|
+
/** Loads the model once; loading takes about a second. */
|
|
271
|
+
function loadSherpaTts(voice: Voice): Promise<SherpaTts> {
|
|
272
|
+
const key = voice.supertonic.ttsJson;
|
|
273
|
+
let tts = loaded.get(key);
|
|
274
|
+
if (!tts) {
|
|
275
|
+
// sherpa-onnx-node is CommonJS with a native addon.
|
|
276
|
+
// A plain import, not createRequire: Pi's compiled extension loader resolves only imports.
|
|
277
|
+
tts = import("sherpa-onnx-node").then(async (module) => {
|
|
278
|
+
const { OfflineTts, GenerationConfig } = module.default ?? module;
|
|
279
|
+
const engine = await OfflineTts.createAsync({
|
|
280
|
+
model: { supertonic: voice.supertonic, numThreads: 2, provider: "cpu", debug: 0 },
|
|
281
|
+
});
|
|
282
|
+
return {
|
|
283
|
+
sampleRate: engine.sampleRate,
|
|
284
|
+
generateAsync: ({ generationConfig, ...request }: SherpaRequest) =>
|
|
285
|
+
engine.generateAsync({ ...request, generationConfig: new GenerationConfig(generationConfig) }),
|
|
286
|
+
};
|
|
287
|
+
});
|
|
288
|
+
tts.catch(() => loaded.delete(key));
|
|
289
|
+
loaded.set(key, tts);
|
|
290
|
+
}
|
|
291
|
+
return tts;
|
|
292
|
+
}
|
package/src/questions.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const FENCE = /^\s{0,3}(```|~~~)/;
|
|
2
|
+
/**
|
|
3
|
+
* The closing questions heading of a reply (S8): "Needs your decision" as a Markdown heading or a bold-only line,
|
|
4
|
+
* any case, with or without a final colon (inside or outside the bold).
|
|
5
|
+
*/
|
|
6
|
+
const QUESTIONS =
|
|
7
|
+
/^\s{0,3}(?:#{1,6}\s+(?:\*\*|__)?needs your decision:?(?:\*\*|__)?:?(?:\s+#+)?|(?:\*\*|__)needs your decision:?(?:\*\*|__):?)\s*$/i;
|
|
8
|
+
|
|
9
|
+
/** The last "Needs your decision" heading or bold-only line and everything after it, or undefined when there is none. */
|
|
10
|
+
export function questionsSection(markdown: string): string | undefined {
|
|
11
|
+
const lines = markdown.split("\n");
|
|
12
|
+
let start: number | undefined;
|
|
13
|
+
let fenced = false;
|
|
14
|
+
lines.forEach((line, index) => {
|
|
15
|
+
if (FENCE.test(line)) fenced = !fenced;
|
|
16
|
+
if (!fenced && QUESTIONS.test(line)) start = index;
|
|
17
|
+
});
|
|
18
|
+
return start === undefined ? undefined : lines.slice(start).join("\n").trimEnd();
|
|
19
|
+
}
|