Files
led-controller/main/beat.c
T
jimmyandClaude Opus 5.5 bfeeb958df feat: main's import/export, zones, audio API and web UI
Profiles, presets and sequences export and import as main's bundles.
Zones carry group_ids, preset_group_ids, sequence_ids, content_kind and
brightness; profiles seed rainbow as colour_cycle, and deleting a
profile also deletes its sequences. Groups, zones and sequences written
by older versions are migrated at boot.

/api/audio (status, event stream, start/stop, reset, anchor-bar,
devices) maps main's audio detector onto the mic's beat detector. Audio
start/stop picks whether mic beats or the BPM clock time sequences; the
clock's beats also go to the drivers while the mic is off.
/ws now greets from a post-handshake callback (httpd does not call the
handler for the upgrade).

The web UI is main's, plus v2's outputs editor, list search and
hostname/static IP settings; the bridge, Wi-Fi AP and led-tool parts
are gone. API tests follow main's behaviour (119 pass on the board).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-03 20:34:22 +13:00

228 lines
6.6 KiB
C

/*
* Beat detection glue: runs beat_dsp on the mic feed, pulses the beat LED
* (longer on the downbeat), keeps the latest tempo and bar position for
* /status and fans events out to SSE listeners.
*/
#include <math.h>
#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/semphr.h"
#include "esp_log.h"
#include "esp_check.h"
#include "esp_timer.h"
#include "driver/gpio.h"
#include "beat_dsp.h"
#include "beat.h"
#define MAX_SUBSCRIBERS 4
#define LOG_EVERY_BEATS 64
static const char *TAG = "beat";
static beat_dsp_t s_dsp; // ~10 KB, too big for the mic task's stack
static portMUX_TYPE s_lock = portMUX_INITIALIZER_UNLOCKED;
static beat_info_t s_info;
// Queue sends may yield, so the subscriber list gets a mutex, not s_lock
static SemaphoreHandle_t s_subs_mutex;
static QueueHandle_t s_subs[MAX_SUBSCRIBERS];
static esp_timer_handle_t s_led_timer;
static bool s_was_locked;
static int64_t s_tap_us; // pending tap time, 0 if none (guarded by s_lock)
static void led_set(bool on)
{
#if CONFIG_BEAT_LED_GPIO >= 0
#if CONFIG_BEAT_LED_ACTIVE_LOW
on = !on;
#endif
gpio_set_level(CONFIG_BEAT_LED_GPIO, on);
#endif
}
static void led_off_cb(void *arg)
{
led_set(false);
}
static beat_cb_t s_cb;
void beat_set_callback(beat_cb_t cb)
{
s_cb = cb;
}
static void publish(beat_event_t ev)
{
xSemaphoreTake(s_subs_mutex, portMAX_DELAY);
for (int i = 0; i < MAX_SUBSCRIBERS; i++) {
if (s_subs[i]) {
// A slow listener just misses events rather than stalling audio
xQueueSend(s_subs[i], &ev, 0);
}
}
xSemaphoreGive(s_subs_mutex);
}
static void dsp_event(void *ctx, int events, const beat_dsp_t *d)
{
int64_t now = esp_timer_get_time();
beat_info_t info;
portENTER_CRITICAL(&s_lock);
if (events & BEAT_EV_ONSET) {
s_info.onset_count++;
s_info.last_onset_us = now;
}
if (events & BEAT_EV_BEAT) {
s_info.beat_count++;
s_info.last_beat_us = now;
s_info.meter = d->meter;
s_info.bar_beat = d->bar_beat;
s_info.bar_sure = d->bar_sure;
}
if (events & BEAT_EV_DOWNBEAT) {
s_info.downbeat_count++;
}
info = s_info;
portEXIT_CRITICAL(&s_lock);
if (events & BEAT_EV_ONSET) {
publish(BEAT_EVENT_ONSET);
}
if (events & BEAT_EV_BEAT) {
if (s_cb) {
s_cb(&info, beat_dsp_bpm(d), (events & BEAT_EV_DOWNBEAT) != 0);
}
led_set(true);
esp_timer_stop(s_led_timer);
int pulse_ms = events & BEAT_EV_DOWNBEAT ? CONFIG_BEAT_LED_DOWNBEAT_MS : CONFIG_BEAT_LED_PULSE_MS;
esp_timer_start_once(s_led_timer, pulse_ms * 1000);
publish(BEAT_EVENT_BEAT);
if (info.beat_count % LOG_EVERY_BEATS == 0) {
ESP_LOGI(TAG, "%.1f BPM, confidence %.2f, %lu beats, %d/4 time, bar contrast %.2f",
beat_dsp_bpm(d), d->confidence, (unsigned long)info.beat_count, d->meter, d->bar_contrast);
}
}
}
static float s_rate;
static bool s_reset;
esp_err_t beat_start(float sample_rate)
{
float gate_rms = powf(10.0f, CONFIG_BEAT_GATE_DBFS / 20.0f);
s_rate = sample_rate;
beat_dsp_init(&s_dsp, sample_rate, gate_rms, CONFIG_BEAT_LEAD_MS / 1000.0f);
s_info.meter = s_dsp.meter;
s_subs_mutex = xSemaphoreCreateMutex();
ESP_RETURN_ON_FALSE(s_subs_mutex, ESP_ERR_NO_MEM, TAG, "Mutex alloc failed");
#if CONFIG_BEAT_LED_GPIO >= 0
gpio_config_t io = {
.pin_bit_mask = 1ULL << CONFIG_BEAT_LED_GPIO,
.mode = GPIO_MODE_OUTPUT,
};
ESP_RETURN_ON_ERROR(gpio_config(&io), TAG, "LED GPIO config failed");
#endif
led_set(false);
const esp_timer_create_args_t timer_args = { .callback = led_off_cb, .name = "beat_led" };
ESP_RETURN_ON_ERROR(esp_timer_create(&timer_args, &s_led_timer), TAG, "LED timer create failed");
ESP_LOGI(TAG, "Beat detection on, %.1f onset frames/s, gate %d dBFS",
s_dsp.frame_rate, CONFIG_BEAT_GATE_DBFS);
return ESP_OK;
}
void beat_reset(void)
{
portENTER_CRITICAL(&s_lock);
s_reset = true;
portEXIT_CRITICAL(&s_lock);
}
void beat_tap(void)
{
portENTER_CRITICAL(&s_lock);
s_tap_us = esp_timer_get_time();
portEXIT_CRITICAL(&s_lock);
}
void beat_feed(const float *x, size_t n)
{
portENTER_CRITICAL(&s_lock);
bool reset = s_reset;
s_reset = false;
portEXIT_CRITICAL(&s_lock);
if (reset) { // start tempo and bar tracking over, in the detector's task
beat_dsp_init(&s_dsp, s_rate, powf(10.0f, CONFIG_BEAT_GATE_DBFS / 20.0f), CONFIG_BEAT_LEAD_MS / 1000.0f);
portENTER_CRITICAL(&s_lock);
s_info.bar_beat = 0;
s_info.bar_sure = false;
s_info.meter = s_dsp.meter;
portEXIT_CRITICAL(&s_lock);
ESP_LOGI(TAG, "Tracking reset");
}
beat_dsp_feed(&s_dsp, x, n, dsp_event, NULL);
// Taps arrive from the HTTP task; apply them here, in the detector's task
portENTER_CRITICAL(&s_lock);
int64_t tap_us = s_tap_us;
s_tap_us = 0;
portEXIT_CRITICAL(&s_lock);
if (tap_us) {
// The newest analysed frame is ~30 ms behind real time (capture block
// plus half the FFT window), so the tap is that much further back
float ago_s = (esp_timer_get_time() - tap_us) / 1e6f + 0.03f;
beat_dsp_tap(&s_dsp, ago_s * s_dsp.frame_rate);
ESP_LOGI(TAG, "Tap: beat 1 set, holding the bar for a while");
}
portENTER_CRITICAL(&s_lock);
s_info.bpm = beat_dsp_bpm(&s_dsp);
s_info.confidence = s_dsp.confidence;
s_info.locked = s_dsp.locked;
portEXIT_CRITICAL(&s_lock);
if (s_dsp.locked != s_was_locked) {
s_was_locked = s_dsp.locked;
if (s_was_locked) {
ESP_LOGI(TAG, "Locked at %.1f BPM (confidence %.2f)", beat_dsp_bpm(&s_dsp), s_dsp.confidence);
} else {
ESP_LOGI(TAG, "Lost the beat");
}
}
}
void beat_get_info(beat_info_t *out)
{
portENTER_CRITICAL(&s_lock);
*out = s_info;
portEXIT_CRITICAL(&s_lock);
}
esp_err_t beat_subscribe(QueueHandle_t q)
{
esp_err_t ret = ESP_ERR_NO_MEM;
xSemaphoreTake(s_subs_mutex, portMAX_DELAY);
for (int i = 0; i < MAX_SUBSCRIBERS; i++) {
if (!s_subs[i]) {
s_subs[i] = q;
ret = ESP_OK;
break;
}
}
xSemaphoreGive(s_subs_mutex);
return ret;
}
void beat_unsubscribe(QueueHandle_t q)
{
// Once this returns the queue is no longer touched and can be deleted
xSemaphoreTake(s_subs_mutex, portMAX_DELAY);
for (int i = 0; i < MAX_SUBSCRIBERS; i++) {
if (s_subs[i] == q) {
s_subs[i] = NULL;
}
}
xSemaphoreGive(s_subs_mutex);
}