CODY-025: stopped or replaced speech never starts; CODY-026: README; note when the phone page holds off idle exit

- ui/speaker.ts: a generation counter. Every play() and stop() advances it; a play() that
  finds it changed after resume() or decodeAudioData() returns without creating a source.
- apps/even-client/test/speaker.test.ts: nine tests with a fake sound engine whose resume and
  decode finish when the test says.
- README: 'Not in this slice' no longer says text-to-speech is absent.
- controller: one diagnostic line when phone-page activity starts holding off the idle exit.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-29 12:31:45 -08:00
co-authored by Claude Fable 5.2
parent 958567453c
commit 2a4af63f00
4 changed files with 255 additions and 11 deletions
+18 -6
View File
@@ -7,16 +7,22 @@ import type { SpeakerPort } from '../core/controller.ts';
*
* Device evidence (docs/evidence/device/2026-11-29.md): in the Even app on Dan's phone a page
* may play sound without a touch, and did so 20 s after a press. Playing an MP3 this way has
* NOT been tried on the device.
* NOT been tried in any real browser or on the device; the tests use a fake sound engine.
*/
export function createSpeaker(win: Window): SpeakerPort | null {
const w = win as unknown as { AudioContext?: typeof AudioContext; webkitAudioContext?: typeof AudioContext };
const Ctor = w.AudioContext ?? w.webkitAudioContext;
if (!Ctor) return null;
let context: AudioContext | null = null;
/**
* Counts every play() and every stop(). A play() that finds a different number when one of
* its waits is over has been stopped or replaced, and must not start (or touch) anything.
*/
let generation = 0;
let current: { source: AudioBufferSourceNode; done: () => void } | null = null;
const stop = () => {
/** Silence whatever is sounding. Does not by itself invalidate work still in progress. */
const silence = () => {
const playing = current;
current = null;
if (!playing) return;
@@ -31,16 +37,19 @@ export function createSpeaker(win: Window): SpeakerPort | null {
return {
async play(audio: Uint8Array): Promise<void> {
stop();
// A new play replaces the old one, whatever stage that had reached.
const mine = ++generation;
silence();
context ??= new Ctor();
const ctx = context;
await Promise.race([ctx.resume(), new Promise((r) => win.setTimeout(r, 800))]);
if (mine !== generation) return;
if (ctx.state !== 'running') throw new Error(`sound is blocked (${ctx.state})`);
// decodeAudioData takes ownership of the buffer it is given: hand it a copy.
const copy = audio.buffer.slice(audio.byteOffset, audio.byteOffset + audio.byteLength) as ArrayBuffer;
const buffer = await ctx.decodeAudioData(copy);
// Stopped, or replaced by a newer answer, while decoding.
if (current) stop();
// Stopped, or replaced by a newer answer, while decoding: nothing is created or started.
if (mine !== generation) return;
const source = ctx.createBufferSource();
source.buffer = buffer;
source.connect(ctx.destination);
@@ -53,6 +62,9 @@ export function createSpeaker(win: Window): SpeakerPort | null {
source.start();
});
},
stop,
stop(): void {
generation += 1;
silence();
},
};
}