pi-reply-reader 0.0.0-stage → 0.2.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 +200 -0
- package/LICENSE +21 -0
- package/README.md +710 -2
- package/dist/bin/reply-reader.js +49 -0
- package/dist/src/cli.js +329 -0
- package/dist/src/listener.js +532 -0
- package/dist/src/music.js +452 -0
- package/dist/src/outputs.js +86 -0
- package/dist/src/playback.js +369 -0
- package/dist/src/player.js +265 -0
- package/dist/src/project.js +50 -0
- package/dist/src/reading-lock.js +371 -0
- package/dist/src/settings.js +126 -0
- package/dist/src/speech-text.js +1028 -0
- package/dist/src/voice.js +173 -0
- package/examples/reply-reader-desktop-audio.service +57 -0
- package/examples/reply-reader-vps-audio.service +57 -0
- package/extensions/reply-reader.ts +1278 -0
- package/package.json +71 -4
- package/src/auto-queue.ts +123 -0
- package/src/cli.ts +346 -0
- package/src/listener.ts +522 -0
- package/src/music.ts +474 -0
- package/src/outputs.ts +105 -0
- package/src/pause-key.ts +105 -0
- package/src/playback.ts +423 -0
- package/src/player.ts +255 -0
- package/src/project.ts +46 -0
- package/src/prompts.ts +142 -0
- package/src/questions.ts +19 -0
- package/src/reader-events.ts +44 -0
- package/src/reading-lock.ts +465 -0
- package/src/replies.ts +64 -0
- package/src/sections.ts +47 -0
- package/src/settings.ts +125 -0
- package/src/sherpa-onnx-node.d.ts +29 -0
- package/src/speech-text.ts +1215 -0
- package/src/voice.ts +195 -0
|
@@ -0,0 +1,173 @@
|
|
|
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 { promisify } from "node:util";
|
|
8
|
+
/** The local engine (S3): Supertonic 3, int8, as sherpa-onnx packages it for its v2 Node binding. */
|
|
9
|
+
const VOICE = "Supertonic 3";
|
|
10
|
+
const BUNDLE = "sherpa-onnx-supertonic-3-tts-int8-2026-05-11";
|
|
11
|
+
/** sherpa-onnx's supertonic config key → file in the bundle. */
|
|
12
|
+
const MODEL_FILES = {
|
|
13
|
+
durationPredictor: "duration_predictor.int8.onnx",
|
|
14
|
+
textEncoder: "text_encoder.int8.onnx",
|
|
15
|
+
vectorEstimator: "vector_estimator.int8.onnx",
|
|
16
|
+
vocoder: "vocoder.int8.onnx",
|
|
17
|
+
ttsJson: "tts.json",
|
|
18
|
+
unicodeIndexer: "unicode_indexer.bin",
|
|
19
|
+
voiceStyle: "voice.bin",
|
|
20
|
+
};
|
|
21
|
+
const BUNDLE_FILES = Object.values(MODEL_FILES);
|
|
22
|
+
/** sherpa-onnx's Supertonic 3 bundle; size and digest as published by GitHub for the release asset. */
|
|
23
|
+
export const VOICE_ARCHIVE = {
|
|
24
|
+
url: `https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/${BUNDLE}.tar.bz2`,
|
|
25
|
+
bytes: 128774318,
|
|
26
|
+
sha256: "82fa96f91c4ef8abaae3a14a3f4153facf88bed821d1f7331cec2700f432c427",
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* The two voices the model ships (S3, L20): male is the default, female is selectable. Each gain
|
|
30
|
+
* lifts the voice's speech to about -16 dBFS (L22). Measured on the hs bake-off WAVs, speech RMS was
|
|
31
|
+
* -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
|
|
32
|
+
* peaks go past full scale and playback limits them (playback.ts).
|
|
33
|
+
*/
|
|
34
|
+
export const SPEAKERS = {
|
|
35
|
+
male: { sid: 6, gain: 10 ** ((-16 - -22.7) / 20) },
|
|
36
|
+
female: { sid: 0, gain: 10 ** ((-16 - -23.5) / 20) },
|
|
37
|
+
};
|
|
38
|
+
/** The voice this package downloaded, when every model file is in place. */
|
|
39
|
+
export function findVoice(env) {
|
|
40
|
+
const data = dataHome(env);
|
|
41
|
+
if (!data)
|
|
42
|
+
return undefined;
|
|
43
|
+
const dir = join(data, "pi-reply-reader", "voices", BUNDLE);
|
|
44
|
+
if (!BUNDLE_FILES.every((name) => existsSync(join(dir, name))))
|
|
45
|
+
return undefined;
|
|
46
|
+
const files = Object.entries(MODEL_FILES).map(([key, name]) => [key, join(dir, name)]);
|
|
47
|
+
return { supertonic: Object.fromEntries(files) };
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Downloads the pinned voice after `confirm` agrees. The archive is checked for size and sha256
|
|
51
|
+
* before extraction, extracted beside its destination and moved into place whole, so a failure
|
|
52
|
+
* leaves nothing behind and a half-written voice is never found.
|
|
53
|
+
*/
|
|
54
|
+
export async function downloadVoice(options) {
|
|
55
|
+
const { env, confirm, archive = VOICE_ARCHIVE, idleTimeoutMs = 30_000, signal } = options;
|
|
56
|
+
const existing = findVoice(env);
|
|
57
|
+
if (existing)
|
|
58
|
+
return existing;
|
|
59
|
+
const data = dataHome(env);
|
|
60
|
+
if (!data) {
|
|
61
|
+
throw new Error("Voice download failed: neither XDG_DATA_HOME nor HOME is an absolute path. Nothing was kept.");
|
|
62
|
+
}
|
|
63
|
+
const dir = join(data, "pi-reply-reader", "voices");
|
|
64
|
+
const megabytes = Math.round(archive.bytes / 1_000_000);
|
|
65
|
+
if (!(await confirm(`Download the Spanish voice ${VOICE} (${megabytes} MB) from ${new URL(archive.url).hostname} into ${dir}?`))) {
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
mkdirSync(dir, { recursive: true });
|
|
69
|
+
const work = mkdtempSync(join(dir, ".download-"));
|
|
70
|
+
try {
|
|
71
|
+
const file = join(work, "voice.tar.bz2");
|
|
72
|
+
await fetchVerified(archive, file, idleTimeoutMs, signal);
|
|
73
|
+
await promisify(execFile)("tar", ["-xjf", file, "--no-same-owner", "-C", work]);
|
|
74
|
+
for (const name of BUNDLE_FILES) {
|
|
75
|
+
if (!existsSync(join(work, BUNDLE, name)))
|
|
76
|
+
throw new Error(`the archive has no ${BUNDLE}/${name}`);
|
|
77
|
+
}
|
|
78
|
+
// An incomplete voice in the way (findVoice already said there is none) goes out with the work dir.
|
|
79
|
+
if (existsSync(join(dir, BUNDLE)))
|
|
80
|
+
renameSync(join(dir, BUNDLE), join(work, "incomplete"));
|
|
81
|
+
try {
|
|
82
|
+
renameSync(join(work, BUNDLE), join(dir, BUNDLE));
|
|
83
|
+
}
|
|
84
|
+
catch (error) {
|
|
85
|
+
// Another session finished the same download first; its copy is as good as ours.
|
|
86
|
+
if (!findVoice(env))
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
return findVoice(env);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
throw new Error(`Voice download failed: ${error.message}. Nothing was kept.`);
|
|
93
|
+
}
|
|
94
|
+
finally {
|
|
95
|
+
rmSync(work, { recursive: true, force: true });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Streams the archive to `file`, failing on HTTP errors, silence, oversize and disk errors alike. */
|
|
99
|
+
async function fetchVerified(archive, file, idleTimeoutMs, cancel) {
|
|
100
|
+
const idle = new AbortController();
|
|
101
|
+
const signal = cancel ? AbortSignal.any([idle.signal, cancel]) : idle.signal;
|
|
102
|
+
let timer;
|
|
103
|
+
const wait = () => {
|
|
104
|
+
clearTimeout(timer);
|
|
105
|
+
timer = setTimeout(() => idle.abort(new Error(`no data for ${idleTimeoutMs / 1000} s from ${archive.url}`)), idleTimeoutMs);
|
|
106
|
+
};
|
|
107
|
+
const hash = createHash("sha256");
|
|
108
|
+
let received = 0;
|
|
109
|
+
try {
|
|
110
|
+
wait();
|
|
111
|
+
const response = await fetch(archive.url, { signal });
|
|
112
|
+
if (!response.ok || !response.body)
|
|
113
|
+
throw new Error(`HTTP ${response.status} from ${archive.url}`);
|
|
114
|
+
await pipeline(
|
|
115
|
+
// fetch's body is typed with the DOM stream; at runtime it is Node's web stream.
|
|
116
|
+
Readable.fromWeb(response.body), async function* (source) {
|
|
117
|
+
for await (const chunk of source) {
|
|
118
|
+
wait();
|
|
119
|
+
received += chunk.length;
|
|
120
|
+
if (received > archive.bytes)
|
|
121
|
+
throw new Error(`expected ${archive.bytes} bytes, got more`);
|
|
122
|
+
hash.update(chunk);
|
|
123
|
+
yield chunk;
|
|
124
|
+
}
|
|
125
|
+
}, createWriteStream(file), { signal });
|
|
126
|
+
}
|
|
127
|
+
catch (error) {
|
|
128
|
+
if (idle.signal.aborted)
|
|
129
|
+
throw idle.signal.reason;
|
|
130
|
+
throw cancel?.aborted ? new Error("cancelled") : error;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
clearTimeout(timer);
|
|
134
|
+
}
|
|
135
|
+
if (received !== archive.bytes)
|
|
136
|
+
throw new Error(`expected ${archive.bytes} bytes, got ${received}`);
|
|
137
|
+
const actual = hash.digest("hex");
|
|
138
|
+
if (actual !== archive.sha256)
|
|
139
|
+
throw new Error(`sha256 mismatch (expected ${archive.sha256}, got ${actual})`);
|
|
140
|
+
}
|
|
141
|
+
/** XDG_DATA_HOME, then ~/.local/share; relative values are ignored, as the XDG spec says. */
|
|
142
|
+
function dataHome(env) {
|
|
143
|
+
if (env.XDG_DATA_HOME && isAbsolute(env.XDG_DATA_HOME))
|
|
144
|
+
return env.XDG_DATA_HOME;
|
|
145
|
+
if (env.HOME && isAbsolute(env.HOME))
|
|
146
|
+
return join(env.HOME, ".local", "share");
|
|
147
|
+
return undefined;
|
|
148
|
+
}
|
|
149
|
+
/** An executable `name` in an absolute PATH entry. */
|
|
150
|
+
export function onPath(name, env) {
|
|
151
|
+
for (const dir of (env.PATH ?? "").split(delimiter)) {
|
|
152
|
+
if (!isAbsolute(dir))
|
|
153
|
+
continue;
|
|
154
|
+
const candidate = join(dir, name);
|
|
155
|
+
try {
|
|
156
|
+
accessSync(candidate, constants.X_OK);
|
|
157
|
+
if (isFile(candidate))
|
|
158
|
+
return candidate;
|
|
159
|
+
}
|
|
160
|
+
catch {
|
|
161
|
+
// not here, keep looking
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
function isFile(path) {
|
|
167
|
+
try {
|
|
168
|
+
return statSync(path).isFile();
|
|
169
|
+
}
|
|
170
|
+
catch {
|
|
171
|
+
return false;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Carries pi-reply-reader's audio from the agent host (the machine that runs Pi; in this example a
|
|
2
|
+
# home server) to the PipeWire of the listener (the machine you sit at; here called "desktop"), over
|
|
3
|
+
# SSH. Use it when the agent host can reach the listener (same LAN, Tailscale); for a cloud VPS that
|
|
4
|
+
# cannot reach it, use reply-reader-vps-audio.service on the listener instead. It also serves as the
|
|
5
|
+
# "fallback" of a listener output that synthesizes on the listener (README, "Synthesize on the listener").
|
|
6
|
+
#
|
|
7
|
+
# Why a tunnel and not a TCP PulseAudio port on the desktop: pipewire-pulse has no IP allow-list,
|
|
8
|
+
# so a LAN port would let anyone on the network play sound on the desktop or record its microphone
|
|
9
|
+
# unless a firewall closes it. The tunnel opens nothing on the desktop: it forwards a socket on this
|
|
10
|
+
# machine to the desktop's own Pulse socket, with SSH doing authentication and encryption.
|
|
11
|
+
#
|
|
12
|
+
# Why a Unix socket and not a loopback port such as 127.0.0.1:4714: the forwarded Pulse socket gives
|
|
13
|
+
# the desktop's whole audio, microphone included. A loopback port is open to every process here,
|
|
14
|
+
# other users and containers with host networking. The socket is created mode 0600
|
|
15
|
+
# (StreamLocalBindMask=0177) inside your 0700 runtime dir (%t), so only your user can open it.
|
|
16
|
+
#
|
|
17
|
+
# Install (on the machine that runs Pi):
|
|
18
|
+
# cp examples/reply-reader-desktop-audio.service ~/.config/systemd/user/
|
|
19
|
+
# systemctl --user daemon-reload && systemctl --user enable --now reply-reader-desktop-audio.service
|
|
20
|
+
# Then name the output in ~/.config/pi-reply-reader/outputs.json (your user ID on this machine,
|
|
21
|
+
# `id -u`, in place of 1000):
|
|
22
|
+
# { "desktop": "unix:/run/user/1000/reply-reader-pulse.sock" }
|
|
23
|
+
# Verify (prints the desktop's host name; plays nothing):
|
|
24
|
+
# PULSE_SERVER=unix:$XDG_RUNTIME_DIR/reply-reader-pulse.sock pactl info | grep 'Host Name'
|
|
25
|
+
#
|
|
26
|
+
# It is down while the desktop is off; the restart policy retries, and a reading on "desktop"
|
|
27
|
+
# fails with "Playback on "desktop" failed: ..." in the meantime.
|
|
28
|
+
|
|
29
|
+
[Unit]
|
|
30
|
+
Description=SSH tunnel for pi-reply-reader audio to the desktop's PipeWire (socket in the runtime dir)
|
|
31
|
+
After=network-online.target
|
|
32
|
+
Wants=network-online.target
|
|
33
|
+
|
|
34
|
+
[Service]
|
|
35
|
+
Type=simple
|
|
36
|
+
# The desktop's address and its user's ID there, in one place.
|
|
37
|
+
Environment=AUDIO_HOST=192.168.1.50
|
|
38
|
+
Environment=AUDIO_UID=1000
|
|
39
|
+
# StreamLocalBindUnlink replaces a socket left by a dropped connection; without it the next start
|
|
40
|
+
# fails with "Could not request local forwarding".
|
|
41
|
+
ExecStart=/usr/bin/ssh -N \
|
|
42
|
+
-o BatchMode=yes \
|
|
43
|
+
-o ExitOnForwardFailure=yes \
|
|
44
|
+
-o ConnectTimeout=10 \
|
|
45
|
+
-o ServerAliveInterval=30 \
|
|
46
|
+
-o ServerAliveCountMax=3 \
|
|
47
|
+
-o ControlMaster=no \
|
|
48
|
+
-o ControlPath=none \
|
|
49
|
+
-o StreamLocalBindMask=0177 \
|
|
50
|
+
-o StreamLocalBindUnlink=yes \
|
|
51
|
+
-L %t/reply-reader-pulse.sock:/run/user/${AUDIO_UID}/pulse/native \
|
|
52
|
+
you@${AUDIO_HOST}
|
|
53
|
+
Restart=always
|
|
54
|
+
RestartSec=15
|
|
55
|
+
|
|
56
|
+
[Install]
|
|
57
|
+
WantedBy=default.target
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
# Carries pi-reply-reader's audio from a cloud VPS that runs Pi to the PipeWire of the machine you
|
|
2
|
+
# sit at (here called "desktop"), over SSH. Install it ON THE DESKTOP: a VPS usually cannot reach a
|
|
3
|
+
# desktop behind NAT, so the desktop connects out and asks the VPS to forward a socket back
|
|
4
|
+
# (ssh -R). Nothing is opened on the desktop or on the VPS's network.
|
|
5
|
+
#
|
|
6
|
+
# On the VPS the socket is /run/user/<your VPS uid>/reply-reader-pulse.sock, mode 0600 (sshd's
|
|
7
|
+
# default StreamLocalBindMask), so only your user there can open it. It still gives the desktop's
|
|
8
|
+
# whole audio, microphone included, to whoever can act as that user on the VPS: root and your cloud
|
|
9
|
+
# provider among them. Keep the unit stopped when you do not need readings there.
|
|
10
|
+
#
|
|
11
|
+
# On the VPS, once:
|
|
12
|
+
# loginctl enable-linger # /run/user/<uid> exists without a login session
|
|
13
|
+
# On the desktop:
|
|
14
|
+
# cp examples/reply-reader-vps-audio.service ~/.config/systemd/user/
|
|
15
|
+
# (edit VPS_HOST, VPS_UID and the user in you@${VPS_HOST})
|
|
16
|
+
# systemctl --user daemon-reload && systemctl --user enable --now reply-reader-vps-audio.service
|
|
17
|
+
# Then on the VPS, in ~/.config/pi-reply-reader/outputs.json (VPS_UID in place of 1000):
|
|
18
|
+
# { "desktop": "unix:/run/user/1000/reply-reader-pulse.sock" }
|
|
19
|
+
# Verify on the VPS (prints the desktop's host name; plays nothing):
|
|
20
|
+
# PULSE_SERVER=unix:$XDG_RUNTIME_DIR/reply-reader-pulse.sock pactl info | grep 'Host Name'
|
|
21
|
+
#
|
|
22
|
+
# The VPS's sshd keeps the socket after a dropped connection and then refuses the next forward
|
|
23
|
+
# ("remote port forwarding failed for listen path"), so ExecStartPre removes it first. With
|
|
24
|
+
# "StreamLocalBindUnlink yes" in the VPS's sshd_config that step is not needed.
|
|
25
|
+
|
|
26
|
+
[Unit]
|
|
27
|
+
Description=SSH reverse tunnel for pi-reply-reader audio from a VPS to this machine's PipeWire
|
|
28
|
+
After=network-online.target
|
|
29
|
+
Wants=network-online.target
|
|
30
|
+
|
|
31
|
+
[Service]
|
|
32
|
+
Type=simple
|
|
33
|
+
# The VPS's address and your user's ID there, in one place.
|
|
34
|
+
Environment=VPS_HOST=vps.example.com
|
|
35
|
+
Environment=VPS_UID=1000
|
|
36
|
+
# "-" lets the tunnel start (and fail and retry) even when this step cannot reach the VPS.
|
|
37
|
+
ExecStartPre=-/usr/bin/ssh \
|
|
38
|
+
-o BatchMode=yes \
|
|
39
|
+
-o ConnectTimeout=10 \
|
|
40
|
+
-o ControlMaster=no \
|
|
41
|
+
-o ControlPath=none \
|
|
42
|
+
you@${VPS_HOST} rm -f /run/user/${VPS_UID}/reply-reader-pulse.sock
|
|
43
|
+
ExecStart=/usr/bin/ssh -N \
|
|
44
|
+
-o BatchMode=yes \
|
|
45
|
+
-o ExitOnForwardFailure=yes \
|
|
46
|
+
-o ConnectTimeout=10 \
|
|
47
|
+
-o ServerAliveInterval=30 \
|
|
48
|
+
-o ServerAliveCountMax=3 \
|
|
49
|
+
-o ControlMaster=no \
|
|
50
|
+
-o ControlPath=none \
|
|
51
|
+
-R /run/user/${VPS_UID}/reply-reader-pulse.sock:%t/pulse/native \
|
|
52
|
+
you@${VPS_HOST}
|
|
53
|
+
Restart=always
|
|
54
|
+
RestartSec=15
|
|
55
|
+
|
|
56
|
+
[Install]
|
|
57
|
+
WantedBy=default.target
|