Add support for buzzer sound effects on iOS

This commit is contained in:
James Babcock committed 2026-09-19 12:03:57 -07:00
1 parent 1412db8e82
commit e50da605e0
19 files changed
+211 -48

No files matched your search

+4 -7
View File
@@ -17,6 +17,7 @@ import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import { playWorkerBuzzerSequence } from "../../native/worker-buzzer";
import { getActiveDisplay } from "../../native/active-display";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
@@ -33,7 +34,6 @@ import {
} from "../../ui/gestures";
declare const global: any;
declare const com: any;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
@@ -295,16 +295,13 @@ function inferForeground(window: BlocksWindow, focused: boolean): void {
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread
* (same path as frame submission). Effects never exceed one message, so no
* phrase pacing is needed.
* steps on its own timer; the platform bridge routes it from the worker.
* Effects never exceed one message, so no phrase pacing is needed.
*/
function playSfx(window: BlocksWindow, steps: Step[]): void {
if (!window.soundOn || steps.length === 0) return;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
playWorkerBuzzerSequence(buildSoundSequencePayload(steps));
} catch (error) {
console.warn(`blocks sfx failed: ${error}`);
}
+2 -1
View File
@@ -103,7 +103,8 @@ async function startApp(
cleanup?: () => void,
): Promise<void> {
const windowId = `evenhub:app:${nextSerial++}`;
const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl);
const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl,
payload => ctx.actions.playBuzzerSequence(payload));
const webView = createEvenHubWebView(session);
session.attachWebView({
evaluateJs: webView.evaluateJs,
+2 -14
View File
@@ -65,8 +65,6 @@ import { ensureFineLocationPermission } from "../../native/location-permissions"
const UPNG = require("upng-js");
declare const com: any;
/** OsEventTypeList values (PB ordinals). */
const CLICK_EVENT = 0;
const SCROLL_TOP_EVENT = 1;
@@ -371,6 +369,7 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
distDir: string,
log: (message: string) => void,
remoteUrl = "",
private readonly sendBuzzerSequence: (payload: Uint8Array) => Promise<void> | void = () => {},
) {
this.manifest = manifest;
this.distDir = distDir;
@@ -1164,13 +1163,11 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
private async playBuzzer(rawSteps: unknown[]): Promise<void> {
const steps = normalizeBuzzerSteps(rawSteps);
if (steps.length === 0) return;
const communicator = activeCommunicator();
if (!communicator) return;
for (let i = 0; i < steps.length; i += CFW_SEQ_MAX) {
const chunk = steps.slice(i, i + CFW_SEQ_MAX);
const payload = buildSoundSequencePayload(chunk);
try {
communicator.playBuzzerSequence(payload.buffer);
await this.sendBuzzerSequence(payload);
} catch (error) {
this.log(`evenhub: playBuzzer failed: ${error}`);
return;
@@ -1274,15 +1271,6 @@ function clampImuFreq(freq: number): number {
return Math.min(1000, Math.max(100, snapped));
}
function activeCommunicator(): any {
if (!global.isAndroid) return null;
try {
return com.faceclaw.app.FaceclawBleCommunicator.getActive();
} catch {
return null;
}
}
/**
* Copy content into a `preserve` container from the same-named, same-kind
* container in the previous page (if any), so a replaceLayout keeps its pixels /
+3 -5
View File
@@ -31,6 +31,7 @@ import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import { playWorkerBuzzerSequence } from "../../native/worker-buzzer";
import { getActiveDisplay } from "../../native/active-display";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
@@ -64,7 +65,6 @@ import {
} from "./flappy-sprites";
declare const global: any;
declare const com: any;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
@@ -311,7 +311,7 @@ function saveHighScore(window: FlappyWindow): void {
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread.
* steps on its own timer; the platform bridge routes it from the worker.
* Minor (frequent) effects are dropped when they'd arrive within the
* rate-limit gap; newsworthy ones always play.
*/
@@ -321,9 +321,7 @@ function playSfx(window: FlappyWindow, steps: Step[], minor = false): void {
if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return;
if (minor) window.lastMinorSfxAtMs = now;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
playWorkerBuzzerSequence(buildSoundSequencePayload(steps));
} catch (error) {
console.warn(`flappy sfx failed: ${error}`);
}
+3 -5
View File
@@ -25,6 +25,7 @@ import { getFont } from "../../graphics/bdffont";
import { ensurePreinstalledFonts, installedFontPath } from "../../graphics/installed-fonts";
import { TtfFont } from "../../graphics/ttf-font";
import * as frameTimings from "../../native/frame-timings";
import { playWorkerBuzzerSequence } from "../../native/worker-buzzer";
import { getActiveDisplay } from "../../native/active-display";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
@@ -34,7 +35,6 @@ import type { WorkerAppMessage, WorkerAppReply } from "../../ui/shell/worker-win
import { directionalFallback, GESTURE_CLICK, GESTURE_DOUBLE_CLICK, GESTURE_LONG_PRESS, type InputEvent } from "../../ui/gestures";
declare const global: any;
declare const com: any;
ensurePreinstalledFonts();
const fontPath = installedFontPath("Roboto-Regular.ttf");
@@ -206,14 +206,12 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread.
* steps on its own timer; the platform bridge routes it from the worker.
*/
function playSfx(window: FreecellWindow, steps: Step[]): void {
if (!window.soundOn || steps.length === 0) return;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
playWorkerBuzzerSequence(buildSoundSequencePayload(steps));
} catch (error) {
console.warn(`freecell sfx failed: ${error}`);
}
@@ -25,6 +25,7 @@ import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import * as frameTimings from "../../native/frame-timings";
import { playWorkerBuzzerSequence } from "../../native/worker-buzzer";
import { getActiveDisplay } from "../../native/active-display";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
import { loadSoundEnabled, saveSoundEnabled } from "../../ui/sound-setting";
@@ -43,7 +44,6 @@ import {
import { clamp } from "../../util/numeric-util";
declare const global: any;
declare const com: any;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
@@ -278,14 +278,12 @@ function saveDifficultyIndex(window: MinesweeperWindow): void {
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread.
* steps on its own timer; the platform bridge routes it from the worker.
*/
function playSfx(window: MinesweeperWindow, steps: Step[]): void {
if (!window.soundOn || steps.length === 0) return;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
playWorkerBuzzerSequence(buildSoundSequencePayload(steps));
} catch (error) {
console.warn(`minesweeper sfx failed: ${error}`);
}
+3 -5
View File
@@ -27,6 +27,7 @@ import { prepareFrameDraws } from "../../graphics/glyph-wire";
import { getFont } from "../../graphics/bdffont";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import * as frameTimings from "../../native/frame-timings";
import { playWorkerBuzzerSequence } from "../../native/worker-buzzer";
import { getActiveDisplay } from "../../native/active-display";
import { getStringSetting, setStringSetting } from "../../native/settings-store";
import { buildSoundSequencePayload, type Step } from "../../ui/sound-effects";
@@ -44,7 +45,6 @@ import {
import { clamp } from "../../util/numeric-util";
declare const global: any;
declare const com: any;
const largeFont = getFont("terminus32");
const mediumFont = getFont("terminus24");
@@ -429,7 +429,7 @@ function saveHighScore(window: PinballWindow): void {
/**
* Fire a buzzer effect. Non-blocking: the firmware's sequencer plays the
* steps on its own timer, and the Java call is safe from the worker thread.
* steps on its own timer; the platform bridge routes it from the worker.
* Minor (frequent) effects are dropped when they'd arrive within the
* rate-limit gap; newsworthy ones always play.
*/
@@ -439,9 +439,7 @@ function playSfx(window: PinballWindow, steps: Step[], minor = false): void {
if (minor && now - window.lastMinorSfxAtMs < MINOR_SFX_MIN_GAP_MS) return;
if (minor) window.lastMinorSfxAtMs = now;
try {
const communicator = com.faceclaw.app.FaceclawBleCommunicator.getActive();
if (!communicator) return;
communicator.playBuzzerSequence(buildSoundSequencePayload(steps).buffer);
playWorkerBuzzerSequence(buildSoundSequencePayload(steps));
} catch (error) {
console.warn(`pinball sfx failed: ${error}`);
}