clavarium 0.2.176-next.1 → 0.2.178-next.1

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.
@@ -1,12 +1,12 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.176-next.1",
3
+ "version": "0.2.178-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.176-next.1",
9
+ "version": "0.2.178-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
@@ -14,7 +14,7 @@
14
14
  "clavarium-binder": "0.1.43-next.1",
15
15
  "clavarium-docket": "0.1.1",
16
16
  "clavarium-parley": "0.17.33-next.1",
17
- "clavarium-tether": "0.1.0-next.42",
17
+ "clavarium-tether": "0.1.0-next.43",
18
18
  "mermaid": "11.17.2",
19
19
  "node-pty": "^1.1.0",
20
20
  "pdfjs-dist": "6.4.299",
@@ -760,9 +760,9 @@
760
760
  }
761
761
  },
762
762
  "node_modules/clavarium-tether": {
763
- "version": "0.1.0-next.42",
764
- "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.42.tgz",
765
- "integrity": "sha512-ms19DnB+9f6x8C0H5Qp6XCJIk/TA8oIT4UisAtiyEUlfa0tHVYITaAh7jO11tDRvvk2lnh439EBRgGVeKdFJIA==",
763
+ "version": "0.1.0-next.43",
764
+ "resolved": "https://registry.npmjs.org/clavarium-tether/-/clavarium-tether-0.1.0-next.43.tgz",
765
+ "integrity": "sha512-vAoj19cWZiXMpDkRnhV1P+oNWv6dGL65eur/2PbxNiP5i9uWI5tipRoQfnff5Xkqd5o+E4vbRnWvzlHAJTTX5A==",
766
766
  "license": "MIT",
767
767
  "bin": {
768
768
  "tether": "bin/cli.mjs",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.176-next.1",
3
+ "version": "0.2.178-next.1",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "description": "A conversation is a VM with a terminal in it. Everything else is a folder.",
@@ -47,7 +47,7 @@
47
47
  "clavarium-binder": "0.1.43-next.1",
48
48
  "clavarium-docket": "0.1.1",
49
49
  "clavarium-parley": "0.17.33-next.1",
50
- "clavarium-tether": "0.1.0-next.42",
50
+ "clavarium-tether": "0.1.0-next.43",
51
51
  "mermaid": "11.17.2",
52
52
  "node-pty": "^1.1.0",
53
53
  "pdfjs-dist": "6.4.299",
package/server/boxes.mjs CHANGED
@@ -59,6 +59,30 @@ export function wavOf(pcm, rate = 16_000) {
59
59
  return Buffer.concat([h, pcm]);
60
60
  }
61
61
 
62
+ /* A .wav (16-bit PCM, any rate, mono or stereo) as a box plays it: 16 kHz mono, a minute at most. */
63
+ export const PLAY_SECONDS = 70;
64
+ export function pcmOfWav(wav) {
65
+ const b = Buffer.isBuffer(wav) ? wav : Buffer.from(wav);
66
+ if (b.toString('ascii', 0, 4) !== 'RIFF' || b.toString('ascii', 8, 12) !== 'WAVE') throw new Refusal(400, 'Not a .wav file.');
67
+ let at = 12; let fmt = null; let data = null;
68
+ while (at + 8 <= b.length) {
69
+ const id = b.toString('ascii', at, at + 4); const size = b.readUInt32LE(at + 4);
70
+ if (id === 'fmt ') fmt = { format: b.readUInt16LE(at + 8), channels: b.readUInt16LE(at + 10), rate: b.readUInt32LE(at + 12), bits: b.readUInt16LE(at + 22) };
71
+ if (id === 'data') { data = b.subarray(at + 8, Math.min(b.length, at + 8 + size)); break; }
72
+ at += 8 + size + (size & 1);
73
+ }
74
+ if (!fmt || !data || fmt.format !== 1 || fmt.bits !== 16 || !fmt.channels || !fmt.rate) throw new Refusal(400, 'A .wav of 16-bit PCM, please.');
75
+ const frames = Math.floor(data.length / (2 * fmt.channels));
76
+ const n = Math.min(Math.floor(frames * 16_000 / fmt.rate), PLAY_SECONDS * 16_000);
77
+ const out = Buffer.alloc(n * 2);
78
+ const sample = (f) => { let sum = 0; for (let c = 0; c < fmt.channels; c++) sum += data.readInt16LE((f * fmt.channels + c) * 2); return sum / fmt.channels; };
79
+ for (let i = 0; i < n; i++) {
80
+ const x = i * fmt.rate / 16_000; const j = Math.floor(x); const k = Math.min(j + 1, frames - 1);
81
+ out.writeInt16LE(Math.max(-32768, Math.min(32767, Math.round(sample(j) + (sample(k) - sample(j)) * (x - j)))), i * 2);
82
+ }
83
+ return out;
84
+ }
85
+
62
86
  /* Text for the ear: what a written message holds and a voice should not read. */
63
87
  export function forSpeech(text) {
64
88
  return String(text ?? '')
@@ -147,6 +171,15 @@ export class Boxes {
147
171
  return a;
148
172
  }
149
173
 
174
+ /* A sound played on a box (a sample, a recorded message), as its answers are. */
175
+ play(id, wav) {
176
+ const link = this.#links.get(id);
177
+ if (!link) throw new Refusal(409, 'This voice box is not connected.');
178
+ const pcm = pcmOfWav(wav);
179
+ link.play(pcm);
180
+ return { seconds: Math.round(pcm.length / 32_000) };
181
+ }
182
+
150
183
  /* UPDATED OVER ITS WI-FI: the image is offered at a one-use address on this server (under /c/,
151
184
  no sign-on: the token is the right), the box told to fetch it with its sha256; it answers
152
185
  once installed, then restarts. */
@@ -372,6 +405,7 @@ export function voiceboxConnector(boxes) {
372
405
  async run(verb, t, a = {}) {
373
406
  const id = t.config?.box;
374
407
  if (verb === 'voicebox.volume') { const r = await boxes.setVolume(id, a.level); return { text: r?.offline ? `Volume ${a.level} kept: the box is offline, it gets it when it comes back.` : `Volume set to ${a.level}.` }; }
408
+ if (verb === 'voicebox.play') { const r = boxes.play(id, Buffer.from(String(a.image ?? ''), 'base64')); return { text: `Played (${r.seconds} s).` }; }
375
409
  if (verb === 'voicebox.update') { await boxes.update(id, a.image, a.version); return { text: `Installed${a.version ? ` (${a.version})` : ''}: the box restarts and comes back in a moment.` }; }
376
410
  if (verb === 'voicebox.alert') { await boxes.command(id, { t: 'alert', text: String(a.text ?? '').slice(0, 300) }, 10 * 60_000); return { text: 'The alert was seen: someone pressed its button.' }; }
377
411
  throw new Error(`${verb} is not for a voice box`);
package/server/speech.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  /* SPEECH: text read aloud, for the voice boxes (boxes.mjs).
2
2
  *
3
3
  * On this server, with nothing sent anywhere: a Piper voice (open, French by
4
- * default) run by sherpa-onnx. The voice is fetched once, the first time it is
4
+ * default: UPMC, its first speaker) run by sherpa-onnx. The voice is fetched once, the first time it is
5
5
  * needed, into the data folder; then it works offline. About 0.3 s of work for
6
6
  * 4 s of speech on one core.
7
7
  *
@@ -14,7 +14,7 @@ import { execFile } from 'node:child_process';
14
14
  import { promisify } from 'node:util';
15
15
 
16
16
  export const VOICES = Object.freeze({
17
- fr: 'vits-piper-fr_FR-siwis-medium',
17
+ fr: 'vits-piper-fr_FR-upmc-medium', /* chosen by ear on the real box (31-10), over Siwis, Tom, Gilles and Kokoro */
18
18
  en: 'vits-piper-en_US-amy-medium',
19
19
  });
20
20
  export const VOICE_URL = (name) => `https://github.com/k2-fsa/sherpa-onnx/releases/download/tts-models/${name}.tar.bz2`;