Files
WaveShare_ESP32_BoomCast_FW/BoomCast_Waveshare_Arduino/boomcast_audio.h
T
2026-10-10 16:03:44 +02:00

56 lines
1.9 KiB
C++

#pragma once
#include <Arduino.h>
#include <atomic>
#include "boomcast_queue.h"
#include "boomcast_config.h"
#include "boomcast_detector.h"
class BoomAudio {
public:
bool begin(BoomQueue* queue);
void setSensitivity(uint8_t value);
void requestMonitoring(bool enabled) { requested_.store(enabled); }
bool active() const { return active_.load(); }
bool suspended() const { return suspended_.load(); }
bool fault() const { return fault_.load(); }
bool restartRequired() const { return restartRequired_.load(); }
private:
std::atomic<bool> requested_{true}, active_{false}, suspended_{false}, fault_{false}, restartRequired_{false};
bool openCodec();
void closeCodec();
void resetBuffers();
BoomQueue* queue_ = nullptr;
TaskHandle_t task_ = nullptr;
int16_t* ring_ = nullptr;
int16_t* capture_ = nullptr;
size_t ringWrite_ = 0;
size_t ringFilled_ = 0;
size_t captureWrite_ = 0;
size_t captureTarget_ = 0;
uint64_t captureStartedMs_ = 0;
uint64_t triggeredMs_ = 0;
uint32_t failedReads_ = 0;
uint32_t droppedSamples_ = 0;
uint32_t recordingErrors_ = 0;
bool capturing_ = false;
BoomDetector detector_{AUDIO_SAMPLE_RATE,
(AUDIO_SAMPLE_RATE + DETECTION_WINDOWS_PER_SECOND / 2) / DETECTION_WINDOWS_PER_SECOND,
AUDIO_SAMPLE_RATE * POST_EVENT_MS / 1000,
DETECTION_WARMUP_SAMPLES, DETECTION_COOLDOWN_SAMPLES,
DETECTION_INITIAL_BASELINE, DETECTION_BASELINE_ALPHA, DETECTION_BASELINE_CLAMP,
DETECTION_RMS_FLOOR, DETECTION_RMS_MULTIPLIER, DETECTION_PEAK_FLOOR};
volatile uint8_t sensitivity_ = 55;
static void taskThunk(void* arg);
void taskLoop();
void processMonoBlock(const int16_t* mono, size_t frames);
void pushRing(const int16_t* mono, size_t frames);
void beginCapture();
void appendCapture(int16_t sample);
void finishCapture();
void updateWaveform(const int16_t* mono, size_t frames);
};
extern BoomAudio boomAudio;