/* * 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; }