Files
hermeven/apps/even-client/test/vad.test.ts
T
DanandClaude Fable 5.2 e1d183b54d Opening menu and hands-free mode: call an agent by name (0.3.0)
The app now opens on a menu, which is the root page: "Hands free" first,
"Agents" second. Agents is the 0.2.0 app unchanged. Hands free is the same
agents, conversations and question path with one flag set: the microphone
listens while the app is at rest and the app does not leave by itself.
The mode is never remembered; every launch starts at the menu with the
microphone closed, and double tap at Ready returns to the menu and closes
it.

Client: a loudness-only speech segmenter (core/vad.ts) cuts the stream
into utterances; each goes to the bridge for a wake check. "Wendy, <q>"
becomes a text turn to Wendy, with what was heard shown while she works.
A name alone starts a recording that sends itself when the speaker stops.
The status line starts with a dot for exactly as long as the microphone is
open at rest. In wake mode every microphone open and close goes through
one serialised reconciler; without the option the old path is untouched.

Bridge: POST /v1/wake transcribes one utterance in memory and reports
whether it BEGAN with an agent's name (first word of the display name, or
an alias from HERMEVEN_AGENT_<ID>_WAKE). Exact match only; a near miss is
reported as one word and never acted on. Creates no turn, stores nothing,
returns and logs nothing that was not addressed to an agent. One check at
a time per device, HERMEVEN_WAKE_CHECKS_PER_MINUTE (30).

Docs: decision 0004, bridge API, security gaps 3c/3d, device plan D.
The manifest's microphone description changed.

Not reviewed, not deployed, not uploaded, not device-tested.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-28 10:52:06 -08:00

95 lines
4.4 KiB
TypeScript

import { describe, expect, it } from 'vitest';
import { SpeechSegmenter, WAKE_SEGMENTER, type SegmentEvent } from '../src/core/vad.ts';
/** Synthetic audio: a tone stands in for a voice, low noise for a room. Not speech. */
function tone(ms: number, amplitude = 6000): Uint8Array {
const samples = (ms * 16000) / 1000;
const out = new Uint8Array(samples * 2);
for (let i = 0; i < samples; i++) {
const v = Math.round(Math.sin((2 * Math.PI * 300 * i) / 16000) * amplitude) & 0xffff;
out[2 * i] = v & 0xff;
out[2 * i + 1] = v >> 8;
}
return out;
}
const room = (ms: number, amplitude = 40) => tone(ms, amplitude);
function feed(segmenter: SpeechSegmenter, ...clips: Uint8Array[]): SegmentEvent[] {
const events: SegmentEvent[] = [];
for (const clip of clips) {
// In 100 ms frames, as the glasses deliver them.
for (let offset = 0; offset < clip.length; offset += 3200) events.push(...segmenter.push(clip.subarray(offset, offset + 3200)));
}
return events;
}
const ms = (pcm: Uint8Array) => pcm.length / 32;
describe('speech segmenter', () => {
it('silence and steady room noise produce nothing', () => {
const segmenter = new SpeechSegmenter();
expect(feed(segmenter, new Uint8Array(32000 * 5), room(5000))).toEqual([]);
expect(segmenter.speaking).toBe(false);
});
it('delivers one utterance, with its lead-in, once the speaker pauses', () => {
const segmenter = new SpeechSegmenter();
const events = feed(segmenter, room(1000), tone(1500), room(1000));
expect(events.map((e) => e.type)).toEqual(['start', 'end']);
const end = events[1] as Extract<SegmentEvent, { type: 'end' }>;
// Speech, the preroll before it and the closing pause; not the whole second of lead-in.
expect(ms(end.pcm)).toBeGreaterThanOrEqual(1500 + WAKE_SEGMENTER.endSilenceMs);
expect(ms(end.pcm)).toBeLessThanOrEqual(1500 + WAKE_SEGMENTER.endSilenceMs + WAKE_SEGMENTER.prerollMs + 60);
expect(end.pcm.length % 2).toBe(0);
expect(segmenter.speaking).toBe(false);
});
it('a short pause inside a sentence does not split it; a long one does', () => {
const segmenter = new SpeechSegmenter();
const one = feed(segmenter, room(500), tone(600), room(300), tone(600), room(1000));
expect(one.map((e) => e.type)).toEqual(['start', 'end']);
const two = feed(segmenter, tone(600), room(900), tone(600), room(900));
expect(two.map((e) => e.type)).toEqual(['start', 'end', 'start', 'end']);
});
it('a click is discarded, not delivered', () => {
const segmenter = new SpeechSegmenter();
expect(feed(segmenter, room(500), tone(100), room(1000)).map((e) => e.type)).toEqual(['start', 'discard']);
});
it('frames of any size give the same result', () => {
const clip = new Uint8Array([...room(600), ...tone(900), ...room(900)]);
const a = new SpeechSegmenter();
const b = new SpeechSegmenter();
const whole = a.push(clip);
const pieces: SegmentEvent[] = [];
for (let offset = 0; offset < clip.length; offset += 1001) pieces.push(...b.push(clip.subarray(offset, offset + 1001)));
expect(pieces).toEqual(whole);
});
it('a noise that never pauses is cut at the limit and then becomes the background', () => {
const segmenter = new SpeechSegmenter({ ...WAKE_SEGMENTER, maxMs: 3000 });
const events = feed(segmenter, room(500), tone(10_000, 3000));
expect(events.filter((e) => e.type === 'end')).toHaveLength(1);
// Something clearly louder than that noise is still heard as speech.
expect(feed(segmenter, tone(1000, 20_000), tone(1000, 3000)).map((e) => e.type)).toEqual(['start', 'end']);
});
it('adapts to a louder room: steady noise above the minimum level stops counting as speech', () => {
const segmenter = new SpeechSegmenter({ ...WAKE_SEGMENTER, maxMs: 2000 });
feed(segmenter, room(4000, 600));
expect(feed(segmenter, room(4000, 600))).toEqual([]);
});
it('hands over what it is holding, and can be retimed without losing it', () => {
const segmenter = new SpeechSegmenter();
feed(segmenter, room(1000), tone(800));
expect(segmenter.speaking).toBe(true);
expect(ms(segmenter.buffered())).toBeGreaterThanOrEqual(800);
segmenter.configure({ ...WAKE_SEGMENTER, endSilenceMs: 1200 });
expect(feed(segmenter, room(1000))).toEqual([]);
expect(feed(segmenter, room(300)).map((e) => e.type)).toEqual(['end']);
segmenter.reset();
expect(segmenter.buffered().length).toBe(0);
});
});