Sound effects demo, and music controller

This commit is contained in:
James Babcock committed 2026-07-07 21:21:02 -07:00
1 parent 5f82981b92
commit 56d0f8c351
13 files changed
+717 -145

No files matched your search

+79 -44
View File
@@ -1,57 +1,89 @@
import { getDefaultSmallFont } from "../../graphics/bdffont";
import { GrayImage } from "../../graphics/image";
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
import { clamp } from "../../util/numeric-util";
import { Layer, type DashboardInputEvent, type LayerActions, type LayerContext } from "../layers";
import {
buildSoundSequencePayload,
CFW_SEQ_MAX,
effectPhrases,
phraseDurationMs,
SOUND_EFFECTS,
type SoundEffect,
} from "./sound-effects";
const NOTE_NAMES = ["C", "D", "E", "F", "G", "A", "B"] as const;
const DEFAULT_BEAT = 4; // ~250ms at 62ms units
const HEADER_HEIGHT = 30;
const ROW_HEIGHT = 16;
const LIST_X = 20;
const FOOTER_HEIGHT = 22;
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}
/**
* Buzzer demo: pick a sound effect from the catalog and play it on the
* glasses' piezo. Multi-phrase effects are paced phrase-by-phrase so each
* sequencer message lands as the previous phrase finishes.
*/
export class BuzzerDemoLayer implements Layer {
private note = 1;
private oct = 2;
private selectedIndex = 0;
private scrollRow = 0;
private playing: string | null = null;
paint(ctx: LayerContext, _paintBelow: () => GrayImage): GrayImage {
// Sized to the hosting stack (the Debug tests app viewport).
paint(ctx: LayerContext): GrayImage {
const font = getDefaultSmallFont();
const { width, height } = ctx.stack.getBaseSize();
const image = new GrayImage(width, height, 0);
const font = getDefaultSmallFont();
image.drawText(font, 24, 20, "Buzzer demo", 200);
image.drawText(font, 24, 44, `Note: ${NOTE_NAMES[this.note - 1]} Octave: ${this.oct}`, 180);
image.drawText(font, 24, 68, "Scroll: change note", 150);
image.drawText(font, 24, 84, "Click: play note", 150);
image.drawText(font, 24, height - 24, "Double-click: back", 130);
image.drawText(font, 20, 8, "Buzzer demo", 220);
const status = this.playing ? `Playing: ${this.playing}` : "Click to play";
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
const keyWidth = 64;
const keyY = 130;
const keyX = Math.floor((width - keyWidth * NOTE_NAMES.length) / 2);
for (let i = 0; i < NOTE_NAMES.length; i++) {
const selected = i + 1 === this.note;
const x = keyX + i * keyWidth;
image.drawRect(x + 2, keyY, keyWidth - 4, 48, selected ? 220 : 80);
const label = NOTE_NAMES[i]!;
image.drawText(
font,
x + Math.max(4, Math.floor((keyWidth - font.measureText(label)) / 2)),
keyY + 18,
label,
selected ? 0 : 200,
);
const listHeight = height - HEADER_HEIGHT - FOOTER_HEIGHT;
const visibleRows = Math.max(1, (listHeight / ROW_HEIGHT) | 0);
if (this.selectedIndex < this.scrollRow) {
this.scrollRow = this.selectedIndex;
} else if (this.selectedIndex >= this.scrollRow + visibleRows) {
this.scrollRow = this.selectedIndex - visibleRows + 1;
}
this.scrollRow = clamp(this.scrollRow, 0, Math.max(0, SOUND_EFFECTS.length - visibleRows));
const lastVisible = Math.min(SOUND_EFFECTS.length, this.scrollRow + visibleRows);
for (let index = this.scrollRow; index < lastVisible; index++) {
const effect = SOUND_EFFECTS[index]!;
const y = HEADER_HEIGHT + (index - this.scrollRow) * ROW_HEIGHT;
const selected = index === this.selectedIndex;
if (selected) {
image.fillRoundedRect(LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, 15, 4);
image.drawRoundedRect(LIST_X - 6, y - 1, width - 2 * LIST_X + 12, ROW_HEIGHT - 1, 45, 4);
}
image.drawText(font, LIST_X, y + 1, effect.name, selected ? 255 : 200);
image.drawText(font, LIST_X + 110, y + 1, effect.desc, selected ? 180 : 120);
}
image.drawText(font, 20, height - 16, "Scroll: select Click: play Double-click: back", 110);
return image;
}
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
switch (event.type) {
case "scroll-up":
this.advanceNote(1);
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
return;
case "scroll-down":
this.advanceNote(-1);
this.selectedIndex = Math.min(SOUND_EFFECTS.length - 1, this.selectedIndex + 1);
return;
case "click":
await ctx.actions.playBuzzerNote(this.note, this.oct, DEFAULT_BEAT);
case "click": {
const effect = SOUND_EFFECTS[clamp(this.selectedIndex, 0, SOUND_EFFECTS.length - 1)];
if (effect && !this.playing) {
// Fire-and-forget: playback paces itself with sleeps between
// phrases; awaiting it here would stall input for seconds.
void this.playEffect(effect, ctx.actions).catch((error) => {
console.warn(`buzzer effect failed: ${error}`);
});
}
return;
}
case "double-click":
ctx.stack.pop();
return;
@@ -60,18 +92,21 @@ export class BuzzerDemoLayer implements Layer {
}
}
private advanceNote(delta: number): void {
let next = this.note + delta;
if (next > 7) {
this.note = 1;
this.oct = Math.min(3, this.oct + 1);
return;
private async playEffect(effect: SoundEffect, actions: LayerActions): Promise<void> {
this.playing = effect.name;
actions.requestRender();
try {
for (const phrase of effectPhrases(effect.make())) {
// Safety net: chop any over-long phrase into <=48-step messages.
for (let index = 0; index < phrase.length; index += CFW_SEQ_MAX) {
const chunk = phrase.slice(index, index + CFW_SEQ_MAX);
await actions.playBuzzerSequence(buildSoundSequencePayload(chunk));
await sleep(phraseDurationMs(chunk));
}
}
} finally {
this.playing = null;
actions.requestRender();
}
if (next < 1) {
this.note = 7;
this.oct = Math.max(0, this.oct - 1);
return;
}
this.note = next;
}
}