diff --git a/v2/main/CMakeLists.txt b/v2/main/CMakeLists.txt index 5fe77bc..e808e11 100644 --- a/v2/main/CMakeLists.txt +++ b/v2/main/CMakeLists.txt @@ -1,4 +1,4 @@ -idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" +idf_component_register(SRCS "main.c" "mic.c" "beat.c" "beat_dsp.c" "espnow_tx.c" "store.c" "api.c" "web.c" "envelope.c" "engine.c" "rel.c" "bench.c" "audio.c" INCLUDE_DIRS "." REQUIRES esp_eth esp_netif esp_event esp_driver_spi esp_driver_gpio nvs_flash esp_http_server esp_timer esp_driver_i2s esp_wifi esp_rom json esp_driver_gpio diff --git a/v2/main/audio.c b/v2/main/audio.c new file mode 100644 index 0000000..7849602 --- /dev/null +++ b/v2/main/audio.c @@ -0,0 +1,227 @@ +/* + * Live audio and beat endpoints: /audio.wav (16 kHz mono PCM stream), /beats (server-sent events from the + * beat detector), POST /tap (correct the bar position), /time (board clock). Ported from esp32-eth. + */ +#include "audio.h" + +#include +#include +#include "beat.h" +#include "esp_log.h" +#include "esp_timer.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "lwip/sockets.h" +#include "mic.h" + +static const char *TAG = "audio"; + +static void wav_header(uint8_t h[44]) +{ + const uint32_t rate = MIC_SAMPLE_RATE, byte_rate = MIC_SAMPLE_RATE * 2, unknown = 0xFFFFFFFF; + const uint16_t pcm = 1, channels = 1, block_align = 2, bits = 16; + const uint32_t fmt_len = 16; + memcpy(h, "RIFF", 4); memcpy(h + 4, &unknown, 4); + memcpy(h + 8, "WAVEfmt ", 8); memcpy(h + 16, &fmt_len, 4); + memcpy(h + 20, &pcm, 2); memcpy(h + 22, &channels, 2); + memcpy(h + 24, &rate, 4); memcpy(h + 28, &byte_rate, 4); + memcpy(h + 32, &block_align, 2); memcpy(h + 34, &bits, 2); + memcpy(h + 36, "data", 4); memcpy(h + 40, &unknown, 4); +} + +// httpd_send may write only part of the buffer; keep going until done +static bool send_all(httpd_req_t *req, const void *data, size_t len) +{ + const char *p = data; + while (len > 0) { + int sent = httpd_send(req, p, len); + if (sent <= 0) { + return false; + } + p += sent; + len -= sent; + } + return true; +} + +static void audio_stream_task(void *arg) +{ + httpd_req_t *req = arg; + httpd_handle_t server = req->handle; + int fd = httpd_req_to_sockfd(req); + static uint8_t buf[1024]; + + // Send each 20 ms block immediately instead of letting Nagle batch them + int one = 1; + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); + + // Raw (non-chunked) response: chunked encoding splits every block into + // three tiny TCP segments, which fills lwIP's send queue and stalls on + // the client's delayed ACKs. One segment per block avoids that. + static const char http_hdr[] = + "HTTP/1.1 200 OK\r\n" + "Content-Type: audio/wav\r\n" + "Cache-Control: no-store\r\n" + "Connection: close\r\n\r\n"; + uint8_t head[sizeof(http_hdr) - 1 + 44]; + memcpy(head, http_hdr, sizeof(http_hdr) - 1); + wav_header(head + sizeof(http_hdr) - 1); + + bool ok = send_all(req, head, sizeof(head)); + ESP_LOGI(TAG, "Audio listener connected"); + + while (ok) { + size_t n = mic_stream_read(buf, sizeof(buf), pdMS_TO_TICKS(1000)); + if (n > 0) { + ok = send_all(req, buf, n); + } + } + + mic_stream_close(); + ESP_LOGI(TAG, "Audio listener disconnected"); + httpd_req_async_handler_complete(req); + httpd_sess_trigger_close(server, fd); + vTaskDelete(NULL); +} + +static esp_err_t audio_get_handler(httpd_req_t *req) +{ + if (mic_stream_open() != ESP_OK) { + httpd_resp_set_status(req, "409 Conflict"); + return httpd_resp_sendstr(req, "Another listener is already connected\n"); + } + + // Stream from our own task so the server keeps answering /status + httpd_req_t *async_req = NULL; + if (httpd_req_async_handler_begin(req, &async_req) != ESP_OK) { + mic_stream_close(); + return ESP_FAIL; + } + if (xTaskCreate(audio_stream_task, "audio_http", 4096, async_req, 5, NULL) != pdPASS) { + mic_stream_close(); + httpd_req_async_handler_complete(async_req); + return ESP_FAIL; + } + return ESP_OK; +} + +// Board clock for the page to line its own clock up with (esp_timer, us) +static esp_err_t time_get_handler(httpd_req_t *req) +{ + char buf[48]; + snprintf(buf, sizeof(buf), "{\"us\":%lld}", (long long)esp_timer_get_time()); + httpd_resp_set_type(req, "application/json"); + httpd_resp_set_hdr(req, "Cache-Control", "no-store"); + return httpd_resp_sendstr(req, buf); +} + +static esp_err_t tap_post_handler(httpd_req_t *req) +{ + beat_tap(); + httpd_resp_set_type(req, "application/json"); + return httpd_resp_sendstr(req, "{\"ok\":true}"); +} + +#define BEAT_QUEUE_LEN 16 + +static void beats_stream_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(BEAT_QUEUE_LEN, sizeof(beat_event_t)); + char msg[160]; + + int one = 1; + setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); + + static const char http_hdr[] = + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/event-stream\r\n" + "Cache-Control: no-store\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 beat listeners\n"; + send_all(req, busy, sizeof(busy) - 1); + } else { + ok = send_all(req, http_hdr, sizeof(http_hdr) - 1); + ESP_LOGI(TAG, "Beat listener connected"); + } + + // Tempo goes out once a second even without beats, which also notices + // when the client has gone away + int64_t next_tempo = 0; + while (ok) { + int64_t now = esp_timer_get_time(); + if (now >= next_tempo) { + beat_info_t b; + beat_get_info(&b); + int n = snprintf(msg, sizeof(msg), + "event: tempo\ndata: {\"bpm\":%.1f,\"confidence\":%.2f,\"locked\":%s,\"beats\":%lu}\n\n", + b.bpm, b.confidence, b.locked ? "true" : "false", (unsigned long)b.beat_count); + ok = send_all(req, msg, n); + next_tempo = now + 1000000; + continue; + } + beat_event_t ev; + TickType_t wait = pdMS_TO_TICKS((next_tempo - now) / 1000) + 1; + if (xQueueReceive(q, &ev, wait) != pdTRUE) { + continue; + } + int n; + if (ev == BEAT_EVENT_BEAT) { + beat_info_t b; + beat_get_info(&b); + n = snprintf(msg, sizeof(msg), + "event: beat\ndata: {\"bpm\":%.1f,\"n\":%lu,\"bar\":%d,\"meter\":%d,\"sure\":%s,\"us\":%lld}\n\n", + b.bpm, (unsigned long)b.beat_count, b.bar_beat, b.meter, b.bar_sure ? "true" : "false", + (long long)b.last_beat_us); + } else { + n = snprintf(msg, sizeof(msg), "event: onset\ndata: {}\n\n"); + } + ok = send_all(req, msg, n); + } + + if (q) { + beat_unsubscribe(q); + vQueueDelete(q); + } + ESP_LOGI(TAG, "Beat listener disconnected"); + httpd_req_async_handler_complete(req); + httpd_sess_trigger_close(server, fd); + vTaskDelete(NULL); +} + +static esp_err_t beats_get_handler(httpd_req_t *req) +{ + // Long-lived like /audio.wav, so it gets its own task too + httpd_req_t *async_req = NULL; + if (httpd_req_async_handler_begin(req, &async_req) != ESP_OK) { + return ESP_FAIL; + } + if (xTaskCreate(beats_stream_task, "beats_http", 4096, async_req, 5, NULL) != pdPASS) { + httpd_req_async_handler_complete(async_req); + return ESP_FAIL; + } + return ESP_OK; +} + +esp_err_t audio_register(httpd_handle_t server) +{ + const httpd_uri_t uris[] = { + { .uri = "/audio.wav", .method = HTTP_GET, .handler = audio_get_handler }, + { .uri = "/beats", .method = HTTP_GET, .handler = beats_get_handler }, + { .uri = "/tap", .method = HTTP_POST, .handler = tap_post_handler }, + { .uri = "/time", .method = HTTP_GET, .handler = time_get_handler }, + }; + for (size_t i = 0; i < sizeof(uris) / sizeof(uris[0]); i++) { + esp_err_t err = httpd_register_uri_handler(server, &uris[i]); + if (err != ESP_OK) { + ESP_LOGE(TAG, "register %s: %s", uris[i].uri, esp_err_to_name(err)); + return err; + } + } + return ESP_OK; +} diff --git a/v2/main/audio.h b/v2/main/audio.h new file mode 100644 index 0000000..f7216e2 --- /dev/null +++ b/v2/main/audio.h @@ -0,0 +1,7 @@ +#pragma once + +#include "esp_err.h" +#include "esp_http_server.h" + +// Register /audio.wav, /beats, /tap and /time (the page at /audio uses them) +esp_err_t audio_register(httpd_handle_t server); diff --git a/v2/main/main.c b/v2/main/main.c index 5a0b4a1..e8bdd38 100644 --- a/v2/main/main.c +++ b/v2/main/main.c @@ -25,6 +25,7 @@ #include "engine.h" #include "rel.h" #include "web.h" +#include "audio.h" static const char *TAG = "eth"; @@ -34,25 +35,39 @@ static esp_netif_t *s_eth_netif = NULL; static esp_err_t status_get_handler(httpd_req_t *req) { beat_info_t bi; + mic_level_t level; espnow_stats_t es; rel_stats_t rs; + esp_netif_ip_info_t ip_info = { 0 }; + uint8_t mac[6] = { 0 }; + char ip[16], macs[18], buf[1024]; beat_get_info(&bi); + mic_get_level(&level); espnow_tx_stats(&es); rel_stats(&rs); - char buf[512]; - int n = snprintf(buf, sizeof(buf), - "{\"uptime_s\":%lld,\"free_heap\":%lu,\"bpm\":%.1f,\"beats\":%lu," + esp_netif_get_ip_info(s_eth_netif, &ip_info); + esp_netif_get_mac(s_eth_netif, mac); + snprintf(ip, sizeof(ip), IPSTR, IP2STR(&ip_info.ip)); + snprintf(macs, sizeof(macs), MACSTR, MAC2STR(mac)); + snprintf(buf, sizeof(buf), + "{\"ip\":\"%s\",\"mac\":\"%s\",\"uptime_s\":%lld,\"free_heap\":%lu," + "\"mic_rms_dbfs\":%.1f,\"mic_peak_dbfs\":%.1f,\"mic_spl_db\":%.1f," + "\"bpm\":%.1f,\"beat_confidence\":%.2f,\"beat_locked\":%s,\"beats\":%lu,\"onsets\":%lu," + "\"meter\":%d,\"bar_beat\":%d,\"bar_sure\":%s,\"downbeats\":%lu," "\"espnow\":{\"beats_sent\":%lu,\"beat_send_fail\":%lu,\"bulk_sent\":%lu," "\"bulk_retries\":%lu,\"bulk_failed\":%lu,\"sim_dropped\":%lu}," "\"link\":{\"ver\":%u,\"acked\":%lu,\"ack_timeouts\":%lu,\"retransmits\":%lu,\"resyncs\":%lu,\"digests\":%lu}}", - (long long)(esp_timer_get_time() / 1000000), (unsigned long)esp_get_free_heap_size(), - bi.bpm, (unsigned long)bi.beat_count, (unsigned long)es.beats_sent, - (unsigned long)es.beat_send_fail, (unsigned long)es.bulk_sent, + ip, macs, (long long)(esp_timer_get_time() / 1000000), (unsigned long)esp_get_free_heap_size(), + level.rms_dbfs, level.peak_dbfs, level.spl_db, + bi.bpm, bi.confidence, bi.locked ? "true" : "false", (unsigned long)bi.beat_count, + (unsigned long)bi.onset_count, bi.meter, bi.bar_beat, bi.bar_sure ? "true" : "false", + (unsigned long)bi.downbeat_count, + (unsigned long)es.beats_sent, (unsigned long)es.beat_send_fail, (unsigned long)es.bulk_sent, (unsigned long)es.bulk_retries, (unsigned long)es.bulk_failed, (unsigned long)es.sim_dropped, rel_ver(), (unsigned long)rs.acked, (unsigned long)rs.ack_timeouts, (unsigned long)rs.retransmits, (unsigned long)rs.resyncs, (unsigned long)rs.digests); httpd_resp_set_type(req, "application/json"); - return httpd_resp_send(req, buf, n); + return httpd_resp_sendstr(req, buf); } static void start_webserver(void) @@ -72,6 +87,7 @@ static void start_webserver(void) const httpd_uri_t status_uri = { .uri = "/status", .method = HTTP_GET, .handler = status_get_handler }; httpd_register_uri_handler(s_server, &status_uri); web_register(s_server); + audio_register(s_server); api_register(s_server); ESP_LOGI(TAG, "HTTP server started on port %d", config.server_port); } diff --git a/v2/main/web.c b/v2/main/web.c index 8dbd24b..64a6968 100644 --- a/v2/main/web.c +++ b/v2/main/web.c @@ -77,6 +77,11 @@ static esp_err_t index_handler(httpd_req_t *req) return send_gz(req, "index.html", false); } +static esp_err_t audio_page_handler(httpd_req_t *req) +{ + return send_gz(req, "audio.html", false); +} + static esp_err_t static_handler(httpd_req_t *req) { char path[96]; @@ -100,6 +105,7 @@ esp_err_t web_register(httpd_handle_t server) { const httpd_uri_t uris[] = { { .uri = "/", .method = HTTP_GET, .handler = index_handler }, + { .uri = "/audio", .method = HTTP_GET, .handler = audio_page_handler }, { .uri = "/static/*", .method = HTTP_GET, .handler = static_handler }, { .uri = "/favicon.ico", .method = HTTP_GET, .handler = favicon_handler }, }; diff --git a/v2/tools/build_web.sh b/v2/tools/build_web.sh index 5f9b186..168459b 100755 --- a/v2/tools/build_web.sh +++ b/v2/tools/build_web.sh @@ -21,10 +21,24 @@ for f in "$SRC"/static/*; do *) cp "$f" "$OUT/static/$b" ;; esac done +# The Pi does not serve /audio, so the Audio menu entries are added for this build only +# (desktop button row and the mobile dropdown, both just before Help). +tmp=$(mktemp -d) +sed -e "s||\n&|" \ + -e "s||\n&|" \ + "$SRC/templates/index.html" > "$tmp/index.html" +cp "$SRC/templates/settings.html" "$tmp/settings.html" +grep -q 'id="audio-btn"' "$tmp/index.html" && [ "$(grep -c 'audio-btn' "$tmp/index.html")" -eq 2 ] \ + || { echo "ERROR: could not add the Audio menu entries to index.html" >&2; exit 1; } for p in index settings; do npx --yes html-minifier-terser@7.2.0 --collapse-whitespace --remove-comments \ - --minify-css true --minify-js true -o "$OUT/$p.html" "$SRC/templates/$p.html" + --minify-css true --minify-js true -o "$OUT/$p.html" "$tmp/$p.html" done +rm -rf "$tmp" + +# Live audio and beat page (served at /audio) +npx --yes html-minifier-terser@7.2.0 --collapse-whitespace --remove-comments \ + --minify-css true --minify-js true -o "$OUT/audio.html" web/audio.html # Ship only the .gz copies; the server sends Content-Encoding: gzip find "$OUT" -type f -exec gzip -9 -n -f {} \; diff --git a/v2/web/audio.html b/v2/web/audio.html new file mode 100644 index 0000000..9aa8884 --- /dev/null +++ b/v2/web/audio.html @@ -0,0 +1 @@ +Audio and beats

← Controller

Audio and beats

IP
MAC
Uptime
Free heap
Mic RMS
Mic peak
Approx. SPL
Tempo–
Confidence
Time–

You vs the computer

Press Space on every beat.

Your tempo–
Your timing–
Agreement–

/status (JSON) · /audio.wav (live) · /beats (event stream)

\ No newline at end of file