CODY-024: the delayed sound is primed during its own press; CODY-023: README current-state rows

- soundCheck: prime() calls AudioContext.resume() synchronously in the click handler of both
  sound buttons; the delayed sound is scheduled only once the engine is running, and a refusal
  is shown and re-enables the button.
- apps/even-client/test/soundCheck.test.ts: six tests with a fake, activation-gated sound engine.
- README: every current-state row names e4c102a / 0.4.0; 44ea114 is historical.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
Dan
2026-11-29 11:42:05 -08:00
co-authored by Claude Fable 5.2
parent 2c3c518955
commit aea8ed80a9
3 changed files with 266 additions and 29 deletions
+56 -25
View File
@@ -50,26 +50,47 @@ export function mountSoundCheck(doc: Document, win: Window): HTMLElement {
return context;
};
/** Two short notes. Resolves with whether the sound engine was actually running. */
async function tone(): Promise<string> {
/**
* Start the sound engine. MUST be called synchronously from a button's click handler: a
* browser that blocks sound only lets `resume()` through while the press is still "active",
* and that expires before any timer fires. Resolves with null when the engine is running, or
* with the reason it is not. Never rejects.
*/
function prime(): Promise<string | null> {
const ctx = getContext();
if (!ctx) return 'no sound engine in this page';
if (!ctx) return Promise.resolve('no sound engine in this page');
let resumed: Promise<unknown>;
try {
await Promise.race([ctx.resume(), new Promise((r) => win.setTimeout(r, 800))]);
resumed = ctx.resume();
} catch (err) {
return `blocked (${reason(err)})`;
return Promise.resolve(`blocked (${reason(err)})`);
}
// A blocked resume() may never settle at all, so do not wait for it for ever.
return Promise.race([resumed, new Promise((r) => win.setTimeout(r, 800))]).then(
() => (ctx.state === 'running' ? null : `blocked (sound engine is ${ctx.state})`),
(err) => `blocked (${reason(err)})`,
);
}
/** Two short notes, if the engine can be started. Resolves with what happened. */
async function tone(): Promise<string> {
const blocked = await prime();
const ctx = context;
if (blocked || !ctx) return blocked ?? 'no sound engine in this page';
try {
const gain = ctx.createGain();
gain.gain.value = 0.2;
gain.connect(ctx.destination);
[660, 880].forEach((frequency, i) => {
const osc = ctx.createOscillator();
osc.frequency.value = frequency;
osc.connect(gain);
osc.start(ctx.currentTime + i * 0.45);
osc.stop(ctx.currentTime + i * 0.45 + 0.35);
});
} catch (err) {
return `failed (${reason(err)})`;
}
if (ctx.state !== 'running') return `blocked (sound engine is ${ctx.state})`;
const gain = ctx.createGain();
gain.gain.value = 0.2;
gain.connect(ctx.destination);
[660, 880].forEach((frequency, i) => {
const osc = ctx.createOscillator();
osc.frequency.value = frequency;
osc.connect(gain);
osc.start(ctx.currentTime + i * 0.45);
osc.stop(ctx.currentTime + i * 0.45 + 0.35);
});
return 'played';
}
const stamp = () => new Date().toLocaleTimeString();
@@ -96,20 +117,30 @@ export function mountSoundCheck(doc: Document, win: Window): HTMLElement {
void tone().then((outcome) => show('Test sound', `${outcome} at ${stamp()}`));
});
// 3. A sound later, with nobody touching anything at that moment.
// 3. A sound later, with nobody touching anything at that moment. The engine is started by
// THIS press (see prime), so what is measured 20 s later is whether sound that was allowed
// keeps working in the background, not whether the press has worn off.
const laterButton = node(doc, 'button', `Play again in ${DELAY_SECONDS} s`, 'secondary');
laterButton.type = 'button';
laterButton.addEventListener('click', () => {
laterButton.disabled = true;
show('Delayed sound', `waiting since ${stamp()}; lock the phone or leave the app now`);
const due = Date.now() + DELAY_SECONDS * 1000;
win.setTimeout(() => {
const lateMs = Date.now() - due;
void tone().then((outcome) => {
show('Delayed sound', `${outcome} at ${stamp()}${lateMs > 1500 ? `, ${Math.round(lateMs / 1000)} s late` : ''}`);
show('Delayed sound', 'starting…');
void prime().then((blocked) => {
if (blocked) {
show('Delayed sound', `not started: ${blocked} at ${stamp()}`);
laterButton.disabled = false;
});
}, DELAY_SECONDS * 1000);
return;
}
show('Delayed sound', `waiting since ${stamp()}; lock the phone or leave the app now`);
const due = Date.now() + DELAY_SECONDS * 1000;
win.setTimeout(() => {
const lateMs = Date.now() - due;
void tone().then((outcome) => {
show('Delayed sound', `${outcome} at ${stamp()}${lateMs > 1500 ? `, ${Math.round(lateMs / 1000)} s late` : ''}`);
laterButton.disabled = false;
});
}, DELAY_SECONDS * 1000);
});
});
// The phone's own voice, as a free fallback to a paid one.