Files
led-controller/main/audio_api.c
T
jimmyandClaude Opus 5.5 581b751904 feat: browser simulator served by the controller, desktop build, browser bridge
- led-simulator: the rig runs the C++ led-driver as WebAssembly (sim-driver/),
  one single-threaded worker per bar, no cross-origin isolation needed. Rooms
  (one JSON per room) with an on-page editor, and a pattern gallery. The
  Python server, Pyodide boot and ring buffer are removed.
- sim-controller sim_rig.c: serves /sim/ from an embedded pack, the page's
  WebSocket and the rooms/flash API. The desktop build embeds it too.
- bridge.c: a "browser" bridge kind that carries ESP-NOW frames to the page;
  devices announced through it are marked sim.
- UI: Simulator in the Menu, split view with resize and pop-out, swipe
  between controller and rig on phones; SIM/REAL badges in Devices.
- Fades: preset pushes and Wi-Fi driver bodies always carry fade and
  beat_time, so "off" no longer inherits an earlier fade.
- Also the sequences-v2 work in progress: desktop port, bridge firmware,
  serial bridge, Wi-Fi drivers, sequence timeline, audio and API changes.
- led-driver: ESP-NOW link, beat sync, control messages, sim hooks.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-16 19:32:46 +13:00

377 lines
13 KiB
C

