63 lines
2.7 KiB
JavaScript
Executable File
63 lines
2.7 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
/**
|
|
* @file speech-server: the speech capability over Cap'n Web, behind a gate.
|
|
* 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), with any PIPER_OPTIONs.
|
|
*/
|
|
import { newWebSocketRpcSession } from 'capnweb';
|
|
import { spawn as spawnChild } from 'node:child_process';
|
|
import { readFile } from 'node:fs/promises';
|
|
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';
|
|
|
|
/**
|
|
* 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 = new Gate(key, speech);
|
|
// A clip travels as base64 inside JSON: 4/3 of its size, plus a little.
|
|
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}`);
|
|
serve(ws, gate);
|
|
});
|
|
console.log(`speech-server: listening on ${host}:${port}`);
|
|
};
|
|
|
|
main().catch((err) => {
|
|
console.error(err);
|
|
process.exitCode = 1;
|
|
});
|