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

@@ -361,23 +361,35 @@ public class FaceclawBleCommunicator implements FaceclawBleListener, Runnable {
interruptibleSleep.interrupt();
}
/** Play one quantized buzzer tone via CFW load_image_z mode 5 kind 1. */
public void playBuzzerNote(int note, int oct, int beat) {
/**
* Play a tone sequence via CFW load_image_z mode 5 kind 4. The payload is
* the complete wire buffer ([5][4][nSteps][freqLo,freqHi,duty,msLo,msHi]*n,
* up to 48 steps), built on the TS side; it rides the arbitrary-payload
* image path like the other mode-5 controls.
*/
public void playBuzzerSequence(java.nio.ByteBuffer payload) {
synchronized (lock) {
if (!running || !sessionReady || !fixedLayoutCreated) {
logLine("skip buzzer note; session not ready");
logLine("skip buzzer sequence; session not ready");
return;
}
byte[] bytes = new byte[payload == null ? 0 : payload.remaining()];
if (payload != null) {
payload.get(bytes);
}
if (bytes.length < 3) {
logLine("skip buzzer sequence; empty payload");
return;
}
byte[] payload = BleProtocol.buildBuzzerNotePayload(note, oct, beat);
OutboundMessage message = messageBuilder.imagePayload(
DASHBOARD_TILE,
nextMapSessionId(),
payload,
"buzzer note=" + note + " oct=" + oct + " beat=" + beat,
bytes,
"buzzer sequence " + bytes.length + "B",
connectionOptions.sendImagesToLeft
);
message.onTimeout = () -> {
handleTransportFailure("buzzer note ack timeout");
handleTransportFailure("buzzer sequence ack timeout");
};
pendingMessages.addLast(message);
logLine("queue " + message.label);
@@ -3,13 +3,20 @@ package com.faceclaw.app;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaDescription;
import android.media.MediaMetadata;
import android.media.session.MediaController;
import android.media.session.MediaSession;
import android.media.session.MediaSessionManager;
import android.media.session.PlaybackState;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.util.List;
@@ -35,6 +42,13 @@ public class FaceclawMediaController {
}
}
@Override
public void onQueueChanged(List<MediaSession.QueueItem> queue) {
synchronized (lock) {
emitStateLocked();
}
}
@Override
public void onSessionDestroyed() {
synchronized (lock) {
@@ -149,6 +163,107 @@ public class FaceclawMediaController {
}
}
public void skipToQueueItem(long queueId) {
synchronized (lock) {
if (activeController != null) {
activeController.getTransportControls().skipToQueueItem(queueId);
}
}
}
/**
* Album art for the active session's current item, grayscale, scaled to
* fit within maxSize x maxSize preserving aspect. Returns
* [widthLo, widthHi, heightLo, heightHi, pixels...] or an empty array
* when no art is available.
*/
public byte[] getAlbumArtGray(int maxSize) {
Bitmap art;
synchronized (lock) {
if (activeController == null) {
return new byte[0];
}
MediaMetadata metadata = activeController.getMetadata();
if (metadata == null) {
return new byte[0];
}
art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART);
if (art == null) {
art = metadata.getBitmap(MediaMetadata.METADATA_KEY_ART);
}
if (art == null) {
art = metadata.getBitmap(MediaMetadata.METADATA_KEY_DISPLAY_ICON);
}
}
if (art == null || art.getWidth() <= 0 || art.getHeight() <= 0 || maxSize <= 0) {
return new byte[0];
}
try {
float scale = Math.min(1f, Math.min(
(float) maxSize / art.getWidth(),
(float) maxSize / art.getHeight()));
int width = Math.max(1, Math.round(art.getWidth() * scale));
int height = Math.max(1, Math.round(art.getHeight() * scale));
Bitmap scaled = Bitmap.createScaledBitmap(art, width, height, true);
if (scaled.getConfig() == Bitmap.Config.HARDWARE) {
scaled = scaled.copy(Bitmap.Config.ARGB_8888, false);
}
int[] pixels = new int[width * height];
scaled.getPixels(pixels, 0, width, 0, 0, width, height);
byte[] out = new byte[4 + width * height];
out[0] = (byte) (width & 0xff);
out[1] = (byte) ((width >> 8) & 0xff);
out[2] = (byte) (height & 0xff);
out[3] = (byte) ((height >> 8) & 0xff);
for (int i = 0; i < pixels.length; i++) {
int p = pixels[i];
int r = (p >> 16) & 0xff;
int g = (p >> 8) & 0xff;
int b = p & 0xff;
out[4 + i] = (byte) ((r * 299 + g * 587 + b * 114) / 1000);
}
return out;
} catch (Exception e) {
Log.w("FaceclawMedia", "album art conversion failed", e);
return new byte[0];
}
}
/**
* The active session's queue (playlist) as JSON:
* [{"id": long, "title": string, "active": bool}, ...]. Empty string when
* the player exposes no queue.
*/
public String getQueueJson() {
synchronized (lock) {
if (activeController == null) {
return "";
}
List<MediaSession.QueueItem> queue = activeController.getQueue();
if (queue == null || queue.isEmpty()) {
return "";
}
PlaybackState state = activeController.getPlaybackState();
long activeId = state == null ? -1 : state.getActiveQueueItemId();
try {
JSONArray out = new JSONArray();
for (MediaSession.QueueItem item : queue) {
MediaDescription description = item.getDescription();
CharSequence title = description == null ? null : description.getTitle();
JSONObject entry = new JSONObject();
entry.put("id", item.getQueueId());
entry.put("title", title == null ? "" : title.toString());
entry.put("active", item.getQueueId() == activeId);
out.put(entry);
}
return out.toString();
} catch (Exception e) {
Log.w("FaceclawMedia", "queue serialization failed", e);
return "";
}
}
}
public void openNotificationAccessSettings() {
Intent intent = new Intent(Settings.ACTION_NOTIFICATION_LISTENER_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
@@ -121,14 +121,6 @@ public class BleProtocol {
return wrapEvenHub(5, magic, 9, concat(inner));
}
/** CFW load_image_z mode 5 kind 1: play one quantized buzzer note. */
public static byte[] buildBuzzerNotePayload(int note, int oct, int beat) {
int clampedNote = Math.max(1, Math.min(7, note));
int clampedOct = Math.max(0, Math.min(3, oct));
int clampedBeat = Math.max(1, Math.min(255, beat));
return new byte[] {5, 1, (byte) clampedNote, (byte) clampedOct, (byte) clampedBeat};
}
public static byte[] buildImageRawData(ImageTileOptions tile, int sessionId, int totalSize, ImageFragment fragment, int magic) {
List<byte[]> inner = new ArrayList<>();
inner.add(encodeVarintField(1, tile.containerId));