// main's /api/audio routes on this board's microphone: status and an event stream in main's shape,
// start/stop (whether mic beats time sequences), tracking reset and bar anchoring. There is one input,
// the I2S microphone, so device selection is stored but changes nothing.
#include "audio_api.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "api_util.h"
#include "beat.h"
#include "cJSON.h"
#include "esp_log.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "lwip/sockets.h"
#include "mic.h"
#include "seqplay.h"
#include "store.h"
static const char *TAG = "audio_api";
#define EVENTS_HEARTBEAT_US 500000
static int setting_int(const char *key, int dflt, int lo, int hi)
{
const cJSON *v = cJSON_GetObjectItem(store_settings(), key);
int x = cJSON_IsNumber(v) ? (int)v->valuedouble : dflt;
return x < lo ? lo : (x > hi ? hi : x);
}
#ifdef LED_DESKTOP_ALSA
// Desktop: the microphone reads the device chosen in the settings while audio is running
static void mic_follow_setting(bool start)
{
if (!start) {
mic_close();
return;
}
char dev[128] = "";
store_lock();
const cJSON *sel = cJSON_GetObjectItem(store_settings(), "audio_device_select");
if (cJSON_IsString(sel) && strlen(sel->valuestring) < sizeof(dev)) {
strcpy(dev, sel->valuestring);
}
store_unlock();
if (mic_open(dev) != ESP_OK) {
ESP_LOGW(TAG, "cannot use audio device \"%s\"", dev);
}
}
#endif
static cJSON *audio_run(void)
{
cJSON *o = cJSON_CreateObject();
cJSON_AddBoolToObject(o, "enabled", seqplay_audio());
const cJSON *sel = cJSON_GetObjectItem(store_settings(), "audio_device_select");
cJSON_AddNullToObject(o, "device");
cJSON_AddStringToObject(o, "device_override", "");
cJSON_AddStringToObject(o, "device_select", cJSON_IsString(sel) ? sel->valuestring : "");
return o;
}
// main's audio_status_payload
static cJSON *status_payload(void)
{
beat_info_t bi;
beat_get_info(&bi);
mic_level_t lv;
mic_get_level(&lv);
bool running = seqplay_audio();
int meter = bi.meter > 0 ? bi.meter : 4;
cJSON *o = cJSON_CreateObject();
store_lock();
int sim_bpm = setting_int("audio_simulated_bpm", 120, 60, 200);
cJSON_AddBoolToObject(o, "running", running);
#ifdef LED_DESKTOP_ALSA
const cJSON *dev = cJSON_GetObjectItem(store_settings(), "audio_device_select");
cJSON_AddStringToObject(o, "device", cJSON_IsString(dev) ? dev->valuestring : "");
#else
cJSON_AddStringToObject(o, "device", "i2s");
#endif
#ifdef LED_DESKTOP_ALSA
char mic_err[160];
mic_error(mic_err, sizeof(mic_err));
if (mic_err[0]) {
cJSON_AddStringToObject(o, "error", mic_err);
} else {
cJSON_AddNullToObject(o, "error");
}
#else
cJSON_AddNullToObject(o, "error");
#endif
if (!running) {
cJSON_AddNumberToObject(o, "bpm", sim_bpm);
} else if (bi.locked && bi.bpm > 0) {
cJSON_AddNumberToObject(o, "bpm", bi.bpm < 60 ? 60 : (bi.bpm > 200 ? 200 : bi.bpm));
} else {
cJSON_AddNullToObject(o, "bpm");
}
cJSON_AddNumberToObject(o, "beat_seq", bi.beat_count);
cJSON_AddNumberToObject(o, "beats_per_bar", meter);
cJSON_AddStringToObject(o, "beat_type", "unknown");
cJSON_AddNumberToObject(o, "beat_type_confidence", 0);
cJSON_AddNumberToObject(o, "input_level", running ? powf(10.0f, lv.rms_dbfs / 20.0f) : 0);
cJSON_AddItemToObject(o, "last_beat_ts", bi.last_beat_us ? cJSON_CreateNumber(bi.last_beat_us / 1e6) : cJSON_CreateNull());
int bar_beat;
double phase_conf;
if (running) {
bar_beat = bi.bar_beat > 0 ? bi.bar_beat : 1;
phase_conf = bi.bar_sure ? 1.0 : 0.0;
} else {
uint32_t tick;
seqplay_clock(&bar_beat, &tick);
phase_conf = 0;
}
char readout[24];
snprintf(readout, sizeof(readout), "%d/%d", bar_beat, running ? meter : 4);
cJSON_AddNumberToObject(o, "bar_beat", bar_beat);
cJSON_AddBoolToObject(o, "is_downbeat", bar_beat == 1);
cJSON_AddStringToObject(o, "bar_phase_readout", readout);
cJSON_AddNumberToObject(o, "phase_confidence", phase_conf);
cJSON *seq = seqplay_status();
const cJSON *ro = cJSON_GetObjectItem(seq, "beat_readout");
if (cJSON_IsString(ro) && ro->valuestring[0]) {
snprintf(readout, sizeof(readout), "%s", ro->valuestring);
} else if (seqplay_done_readout()[0]) {
snprintf(readout, sizeof(readout), "%s", seqplay_done_readout());
} else if (running && bi.beat_count > 0) {
snprintf(readout, sizeof(readout), "%lu", (unsigned long)bi.beat_count);
} else {
readout[0] = 0;
}
cJSON_AddItemToObject(o, "sequence", seq);
cJSON *stride = cJSON_AddObjectToObject(o, "manual_beat_stride");
cJSON_AddFalseToObject(stride, "active"); // drivers step manual presets on the beats themselves
cJSON_AddStringToObject(o, "beat_readout", readout);
cJSON_AddNumberToObject(o, "beat_phase_ms", setting_int("audio_beat_phase_ms", 0, 0, 500));
cJSON_AddNumberToObject(o, "input_volume", setting_int("audio_input_volume", 100, 0, 200));
const cJSON *sw = cJSON_GetObjectItem(store_settings(), "sequence_switch_wait");
cJSON_AddStringToObject(o, "sequence_switch_wait_saved",
cJSON_IsString(sw) && !strcasecmp(sw->valuestring, "downbeat") ? "downbeat" : "beat");
cJSON_AddStringToObject(o, "sequence_switch_wait", seqplay_switch_wait());
cJSON_AddItemToObject(o, "audio_run", audio_run());
cJSON_AddNumberToObject(o, "audio_simulated_bpm", sim_bpm);
cJSON_AddItemToObject(o, "sequence_pending", seqplay_pending());
cJSON_AddBoolToObject(o, "bpm_simulated", !(running && bi.locked));
int sim_bar;
uint32_t tick;
seqplay_clock(&sim_bar, &tick);
cJSON_AddNumberToObject(o, "simulated_beat_tick", tick);
store_unlock();
return o;
}
static esp_err_t send_json(httpd_req_t *req, const char *status, cJSON *o)
{
char *s = cJSON_PrintUnformatted(o);
cJSON_Delete(o);
if (!s) {
return httpd_resp_send_500(req);
}
httpd_resp_set_status(req, status);
httpd_resp_set_type(req, "application/json");
esp_err_t r = httpd_resp_sendstr(req, s);
free(s);
return r;
}
static esp_err_t ok_with_status(httpd_req_t *req)
{
cJSON *o = cJSON_CreateObject();
cJSON_AddTrueToObject(o, "ok");
cJSON_AddItemToObject(o, "status", status_payload());
return send_json(req, "200 OK", o);
}
/* ------------------------------------------------------------ event stream */
static bool send_all(int fd, const char *p, size_t len)
{
while (len > 0) {
int sent = send(fd, p, len, 0);
if (sent <= 0) {
return false;
}
p += sent;
len -= sent;
}
return true;
}
static bool send_event(int fd, const char *type)
{
cJSON *o = cJSON_CreateObject();
cJSON_AddStringToObject(o, "type", type);
cJSON_AddItemToObject(o, "status", status_payload());
char *s = cJSON_PrintUnformatted(o);
cJSON_Delete(o);
bool ok = s && send_all(fd, "data: ", 6) && send_all(fd, s, strlen(s)) && send_all(fd, "\n\n", 2);
free(s);
return ok;
}
static void events_task(void *arg)
{
httpd_req_t *req = arg;
httpd_handle_t server = req->handle;
int fd = httpd_req_to_sockfd(req);
QueueHandle_t q = xQueueCreate(8, sizeof(beat_event_t));
static const char hdr[] = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-cache\r\n"
"Connection: close\r\n\r\n";
bool ok = q && beat_subscribe(q) == ESP_OK;
if (!ok) {
static const char busy[] = "HTTP/1.1 503 Service Unavailable\r\nConnection: close\r\n\r\nToo many listeners\n";
send_all(fd, busy, sizeof(busy) - 1);
} else {
ok = send_all(fd, hdr, sizeof(hdr) - 1) && send_event(fd, "status");
}
int64_t next = esp_timer_get_time() + EVENTS_HEARTBEAT_US;
while (ok) {
int64_t now = esp_timer_get_time();
beat_event_t ev;
if (now >= next) {
ok = send_event(fd, "status");
next = now + EVENTS_HEARTBEAT_US;
} else if (xQueueReceive(q, &ev, pdMS_TO_TICKS((next - now) / 1000) + 1) == pdTRUE && ev == BEAT_EVENT_BEAT) {
vTaskDelay(pdMS_TO_TICKS(20)); // let the sequencer count this beat first
ok = send_event(fd, "beat");
}
}
if (q) {
beat_unsubscribe(q);
vQueueDelete(q);
}
httpd_req_async_handler_complete(req);
httpd_sess_trigger_close(server, fd);
vTaskDelete(NULL);
}
static esp_err_t events(httpd_req_t *req)
{
httpd_req_t *async_req = NULL;
if (httpd_req_async_handler_begin(req, &async_req) != ESP_OK) {
return ESP_FAIL;
}
if (xTaskCreate(events_task, "audio_events", 6144, async_req, 5, NULL) != pdPASS) {
httpd_req_async_handler_complete(async_req);
return ESP_FAIL;
}
return ESP_OK;
}
/* --------------------------------------------------------------- handlers */
static esp_err_t audio_handler(httpd_req_t *req)
{
const char *action = req->uri + strlen("/api/audio/");
size_t alen = strcspn(action, "?");
char act[16] = "";
if (alen < sizeof(act)) {
memcpy(act, action, alen);
}
if (req->method == HTTP_GET && !strcmp(act, "status")) {
cJSON *o = cJSON_CreateObject();
cJSON_AddItemToObject(o, "status", status_payload());
return send_json(req, "200 OK", o);
}
if (req->method == HTTP_GET && !strcmp(act, "events")) {
return events(req);
}
#ifdef LED_DESKTOP_ALSA
if (req->method == HTTP_GET && !strcmp(act, "devices")) {
cJSON *o = cJSON_CreateObject(), *devs = cJSON_AddArrayToObject(o, "devices");
mic_device_t found[16];
int n = mic_list_devices(found, 16);
for (int i = 0; i < n; i++) {
cJSON *d = cJSON_CreateObject();
cJSON_AddStringToObject(d, "id", found[i].id);
cJSON_AddStringToObject(d, "name", found[i].name);
cJSON_AddStringToObject(d, "display_name", found[i].name);
cJSON_AddStringToObject(d, "label", found[i].label);
cJSON_AddItemToArray(devs, d);
}
cJSON_AddObjectToObject(o, "diagnostics");
return send_json(req, "200 OK", o);
}
#endif
if (req->method == HTTP_GET && !strcmp(act, "devices")) {
cJSON *o = cJSON_CreateObject(), *devs = cJSON_AddArrayToObject(o, "devices"), *d = cJSON_CreateObject();
cJSON_AddStringToObject(d, "id", "i2s");
cJSON_AddStringToObject(d, "name", "I2S microphone");
cJSON_AddStringToObject(d, "display_name", "I2S microphone");
cJSON_AddTrueToObject(d, "is_default");
cJSON_AddItemToArray(devs, d);
cJSON_AddObjectToObject(o, "diagnostics");
return send_json(req, "200 OK", o);
}
if (req->method == HTTP_PUT && !strcmp(act, "device")) {
cJSON *body = api_body_opt(req);
if (!body) {
return ESP_OK;
}
const cJSON *sel = cJSON_GetObjectItem(body, "device_select");
store_lock();
api_set(store_settings(), "audio_device_select", cJSON_CreateString(cJSON_IsString(sel) ? sel->valuestring : ""));
store_save("settings");
#ifdef LED_DESKTOP_ALSA
bool reopen = mic_is_open(); // switch a running detector to the new device, after the store unlock
#endif
cJSON *o = cJSON_CreateObject();
cJSON_AddTrueToObject(o, "ok");
cJSON_AddItemToObject(o, "audio_run", audio_run());
store_unlock();
#ifdef LED_DESKTOP_ALSA
if (reopen) {
mic_follow_setting(true);
}
#endif
cJSON_Delete(body);
return send_json(req, "200 OK", o);
}
if (req->method != HTTP_POST) {
return httpd_resp_send_404(req);
}
if (!strcmp(act, "start") || !strcmp(act, "stop")) {
seqplay_set_audio(act[2] == 'a'); // st[a]rt
#ifdef LED_DESKTOP_ALSA
mic_follow_setting(act[2] == 'a');
#endif
ESP_LOGI(TAG, "mic beats %s sequences", act[2] == 'a' ? "time" : "no longer time");
return ok_with_status(req);
}
if (!strcmp(act, "reset") || !strcmp(act, "anchor-bar")) {
if (!seqplay_audio()) {
cJSON *o = cJSON_CreateObject();
cJSON_AddFalseToObject(o, "ok");
cJSON_AddStringToObject(o, "error", "Audio detector is not running");
return send_json(req, "409 Conflict", o);
}
if (act[0] == 'r') {
beat_reset();
} else {
beat_tap();
}
return ok_with_status(req);
}
return httpd_resp_send_404(req);
}
#ifdef LED_DESKTOP_ALSA
void audio_api_resume_mic(void)
{
if (seqplay_audio()) {
mic_follow_setting(true);
}
}
#endif
esp_err_t audio_api_register(httpd_handle_t server)
{
static const httpd_method_t METHODS[] = { HTTP_GET, HTTP_POST, HTTP_PUT };
for (size_t m = 0; m < 3; m++) {
httpd_uri_t u = { .uri = "/api/audio/*", .method = METHODS[m], .handler = audio_handler };
esp_err_t err = httpd_register_uri_handler(server, &u);
if (err != ESP_OK) {
return err;
}
}
return ESP_OK;
}