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
package/src/settings.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { dirname } from "node:path";
|
|
3
|
+
import { configFile } from "./outputs.ts";
|
|
4
|
+
|
|
5
|
+
/** Supertonic's voice speed when none is saved (S3, L46). */
|
|
6
|
+
export const DEFAULT_SPEED = 1.1;
|
|
7
|
+
export const MIN_SPEED = 0.5;
|
|
8
|
+
export const MAX_SPEED = 2;
|
|
9
|
+
/** What `/read-speed` offers without a number. */
|
|
10
|
+
export const SPEED_CHOICES = ["0.8", "0.9", "1.0", "1.1", "1.2", "1.3", "1.5"];
|
|
11
|
+
export const SPEED_HINT = "Speed must be a number from 0.5 to 2.0.";
|
|
12
|
+
|
|
13
|
+
const SETTINGS = "settings.json";
|
|
14
|
+
/** Digits with an optional decimal part after a period or a comma, or only the decimal part (".9", ",9"). */
|
|
15
|
+
const NUMBER = /^(?:\d+(?:[.,]\d+)?|[.,]\d+)$/;
|
|
16
|
+
|
|
17
|
+
const inRange = (value: unknown): value is number =>
|
|
18
|
+
typeof value === "number" && Number.isFinite(value) && value >= MIN_SPEED && value <= MAX_SPEED;
|
|
19
|
+
|
|
20
|
+
/** A speed typed by the user ("1.15", "1,15" or ".9"), or undefined when it is not one from 0.5 to 2.0. */
|
|
21
|
+
export function parseSpeed(text: string): number | undefined {
|
|
22
|
+
const trimmed = text.trim();
|
|
23
|
+
if (!NUMBER.test(trimmed)) return undefined;
|
|
24
|
+
const value = Number(trimmed.replace(",", "."));
|
|
25
|
+
return inRange(value) ? value : undefined;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** "1.15", and whole numbers with one decimal ("1.0", "2.0") as the selector shows them. */
|
|
29
|
+
export function formatSpeed(speed: number): string {
|
|
30
|
+
return Number.isInteger(speed) ? speed.toFixed(1) : String(speed);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The saved speed from `$XDG_CONFIG_HOME/pi-reply-reader/settings.json`. A missing file, folder or key means
|
|
35
|
+
* the default with no problem; an unreadable or invalid file, or a speed out of range, means the default and
|
|
36
|
+
* a `problem` for the user. Nothing throws.
|
|
37
|
+
*/
|
|
38
|
+
export function loadSpeed(env: NodeJS.ProcessEnv): { speed: number; problem?: string } {
|
|
39
|
+
const path = configFile(env, SETTINGS);
|
|
40
|
+
if (!path) return { speed: DEFAULT_SPEED };
|
|
41
|
+
const ignored = (reason: string) => ({ speed: DEFAULT_SPEED, problem: `Ignored ${path}: ${reason}. Readings use speed ${formatSpeed(DEFAULT_SPEED)}.` });
|
|
42
|
+
|
|
43
|
+
let raw: string;
|
|
44
|
+
try {
|
|
45
|
+
raw = readFileSync(path, "utf8");
|
|
46
|
+
} catch (error) {
|
|
47
|
+
if ((error as NodeJS.ErrnoException).code === "ENOENT") return { speed: DEFAULT_SPEED };
|
|
48
|
+
return ignored(`cannot be read (${(error as Error).message})`);
|
|
49
|
+
}
|
|
50
|
+
let parsed: unknown;
|
|
51
|
+
try {
|
|
52
|
+
parsed = JSON.parse(raw);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return ignored(`not valid JSON (${(error as Error).message})`);
|
|
55
|
+
}
|
|
56
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return ignored("expected an object");
|
|
57
|
+
if (!("speed" in parsed)) return { speed: DEFAULT_SPEED };
|
|
58
|
+
const { speed } = parsed as { speed: unknown };
|
|
59
|
+
return inRange(speed) ? { speed } : ignored("speed must be a number from 0.5 to 2.0");
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** What `/read-auto` saves (S24): `on` reads in full, `questions` only the questions section. */
|
|
63
|
+
export type SavedAuto = "on" | "questions" | "off";
|
|
64
|
+
const AUTO_VALUES: readonly string[] = ["on", "questions", "off"];
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* The auto-read mode and output the last session chose (S24), from settings.json `autoRead` and `output`. A missing
|
|
68
|
+
* file, key, or a value of the wrong kind means none saved (an unreadable or invalid file is reported by `loadSpeed`
|
|
69
|
+
* at the first reading). Nothing throws.
|
|
70
|
+
*/
|
|
71
|
+
export function loadSaved(env: NodeJS.ProcessEnv): { autoRead?: SavedAuto; output?: string } {
|
|
72
|
+
const path = configFile(env, SETTINGS);
|
|
73
|
+
if (!path) return {};
|
|
74
|
+
let parsed: unknown;
|
|
75
|
+
try {
|
|
76
|
+
parsed = JSON.parse(readFileSync(path, "utf8"));
|
|
77
|
+
} catch {
|
|
78
|
+
return {};
|
|
79
|
+
}
|
|
80
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return {};
|
|
81
|
+
const { autoRead, output } = parsed as { autoRead?: unknown; output?: unknown };
|
|
82
|
+
return {
|
|
83
|
+
...(typeof autoRead === "string" && AUTO_VALUES.includes(autoRead) ? { autoRead: autoRead as SavedAuto } : {}),
|
|
84
|
+
...(typeof output === "string" && output ? { output } : {}),
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/** Saves `speed` in settings.json, keeping every other key (see `saveSettings`). */
|
|
89
|
+
export function saveSpeed(env: NodeJS.ProcessEnv, speed: number): void {
|
|
90
|
+
saveSettings(env, { speed });
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Saves `values` in settings.json, keeping every other key. Throws an Error naming the file and why when it
|
|
95
|
+
* cannot; an existing file that is not a JSON object is left as it is rather than overwritten.
|
|
96
|
+
*/
|
|
97
|
+
export function saveSettings(env: NodeJS.ProcessEnv, values: Record<string, unknown>): void {
|
|
98
|
+
const path = configFile(env, SETTINGS);
|
|
99
|
+
if (!path) throw new Error(`it was not saved: no config folder (set XDG_CONFIG_HOME or HOME to an absolute path)`);
|
|
100
|
+
const fail = (reason: string) => new Error(`it was not saved to ${path}: ${reason}`);
|
|
101
|
+
|
|
102
|
+
let settings: Record<string, unknown> = {};
|
|
103
|
+
let raw: string | undefined;
|
|
104
|
+
try {
|
|
105
|
+
raw = readFileSync(path, "utf8");
|
|
106
|
+
} catch (error) {
|
|
107
|
+
if ((error as NodeJS.ErrnoException).code !== "ENOENT") throw fail((error as Error).message);
|
|
108
|
+
}
|
|
109
|
+
if (raw !== undefined) {
|
|
110
|
+
let parsed: unknown;
|
|
111
|
+
try {
|
|
112
|
+
parsed = JSON.parse(raw);
|
|
113
|
+
} catch {
|
|
114
|
+
throw fail("it is not valid JSON");
|
|
115
|
+
}
|
|
116
|
+
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) throw fail("it is not a JSON object");
|
|
117
|
+
settings = parsed as Record<string, unknown>;
|
|
118
|
+
}
|
|
119
|
+
try {
|
|
120
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
121
|
+
writeFileSync(path, `${JSON.stringify({ ...settings, ...values }, null, 2)}\n`);
|
|
122
|
+
} catch (error) {
|
|
123
|
+
throw fail((error as Error).message);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** The part of sherpa-onnx-node 2.x (CommonJS, untyped) that playback.ts uses. */
|
|
2
|
+
declare module "sherpa-onnx-node" {
|
|
3
|
+
export class GenerationConfig {
|
|
4
|
+
constructor(options: {
|
|
5
|
+
sid?: number;
|
|
6
|
+
speed?: number;
|
|
7
|
+
numSteps?: number;
|
|
8
|
+
silenceScale?: number;
|
|
9
|
+
extra?: Record<string, number | string>;
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class OfflineTts {
|
|
14
|
+
static createAsync(config: {
|
|
15
|
+
model: { supertonic: Record<string, string>; numThreads?: number; provider?: string; debug?: number };
|
|
16
|
+
maxNumSentences?: number;
|
|
17
|
+
}): Promise<OfflineTts>;
|
|
18
|
+
readonly sampleRate: number;
|
|
19
|
+
readonly numSpeakers: number;
|
|
20
|
+
generateAsync(request: {
|
|
21
|
+
text: string;
|
|
22
|
+
generationConfig: GenerationConfig;
|
|
23
|
+
onProgress?: (info: { samples: Float32Array; progress: number }) => number | boolean | void;
|
|
24
|
+
}): Promise<{ samples: Float32Array; sampleRate: number }>;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const sherpa: { OfflineTts: typeof OfflineTts; GenerationConfig: typeof GenerationConfig };
|
|
28
|
+
export default sherpa;
|
|
29
|
+
}
|