#include "boomcast_audio.h" #include "boomcast_audio_session.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; extern esp_codec_dev_handle_t playback; #include "src/codec_board/codec_board.h" #include "src/codec_board/codec_init.h" } 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_) { fault_.store(true); restartRequired_.store(true); free(ring_); free(capture_); ring_ = capture_ = nullptr; 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)); // Use the same board setup without the example's abort-on-error wrapper. set_codec_board_type("S3_ePaper_1_54"); codec_init_cfg_t cfg{}; cfg.in_mode = cfg.out_mode = CODEC_I2S_MODE_STD; if (init_codec(&cfg) != 0) { fault_.store(true); restartRequired_.store(true); return false; } record = get_record_handle(); playback = get_playback_handle(); if (!record || !playback) { fault_.store(true); restartRequired_.store(true); return false; } if (xTaskCreatePinnedToCore(taskThunk, "boom_audio", 8192, this, 5, &task_, 0) != pdPASS) { fault_.store(true); restartRequired_.store(true); return false; } Serial.println("[Audio] detector task started"); return true; } void BoomAudio::resetBuffers() { ringFilled_ = ringWrite_ = captureWrite_ = 0; detector_.reset(); int8_t empty[BOOMCAST_WAVEFORM_POINTS]{}; boomState.setWaveform(empty, BOOMCAST_WAVEFORM_POINTS); } void BoomAudio::closeCodec() { // Both handles share I2S. Close both to stop RX and TX DMA; keep allocated // handles/buffers for reuse (deinit_codec also destroys the shared RTC I2C bus). esp_codec_dev_close(record); esp_codec_dev_close(playback); active_.store(false); resetBuffers(); } bool BoomAudio::openCodec() { esp_codec_dev_sample_info_t fs{}; fs.sample_rate = AUDIO_SAMPLE_RATE; fs.channel = 2; fs.bits_per_sample = 16; bool ok = esp_codec_dev_open(playback, &fs) == ESP_CODEC_DEV_OK && esp_codec_dev_open(record, &fs) == ESP_CODEC_DEV_OK && esp_codec_dev_set_in_gain(record, BOOMCAST_CODEC_INPUT_GAIN_DB) == ESP_CODEC_DEV_OK; if (!ok) closeCodec(); resetBuffers(); fault_.store(!ok); return ok; } 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) { fault_.store(true); restartRequired_.store(true); free(raw); free(mono); Serial.println("[Audio] block allocation failed"); vTaskDelete(nullptr); return; } BoomAudioSession session(AUDIO_SAMPLE_RATE); while (true) { auto action = session.next(requested_.load(), capturing_); if (action == BoomAudioSession::Action::Suspend) { if (session.isOpen()) { closeCodec(); session.closed(); } suspended_.store(true); delay(200); continue; } suspended_.store(false); if (action == BoomAudioSession::Action::Open) { if (!openCodec()) { delay(1000); continue; } session.opened(); // Drain a second of fresh DMA input before feeding baseline/pre-trigger // buffers. Detector reset then adds its usual 3-second trigger cooldown. } 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_; active_.store(false); fault_.store(true); if (session.failedRead()) { closeCodec(); session.closed(); } 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; detector_.reset(); 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; } fault_.store(false); if (!session.usableRead(AUDIO_FRAMES_PER_BLOCK)) continue; active_.store(true); 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); } void BoomAudio::processMonoBlock(const int16_t* mono, size_t frames) { updateWaveform(mono, frames); for (size_t i = 0; i < frames; ++i) { const int16_t sample = mono[i]; // The slider changes the effective input level; the worklet thresholds stay fixed. float normalised = (float(sample) / 32768.0f) * AUDIO_MIC_NORMALISED_GAIN * (float(sensitivity_) / AUDIO_SENSITIVITY_REFERENCE); normalised = constrain(normalised, -1.0f, 1.0f); if (!capturing_) pushRing(&sample, 1); const BoomDetector::Result result = detector_.process(normalised); if (result == BoomDetector::Result::Trigger) beginCapture(); else if (capturing_ && result != BoomDetector::Result::TailDone) appendCapture(sample); if (result == BoomDetector::Result::TailDone) { appendCapture(sample); finishCapture(); if (!requested_.load()) return; } } } 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() { 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); // 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]; } } void BoomAudio::appendCapture(int16_t sample) { if (captureWrite_ < captureTarget_) capture_[captureWrite_++] = sample; } 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_); } // Browser reset clears its rolling audio and starts a fresh 3 s cooldown. ringFilled_ = 0; ringWrite_ = 0; detector_.reset(); captureWrite_ = 0; capturing_ = false; boomState.setCapturing(false); }