85 lines
3.4 KiB
C++
85 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
#include <freertos/task.h>
|
|
|
|
namespace borla {
|
|
|
|
// Debug-only boot-to-shutdown audio recorder.
|
|
//
|
|
// The device has nowhere to store an hour of PCM (no SD card, and flash is
|
|
// both too small and too slow to write from the audio path), so the tap keeps
|
|
// two ring buffers in PSRAM — everything the microphone captured and
|
|
// everything that reached the DAC, both 24 kHz mono s16 at the hardware
|
|
// boundary — and streams them over a plain TCP socket to
|
|
// tools/audio_tap_dump.py, which writes the pair of WAV files. ~32 s of
|
|
// pre-buffer per channel covers the gap between the boot prompt and the host
|
|
// connecting, so a capture started before power-on is gapless from boot; the
|
|
// files end when the device powers off and the socket dies.
|
|
//
|
|
// The push side is called from the capture and playback tasks and must never
|
|
// block or lock: each ring is single-producer/single-consumer with monotonic
|
|
// indices, and a full ring drops the NEWEST audio (keeping the oldest, which
|
|
// is what preserves the boot prompt while no host is attached).
|
|
//
|
|
// This is an open microphone on an unauthenticated LAN port. It exists behind
|
|
// CONFIG_BORLA_AUDIO_TAP for debugging sessions only and must never ship
|
|
// enabled.
|
|
class AudioTap final {
|
|
public:
|
|
bool Initialize();
|
|
// Capture task: raw ADC output, before any DSP.
|
|
void PushInput(const int16_t* samples, size_t sample_count);
|
|
// Playback task: volume-scaled PCM, exactly what the DAC received.
|
|
void PushOutput(const int16_t* samples, size_t sample_count);
|
|
// Capture task: the server's-ear channel — post-AEC, post-gate 16 kHz
|
|
// PCM exactly as it goes to the Opus encoder. nullptr pushes silence so
|
|
// suppressed/idle frames keep the timeline aligned with the other two
|
|
// channels.
|
|
void PushUplink(const int16_t* samples, size_t sample_count);
|
|
|
|
private:
|
|
struct Ring {
|
|
uint8_t* data = nullptr;
|
|
size_t capacity = 0;
|
|
// Monotonic byte counters; unsigned wraparound keeps head - tail
|
|
// correct. Producer owns head, the tap task owns tail.
|
|
std::atomic<size_t> head{0};
|
|
std::atomic<size_t> tail{0};
|
|
std::atomic<uint32_t> dropped_bytes{0};
|
|
};
|
|
|
|
static constexpr uint16_t kPort = 3401;
|
|
// 1.5 MB per channel = ~32 s at 24 kHz mono s16 (48 KB/s).
|
|
static constexpr size_t kRingBytes = 1536 * 1024;
|
|
// One 60 ms frame per TCP record keeps the on-stack scratch small.
|
|
static constexpr size_t kSendChunkBytes = 2880;
|
|
static constexpr uint32_t kIdlePollMs = 20;
|
|
//2026-9-23 ckkj debug for wifi mem fail
|
|
static constexpr size_t kTaskStackBytes = 6 * 1024;
|
|
static void TaskEntry(void* context);
|
|
[[noreturn]] void Run();
|
|
void ServeClient(int client_fd);
|
|
void Push(Ring& ring, const uint8_t* bytes, size_t size);
|
|
// Sends everything currently in the ring as [channel u8, 0 u8,
|
|
// size u16 LE, payload] records. Returns false when the socket died.
|
|
bool DrainAndSend(int socket_fd, Ring& ring, uint8_t channel);
|
|
bool SendAll(int socket_fd, const void* data, size_t size);
|
|
|
|
Ring input_;
|
|
Ring output_;
|
|
Ring uplink_;
|
|
//2026-9-23 ckkj debug for wifi mem fail
|
|
StackType_t task_stack_[kTaskStackBytes] = {};
|
|
StaticTask_t task_control_ {};
|
|
//
|
|
TaskHandle_t task_ = nullptr;
|
|
bool initialized_ = false;
|
|
};
|
|
|
|
} // namespace borla
|