forked from Blendman974/faceclaw
Sound effects demo, and music controller
This commit is contained in:
1 parent
5f82981b92
commit
56d0f8c351
13 files changed
+717
-145
No files matched your search
+79
-44
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@ import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { mediaControllerBridge, type MediaControllerState } from "../../native/media-controller";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
|
||||
|
||||
|
||||
export class MusicControllerDashboardPlugin extends DashboardPlugin {
|
||||
@@ -38,81 +37,8 @@ export class MusicControllerDashboardPlugin extends DashboardPlugin {
|
||||
image.drawText(font, bounds.x + 10, bounds.y + bounds.height - 18, playbackLabel(media), 130);
|
||||
}
|
||||
|
||||
override createFullscreenLayer(getState: () => DashboardPluginState): Layer {
|
||||
return new MusicControllerLayer(getState);
|
||||
}
|
||||
}
|
||||
|
||||
class MusicControllerLayer implements Layer {
|
||||
constructor(private readonly getState: () => DashboardPluginState) {}
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const image = new GrayImage(576, 288, 0);
|
||||
const media = this.getState().media;
|
||||
image.drawRect(12, 12, 552, 264, 52);
|
||||
image.drawText(font, 22, 16, "Music controller", 220);
|
||||
|
||||
if (!media.accessEnabled) {
|
||||
const lines = wrapText(
|
||||
font,
|
||||
"Notification access is required before Android will expose active media sessions to Faceclaw. Click to open settings.",
|
||||
520,
|
||||
);
|
||||
for (let i = 0; i < lines.length; i++) {
|
||||
image.drawText(font, 22, 44 + i * 14, lines[i]!, 180);
|
||||
}
|
||||
image.drawText(font, 22, 252, "Click: open settings Double-click: back", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
if (!media.available) {
|
||||
image.drawText(font, 22, 44, "No active media session.", 180);
|
||||
image.drawText(font, 22, 58, "Start playback in another app,", 180);
|
||||
image.drawText(font, 22, 72, "then reopen this card.", 180);
|
||||
image.drawText(font, 22, 252, "Double-click: back", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
image.drawText(font, 22, 44, media.title || "Unknown title", 220);
|
||||
image.drawText(font, 22, 60, media.artist || media.album || "Unknown artist", 180);
|
||||
image.drawText(font, 22, 76, media.packageName, 130);
|
||||
image.drawText(font, 22, 104, `State: ${playbackLabel(media)}`, 180);
|
||||
image.drawText(font, 22, 132, "Click: play/pause", 180);
|
||||
image.drawText(font, 22, 146, "Scroll up: previous", 180);
|
||||
image.drawText(font, 22, 160, "Scroll down: next", 180);
|
||||
image.drawText(font, 22, 252, "Double-click: back", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
const media = this.getState().media;
|
||||
switch (event.type) {
|
||||
case "click":
|
||||
if (!media.accessEnabled) {
|
||||
mediaControllerBridge.openNotificationAccessSettings();
|
||||
} else if (media.canPlayPause) {
|
||||
await mediaControllerBridge.playPause();
|
||||
}
|
||||
return;
|
||||
case "scroll-up":
|
||||
if (media.canSkipPrevious) {
|
||||
await mediaControllerBridge.skipPrevious();
|
||||
}
|
||||
return;
|
||||
case "scroll-down":
|
||||
if (media.canSkipNext) {
|
||||
await mediaControllerBridge.skipNext();
|
||||
}
|
||||
return;
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function playbackLabel(media: MediaControllerState): string {
|
||||
switch (media.playbackState) {
|
||||
|
||||
@@ -0,0 +1,192 @@
|
||||
/**
|
||||
* Sound-effect catalog for the CFW buzzer tone sequencer (load_image_z mode 5
|
||||
* kind 4). Ported from g2-kit-unofficial/examples/tunes.ts.
|
||||
*
|
||||
* The G2 "speaker" is a single monophonic PWM piezo buzzer. One message
|
||||
* carries up to 48 back-to-back {freq, duty, ms} steps that the firmware
|
||||
* plays on its own one-shot timer (non-blocking, auto-stops after the last
|
||||
* step). Effects longer than 48 steps split into phrases, each its own
|
||||
* message sent back-to-back, broken at musical rests so the tiny
|
||||
* inter-message gap is inaudible.
|
||||
*
|
||||
* Piezo reality check: the element resonates best around ~1-4 kHz; below
|
||||
* ~150 Hz it is faint, above ~8 kHz it thins out. duty ~50% is the fullest
|
||||
* square wave; lower duty reads as thinner and quieter.
|
||||
*/
|
||||
|
||||
/** One PWM segment; duty 0 means "rest" (silence) for `ms`. */
|
||||
export type Step = { freq: number; duty?: number; ms: number };
|
||||
/** One or more phrases; each phrase is one sequencer message. */
|
||||
export type Effect = Step[] | Step[][];
|
||||
|
||||
/** Firmware cap: steps per mode-5 kind-4 message. */
|
||||
export const CFW_SEQ_MAX = 48;
|
||||
|
||||
/** Build the wire buffer: [5][4][nSteps][ freqLo,freqHi,duty,msLo,msHi ]*n. */
|
||||
export function buildSoundSequencePayload(steps: Step[]): Uint8Array {
|
||||
const count = Math.min(steps.length, CFW_SEQ_MAX);
|
||||
const out = new Uint8Array(3 + count * 5);
|
||||
out[0] = 0x05;
|
||||
out[1] = 0x04;
|
||||
out[2] = count;
|
||||
for (let index = 0; index < count; index++) {
|
||||
const { freq, duty = 50, ms } = steps[index]!;
|
||||
const offset = 3 + index * 5;
|
||||
const clampedFreq = Math.max(1, Math.min(20000, Math.round(freq)));
|
||||
const clampedDuty = Math.max(0, Math.min(100, Math.round(duty)));
|
||||
const clampedMs = Math.max(1, Math.min(65535, Math.round(ms)));
|
||||
out[offset] = clampedFreq & 0xff;
|
||||
out[offset + 1] = (clampedFreq >> 8) & 0xff;
|
||||
out[offset + 2] = clampedDuty;
|
||||
out[offset + 3] = clampedMs & 0xff;
|
||||
out[offset + 4] = (clampedMs >> 8) & 0xff;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
export function phraseDurationMs(steps: Step[]): number {
|
||||
return steps.reduce((total, step) => total + step.ms, 0);
|
||||
}
|
||||
|
||||
/** Normalize an Effect into its list of phrases. */
|
||||
export function effectPhrases(effect: Effect): Step[][] {
|
||||
return Array.isArray(effect[0]) ? (effect as Step[][]) : [effect as Step[]];
|
||||
}
|
||||
|
||||
// ============================ musical helpers ===============================
|
||||
// Equal-tempered pitch from a name like "A4", "C#5", "Bb3". A4 = 440 Hz.
|
||||
const SEMI: Record<string, number> = { C: 0, D: 2, E: 4, F: 5, G: 7, A: 9, B: 11 };
|
||||
function hz(name: string): number {
|
||||
const match = /^([A-Ga-g])([#b]?)(-?\d)$/.exec(name.trim());
|
||||
if (!match) throw new Error(`bad note: ${name}`);
|
||||
let semitone = SEMI[match[1]!.toUpperCase()]!;
|
||||
if (match[2] === "#") semitone += 1;
|
||||
else if (match[2] === "b") semitone -= 1;
|
||||
const midi = semitone + (parseInt(match[3]!, 10) + 1) * 12;
|
||||
return Math.round(440 * 2 ** ((midi - 69) / 12));
|
||||
}
|
||||
|
||||
// Compact melody DSL: whitespace-separated "NOTE/ms" tokens. "." or "r" = rest.
|
||||
function notes(dsl: string, defMs = 140, duty = 50): Step[] {
|
||||
return dsl.trim().split(/\s+/).map((token) => {
|
||||
const [note, durationText] = token.split("/");
|
||||
const ms = durationText ? parseInt(durationText, 10) : defMs;
|
||||
if (note === "." || note === "r" || note === "rest") return { freq: 1, duty: 0, ms };
|
||||
return { freq: hz(note!), duty, ms };
|
||||
});
|
||||
}
|
||||
|
||||
// Geometric (log-linear = constant perceived glide) frequency sweep across
|
||||
// `ms`, approximated by `steps` short constant-tone segments.
|
||||
function sweep(from: number, to: number, ms: number, steps = 16, duty = 50): Step[] {
|
||||
steps = Math.max(2, Math.min(CFW_SEQ_MAX, steps));
|
||||
const per = Math.max(1, ms / steps);
|
||||
const out: Step[] = [];
|
||||
for (let index = 0; index < steps; index++) {
|
||||
const freq = from * (to / from) ** (index / (steps - 1));
|
||||
out.push({ freq, duty, ms: per });
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
const cat = (...parts: Step[][]): Step[] => parts.flat();
|
||||
const rest = (ms: number): Step => ({ freq: 1, duty: 0, ms });
|
||||
const rep = (steps: Step[], n: number): Step[] => Array.from({ length: n }, () => steps).flat();
|
||||
|
||||
// ================================ catalog ===================================
|
||||
// Each effect keeps every phrase <= 48 steps. Ordered roughly best-first.
|
||||
export type SoundEffect = { name: string; desc: string; make: () => Effect };
|
||||
|
||||
export const SOUND_EFFECTS: SoundEffect[] = [
|
||||
// --- game SFX ---
|
||||
{ name: "coin", desc: "Mario coin: blip then a bright ring", make: () =>
|
||||
notes("B5/70 E6/520") },
|
||||
{ name: "powerup", desc: "ascending arpeggio glissando", make: () => {
|
||||
const out: Step[] = [];
|
||||
const cells = ["G4", "C5", "E5", "G5", "C6", "E6", "G6"];
|
||||
for (const cell of cells) out.push({ freq: hz(cell), duty: 50, ms: 42 });
|
||||
out.push(rest(20), { freq: hz("G6"), duty: 50, ms: 60 },
|
||||
{ freq: hz("C7"), duty: 50, ms: 120 });
|
||||
return out;
|
||||
} },
|
||||
{ name: "oneup", desc: "Mario 1-UP jingle", make: () =>
|
||||
notes("E5/130 G5/130 E6/130 C6/130 D6/130 G6/300") },
|
||||
{ name: "laser", desc: "downward pew, 1.8k -> 320 Hz", make: () =>
|
||||
sweep(1800, 320, 190, 26, 50) },
|
||||
{ name: "pew", desc: "short high blaster", make: () =>
|
||||
sweep(2800, 700, 80, 12, 45) },
|
||||
{ name: "zap", desc: "up-then-down electric snap", make: () =>
|
||||
cat(sweep(400, 2300, 55, 9), sweep(2300, 260, 95, 13)) },
|
||||
{ name: "warp", desc: "teleport: rising whoosh with vibrato tail", make: () =>
|
||||
cat(sweep(220, 2600, 240, 30, 45),
|
||||
[{ freq: 2600, duty: 50, ms: 30 }, { freq: 2200, duty: 50, ms: 30 },
|
||||
{ freq: 2600, duty: 50, ms: 30 }, { freq: 2000, duty: 50, ms: 40 }]) },
|
||||
{ name: "explosion", desc: "descending rumble (piezo-faint low end)", make: () =>
|
||||
cat(sweep(900, 150, 380, 30, 60), [rest(30), { freq: 160, duty: 70, ms: 120 }]) },
|
||||
|
||||
// --- UI / notification ---
|
||||
{ name: "success", desc: "rising major arpeggio -> resolve", make: () =>
|
||||
notes("C5/90 E5/90 G5/90 C6/240") },
|
||||
{ name: "notify", desc: "two-tone up chime", make: () =>
|
||||
notes("A5/90 E6/260") },
|
||||
{ name: "chime", desc: "two-tone down chime (softer)", make: () =>
|
||||
notes("E6/90 A5/300", 140, 45) },
|
||||
{ name: "doorbell", desc: "ding-dong", make: () =>
|
||||
notes("E5/300 ./40 C5/560") },
|
||||
{ name: "error", desc: "harsh low descending two-tone", make: () =>
|
||||
[{ freq: 233, duty: 55, ms: 200 }, rest(20), { freq: 175, duty: 55, ms: 300 }] },
|
||||
{ name: "deny", desc: "double low buzz (nope)", make: () =>
|
||||
[{ freq: 150, duty: 60, ms: 110 }, rest(60), { freq: 150, duty: 60, ms: 110 }] },
|
||||
{ name: "tick", desc: "tiny UI click", make: () =>
|
||||
[{ freq: 2000, duty: 22, ms: 6 }] },
|
||||
{ name: "doublebeep", desc: "two clean beeps", make: () =>
|
||||
[{ freq: 1568, duty: 50, ms: 60 }, rest(60), { freq: 1568, duty: 50, ms: 60 }] },
|
||||
{ name: "sonar", desc: "single sonar ping", make: () =>
|
||||
[{ freq: 1400, duty: 50, ms: 40 }, { freq: 1350, duty: 30, ms: 120 }] },
|
||||
|
||||
// --- alarms / attention ---
|
||||
{ name: "alarm", desc: "beep-beep-beep-beep-beep", make: () =>
|
||||
rep([{ freq: 1000, duty: 55, ms: 120 }, rest(90)], 5) },
|
||||
{ name: "siren", desc: "police wail (two-tone x5)", make: () =>
|
||||
rep([{ freq: 900, duty: 50, ms: 190 }, { freq: 680, duty: 50, ms: 190 }], 5) },
|
||||
{ name: "wail", desc: "continuous rise/fall siren", make: () =>
|
||||
cat(sweep(500, 1200, 260, 16), sweep(1200, 500, 260, 16),
|
||||
sweep(500, 1200, 260, 14)) },
|
||||
{ name: "heartbeat", desc: "lub-dub x2", make: () =>
|
||||
[{ freq: 160, duty: 70, ms: 70 }, rest(40), { freq: 150, duty: 70, ms: 90 },
|
||||
rest(320),
|
||||
{ freq: 160, duty: 70, ms: 70 }, rest(40), { freq: 150, duty: 70, ms: 90 }] },
|
||||
|
||||
// --- character / fun ---
|
||||
{ name: "sadtrombone", desc: "wah-wah-wah-waaah", make: () =>
|
||||
cat(sweep(hz("Bb4"), hz("A4"), 260, 8, 55), [rest(30)],
|
||||
sweep(hz("A4"), hz("Ab4"), 260, 8, 55), [rest(30)],
|
||||
sweep(hz("Ab4"), hz("G4"), 260, 8, 55), [rest(30)],
|
||||
sweep(hz("G4"), hz("Gb4"), 520, 14, 55)) },
|
||||
{ name: "r2d2", desc: "warbling astromech chatter", make: () =>
|
||||
cat(sweep(900, 1900, 90, 8), sweep(1600, 700, 70, 6),
|
||||
[rest(30)], sweep(1100, 2200, 80, 7), sweep(2000, 1200, 60, 5),
|
||||
[rest(30)], sweep(800, 1500, 70, 6), sweep(1400, 2400, 90, 8)) },
|
||||
{ name: "questcomplete", desc: "Zelda secret-found jingle", make: () =>
|
||||
notes("G5/150 F#5/150 D#5/150 A4/150 G#4/150 E5/150 G#5/150 C6/500") },
|
||||
|
||||
// --- melodies (multi-phrase) ---
|
||||
{ name: "nokia", desc: "Nokia tune (Gran Vals)", make: () => [
|
||||
notes("E6/120 D6/120 F#5/240 G#5/240"),
|
||||
notes("C#6/120 B5/120 D5/240 E5/240"),
|
||||
notes("B5/120 A5/120 C#5/240 E5/240"),
|
||||
notes("A5/480"),
|
||||
] },
|
||||
{ name: "imperial", desc: "Imperial March opening", make: () => [
|
||||
notes("G4/300 G4/300 G4/300 Eb4/210 Bb4/90"),
|
||||
notes("G4/300 Eb4/210 Bb4/90 G4/600"),
|
||||
] },
|
||||
{ name: "mario", desc: "Super Mario Bros. opening bars", make: () => [
|
||||
notes("E6/110 E6/110 ./110 E6/110 ./110 C6/110 E6/110"),
|
||||
notes("G6/110 ./330 G5/110 ./330"),
|
||||
notes("C6/150 ./75 G5/150 ./75 E5/150 ./75"),
|
||||
notes("A5/110 B5/110 Bb5/110 A5/150"),
|
||||
] },
|
||||
{ name: "scale", desc: "C-major run up an octave", make: () =>
|
||||
notes("C5/90 D5/90 E5/90 F5/90 G5/90 A5/90 B5/90 C6/180") },
|
||||
];
|
||||
+1
-1
@@ -37,7 +37,7 @@ const dashboardActions: LayerActions = {
|
||||
setTranscribeRenderActive: () => {},
|
||||
startVoiceCapture: () => {},
|
||||
stopVoiceCapture: () => {},
|
||||
playBuzzerNote: () => {},
|
||||
playBuzzerSequence: () => {},
|
||||
};
|
||||
export const TOP_LEFT_MENU_LAYOUT = { x: 8, y: 8, width: 272 };
|
||||
|
||||
|
||||
+2
-1
@@ -23,7 +23,8 @@ export type LayerActions = {
|
||||
startVoiceCapture: () => Promise<void> | void;
|
||||
/** Stop voice capture; for a cloud provider this also commits for a final result. */
|
||||
stopVoiceCapture: () => Promise<void> | void;
|
||||
playBuzzerNote: (note: number, oct: number, beat: number) => Promise<void> | void;
|
||||
/** Play a CFW tone-sequencer payload (see sound-effects.ts). */
|
||||
playBuzzerSequence: (payload: Uint8Array) => Promise<void> | void;
|
||||
};
|
||||
|
||||
export type PaintBelow = () => GrayImage;
|
||||
|
||||
@@ -0,0 +1,243 @@
|
||||
import { getDefaultSmallFont } from "../../graphics/bdffont";
|
||||
import { GrayImage } from "../../graphics/image";
|
||||
import { wrapText } from "../../graphics/textwrap";
|
||||
import { clamp } from "../../util/numeric-util";
|
||||
import {
|
||||
mediaControllerBridge,
|
||||
type MediaControllerState,
|
||||
type MediaQueueItem,
|
||||
} from "../../native/media-controller";
|
||||
import { Layer, type DashboardInputEvent, type LayerContext } from "../layers";
|
||||
import {
|
||||
createInProcessWindow,
|
||||
YieldAtRootLayer,
|
||||
type InProcessAppOptions,
|
||||
type InProcessWindow,
|
||||
} from "./in-process-window";
|
||||
|
||||
export const MUSIC_WINDOW_ID = "music";
|
||||
export const MUSIC_SURFACE_ID = "window:music";
|
||||
|
||||
const ART_SIZE = 96;
|
||||
const ART_X = 22;
|
||||
const ART_Y = 40;
|
||||
const META_X = ART_X + ART_SIZE + 14;
|
||||
const LIST_TOP = 150;
|
||||
const ROW_HEIGHT = 16;
|
||||
const LIST_X = 22;
|
||||
const FOOTER_HEIGHT = 20;
|
||||
|
||||
type MusicRow =
|
||||
| { kind: "play-pause"; label: string; enabled: boolean }
|
||||
| { kind: "previous"; label: string; enabled: boolean }
|
||||
| { kind: "next"; label: string; enabled: boolean }
|
||||
| { kind: "queue-item"; label: string; enabled: true; item: MediaQueueItem };
|
||||
|
||||
/**
|
||||
* Music controller app: metadata + album art + transport controls for the
|
||||
* active Android media session (any player that publishes one), plus the
|
||||
* player's queue when it exposes one (scroll to a track, click to jump).
|
||||
*/
|
||||
class MusicAppLayer implements Layer {
|
||||
private selectedIndex = 0;
|
||||
private scrollRow = 0;
|
||||
private art: GrayImage | null = null;
|
||||
private artKey = "";
|
||||
|
||||
paint(ctx: LayerContext): GrayImage {
|
||||
const font = getDefaultSmallFont();
|
||||
const { width, height } = ctx.stack.getBaseSize();
|
||||
const image = new GrayImage(width, height, 0);
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
|
||||
image.drawText(font, 20, 8, "Music", 220);
|
||||
const status = playbackLabel(media);
|
||||
image.drawText(font, width - 24 - font.measureText(status), 8, status, 140);
|
||||
|
||||
if (!media.accessEnabled) {
|
||||
const lines = wrapText(
|
||||
font,
|
||||
"Notification access is required before Android exposes media sessions. Click to open settings.",
|
||||
width - 48,
|
||||
);
|
||||
for (let index = 0; index < lines.length; index++) {
|
||||
image.drawText(font, 24, 44 + index * 14, lines[index]!, 180);
|
||||
}
|
||||
image.drawText(font, 20, height - 16, "Click: open settings Double-click: back", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
if (!media.available) {
|
||||
image.drawText(font, 24, 44, "No active media session.", 180);
|
||||
image.drawText(font, 24, 62, "Start playback in another app on the phone.", 150);
|
||||
image.drawText(font, 20, height - 16, "Double-click: back to sidebar", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
this.drawArt(image, media);
|
||||
|
||||
const metaWidth = width - META_X - 24;
|
||||
const titleLines = wrapText(font, media.title || "Unknown title", metaWidth).slice(0, 2);
|
||||
for (let index = 0; index < titleLines.length; index++) {
|
||||
image.drawText(font, META_X, ART_Y + 2 + index * 15, titleLines[index]!, 230);
|
||||
}
|
||||
image.drawText(font, META_X, ART_Y + 36, media.artist || "Unknown artist", 180);
|
||||
if (media.album) {
|
||||
image.drawText(font, META_X, ART_Y + 52, truncate(font, media.album, metaWidth), 150);
|
||||
}
|
||||
image.drawText(font, META_X, ART_Y + 70, media.packageName, 110);
|
||||
|
||||
const rows = this.buildRows(media);
|
||||
this.selectedIndex = clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1));
|
||||
const listHeight = height - LIST_TOP - 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, rows.length - visibleRows));
|
||||
|
||||
const lastVisible = Math.min(rows.length, this.scrollRow + visibleRows);
|
||||
for (let index = this.scrollRow; index < lastVisible; index++) {
|
||||
const row = rows[index]!;
|
||||
const y = LIST_TOP + (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);
|
||||
}
|
||||
const value = !row.enabled ? (selected ? 130 : 90) : selected ? 255 : 200;
|
||||
image.drawText(font, LIST_X, y + 1, truncate(font, row.label, width - 2 * LIST_X), value);
|
||||
}
|
||||
|
||||
image.drawText(font, 20, height - 16, "Scroll: select Click: activate Double-click: back", 110);
|
||||
return image;
|
||||
}
|
||||
|
||||
async handleInput(event: DashboardInputEvent, ctx: LayerContext): Promise<void> {
|
||||
const media = mediaControllerBridge.snapshot();
|
||||
if (!media.accessEnabled) {
|
||||
if (event.type === "click") {
|
||||
mediaControllerBridge.openNotificationAccessSettings();
|
||||
} else if (event.type === "double-click") {
|
||||
ctx.stack.pop();
|
||||
}
|
||||
return;
|
||||
}
|
||||
const rows = this.buildRows(media);
|
||||
switch (event.type) {
|
||||
case "scroll-up":
|
||||
this.selectedIndex = Math.max(0, this.selectedIndex - 1);
|
||||
return;
|
||||
case "scroll-down":
|
||||
this.selectedIndex = Math.min(Math.max(0, rows.length - 1), this.selectedIndex + 1);
|
||||
return;
|
||||
case "click": {
|
||||
const row = rows[clamp(this.selectedIndex, 0, Math.max(0, rows.length - 1))];
|
||||
if (!row || !row.enabled) return;
|
||||
if (row.kind === "play-pause") await mediaControllerBridge.playPause();
|
||||
else if (row.kind === "previous") await mediaControllerBridge.skipPrevious();
|
||||
else if (row.kind === "next") await mediaControllerBridge.skipNext();
|
||||
else if (row.kind === "queue-item") await mediaControllerBridge.skipToQueueItem(row.item.id);
|
||||
return;
|
||||
}
|
||||
case "double-click":
|
||||
ctx.stack.pop();
|
||||
return;
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
private buildRows(media: MediaControllerState): MusicRow[] {
|
||||
const rows: MusicRow[] = [
|
||||
{
|
||||
kind: "play-pause",
|
||||
label: media.playbackState === "playing" ? "Pause" : "Play",
|
||||
enabled: media.canPlayPause,
|
||||
},
|
||||
{ kind: "previous", label: "Previous track", enabled: media.canSkipPrevious },
|
||||
{ kind: "next", label: "Next track", enabled: media.canSkipNext },
|
||||
];
|
||||
for (const item of mediaControllerBridge.getQueue()) {
|
||||
rows.push({
|
||||
kind: "queue-item",
|
||||
label: `${item.active ? "> " : " "}${item.title || "(untitled)"}`,
|
||||
enabled: true,
|
||||
item,
|
||||
});
|
||||
}
|
||||
return rows;
|
||||
}
|
||||
|
||||
private drawArt(image: GrayImage, media: MediaControllerState): void {
|
||||
const key = `${media.packageName}|${media.title}|${media.album}`;
|
||||
if (key !== this.artKey) {
|
||||
this.artKey = key;
|
||||
this.art = mediaControllerBridge.getAlbumArt(ART_SIZE);
|
||||
}
|
||||
if (this.art) {
|
||||
// Center within the art box.
|
||||
const dx = ART_X + Math.max(0, ((ART_SIZE - this.art.width) / 2) | 0);
|
||||
const dy = ART_Y + Math.max(0, ((ART_SIZE - this.art.height) / 2) | 0);
|
||||
image.bitBlt(this.art, dx, dy);
|
||||
image.drawRect(dx - 1, dy - 1, this.art.width + 2, this.art.height + 2, 60);
|
||||
} else {
|
||||
image.drawRect(ART_X, ART_Y, ART_SIZE, ART_SIZE, 60);
|
||||
const font = getDefaultSmallFont();
|
||||
image.drawText(font, ART_X + 22, ART_Y + ART_SIZE / 2 - 7, "no art", 90);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export function createMusicAppWindow(options: InProcessAppOptions): InProcessWindow {
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
const app = createInProcessWindow({
|
||||
appId: "music",
|
||||
windowId: MUSIC_WINDOW_ID,
|
||||
title: "Music",
|
||||
iconLetter: "M",
|
||||
closeable: true,
|
||||
actions: options.actions,
|
||||
baseLayer: new YieldAtRootLayer(new MusicAppLayer()),
|
||||
submitFrame: options.submitFrame,
|
||||
setSurfaceVisible: options.setSurfaceVisible,
|
||||
removeSurface: options.removeSurface,
|
||||
onClosed: () => {
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
options.onClosed();
|
||||
},
|
||||
});
|
||||
unsubscribe = mediaControllerBridge.onStateChange(() => {
|
||||
app.requestRender();
|
||||
});
|
||||
return app;
|
||||
}
|
||||
|
||||
function playbackLabel(media: MediaControllerState): string {
|
||||
switch (media.playbackState) {
|
||||
case "playing":
|
||||
return "Playing";
|
||||
case "paused":
|
||||
return "Paused";
|
||||
case "buffering":
|
||||
return "Buffering";
|
||||
case "stopped":
|
||||
return "Stopped";
|
||||
case "notification-access-required":
|
||||
return "Access required";
|
||||
default:
|
||||
return media.status || "Idle";
|
||||
}
|
||||
}
|
||||
|
||||
function truncate(font: import("../../graphics/bdffont").BdfFont, text: string, maxWidth: number): string {
|
||||
if (font.measureText(text) <= maxWidth) return text;
|
||||
let out = text;
|
||||
while (out.length > 1 && font.measureText(`${out}...`) > maxWidth) {
|
||||
out = out.slice(0, -1);
|
||||
}
|
||||
return `${out}...`;
|
||||
}
|
||||
@@ -69,7 +69,7 @@ const noopActions: LayerActions = {
|
||||
setTranscribeRenderActive: () => {},
|
||||
startVoiceCapture: () => {},
|
||||
stopVoiceCapture: () => {},
|
||||
playBuzzerNote: () => {},
|
||||
playBuzzerSequence: () => {},
|
||||
};
|
||||
|
||||
/** Long-press overlay menu; closing it returns focus to the sidebar. */
|
||||
|
||||
Reference in new issue
Block a user