fixup! feat(speech): the speech capability over Cap'n Web, behind a gate
check / check (push) Canceled after 0s
check / check (push) Canceled after 0s
This commit is contained in:
1 parent
372f6a2b96
commit
a97ba6c174
12 files changed
+245
-92
No files matched your search
Regular → Executable
+44
-18
@@ -1,36 +1,62 @@
|
||||
#!/usr/bin/env node
|
||||
/**
|
||||
* @file speech-server: the speech capability over Cap'n Web, behind a gate.
|
||||
* usage: node server.js HOST PORT HEAR_SH MODEL SPEAK_SH VOICE
|
||||
* usage: server.js HOST PORT HEAR_SH MODEL SPEAK_SH VOICE [PIPER_OPTION...]
|
||||
* The key is in $CREDENTIALS_DIRECTORY/speech-key (systemd LoadCredential), not argv, where ps would show it.
|
||||
* HEAR_SH MODEL reads a WAV on stdin and prints whisper's transcript (hear.sh).
|
||||
* SPEAK_SH VOICE reads text on stdin and writes piper's WAV of it (speak.sh).
|
||||
* SPEAK_SH VOICE reads text on stdin and writes piper's WAV of it (speak.sh), with any PIPER_OPTIONs.
|
||||
*/
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { newWebSocketRpcSession } from 'capnweb';
|
||||
import { spawn as spawnChild } from 'node:child_process';
|
||||
import { readFile } from 'node:fs/promises';
|
||||
import { makeGate } from './gate.js';
|
||||
import { makeSpeech, MAX_CLIP_BYTES } from './speech.js';
|
||||
import { runWith, timed } from './run.js';
|
||||
import process from 'node:process';
|
||||
import { WebSocketServer } from 'ws';
|
||||
import { Gate } from './gate.js';
|
||||
import { makeCommand, timed } from './run.js';
|
||||
import { Speech, MAX_CLIP_BYTES } from './speech.js';
|
||||
|
||||
const main = async ([host, port, hearSh, model, speakSh, voice], env) => {
|
||||
const key = (await readFile(`${env.CREDENTIALS_DIRECTORY}/speech-key`, 'utf8')).trim();
|
||||
const speech = makeSpeech(
|
||||
(clip) => timed(`heard ${clip.length} bytes`,
|
||||
async () => (await runWith(hearSh, [model], clip)).toString().trim(), (text) => text),
|
||||
(text) => timed(`spoke ${text.length} chars`,
|
||||
async () => new Uint8Array(await runWith(speakSh, [voice], text)), (wav) => `${wav.length} bytes of WAV`),
|
||||
/**
|
||||
* Serve the speech capability, behind a gate, at HOST:PORT.
|
||||
*
|
||||
* @param {object} [io] what it reaches; the process's own, by default
|
||||
* @param {string[]} [io.argv] HOST PORT HEAR_SH MODEL SPEAK_SH VOICE [PIPER_OPTION...]
|
||||
* @param {Record<string, string | undefined>} [io.env] for CREDENTIALS_DIRECTORY
|
||||
* @param {typeof readFile} [io.read] reads the key
|
||||
* @param {typeof spawnChild} [io.spawn] runs HEAR_SH and SPEAK_SH, and nothing else
|
||||
* @param {() => number} [io.now] the clock, for timing jobs
|
||||
* @param {(options: import('ws').ServerOptions) => WebSocketServer} [io.listen]
|
||||
* @param {typeof newWebSocketRpcSession} [io.serve] serves the gate on one connection
|
||||
*/
|
||||
const main = async ({
|
||||
argv = process.argv.slice(2),
|
||||
env = process.env,
|
||||
read = readFile,
|
||||
spawn = spawnChild,
|
||||
now = Date.now,
|
||||
listen = (options) => new WebSocketServer(options),
|
||||
serve = newWebSocketRpcSession,
|
||||
} = {}) => {
|
||||
const [host, port, hearSh, model, speakSh, voice, ...piper] = argv;
|
||||
const key = (await read(`${env.CREDENTIALS_DIRECTORY}/speech-key`, 'utf8')).trim();
|
||||
const hear = makeCommand(spawn, hearSh, [model]);
|
||||
const say = makeCommand(spawn, speakSh, [voice, ...piper]);
|
||||
const speech = new Speech(
|
||||
(clip) => timed(now, `heard ${clip.length} bytes`,
|
||||
async () => (await hear(clip)).toString().trim(), (text) => text),
|
||||
(text) => timed(now, `spoke ${text.length} chars`,
|
||||
async () => new Uint8Array(await say(text)), (wav) => `${wav.length} bytes of WAV`),
|
||||
);
|
||||
const gate = makeGate(key, speech);
|
||||
const gate = new Gate(key, speech);
|
||||
// A clip travels as base64 inside JSON: 4/3 of its size, plus a little.
|
||||
const wss = new WebSocketServer({ host, port: Number(port), maxPayload: Math.ceil(MAX_CLIP_BYTES * 1.4) });
|
||||
const wss = listen({ host, port: Number(port), maxPayload: Math.ceil(MAX_CLIP_BYTES * 1.4) });
|
||||
wss.on('connection', (ws, req) => {
|
||||
console.log(`connection from ${req.socket.remoteAddress}`);
|
||||
newWebSocketRpcSession(ws, gate);
|
||||
serve(ws, gate);
|
||||
});
|
||||
console.log(`speech-server: listening on ${host}:${port}`);
|
||||
};
|
||||
|
||||
main(process.argv.slice(2), process.env).catch((err) => {
|
||||
main().catch((err) => {
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
process.exitCode = 1;
|
||||
});
|
||||
Reference in new issue
Block a user