clavarium 0.2.218-next.1 → 0.2.220-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.218-next.1",
3
+ "version": "0.2.220-next.1",
4
4
  "lockfileVersion": 3,
5
5
  "requires": true,
6
6
  "packages": {
7
7
  "": {
8
8
  "name": "clavarium",
9
- "version": "0.2.218-next.1",
9
+ "version": "0.2.220-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.218-next.1",
3
+ "version": "0.2.220-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
@@ -144,7 +144,24 @@ export function whyOf(m) {
144
144
  return `réveil : ${reason}${score}${cut}`;
145
145
  }
146
146
 
147
+ /* HOW LOUD WHAT THE BOX SENT WAS (01-11): beside the message, so a sentence that came empty (a wake, then
148
+ silence: Whisper then invents « Sous-titrage ») is seen at once, and where it went wrong told apart --
149
+ the box sent nothing, or the dictation misheard. */
150
+ export function levelOf(pcm) {
151
+ const n = pcm.length >> 1; if (!n) return 0;
152
+ let sum = 0; for (let i = 0; i < n; i++) { const v = pcm.readInt16LE(i * 2); sum += v * v; }
153
+ return Math.round(Math.sqrt(sum / n));
154
+ }
155
+ export const QUIET_LEVEL = 150;
156
+ export function soundOf(pcm) {
157
+ const level = levelOf(pcm);
158
+ return `son ${(pcm.length / 32_000).toFixed(1).replace('.', ',')} s, niveau ${level}${level < QUIET_LEVEL ? ' (presque silencieux)' : ''}`;
159
+ }
160
+
147
161
  export const DOING_WITHIN_MS = 120_000;
162
+ export const TEST_SOUND = 'Test du son. Un, deux, trois. Si tu m\'entends, le son marche.';
163
+ export const TEST_MIC = 'Test du micro. Parle-moi, je t\'écoute.';
164
+ export const TEST_MIC_MS = 45_000;
148
165
  /* A LONG RECORDING (a meeting, a class): asked in words, stopped by the button or the wake word. */
149
166
  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;
150
167
  export const RECORD_MAX_BYTES = 4 * 3600 * 32_000; /* four hours */
@@ -288,6 +305,34 @@ export class Boxes {
288
305
  return a;
289
306
  }
290
307
 
308
+ /* TESTED FROM ITS CARD (01-11): the sound -- it says a test sentence -- and the microphone -- it listens
309
+ once, and what it heard is shown on the page, not sent to the conversation. */
310
+ async testSound(id) {
311
+ const link = this.#links.get(id);
312
+ if (!link) throw new Refusal(409, 'This voice box is not connected.');
313
+ link.play(await this.#speak(TEST_SOUND));
314
+ return { ok: true };
315
+ }
316
+ async testMic(id) {
317
+ const link = this.#links.get(id);
318
+ if (!link) throw new Refusal(409, 'This voice box is not connected.');
319
+ link.testing = { until: this.#now() + TEST_MIC_MS, state: 'listening', text: '' };
320
+ link.play(await this.#speak(TEST_MIC), 'listen');
321
+ return { ok: true };
322
+ }
323
+ testResult(id) {
324
+ const t = this.#links.get(id)?.testing;
325
+ if (!t) return { state: 'none', text: '' };
326
+ if (t.state === 'listening' && this.#now() > t.until) t.state = 'nothing';
327
+ return { state: t.state, text: t.text };
328
+ }
329
+ #tested(link, state, text = '') {
330
+ const t = link.testing;
331
+ if (!t || t.state !== 'listening' || this.#now() > t.until) return false;
332
+ t.state = state; t.text = text;
333
+ return true;
334
+ }
335
+
291
336
  /* A sound played on a box (a sample, a recorded message), as its answers are. */
292
337
  play(id, wav) {
293
338
  const link = this.#links.get(id);
@@ -567,11 +612,19 @@ export class Boxes {
567
612
  }
568
613
  if (m.t === 'event' && m.name === 'wake.own') { this.#log(`voice box ${box.name}: heard its own voice say the wake word, ignored`); return; }
569
614
  if (m.t === 'event' && m.name === 'noise.dropped') { this.#log(`voice box ${box.name}: a sound near full scale was cut by its noise guard`); return; }
615
+ /* The box listens for a sentence while this side still records: the two disagree (a reconnection, a
616
+ lost record.end). The recording is closed here, or the sentence would go into it and arrive empty. */
617
+ if (m.t === 'listen.start' && link.recording) {
618
+ const r = link.recording; link.recording = null;
619
+ this.#log(`voice box ${box.name}: a sentence while recording here, the recording closed`);
620
+ r.out.end(() => { void this.#recorded(link, r); });
621
+ }
570
622
  if (m.t === 'listen.start') link.why = whyOf(m);
571
623
  if (m.t === 'listen.start') { if (this.stop(box.id)) this.#log(`voice box ${box.name}: stopped playing, someone speaks`); sentence = []; size = 0; return; }
572
624
  if (m.t === 'listen.end' && sentence && m.reason === 'nobody') {
573
625
  /* Listened again, nobody spoke: a conversation ends there, in silence. */
574
626
  sentence = null;
627
+ if (this.#tested(link, 'nothing')) { link.send({ t: 'cue', name: 'done' }); return; }
575
628
  if (link.chat) this.#log(`voice box ${box.name}: conversation ended, nobody answered`);
576
629
  link.chat = false; link.recent = [];
577
630
  link.send({ t: 'cue', name: 'done' });
@@ -628,7 +681,7 @@ export class Boxes {
628
681
  async #heard(link, pcm) {
629
682
  const box0 = link.box();
630
683
  const seconds = (pcm.length / 32_000).toFixed(1);
631
- if (pcm.length < 16_000) { this.#log(`voice box ${box0.name}: ${seconds} s heard, too short for a sentence`); link.send({ t: 'cue', name: 'done' }); return; } /* under half a second: a press, not a sentence */
684
+ if (pcm.length < 16_000) { this.#log(`voice box ${box0.name}: ${seconds} s heard, too short for a sentence`); this.#tested(link, 'nothing'); link.send({ t: 'cue', name: 'done' }); return; } /* under half a second: a press, not a sentence */
632
685
  link.send({ t: 'cue', name: 'thinking' });
633
686
  let text;
634
687
  /* A sentence that goes nowhere is said so, in words, rather than with a beep nobody can read (31-10). */
@@ -650,8 +703,14 @@ export class Boxes {
650
703
  };
651
704
  for (const piece of piecesOf(pcm)) said.push(String(await hear(wavOf(piece)) ?? '').trim());
652
705
  text = said.filter(Boolean).join(' ');
653
- } catch (error) { this.#log(`voice box ${box0.name}: ${seconds} s heard, dictation failed (${error.message})`); await lost('Pardon, la dictée ne répond pas. Tu peux répéter ?'); return; }
654
- if (!text) { this.#log(`voice box ${box0.name}: ${seconds} s heard, nothing recognised (silence?)`); await lost("Pardon, je n'ai rien compris. Tu peux répéter ?"); return; }
706
+ } catch (error) { this.#log(`voice box ${box0.name}: ${seconds} s heard, dictation failed (${error.message})`); if (this.#tested(link, 'failed', error.message)) { link.send({ t: 'cue', name: 'error' }); return; } await lost('Pardon, la dictée ne répond pas. Tu peux répéter ?'); return; }
707
+ if (link.testing?.state === 'listening' && this.#now() <= link.testing.until) {
708
+ this.#tested(link, text ? 'heard' : 'nothing', text);
709
+ this.#log(`voice box ${box0.name}: microphone test, ${seconds} s heard`);
710
+ link.send({ t: 'cue', name: 'done' });
711
+ return;
712
+ }
713
+ if (!text) { this.#log(`voice box ${box0.name}: ${seconds} s heard, nothing recognised (${soundOf(pcm)})`); await lost("Pardon, je n'ai rien compris. Tu peux répéter ?"); return; }
655
714
  const box = link.box();
656
715
  /* Only a short order starts a recording ("lance l'enregistrement de la réunion"): a sentence that merely
657
716
  mentions recording does not (01-11). */
@@ -665,7 +724,7 @@ export class Boxes {
665
724
  link.relay = 0;
666
725
  text = `(réponse entendue de l'appareil, pas de la personne) ${text}`;
667
726
  }
668
- try { await this.#say(box.conversation, text, { id: box.id, name: box.name, ...(box.place ? { place: box.place } : {}), ...(link.why ? { why: link.why } : {}) }); } catch (error) { this.#log(`voice box ${box.name}: ${seconds} s heard and dictated, but not said in ${box.conversation} (${error.message})`); await lost("Pardon, je n'ai pas pu transmettre ta phrase. Tu peux répéter ?"); return; }
727
+ try { await this.#say(box.conversation, text, { id: box.id, name: box.name, ...(box.place ? { place: box.place } : {}), why: `${link.why ? `${link.why} ; ` : ''}${soundOf(pcm)}` }); } catch (error) { this.#log(`voice box ${box.name}: ${seconds} s heard and dictated, but not said in ${box.conversation} (${error.message})`); await lost("Pardon, je n'ai pas pu transmettre ta phrase. Tu peux répéter ?"); return; }
669
728
  this.#log(`voice box ${box.name}: ${seconds} s heard, said in ${box.conversation}`);
670
729
  link.heardAt = this.#now();
671
730
  this.#waitFor(link, text);
@@ -697,6 +756,9 @@ export class Boxes {
697
756
  * POST /api/boxes/<id>/move {conversation}: one conversation at a time
698
757
  * POST /api/boxes/<id>/place {place}: where it is (a home), shown with its name
699
758
  * POST /api/boxes/<id>/zone {zone}: its time zone (IANA), for its clock; the page gives its own
759
+ * POST /api/boxes/<id>/test-sound it says a test sentence
760
+ * POST /api/boxes/<id>/test-mic it listens once; what it heard stays here, not in the conversation
761
+ * POST /api/boxes/<id>/test-result -> {state: listening|heard|nothing|failed|none, text}
700
762
  * POST /api/boxes/<id>/revoke
701
763
  * POST /api/boxes/<id>/volume {level 0-100}
702
764
  * POST /api/boxes/<id>/wifi-scan -> {networks}
@@ -742,6 +804,9 @@ export async function handleBoxes(request, response, boxes, originOk, headers =
742
804
  case 'wifi-forget': return answered(await boxes.command(id, { t: 'wifi.forget', ssid: String(body.ssid ?? '').slice(0, 32) }));
743
805
  case 'wifi-join': return answered(await boxes.command(id, { t: 'wifi.join', ssid: String(body.ssid ?? ''), password: String(body.password ?? '') }, 90_000));
744
806
  case 'alert': return answered(await boxes.command(id, { t: 'alert', text: String(body.text ?? '').slice(0, 300) }, 10 * 60_000));
807
+ case 'test-sound': return send(200, await boxes.testSound(id));
808
+ case 'test-mic': return send(200, await boxes.testMic(id));
809
+ case 'test-result': return send(200, boxes.testResult(id));
745
810
  case 'reboot': return answered(await boxes.command(id, { t: 'reboot' }));
746
811
  default: return false;
747
812
  }
package/server/voice.mjs CHANGED
@@ -75,7 +75,7 @@ const PHANTOMS = [
75
75
  /* Any sentence that credits Amara.org, in whatever language ("Sous-titres réalisés par la communauté
76
76
  d'Amara.org", "Untertitel der Amara.org-Community", "Subtítulos por la comunidad de Amara.org"...). */
77
77
  /[^.!?。]*amara\.org[^.!?。]*[.!?。]?/giu,
78
- /sous[- ]titrage\s*st\s*'?\s*\d+[.!]?/giu,
78
+ /sous[- ]titrage\s*st\b\s*'?\s*\d*[.!]?/giu,
79
79
  /sous[- ]titrage\s+fr(?:ançais)?\s*[.!?]?/giu,
80
80
  /sous[- ]titr(?:age|es?)\s+(?:par\s+)?(?:la\s+)?société\s+radio[- ]canada[.!]?/giu,
81
81
  /* A credit to a person, when it is all that was "heard" ("Sous-titres par Jérémy Diaz", 31-10). */
package/web/app.js CHANGED
@@ -8874,6 +8874,35 @@
8874
8874
  const volRow = el('label', 'box-volume'); volRow.append('Volume ', vol, volSaid);
8875
8875
  const acts = el('div', 'action-row'); acts.append(whereRow, place, volRow, rename, remove);
8876
8876
  card.append(head, acts);
8877
+ /* TESTED FROM HERE (01-11): its sound -- it says a test sentence -- and its microphone -- it listens
8878
+ once, and what it heard shows here, not in the conversation. */
8879
+ if (b.online) {
8880
+ const test = el('div', 'action-row box-test');
8881
+ const sound = el('button', '', 'Test the sound'); sound.type = 'button';
8882
+ const mic = el('button', '', 'Test the microphone'); mic.type = 'button';
8883
+ const heard = el('small', 'hint box-test-heard', '');
8884
+ sound.addEventListener('click', async () => {
8885
+ sound.disabled = true; heard.textContent = 'It says a test sentence…';
8886
+ try { await post(`${API}/boxes/${b.id}/test-sound`, {}); heard.textContent = 'Sent: you should hear « Test du son ».'; }
8887
+ catch (error) { heard.textContent = `Not played: ${error.message}`; }
8888
+ sound.disabled = false;
8889
+ });
8890
+ mic.addEventListener('click', async () => {
8891
+ mic.disabled = true; heard.textContent = 'It asks you to speak, then listens…';
8892
+ try { await post(`${API}/boxes/${b.id}/test-mic`, {}); }
8893
+ catch (error) { heard.textContent = `Not started: ${error.message}`; mic.disabled = false; return; }
8894
+ for (let i = 0; i < 50; i++) {
8895
+ await new Promise((r) => setTimeout(r, 1000));
8896
+ const r = await post(`${API}/boxes/${b.id}/test-result`, {}).catch(() => null);
8897
+ if (!r || r.state === 'listening') continue;
8898
+ heard.textContent = r.state === 'heard' ? `Heard: « ${r.text} »` : r.state === 'failed' ? `The dictation failed: ${r.text}` : 'Nothing heard.';
8899
+ break;
8900
+ }
8901
+ mic.disabled = false;
8902
+ });
8903
+ test.append(sound, mic, heard);
8904
+ card.append(test);
8905
+ }
8877
8906
  /* ITS WI-FI NETWORKS (01-11): the box knows several and goes to the strongest it hears; one is added
8878
8907
  here before it moves, from the networks it hears itself -- no phone, no button. */
8879
8908
  if (b.online) {