nixamp 0.21.4 → 0.22.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.
@@ -0,0 +1,177 @@
1
+ /**
2
+ * `nixamp transcribe` -- a recording in, the words out, and into a room.
3
+ *
4
+ * The ear is nixamp.com's (see speech.ts): this machine sends a WAV and
5
+ * gets text back, signed in as whoever it is signed in as. Anything that
6
+ * is not already a WAV goes through the ffmpeg nixamp plays with, to 16 kHz
7
+ * mono, which is what the ear listens at and the smallest thing to send.
8
+ *
9
+ * With `--say SERVER`, the words are posted to that server's trollbox as
10
+ * this account, by the same rules as typing them: one line a second, and
11
+ * signed with the public handle. The MCP tool of the same name is this
12
+ * function with a different front.
13
+ */
14
+ import { spawnSync } from "node:child_process";
15
+ import { readFileSync } from "node:fs";
16
+ import { detectTools, type Tools } from "./audio.ts";
17
+ import { readSession, type Session } from "./session.ts";
18
+ import { RATE, isWav } from "./speech.ts";
19
+
20
+ const HELP = `nixamp transcribe — say it, and have it written down.
21
+
22
+ nixamp transcribe FILE the words in a recording
23
+ nixamp transcribe FILE --say SERVER and post them to that server's trollbox
24
+ nixamp transcribe FILE --say SERVER --channel ID to one channel's room (default: live)
25
+ nixamp transcribe FILE --language de when Whisper should not guess
26
+ nixamp transcribe FILE --json the answer as JSON
27
+
28
+ FILE is any recording ffmpeg can read; a WAV needs no ffmpeg at all. The
29
+ hearing is done by nixamp.com with an open-source model on its own CPU, so
30
+ this needs a sign-in (\`nixamp login\`) and nothing else. Up to a minute at
31
+ a time.
32
+
33
+ SERVER is the address of the nixamp whose room it is, as in its share link:
34
+ https://server1.chovy.nixamp.com:4321. The room is that server's own stream
35
+ unless --channel names one of its channels.
36
+ `;
37
+
38
+ /** Where the sound may be sent, as a request. */
39
+ export interface Ask {
40
+ wav: Uint8Array;
41
+ language?: string;
42
+ /** A room to post the words to: the server's address, and its channel. */
43
+ server?: string;
44
+ channel?: string;
45
+ }
46
+
47
+ export interface Heard {
48
+ text: string;
49
+ seconds: number;
50
+ model?: string;
51
+ /** The trollbox line, when a room was named. */
52
+ message?: { id: string; handle: string; body: string; createdAt: string };
53
+ }
54
+
55
+ export type Answer = { ok: true; heard: Heard } | { ok: false; status: number; error: string };
56
+
57
+ /**
58
+ * The file as a WAV: as it is when it already is one, through ffmpeg to
59
+ * 16 kHz mono otherwise. Throws a sentence when neither is possible.
60
+ */
61
+ export function wavOf(path: string, tools: () => Pick<Tools, "ffmpeg" | "carries"> = detectTools): Uint8Array {
62
+ let bytes: Uint8Array;
63
+ try {
64
+ bytes = new Uint8Array(readFileSync(path));
65
+ } catch {
66
+ throw new Error(`cannot read ${path}`);
67
+ }
68
+ if (isWav(bytes)) return bytes;
69
+ const found = tools();
70
+ if (found.carries === false) throw new Error(`${path} is not a WAV, and there is no ffmpeg here to convert it. Install ffmpeg, or record a WAV.`);
71
+ const [command, ...prefix] = found.ffmpeg;
72
+ const run = spawnSync(command as string, [...prefix, "-v", "error", "-i", path, "-vn", "-ac", "1", "-ar", String(RATE), "-f", "wav", "-"], {
73
+ maxBuffer: 64 * 1024 * 1024,
74
+ });
75
+ if (run.error || run.status !== 0 || !run.stdout || run.stdout.length < 44) {
76
+ const why = run.stderr ? run.stderr.toString("utf8").trim().split("\n").pop() : run.error?.message;
77
+ throw new Error(`ffmpeg could not read ${path}${why ? `: ${why}` : ""}`);
78
+ }
79
+ return new Uint8Array(run.stdout);
80
+ }
81
+
82
+ /** The ask, made: one POST to the site the session belongs to. */
83
+ export async function askToHear(session: Pick<Session, "site" | "token">, ask: Ask, fetcher: typeof fetch = fetch, site = session.site): Promise<Answer> {
84
+ const url = new URL(`${site.replace(/\/+$/, "")}/api/v1/speech/transcribe`);
85
+ if (ask.language) url.searchParams.set("language", ask.language);
86
+ if (ask.server) {
87
+ url.searchParams.set("server", ask.server);
88
+ url.searchParams.set("channel", ask.channel || "live");
89
+ }
90
+ let response: Response;
91
+ try {
92
+ response = await fetcher(url.toString(), {
93
+ method: "POST",
94
+ headers: { authorization: `Bearer ${session.token}`, "content-type": "audio/wav" },
95
+ body: new Blob([ask.wav.buffer.slice(ask.wav.byteOffset, ask.wav.byteOffset + ask.wav.byteLength) as ArrayBuffer]),
96
+ });
97
+ } catch (error) {
98
+ return { ok: false, status: 0, error: `could not reach ${url.origin}: ${(error as Error).message}` };
99
+ }
100
+ const body = (await response.json().catch(() => ({}))) as Partial<Heard> & { error?: string };
101
+ if (!response.ok) return { ok: false, status: response.status, error: body.error ?? `nixamp answered ${response.status}` };
102
+ return {
103
+ ok: true,
104
+ heard: {
105
+ text: body.text ?? "",
106
+ seconds: body.seconds ?? 0,
107
+ ...(body.model ? { model: body.model } : {}),
108
+ ...(body.message ? { message: body.message } : {}),
109
+ },
110
+ };
111
+ }
112
+
113
+ function flag(argv: string[], name: string): string | undefined {
114
+ const at = argv.indexOf(name);
115
+ return at === -1 ? undefined : argv[at + 1];
116
+ }
117
+
118
+ export interface TranscribeDeps {
119
+ fetcher?: typeof fetch;
120
+ wavOf?: typeof wavOf;
121
+ session?: Pick<Session, "site" | "token"> | null;
122
+ }
123
+
124
+ export async function transcribe(argv: string[], deps: TranscribeDeps = {}): Promise<number> {
125
+ if (argv.length === 0 || argv.includes("--help") || argv.includes("-h") || argv[0] === "help") {
126
+ console.log(HELP);
127
+ return argv.length === 0 ? 64 : 0;
128
+ }
129
+ const session = deps.session === undefined ? readSession() : deps.session;
130
+ if (session === null) {
131
+ console.error("nixamp: not signed in. Try `nixamp login`.");
132
+ return 1;
133
+ }
134
+ const withValue = new Set(["--say", "--channel", "--language", "--site"]);
135
+ const file = argv.find((one, at) => !one.startsWith("-") && !(at > 0 && withValue.has(argv[at - 1] as string)));
136
+ if (!file) {
137
+ console.error("nixamp: which recording? `nixamp transcribe clip.m4a`.");
138
+ return 64;
139
+ }
140
+ const server = flag(argv, "--say");
141
+ if (argv.includes("--say") && !server) {
142
+ console.error("nixamp: --say needs the server's address, as in its share link.");
143
+ return 64;
144
+ }
145
+ let wav: Uint8Array;
146
+ try {
147
+ wav = (deps.wavOf ?? wavOf)(file);
148
+ } catch (error) {
149
+ console.error(`nixamp: ${(error as Error).message}`);
150
+ return 1;
151
+ }
152
+ const ask: Ask = {
153
+ wav,
154
+ ...(flag(argv, "--language") ? { language: flag(argv, "--language") } : {}),
155
+ ...(server ? { server, channel: flag(argv, "--channel") ?? "live" } : {}),
156
+ };
157
+ const answer = await askToHear(session, ask, deps.fetcher ?? fetch, flag(argv, "--site") ?? session.site);
158
+ if (!answer.ok) {
159
+ console.error(`nixamp: ${answer.error}`);
160
+ return 1;
161
+ }
162
+ if (argv.includes("--json")) {
163
+ console.log(JSON.stringify(answer.heard, null, 2));
164
+ return 0;
165
+ }
166
+ if (answer.heard.text === "") {
167
+ console.error("nixamp: heard nothing in that.");
168
+ return 1;
169
+ }
170
+ console.log(answer.heard.text);
171
+ if (answer.heard.message) {
172
+ console.error(` Said in the room for ${ask.channel} at ${server} as ${answer.heard.message.handle}.`);
173
+ } else if (server) {
174
+ console.error(" Nothing was posted: there were no words to post.");
175
+ }
176
+ return 0;
177
+ }
@@ -0,0 +1,150 @@
1
+ /**
2
+ * `nixamp transcript` -- what a channel is saying, in the terminal.
3
+ *
4
+ * The server carrying the channel captions it (see captions.ts) and keeps
5
+ * the recent lines; this asks for them, and with --follow keeps asking, a
6
+ * poll every few seconds with the last line's time so nothing is printed
7
+ * twice. A poll rather than the SSE the page uses because each ask also
8
+ * keeps the captioner alive, which is what a terminal that is still
9
+ * reading wants, and there is nothing to parse.
10
+ *
11
+ * Pointed at a server the way `nixamp admin` is: --url and --key from its
12
+ * share link, or the daemon on this machine when neither is given.
13
+ */
14
+ import { resolveTarget } from "./admin.ts";
15
+ import { KEY_HEADER } from "./share.ts";
16
+
17
+ const HELP = `nixamp transcript — what a channel is saying, written down.
18
+
19
+ nixamp transcript --channel ID the recent lines from this machine's daemon
20
+ nixamp transcript --url URL --key K --channel ID from another server, with its share link
21
+ nixamp transcript ... --follow and keep printing as it speaks
22
+ nixamp transcript ... --json the lines as JSON
23
+
24
+ ID is the channel's id as the server names it (the address bar says it when
25
+ you are watching one). The server captions a channel while somebody is asking
26
+ for the transcript, with nixamp.com's ear; it needs an ffmpeg and a sign-in
27
+ (\`nixamp login\`) on that server.
28
+ `;
29
+
30
+ export interface TranscriptLine {
31
+ channel: string;
32
+ at: number;
33
+ until: number;
34
+ text: string;
35
+ }
36
+
37
+ export interface TranscriptAnswer {
38
+ channel: string;
39
+ backlog: number;
40
+ now: number;
41
+ on: boolean;
42
+ lines: number;
43
+ error: string;
44
+ /** The recent lines, oldest first. Named `lines` on the wire; renamed here so the count above keeps its name. */
45
+ recent: TranscriptLine[];
46
+ }
47
+
48
+ export type Fetched = { ok: true; answer: TranscriptAnswer } | { ok: false; status: number; error: string };
49
+
50
+ /** One ask for a channel's recent lines, after a moment when given. */
51
+ export async function readTranscript(
52
+ target: { url: string; key: string | null },
53
+ channel: string,
54
+ after = 0,
55
+ fetcher: typeof fetch = fetch,
56
+ ): Promise<Fetched> {
57
+ const url = new URL(`${target.url.replace(/\/+$/, "")}/api/channels/${encodeURIComponent(channel)}/transcript`);
58
+ if (after > 0) url.searchParams.set("after", String(after));
59
+ let response: Response;
60
+ try {
61
+ response = await fetcher(url.toString(), { headers: target.key ? { [KEY_HEADER]: target.key } : {} });
62
+ } catch (error) {
63
+ return { ok: false, status: 0, error: `could not reach ${url.origin}: ${(error as Error).message}` };
64
+ }
65
+ const body = (await response.json().catch(() => ({}))) as {
66
+ channel?: string; backlog?: number; now?: number; on?: boolean; lines?: TranscriptLine[] | number; error?: string;
67
+ };
68
+ if (!response.ok) return { ok: false, status: response.status, error: body.error ?? `the server answered ${response.status}` };
69
+ const recent = Array.isArray(body.lines) ? body.lines : [];
70
+ return {
71
+ ok: true,
72
+ answer: {
73
+ channel: body.channel ?? channel,
74
+ backlog: body.backlog ?? 0,
75
+ now: body.now ?? Date.now(),
76
+ on: body.on ?? false,
77
+ lines: recent.length,
78
+ error: body.error ?? "",
79
+ recent,
80
+ },
81
+ };
82
+ }
83
+
84
+ /** A line as the terminal prints it: the time its sound was heard, then the words. */
85
+ export function printed(line: TranscriptLine): string {
86
+ const at = new Date(line.at);
87
+ const clock = Number.isNaN(at.getTime()) ? "--:--:--" : at.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit", second: "2-digit" });
88
+ return `${clock} ${line.text}`;
89
+ }
90
+
91
+ function flag(argv: string[], name: string): string | undefined {
92
+ const at = argv.indexOf(name);
93
+ return at === -1 ? undefined : argv[at + 1];
94
+ }
95
+
96
+ export interface TranscriptDeps {
97
+ fetcher?: typeof fetch;
98
+ /** How long --follow waits between asks. Short in the tests. */
99
+ everyMs?: number;
100
+ /** How many asks --follow makes before it stops. Forever, except in the tests. */
101
+ polls?: number;
102
+ sleep?: (ms: number) => Promise<void>;
103
+ }
104
+
105
+ export async function transcript(argv: string[], deps: TranscriptDeps = {}): Promise<number> {
106
+ if (argv.includes("--help") || argv.includes("-h") || argv[0] === "help") {
107
+ console.log(HELP);
108
+ return 0;
109
+ }
110
+ let target: { url: string; key: string | null };
111
+ try {
112
+ target = resolveTarget(argv);
113
+ } catch (error) {
114
+ console.error((error as Error).message);
115
+ return 1;
116
+ }
117
+ const channel = flag(argv, "--channel") ?? "main";
118
+ const follow = argv.includes("--follow") || argv.includes("-f");
119
+ const asJson = argv.includes("--json");
120
+ const fetcher = deps.fetcher ?? fetch;
121
+ const sleep = deps.sleep ?? ((ms: number) => new Promise<void>((resolve) => setTimeout(resolve, ms)));
122
+ let after = 0;
123
+ let polls = 0;
124
+ for (;;) {
125
+ const got = await readTranscript(target, channel, after, fetcher);
126
+ if (!got.ok) {
127
+ console.error(`nixamp: ${got.error}`);
128
+ return 1;
129
+ }
130
+ if (asJson) {
131
+ console.log(JSON.stringify(got.answer.recent, null, 2));
132
+ if (!follow) return 0;
133
+ } else {
134
+ if (after === 0 && got.answer.recent.length === 0 && !follow) {
135
+ console.log(got.answer.error
136
+ ? `Nothing yet: ${got.answer.error}`
137
+ : "Nothing said yet. The server has just started listening; ask again in a few seconds, or --follow.");
138
+ return 0;
139
+ }
140
+ for (const line of got.answer.recent) console.log(printed(line));
141
+ if (!follow) return 0;
142
+ if (after === 0 && got.answer.error) console.error(`nixamp: ${got.answer.error}`);
143
+ }
144
+ const last = got.answer.recent[got.answer.recent.length - 1];
145
+ if (last) after = last.at;
146
+ polls += 1;
147
+ if (deps.polls !== undefined && polls >= deps.polls) return 0;
148
+ await sleep(deps.everyMs ?? 5000);
149
+ }
150
+ }
@@ -0,0 +1,29 @@
1
+ {
2
+ "openaccess": "0.1",
3
+ "name": "nixamp",
4
+ "url": "https://nixamp.com",
5
+ "operator": "https://logicsrc.com/.well-known/openprofile.md",
6
+ "redirect_uris": [
7
+ "https://nixamp.com/api/v1/openaccess/callback"
8
+ ],
9
+ "jwks": {
10
+ "keys": [
11
+ {
12
+ "kty": "OKP",
13
+ "crv": "Ed25519",
14
+ "kid": "CtuHXWFE9Z1P",
15
+ "x": "mHN15NDPYMbdE_Zz8-ePGeskTU2yEM2I7KBp5HqM6xA",
16
+ "alg": "EdDSA",
17
+ "use": "sig"
18
+ }
19
+ ]
20
+ },
21
+ "scopes": {},
22
+ "honours": [
23
+ "profullstack.com/all-access"
24
+ ],
25
+ "webhooks": "https://nixamp.com/api/v1/openaccess/events",
26
+ "hubs": [
27
+ "https://openaccess.logicsrc.com"
28
+ ]
29
+ }
@@ -1 +1 @@
1
- import{t as e}from"./index-DMVUa6vY.js";var t=3;async function n(n){let{media:r,src:i,isTv:a}=n,{default:o}=await e(async()=>{let{default:e}=await import(`./hls-n74Cnh8A.js`);return{default:e}},[]);if(!o.isSupported())return n.onError(`This browser cannot play HLS streams.`),{destroy:()=>void 0,levels:()=>[]};let s=new o({...a?{maxBufferLength:60,maxMaxBufferLength:120,backBufferLength:30,liveSyncDurationCount:4}:{backBufferLength:90},enableWorker:!0}),c=0,l=!1;s.on(o.Events.ERROR,(e,r)=>{if(!l&&r.fatal){if(c>=t){n.onError(`This stream kept failing and has been stopped.`),s.destroy();return}switch(c+=1,r.type){case o.ErrorTypes.NETWORK_ERROR:n.onNotice(`Reconnecting…`),s.startLoad();break;case o.ErrorTypes.MEDIA_ERROR:n.onNotice(`Recovering…`),s.recoverMediaError();break;default:n.onError(`This stream could not be played.`),s.destroy()}}}),s.on(o.Events.MANIFEST_PARSED,()=>{l||(n.onNotice(null),n.onReady?.({live:s.levels.length>0&&!Number.isFinite(r.duration),levels:u()}))}),s.on(o.Events.LEVEL_LOADED,(e,t)=>{l||n.onReady?.({live:t.details.live,levels:u()})}),s.on(o.Events.FRAG_BUFFERED,()=>{l||n.onNotice(null)});function u(){return s.levels.map((e,t)=>({index:t,height:e.height||null,bitrate:e.bitrate||null,label:e.height?`${String(e.height)}p`:`${String(Math.round((e.bitrate||0)/1e3))}k`}))}return s.loadSource(i),s.attachMedia(r),{destroy(){l=!0,s.destroy()},levels:u,setLevel(e){s.currentLevel=e},currentLevel:()=>s.autoLevelEnabled?-1:s.currentLevel}}export{n as createHlsEngine};
1
+ import{t as e}from"./index-BIL7sVli.js";var t=3;async function n(n){let{media:r,src:i,isTv:a}=n,{default:o}=await e(async()=>{let{default:e}=await import(`./hls-n74Cnh8A.js`);return{default:e}},[]);if(!o.isSupported())return n.onError(`This browser cannot play HLS streams.`),{destroy:()=>void 0,levels:()=>[]};let s=new o({...a?{maxBufferLength:60,maxMaxBufferLength:120,backBufferLength:30,liveSyncDurationCount:4}:{backBufferLength:90},enableWorker:!0}),c=0,l=!1;s.on(o.Events.ERROR,(e,r)=>{if(!l&&r.fatal){if(c>=t){n.onError(`This stream kept failing and has been stopped.`),s.destroy();return}switch(c+=1,r.type){case o.ErrorTypes.NETWORK_ERROR:n.onNotice(`Reconnecting…`),s.startLoad();break;case o.ErrorTypes.MEDIA_ERROR:n.onNotice(`Recovering…`),s.recoverMediaError();break;default:n.onError(`This stream could not be played.`),s.destroy()}}}),s.on(o.Events.MANIFEST_PARSED,()=>{l||(n.onNotice(null),n.onReady?.({live:s.levels.length>0&&!Number.isFinite(r.duration),levels:u()}))}),s.on(o.Events.LEVEL_LOADED,(e,t)=>{l||n.onReady?.({live:t.details.live,levels:u()})}),s.on(o.Events.FRAG_BUFFERED,()=>{l||n.onNotice(null)});function u(){return s.levels.map((e,t)=>({index:t,height:e.height||null,bitrate:e.bitrate||null,label:e.height?`${String(e.height)}p`:`${String(Math.round((e.bitrate||0)/1e3))}k`}))}return s.loadSource(i),s.attachMedia(r),{destroy(){l=!0,s.destroy()},levels:u,setLevel(e){s.currentLevel=e},currentLevel:()=>s.autoLevelEnabled?-1:s.currentLevel}}export{n as createHlsEngine};