Added scheduling
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "boomcast_audio.h"
|
||||
#include "boomcast_audio_session.h"
|
||||
#include "boomcast_config.h"
|
||||
#include "boomcast_state.h"
|
||||
#include "audio_bsp.h"
|
||||
@@ -8,6 +9,9 @@
|
||||
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;
|
||||
@@ -18,25 +22,55 @@ bool BoomAudio::begin(BoomQueue* queue) {
|
||||
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));
|
||||
|
||||
// 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;
|
||||
// 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;
|
||||
}
|
||||
|
||||
xTaskCreatePinnedToCore(taskThunk, "boom_audio", 8192, this, 5, &task_, 0);
|
||||
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);
|
||||
}
|
||||
@@ -49,16 +83,37 @@ 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_) {
|
||||
@@ -76,6 +131,9 @@ void BoomAudio::taskLoop() {
|
||||
delay(10);
|
||||
continue;
|
||||
}
|
||||
fault_.store(false);
|
||||
if (!session.usableRead(AUDIO_FRAMES_PER_BLOCK)) continue;
|
||||
active_.store(true);
|
||||
const int16_t* stereo = reinterpret_cast<const int16_t*>(raw);
|
||||
for (size_t i = 0; i < AUDIO_FRAMES_PER_BLOCK; ++i) {
|
||||
mono[i] = stereo[i * 2 + AUDIO_MIC_SLOT];
|
||||
@@ -117,6 +175,7 @@ void BoomAudio::processMonoBlock(const int16_t* mono, size_t frames) {
|
||||
if (result == BoomDetector::Result::TailDone) {
|
||||
appendCapture(sample);
|
||||
finishCapture();
|
||||
if (!requested_.load()) return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user