feat(v2): audio page, beat endpoints and an Audio menu button

Restores the live audio section from esp32-eth: /audio page, /audio.wav
stream, /beats event feed, POST /tap and /time, and the mic and beat state
in /status. The web build adds an Audio entry to the header menu and the
mobile dropdown for the v2 copy of index.html only, so the Pi UI is unchanged.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
jimmyandClaude Sonnet 5.5 committed 2026-10-27 22:30:26 +13:00
1 parent d332d447aa
commit f7ac4ab290
7 files changed
+280 -9

No files matched your search

+1 -1
View File
@@ -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
+227
View File
@@ -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 <stdio.h>
#include <string.h>
#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;
}
+7
View File
@@ -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);
+23 -7
View File
@@ -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);
}
+6
View File
@@ -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 },
};
+15 -1
View File
@@ -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|<button class=\"btn btn-secondary\" id=\"help-btn\">Help</button>|<button class=\"btn btn-secondary\" id=\"audio-btn\" onclick=\"location.href='/audio'\">Audio</button>\n&|" \
-e "s|<button type=\"button\" data-target=\"help-btn\">Help</button>|<button type=\"button\" data-target=\"audio-btn\">Audio</button>\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 {} \;
+1
View File
@@ -0,0 +1 @@
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Audio and beats</title><style>body{font-family:sans-serif;max-width:32em;margin:2em auto;padding:0 1em}td{padding:.2em 1em .2em 0}#bar{height:1.5em;background:#ddd;border-radius:4px;overflow:hidden}#fill{height:100%;width:0;background:#2a9d4b;transition:width .2s}.dot{display:inline-block;width:3em;height:3em;border-radius:50%;background:#ddd;vertical-align:middle}#onset{width:1.2em;height:1.2em;margin-left:1em}.bb{display:inline-block;width:1.6em;height:1.6em;line-height:1.6em;text-align:center;border-radius:4px;background:#eee;margin-right:.3em;font-size:.9em}.bb.on{background:#457b9d;color:#fff}.bb.one.on{background:#e63946}#barpos.guess .bb{opacity:.45}#tl{width:100%;height:90px;background:#f4f4f4;border-radius:4px}kbd{border:1px solid #bbb;border-radius:3px;padding:0 .3em}@keyframes down{from{background:#e63946;box-shadow:0 0 0 .5em #e6394655}to{background:#ddd}}#beat.down{animation:down .5s}@keyframes beat{from{background:#e63946}to{background:#ddd}}@keyframes onset{from{background:#457b9d}to{background:#ddd}}#beat.on{animation:beat .3s}#onset.on{animation:onset .15s}</style></head><body><p><a href="/">&larr; Controller</a></p><h1>Audio and beats</h1><table><tr><td>IP</td><td id="ip"></td></tr><tr><td>MAC</td><td id="mac"></td></tr><tr><td>Uptime</td><td id="up"></td></tr><tr><td>Free heap</td><td id="heap"></td></tr><tr><td>Mic RMS</td><td id="rms"></td></tr><tr><td>Mic peak</td><td id="peak"></td></tr><tr><td>Approx. SPL</td><td id="spl"></td></tr><tr><td>Tempo</td><td id="bpm">&ndash;</td></tr><tr><td>Confidence</td><td id="conf"></td></tr><tr><td>Time</td><td id="meter">&ndash;</td></tr></table><div id="bar"><div id="fill"></div></div><p><span class="dot" id="beat"></span><span class="dot" id="onset"></span></p><p id="barpos"></p><p><button id="tap" title="Press on the first beat of a bar to correct the bar position (or press T)">Tap beat 1</button></p><h2>You vs the computer</h2><p>Press <kbd>Space</kbd> on every beat.</p><canvas id="tl"></canvas><table><tr><td>Your tempo</td><td id="ytempo">&ndash;</td></tr><tr><td>Your timing</td><td id="yoff">&ndash;</td></tr><tr><td>Agreement</td><td id="ymatch">&ndash;</td></tr></table><p><button id="listen">Listen</button> <span id="lat"></span></p><p><a href="/status">/status</a> (JSON) &middot; <a href="/audio.wav">/audio.wav</a> (live) &middot; <a href="/beats">/beats</a> (event stream)</p><script>const $=id=>document.getElementById(id);async function tick(){try{const s=await(await fetch('/status')).json();$('ip').textContent=s.ip;$('mac').textContent=s.mac;$('up').textContent=s.uptime_s+' s';$('heap').textContent=s.free_heap+' bytes';$('rms').textContent=s.mic_rms_dbfs.toFixed(1)+' dBFS';$('peak').textContent=s.mic_peak_dbfs.toFixed(1)+' dBFS';$('spl').textContent=s.mic_spl_db.toFixed(0)+' dB';$('fill').style.width=Math.max(0,Math.min(100,(s.mic_rms_dbfs+90)/90*100))+'%';}catch(e){}setTimeout(tick,250)}tick();function flash(id,c){const d=$(id);d.classList.remove('on','down');void d.offsetWidth;d.classList.add(c||'on');}function bar(m,n){const b=$('barpos');if(b.children.length!=m){b.innerHTML='';for(let i=1;i<=m;i++){const s=document.createElement('span');s.className='bb'+(i==1?' one':'');s.textContent=i;b.appendChild(s);}}[...b.children].forEach((s,i)=>s.classList.toggle('on',i+1==n));}function tap(){fetch('/tap',{method:'POST'});}$('tap').addEventListener('pointerdown',e=>{e.preventDefault();tap();});document.addEventListener('keydown',e=>{if((e.key=='t'||e.key=='T')&&!e.repeat)tap();});let off=null,rtt=1e9,cbpm=0;const comp=[],taps=[];async function sync(){rtt*=1.5;for(let i=0;i<4;i++){try{const a=performance.now(),r=await(await fetch('/time',{cache:'no-store'})).json(),b=performance.now();if(b-a<=rtt){rtt=b-a;off=(a+b)/2-r.us/1000;}}catch(e){}}}sync();setInterval(sync,15000);document.addEventListener('keydown',e=>{if(e.code=='Space'){e.preventDefault();if(!e.repeat)taps.push(performance.now());}});document.addEventListener('keyup',e=>{if(e.code=='Space')e.preventDefault();});function median(a){if(!a.length)return null;const s=[...a].sort((x,y)=>x-y);return s[s.length>>1];}function stats(now){const lim=now-20000;while(comp.length&&comp[0].t<lim)comp.shift();while(taps.length&&taps[0]<lim)taps.shift();const iv=[];for(let i=1;i<taps.length;i++){const d=taps[i]-taps[i-1];if(d>=300&&d<=1000)iv.push(d);}const ti=median(iv.slice(-8));$('ytempo').textContent=ti?(60000/ti).toFixed(1)+' BPM (computer '+(cbpm?cbpm.toFixed(1):'-')+')':'-';const pairs=[];for(const t of taps){if(now-t<400)continue;let b=null;for(const c of comp)if(b===null||Math.abs(c.t-t)<Math.abs(b-t))b=c.t;if(b!==null)pairs.push([t,b]);}const rec=pairs.slice(-16),hit=rec.filter(p=>Math.abs(p[0]-p[1])<=70).map(p=>p[0]-p[1]);if(hit.length){const m=hit.reduce((a,b)=>a+b,0)/hit.length,sd=Math.sqrt(hit.reduce((a,b)=>a+(b-m)*(b-m),0)/hit.length);$('yoff').textContent=(m>=0?'+':Line truncated