/* * Desktop microphone: captures from a Linux ALSA capture device (a USB mic such as the Blue Snowball) and feeds * the same level meter, beat detector and /audio.wav stream as the board's I2S mic.c. Only the devices named by * mic_list_devices() are selectable: plughw:CARD=... capture devices, which convert to 16 kHz mono for us. */ #include #include #include #include #include #include #include #include #include #include #include #include "cJSON.h" #include "esp_log.h" #include "beat.h" #include "bridge.h" #include "mic.h" #define BRIDGE_DEVICE "bridge:mic" // the microphone of a bridge board (bridge/, ESP32-S3), see bridge_serial.h #define BLOCK_FRAMES (MIC_SAMPLE_RATE / 50) // 20 ms per read #define LEVEL_BLOCKS 5 // 100 ms per level reading #define RING_SAMPLES MIC_SAMPLE_RATE // 1 s of 16-bit mono for the stream #define DBFS_FLOOR -120.0f #define SPL_OFFSET 120.0f // same nominal offset as the I2S mic; not calibrated for USB mics #define DEVICE_NAME_MAX 128 static const char *TAG = "mic_alsa"; static pthread_mutex_t s_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t s_cond = PTHREAD_COND_INITIALIZER; static bool s_open; // capture wanted (set by mic_open / mic_close) static uint64_t s_gen; // bumped by mic_open so the capture thread reopens the device static char s_device[DEVICE_NAME_MAX] = "default"; static mic_level_t s_level = { DBFS_FLOOR, DBFS_FLOOR, DBFS_FLOOR + SPL_OFFSET }; static char s_error[160]; // why the last open failed, "" when it works static bool s_streaming; static int16_t s_ring[RING_SAMPLES]; static size_t s_ring_fill; // samples waiting in the ring, oldest first from s_ring_head static size_t s_ring_head; static float to_dbfs(float x) { return x > 0.0f ? fmaxf(20.0f * log10f(x), DBFS_FLOOR) : DBFS_FLOOR; } // Called with s_lock held: a block of 16-bit samples for the stream listener, dropped when it is full static void ring_push_locked(const int16_t *pcm, size_t n) { if (!s_streaming || s_ring_fill + n > RING_SAMPLES) { return; } for (size_t i = 0; i < n; i++) { s_ring[(s_ring_head + s_ring_fill + i) % RING_SAMPLES] = pcm[i]; } s_ring_fill += n; pthread_cond_broadcast(&s_cond); } static void reset_level_locked(void) { s_level.rms_dbfs = DBFS_FLOOR; s_level.peak_dbfs = DBFS_FLOOR; s_level.spl_db = DBFS_FLOOR + SPL_OFFSET; } // Per-stream state of the level meter and high-pass filter, see process_block typedef struct { float hp_in, hp_out; double sum_sq; float peak; size_t frames; size_t blocks; } proc_t; // One block of 16-bit mono at MIC_SAMPLE_RATE: level meter, beat detector and the stream listener's copy static void process_block(proc_t *p, const int16_t *raw, size_t frames) { static float filtered[BLOCK_FRAMES]; static int16_t pcm_out[BLOCK_FRAMES]; const float gain = CONFIG_MIC_STREAM_GAIN; for (size_t i = 0; i < frames; i++) { float x = (float)raw[i] / 32768.0f; float y = x - p->hp_in + 0.995f * p->hp_out; // one-pole high-pass, as in mic.c p->hp_in = x; p->hp_out = y; filtered[i] = y; p->sum_sq += (double)y * y; p->peak = fmaxf(p->peak, fabsf(y)); pcm_out[i] = (int16_t)fmaxf(fminf(y * gain * 32767.0f, 32767.0f), -32768.0f); } p->frames += frames; beat_feed(filtered, frames); pthread_mutex_lock(&s_lock); ring_push_locked(pcm_out, frames); if (++p->blocks >= LEVEL_BLOCKS) { s_level.rms_dbfs = to_dbfs(sqrtf((float)(p->sum_sq / p->frames))); s_level.peak_dbfs = to_dbfs(p->peak); s_level.spl_db = s_level.rms_dbfs + SPL_OFFSET; p->sum_sq = 0.0; p->peak = 0.0f; p->frames = p->blocks = 0; } pthread_mutex_unlock(&s_lock); } // Read blocks from one open device until it fails or mic_open/mic_close changes the request static void capture_loop(snd_pcm_t *pcm, uint64_t gen) { static int16_t raw[BLOCK_FRAMES]; proc_t p = { 0 }; for (;;) { pthread_mutex_lock(&s_lock); bool keep = s_open && s_gen == gen; pthread_mutex_unlock(&s_lock); if (!keep) { return; } snd_pcm_sframes_t n = snd_pcm_readi(pcm, raw, BLOCK_FRAMES); if (n < 0) { if (snd_pcm_recover(pcm, (int)n, 1) < 0) { ESP_LOGW(TAG, "capture read failed: %s", snd_strerror((int)n)); return; } continue; } process_block(&p, raw, (size_t)n); } } // Audio from a bridge board: the bridge's link task calls this while the bridge device is selected static proc_t s_bridge_proc; static void on_bridge_audio(const int16_t *pcm, size_t n) { pthread_mutex_lock(&s_lock); bool mine = s_open && !strcmp(s_device, BRIDGE_DEVICE); pthread_mutex_unlock(&s_lock); if (mine) { process_block(&s_bridge_proc, pcm, n); } } static void *capture_thread(void *arg) { (void)arg; for (;;) { pthread_mutex_lock(&s_lock); bool open = s_open; uint64_t gen = s_gen; char name[DEVICE_NAME_MAX]; memcpy(name, s_device, sizeof(name)); pthread_mutex_unlock(&s_lock); if (!open || !strcmp(name, BRIDGE_DEVICE)) { usleep(100 * 1000); // idle: a bridge device is fed by on_bridge_audio continue; } // "pulse:" is a PipeWire/PulseAudio source, opened through the pulse ALSA plugin const char *pcm_name = name; if (!strncmp(name, "pulse:", 6)) { setenv("PULSE_SOURCE", name + 6, 1); pcm_name = "pulse"; } snd_pcm_t *pcm = NULL; int err = snd_pcm_open(&pcm, pcm_name, SND_PCM_STREAM_CAPTURE, 0); if (err < 0) { ESP_LOGW(TAG, "cannot open %s: %s", name, snd_strerror(err)); pthread_mutex_lock(&s_lock); snprintf(s_error, sizeof(s_error), "Cannot open %s: %s", name, snd_strerror(err)); pthread_mutex_unlock(&s_lock); sleep(1); continue; } err = snd_pcm_set_params(pcm, SND_PCM_FORMAT_S16_LE, SND_PCM_ACCESS_RW_INTERLEAVED, 1, MIC_SAMPLE_RATE, 1, 500000); if (err < 0) { ESP_LOGW(TAG, "cannot set %s to %d Hz mono: %s", name, MIC_SAMPLE_RATE, snd_strerror(err)); snd_pcm_close(pcm); sleep(1); continue; } ESP_LOGI(TAG, "capturing from %s", name); pthread_mutex_lock(&s_lock); s_error[0] = '\0'; pthread_mutex_unlock(&s_lock); capture_loop(pcm, gen); snd_pcm_drop(pcm); snd_pcm_close(pcm); pthread_mutex_lock(&s_lock); if (s_gen == gen) { reset_level_locked(); } pthread_mutex_unlock(&s_lock); } return NULL; } esp_err_t mic_start(void) { esp_err_t err = beat_start(MIC_SAMPLE_RATE); if (err != ESP_OK) { return err; } bridge_set_audio_cb(on_bridge_audio); pthread_t t; if (pthread_create(&t, NULL, capture_thread, NULL) != 0) { return ESP_ERR_NO_MEM; } pthread_detach(t); return ESP_OK; } void mic_get_level(mic_level_t *out) { pthread_mutex_lock(&s_lock); *out = s_level; pthread_mutex_unlock(&s_lock); } esp_err_t mic_open(const char *device) { if (!device || !device[0]) { device = "default"; } if (strlen(device) >= DEVICE_NAME_MAX) { return ESP_ERR_INVALID_ARG; } bool bridge = !strcmp(device, BRIDGE_DEVICE); if (bridge && !bridge_has_mic()) { pthread_mutex_lock(&s_lock); snprintf(s_error, sizeof(s_error), "No bridge board with a microphone is connected"); pthread_mutex_unlock(&s_lock); return ESP_ERR_NOT_FOUND; } if (bridge && bridge_mic(true) != ESP_OK) { pthread_mutex_lock(&s_lock); snprintf(s_error, sizeof(s_error), "The bridge microphone did not start"); pthread_mutex_unlock(&s_lock); return ESP_ERR_NOT_SUPPORTED; } pthread_mutex_lock(&s_lock); strcpy(s_device, device); s_open = true; s_gen++; s_ring_fill = 0; s_bridge_proc = (proc_t){ 0 }; s_error[0] = '\0'; reset_level_locked(); pthread_mutex_unlock(&s_lock); return ESP_OK; } void mic_close(void) { pthread_mutex_lock(&s_lock); s_open = false; s_gen++; reset_level_locked(); pthread_mutex_unlock(&s_lock); bridge_mic(false); // harmless when no bridge is attached } void mic_error(char *out, size_t len) { pthread_mutex_lock(&s_lock); snprintf(out, len, "%s", s_error); pthread_mutex_unlock(&s_lock); } bool mic_is_open(void) { pthread_mutex_lock(&s_lock); bool open = s_open; pthread_mutex_unlock(&s_lock); return open; } esp_err_t mic_stream_open(void) { pthread_mutex_lock(&s_lock); if (s_streaming) { pthread_mutex_unlock(&s_lock); return ESP_ERR_INVALID_STATE; } s_streaming = true; s_ring_fill = 0; pthread_mutex_unlock(&s_lock); return ESP_OK; } size_t mic_stream_read(void *buf, size_t len, TickType_t timeout) { struct timespec deadline; clock_gettime(CLOCK_REALTIME, &deadline); long ms = (long)(timeout * portTICK_PERIOD_MS); deadline.tv_sec += ms / 1000; deadline.tv_nsec += (ms % 1000) * 1000000L; if (deadline.tv_nsec >= 1000000000L) { deadline.tv_sec++; deadline.tv_nsec -= 1000000000L; } pthread_mutex_lock(&s_lock); while (s_ring_fill == 0) { if (pthread_cond_timedwait(&s_cond, &s_lock, &deadline) == ETIMEDOUT) { break; } } size_t want = len / sizeof(int16_t); size_t n = want < s_ring_fill ? want : s_ring_fill; uint8_t *out = buf; for (size_t i = 0; i < n; i++) { int16_t v = s_ring[(s_ring_head + i) % RING_SAMPLES]; out[2 * i] = (uint8_t)(v & 0xFF); out[2 * i + 1] = (uint8_t)((uint16_t)v >> 8); } s_ring_head = (s_ring_head + n) % RING_SAMPLES; s_ring_fill -= n; pthread_mutex_unlock(&s_lock); return n * sizeof(int16_t); } void mic_stream_close(void) { pthread_mutex_lock(&s_lock); s_streaming = false; pthread_mutex_unlock(&s_lock); } // Input sources the desktop can record from. On a PipeWire/PulseAudio desktop the USB mic is shared through the // sound server, so it is listed as a pulse source (pactl). Otherwise the raw ALSA plughw:CARD=... inputs are used. static int list_alsa_devices(mic_device_t *out, int max); static int list_pulse_sources(mic_device_t *out, int max) { FILE *p = popen("pactl -f json list sources 2>/dev/null", "r"); if (!p) { return 0; } char *buf = NULL; size_t cap = 0, len = 0; for (;;) { if (len + 4096 > cap) { cap = cap ? cap * 2 : 8192; buf = realloc(buf, cap); if (!buf) { pclose(p); return 0; } } size_t got = fread(buf + len, 1, cap - len - 1, p); if (got == 0) { break; } len += got; } pclose(p); buf[len] = '\0'; int count = 0; cJSON *list = cJSON_Parse(buf); free(buf); for (const cJSON *src = list ? list->child : NULL; src && count < max; src = src->next) { const cJSON *name = cJSON_GetObjectItem(src, "name"); const cJSON *desc = cJSON_GetObjectItem(src, "description"); if (!cJSON_IsString(name) || strncmp(name->valuestring, "alsa_input.", 11) != 0) { continue; // monitors and other sources } mic_device_t *d = &out[count]; if (strlen(name->valuestring) + 6 >= sizeof(d->id)) { continue; } snprintf(d->id, sizeof(d->id), "pulse:%s", name->valuestring); snprintf(d->name, sizeof(d->name), "%s", cJSON_IsString(desc) ? desc->valuestring : name->valuestring); snprintf(d->label, sizeof(d->label), "%s", name->valuestring); count++; } cJSON_Delete(list); return count; } int mic_list_devices(mic_device_t *out, int max) { int count = 0; if (bridge_has_mic() && max > 0) { mic_device_t *d = &out[count++]; snprintf(d->id, sizeof(d->id), "%s", BRIDGE_DEVICE); snprintf(d->name, sizeof(d->name), "I2S microphone on the bridge board"); snprintf(d->label, sizeof(d->label), "%s", BRIDGE_DEVICE); } int more = list_pulse_sources(out + count, max - count); if (more == 0) { more = list_alsa_devices(out + count, max - count); } return count + more; } // Raw ALSA capture devices: each plughw:CARD=... input (ALSA converts to our format) static int list_alsa_devices(mic_device_t *out, int max) { void **hints = NULL; int count = 0; if (snd_device_name_hint(-1, "pcm", &hints) < 0) { return 0; } for (void **h = hints; *h && count < max; h++) { char *name = snd_device_name_get_hint(*h, "NAME"); char *desc = snd_device_name_get_hint(*h, "DESC"); char *ioid = snd_device_name_get_hint(*h, "IOID"); bool input = !ioid || !strcmp(ioid, "Input"); if (name && input && !strncmp(name, "plughw:CARD=", 12) && strlen(name) < sizeof(out[count].id)) { mic_device_t *d = &out[count]; strcpy(d->id, name); strcpy(d->label, name); // The description runs over several lines; the first is the device's name const char *first = desc ? desc : name; size_t len = strcspn(first, "\n"); if (len >= sizeof(d->name)) { len = sizeof(d->name) - 1; } memcpy(d->name, first, len); d->name[len] = '\0'; count++; } free(name); free(desc); free(ioid); } snd_device_name_free_hint(hints); return count; }