#pragma once /** * Voice processing front-end for phone-call mode. * * Chain (16kHz mono int16): * MIC ─┐ * ├─→ AEC (VOIP_HIGH_PERF, filter_length=4) ─→ NS (pro, aggressive) ─→ AGC (soft) ─→ clean PCM * REF ─┘ * * AEC_MODE_VOIP_HIGH_PERF already includes nonlinear residual echo * suppression — the explicit NS step on top is mainly for ambient noise * (kid-voice / room hum), not echo. AGC is a simple envelope normalizer in * software (ESP-SR's AGC API is coarse; children voice amplitude swing is * better handled by a gentle limiter). * * Chunk handling: AEC has a fixed internal chunk size (typically 256 samples * @ 16kHz). The input and reference buffers for one Process() call can be of * any length; unprocessed tail samples are held in internal accumulators and * processed on the next call. */ #include #include #include #include #include "esp_aec.h" #include "esp_ns.h" class VoiceProcessor { public: VoiceProcessor(); ~VoiceProcessor(); // Initialize AEC and AGC. Call mode also enables NS; barge-in keeps NS off // to reduce CPU on the audio input path. // Safe to call multiple times; second call is a no-op if already inited. bool Init(bool low_cost = false); // Free all allocated resources. Safe to call repeatedly. void Deinit(); bool IsReady() const { return aec_handle_ != nullptr; } bool IsLowCostMode() const { return low_cost_mode_; } // AEC's fixed chunk size in samples (at 16kHz). 0 if not inited. int ChunkSamples() const { return chunk_samples_; } // Process one batch of mic + reference PCM (both 16kHz mono int16, equal // length required). Clean output is appended to `out` (length depends on // chunk alignment — unprocessed tail is buffered for next call). // // Returns false if not initialized or sizes mismatch. bool Process(const int16_t* mic, const int16_t* ref, size_t samples, std::vector& out); // Clear internal accumulators. Call when entering/leaving call mode. void Reset(); private: aec_handle_t* aec_handle_ = nullptr; ns_handle_t ns_handle_ = nullptr; bool low_cost_mode_ = false; int chunk_samples_ = 0; // AEC chunk size in samples (e.g. 256) int ns_frame_samples_ = 0; // NS pro requires 10ms = 160 samples @ 16kHz // Accumulators for chunk alignment. AEC reads N=chunk_samples_ at a time; // any leftover from a partial Process() stays here until next call. std::vector mic_acc_; std::vector ref_acc_; size_t mic_acc_pos_ = 0; size_t ref_acc_pos_ = 0; // NS operates on a different chunk (160 samples). Second-stage accumulator // sits between AEC output and NS input. std::vector ns_in_acc_; size_t ns_in_acc_pos_ = 0; // Pre-allocated scratch buffers for one chunk — avoid per-call alloc. std::vector aec_out_chunk_; std::vector ns_out_chunk_; // Soft AGC state: simple envelope tracker + gain smoother. float envelope_ = 1.0f; // EMA of recent sample peak float gain_ = 1.0f; // current applied gain // Apply soft AGC in-place on `samples`. Gain moves slowly toward a target // that keeps peak envelope near ~0.3 * full-scale (10kRMS). void ApplyAgc(int16_t* samples, size_t n); void CompactAccumulatorsIfNeeded(); };