#include "boomcast_audio.h" #include "boomcast_config.h" #include "boomcast_state.h" #include "audio_bsp.h" #include #include "esp_heap_caps.h" #include extern "C" { #include "src/esp_codec_dev/include/esp_codec_dev.h" extern esp_codec_dev_handle_t record; } BoomAudio boomAudio; bool BoomAudio::begin(BoomQueue* queue) { queue_ = queue; sensitivity_ = boomState.sensitivity(); ring_ = (int16_t*)heap_caps_malloc(PRE_EVENT_SAMPLES * sizeof(int16_t), MALLOC_CAP_SPIRAM); capture_ = (int16_t*)heap_caps_malloc(CAPTURE_SAMPLES * sizeof(int16_t), MALLOC_CAP_SPIRAM); if (!ring_ || !capture_) { Serial.println("[Audio] PSRAM allocation failed"); return false; } memset(ring_, 0, PRE_EVENT_SAMPLES * sizeof(int16_t)); memset(capture_, 0, CAPTURE_SAMPLES * sizeof(int16_t)); // Waveshare's official audio support opens the ES8311 at 16 kHz / 16-bit. audio_bsp_init(); audio_play_init(); if (!record) { Serial.println("[Audio] codec record handle unavailable"); return false; } startedMs_ = millis(); xTaskCreatePinnedToCore(taskThunk, "boom_audio", 8192, this, 5, &task_, 0); Serial.println("[Audio] detector task started"); return true; } void BoomAudio::setSensitivity(uint8_t value) { sensitivity_ = constrain(value, 1, 100); } void BoomAudio::taskThunk(void* arg) { static_cast(arg)->taskLoop(); } void BoomAudio::taskLoop() { uint8_t* raw = (uint8_t*)heap_caps_malloc(AUDIO_RAW_BLOCK_BYTES, MALLOC_CAP_SPIRAM); int16_t* mono = (int16_t*)heap_caps_malloc(AUDIO_FRAMES_PER_BLOCK * sizeof(int16_t), MALLOC_CAP_SPIRAM); if (!raw || !mono) { Serial.println("[Audio] block allocation failed"); vTaskDelete(nullptr); return; } while (true) { memset(raw, 0, AUDIO_RAW_BLOCK_BYTES); const int readResult = esp_codec_dev_read(record, raw, AUDIO_RAW_BLOCK_BYTES); if (readResult != ESP_CODEC_DEV_OK) { ++failedReads_; droppedSamples_ += AUDIO_FRAMES_PER_BLOCK; // A failed read invalidates timing continuity. Discard an active clip. if (capturing_) { ++recordingErrors_; capturing_ = false; captureWrite_ = 0; boomState.setCapturing(false); } ringFilled_ = 0; ringWrite_ = 0; boomState.setAudioDiagnostics(failedReads_, recordingErrors_); if (failedReads_ % 100 == 1) Serial.printf("[Audio] read errors=%lu dropped=%lu recording errors=%lu\n", (unsigned long)failedReads_, (unsigned long)droppedSamples_, (unsigned long)recordingErrors_); delay(10); continue; } const int16_t* stereo = reinterpret_cast(raw); for (size_t i = 0; i < AUDIO_FRAMES_PER_BLOCK; ++i) { mono[i] = stereo[i * 2 + AUDIO_MIC_SLOT]; } processMonoBlock(mono, AUDIO_FRAMES_PER_BLOCK); } } void BoomAudio::updateWaveform(const int16_t* mono, size_t frames) { int8_t points[BOOMCAST_WAVEFORM_POINTS] = {0}; for (size_t p = 0; p < BOOMCAST_WAVEFORM_POINTS; ++p) { size_t start = (p * frames) / BOOMCAST_WAVEFORM_POINTS; size_t end = ((p + 1) * frames) / BOOMCAST_WAVEFORM_POINTS; if (end <= start) end = min(start + 1, frames); int32_t peak = 0; int sign = 1; for (size_t i = start; i < end; ++i) { int32_t v = mono[i]; if (abs(v) > abs(peak)) { peak = v; sign = v < 0 ? -1 : 1; } } int scaled = constrain((abs(peak) * 100) / 32768, 0, 100); points[p] = (int8_t)(scaled * sign); } boomState.setWaveform(points, BOOMCAST_WAVEFORM_POINTS); } bool BoomAudio::shouldTrigger(float rms, int32_t peak) const { float t = sensitivity_ / 100.0f; // At ~55 this stays close to the Atom Echo prototype's 2.8x RMS / 8x peak // philosophy, while the slider gives useful room in both directions. float rmsMult = 4.2f - (2.4f * t); // 4.18 .. 1.80 float peakMult = 10.0f - (5.0f * t); // 9.95 .. 5.00 float rmsFloor = 1400.0f - (900.0f * t);// 1391 .. 500 float peakFloor = 9000.0f - (5000.0f * t);// 8950 .. 4000 float rmsThreshold = max(rmsFloor, noiseRms_ * rmsMult); float peakThreshold = max(peakFloor, noisePeak_ * peakMult); return rms >= rmsThreshold || peak >= peakThreshold; } void BoomAudio::processMonoBlock(const int16_t* mono, size_t frames) { updateWaveform(mono, frames); double squareSum = 0.0; int32_t peak = 0; for (size_t i = 0; i < frames; ++i) { int32_t v = mono[i]; int32_t av = abs(v); if (av > peak) peak = av; squareSum += double(v) * double(v); } float rms = sqrt(squareSum / (frames ? frames : 1)); if (capturing_) { appendCapture(mono, frames); return; } bool warm = (millis() - startedMs_) >= AUDIO_WARMUP_MS; bool cooledDown = (millis() - lastTriggerMs_) >= TRIGGER_COOLDOWN_MS; bool trigger = warm && cooledDown && shouldTrigger(rms, peak); if (trigger) { beginCapture(mono, frames); } else { // Learn normal ambient level slowly, but avoid teaching the detector that a // transient bang is the new normal. if (rms < noiseRms_ * 2.5f) noiseRms_ += (rms - noiseRms_) * NOISE_EMA_ALPHA; if (peak < noisePeak_ * 2.5f) noisePeak_ += (peak - noisePeak_) * NOISE_EMA_ALPHA; pushRing(mono, frames); } } void BoomAudio::pushRing(const int16_t* mono, size_t frames) { for (size_t i = 0; i < frames; ++i) { ring_[ringWrite_] = mono[i]; ringWrite_ = (ringWrite_ + 1) % PRE_EVENT_SAMPLES; if (ringFilled_ < PRE_EVENT_SAMPLES) ++ringFilled_; } } void BoomAudio::beginCapture(const int16_t* current, size_t frames) { captureWrite_ = 0; captureTarget_ = min(CAPTURE_SAMPLES, ringFilled_ + size_t(AUDIO_SAMPLE_RATE * POST_EVENT_MS / 1000)); struct timeval now; gettimeofday(&now, nullptr); triggeredMs_ = now.tv_sec > 1577836800 ? uint64_t(now.tv_sec) * 1000 + now.tv_usec / 1000 : 0; captureStartedMs_ = triggeredMs_ ? triggeredMs_ - (ringFilled_ * 1000 / AUDIO_SAMPLE_RATE) : 0; capturing_ = true; boomState.setCapturing(true); lastTriggerMs_ = millis(); // Copy the rolling pre-trigger buffer in chronological order. size_t count = ringFilled_; size_t start = (ringWrite_ + PRE_EVENT_SAMPLES - count) % PRE_EVENT_SAMPLES; for (size_t i = 0; i < count && captureWrite_ < captureTarget_; ++i) { capture_[captureWrite_++] = ring_[(start + i) % PRE_EVENT_SAMPLES]; } appendCapture(current, frames); } void BoomAudio::appendCapture(const int16_t* mono, size_t frames) { size_t room = captureTarget_ - captureWrite_; size_t take = min(room, frames); memcpy(capture_ + captureWrite_, mono, take * sizeof(int16_t)); captureWrite_ += take; if (captureWrite_ >= captureTarget_) finishCapture(); } void BoomAudio::finishCapture() { String filename; bool saved = queue_ && queue_->saveWav(capture_, captureWrite_, "audio_spike", &filename, captureStartedMs_, triggeredMs_); if (saved) { boomState.incrementTodayEvents(); Serial.printf("[Audio] captured %s\n", filename.c_str()); } else { ++recordingErrors_; boomState.setAudioDiagnostics(failedReads_, recordingErrors_); Serial.printf("[Audio] queue error; recording errors=%lu\n", (unsigned long)recordingErrors_); } // Re-seed the rolling buffer with the tail of the just-finished capture so // the next trigger still has sensible pre-roll context. ringFilled_ = 0; ringWrite_ = 0; size_t start = captureWrite_ > PRE_EVENT_SAMPLES ? captureWrite_ - PRE_EVENT_SAMPLES : 0; pushRing(capture_ + start, captureWrite_ - start); captureWrite_ = 0; capturing_ = false; boomState.setCapturing(false); }