clavarium 0.2.203-next.1 → 0.2.206-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.203-next.1",
3
+ "version": "0.2.206-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.203-next.1",
9
+ "version": "0.2.206-next.1",
10
10
  "license": "MIT",
11
11
  "dependencies": {
12
12
  "@xterm/addon-fit": "^0.11.0",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clavarium",
3
- "version": "0.2.203-next.1",
3
+ "version": "0.2.206-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.",
package/server/boxes.mjs CHANGED
@@ -123,6 +123,15 @@ export function offsetOf(zone, now) {
123
123
  }
124
124
 
125
125
  export const RELAY_WITHIN_MS = 20_000;
126
+ /* Sentences to say one by one: cut after . ! ? … and kept at 40 characters at least (a tiny one joins the next). */
127
+ export function sentencesOf(text) {
128
+ const out = [];
129
+ for (const piece of String(text).split(/(?<=[.!?…])\s+/)) {
130
+ if (out.length && out[out.length - 1].length < 40) out[out.length - 1] += ` ${piece}`; else out.push(piece);
131
+ }
132
+ return out.filter(Boolean);
133
+ }
134
+
126
135
  export const DOING_WITHIN_MS = 120_000;
127
136
  /* A LONG RECORDING (a meeting, a class): asked in words, stopped by the button or the wake word. */
128
137
  export const RECORD_ASKED = /\b(?:lance|d[ée]marre|commence|d[ée]bute)\w*\b.{0,25}\benregistrement\b|\benregistre\b.{0,20}\b(?:r[ée]union|conversation|cours|discussion|consultation)\b/i;
@@ -131,6 +140,22 @@ export const mmss = (s) => `${String(Math.floor(s / 60)).padStart(2, '0')}:${Str
131
140
  /* THE MIXER of a box that plays a stream: words said meanwhile go over it, the stream lowered to DUCK while
132
141
  they last, with a fade down and up (a quarter of a second) rather than a jump. One 20 ms chunk at a time. */
133
142
  export const DUCK = 0.2;
143
+ /* LEVELS (01-11): like a phone, one volume for the whole box (set, and eased to the room by the box itself),
144
+ and under it each kind of sound brought to its own steady level -- a loud radio and a quiet one played alike
145
+ (ffmpeg's loudnorm, EBU R128), the radio a step below the box's voice, its voice always at one loudness. */
146
+ export const STREAM_LUFS = -26;
147
+ export const STREAM_LEVEL = 1;
148
+ export const VOICE_RMS = 3300; /* about -20 dBFS: the loudness the voice was found right at */
149
+ export function levelled(pcm, target = VOICE_RMS) {
150
+ let sum = 0; let n = 0;
151
+ for (let i = 0; i + 1 < pcm.length; i += 2) { const v = pcm.readInt16LE(i); if (Math.abs(v) > 300) { sum += v * v; n++; } }
152
+ if (n < 100) return pcm;
153
+ const gain = Math.min(4, target / Math.sqrt(sum / n));
154
+ if (Math.abs(gain - 1) < 0.05) return pcm;
155
+ const out = Buffer.alloc(pcm.length & ~1);
156
+ for (let i = 0; i + 1 < pcm.length; i += 2) out.writeInt16LE(Math.max(-32768, Math.min(32767, Math.round(pcm.readInt16LE(i) * gain))), i);
157
+ return out;
158
+ }
134
159
  export function mix(stream, chunk) {
135
160
  const words = stream.over[0];
136
161
  const target = words ? DUCK : 1;
@@ -138,7 +163,7 @@ export function mix(stream, chunk) {
138
163
  const step = (1 - DUCK) / (0.25 * 16_000);
139
164
  for (let i = 0; i < chunk.length; i += 2) {
140
165
  stream.gain += Math.max(-step, Math.min(step, target - stream.gain));
141
- let v = chunk.readInt16LE(i) * stream.gain;
166
+ let v = chunk.readInt16LE(i) * stream.gain * STREAM_LEVEL;
142
167
  if (words && stream.gain <= DUCK + 0.05) {
143
168
  stream.at ??= 0;
144
169
  if (stream.at + 1 < words.length) { v += words.readInt16LE(stream.at); stream.at += 2; }
@@ -307,7 +332,7 @@ export class Boxes {
307
332
  this.stop(id);
308
333
  const source = await this.#resolve(url);
309
334
  let child;
310
- try { child = this.#spawn('ffmpeg', ['-nostdin', '-loglevel', 'error', '-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5', '-i', source, '-vn', '-ac', '1', '-ar', '16000', '-f', 's16le', '-'], { stdio: ['ignore', 'pipe', 'pipe'] }); }
335
+ try { child = this.#spawn('ffmpeg', ['-nostdin', '-loglevel', 'error', '-reconnect', '1', '-reconnect_streamed', '1', '-reconnect_delay_max', '5', '-i', source, '-vn', '-af', `loudnorm=I=${STREAM_LUFS}:TP=-3:LRA=11`, '-ac', '1', '-ar', '16000', '-f', 's16le', '-'], { stdio: ['ignore', 'pipe', 'pipe'] }); }
311
336
  catch { throw new Refusal(501, 'This server cannot read sounds: ffmpeg is not installed (an update installs it).'); }
312
337
  const box = link.box(); const label = name || new URL(url).host;
313
338
  const stream = { child, queue: [], queued: 0, sent: 0, started: this.#now(), timer: null, ended: false, failed: '', over: [], gain: 1 };
@@ -431,9 +456,16 @@ export class Boxes {
431
456
  const chatting = !doing && answering && this.#chatting(l);
432
457
  const opening = chatting && !l.chat;
433
458
  if (chatting) l.chat = true;
434
- let pcm;
435
- try { pcm = await this.#speak(opening ? `Je passe en mode conversation : je reste à l'écoute. ${words}` : words); } catch (error) { this.#log(`voice box: no voice for the answer (${error.message})`); l.send({ t: 'cue', name: 'error' }); continue; }
436
- l.play(pcm, question || chatting ? 'listen' : null); /* a question is always listened after: an answer is expected */
459
+ /* SENTENCE BY SENTENCE: the first is said while the next ones are made -- the answer starts sooner
460
+ (31-10: a few seconds of silence before each answer). The box queues them, one after the other. */
461
+ const parts = sentencesOf(opening ? `Je passe en mode conversation : je reste à l'écoute. ${words}` : words);
462
+ let next = this.#speak(parts[0]);
463
+ for (const [i, part] of parts.entries()) {
464
+ let pcm;
465
+ try { pcm = await next; } catch (error) { this.#log(`voice box: no voice for the answer (${error.message})`); l.send({ t: 'cue', name: 'error' }); break; }
466
+ if (i + 1 < parts.length) { next = this.#speak(parts[i + 1]); next.catch(() => {}); }
467
+ l.play(pcm, i + 1 === parts.length && (question || chatting) ? 'listen' : null); /* a question is always listened after: an answer is expected */
468
+ }
437
469
  }
438
470
  }
439
471
  #chatting(link) {
@@ -451,7 +483,7 @@ export class Boxes {
451
483
  socket, send, waiting: new Map(), heardAt: 0, muted: null, volume: null,
452
484
  box: () => this.#all().find((b) => b.id === box.id) ?? box,
453
485
  play: (pcm, then = null) => {
454
- pcm = pcm.subarray(0, pcm.length & ~1); /* whole samples only: an odd byte shifts every sample after it */
486
+ pcm = levelled(pcm.subarray(0, pcm.length & ~1)); /* whole samples only (an odd byte shifts every sample after it), at the voice's level */
455
487
  /* Something is playing (a radio): the words go over it, the radio lowered meanwhile (the mixer, listen()). */
456
488
  const playing = this.#streams.get(box.id);
457
489
  if (playing) { playing.over.push(pcm); return; }