// 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 #include #include #include #include #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); } 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); cJSON_AddStringToObject(o, "device", "i2s"); cJSON_AddNullToObject(o, "error"); 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); } 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"); cJSON *o = cJSON_CreateObject(); cJSON_AddTrueToObject(o, "ok"); cJSON_AddItemToObject(o, "audio_run", audio_run()); store_unlock(); 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 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); } 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; }