bolar_2 backup
This commit is contained in:
133
main/audio/capture_pipeline.h
Normal file
133
main/audio/capture_pipeline.h
Normal file
@@ -0,0 +1,133 @@
|
||||
#pragma once
|
||||
|
||||
#include "audio/audio_input.h"
|
||||
#include "audio/encoded_audio_sink.h"
|
||||
#include "audio/opus_frame_encoder.h"
|
||||
#include "audio/pcm_rate_converter.h"
|
||||
#include "audio/voice_frame_processor.h"
|
||||
#include "core/runtime_state.h"
|
||||
#include "core/task_stack_budget.h"
|
||||
|
||||
#include <atomic>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace borla {
|
||||
class AudioTap;
|
||||
}
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/queue.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <freertos/task.h>
|
||||
|
||||
namespace borla {
|
||||
|
||||
struct CaptureMetrics {
|
||||
uint32_t frames_read = 0;
|
||||
uint32_t frames_encoded = 0;
|
||||
uint32_t frames_sent = 0;
|
||||
uint32_t read_failures = 0;
|
||||
uint32_t encode_failures = 0;
|
||||
uint32_t backpressure_drops = 0;
|
||||
uint32_t maximum_encoded_bytes = 0;
|
||||
};
|
||||
|
||||
// Per-stage cost of the capture loop, accumulated by the capture task and
|
||||
// drained by diagnostics. Added because the loop was measured at 94.6 ms per
|
||||
// frame against a 60 ms budget while duplex was active — over budget means the
|
||||
// I2S RX DMA overflows, frames are lost, and the AEC's microphone/reference
|
||||
// alignment drifts, which is what capped echo cancellation at 13 dB and let the
|
||||
// device's own TTS reach the server's ASR. Which stage is responsible was pure
|
||||
// guesswork without these numbers.
|
||||
struct CaptureTimings {
|
||||
uint32_t frames = 0;
|
||||
uint64_t read_us = 0;
|
||||
uint64_t convert_us = 0;
|
||||
uint64_t process_us = 0;
|
||||
uint64_t encode_us = 0;
|
||||
uint64_t submit_us = 0;
|
||||
uint32_t read_us_max = 0;
|
||||
uint32_t process_us_max = 0;
|
||||
uint32_t encode_us_max = 0;
|
||||
uint32_t submit_us_max = 0;
|
||||
};
|
||||
|
||||
class CapturePipeline final {
|
||||
public:
|
||||
static constexpr uint32_t kHardwareSampleRate = 48000;//24000;
|
||||
static constexpr uint32_t kProtocolSampleRate = 16000;
|
||||
static constexpr uint32_t kFrameDurationMs = 60;
|
||||
|
||||
explicit CapturePipeline(IAudioInput& input);
|
||||
~CapturePipeline();
|
||||
|
||||
bool Initialize();
|
||||
bool Start();
|
||||
void Stop();
|
||||
void SetSink(IEncodedAudioSink* sink);
|
||||
void SetProcessor(IVoiceFrameProcessor* processor);
|
||||
// Debug recorder for the "what does the server actually hear" channel.
|
||||
void SetAudioTap(AudioTap* tap);
|
||||
void ApplyPolicy(AudioPolicy policy);
|
||||
CaptureMetrics Metrics() const;
|
||||
CaptureTimings ConsumeTimings();
|
||||
|
||||
private:
|
||||
// Opus encoding costs ~42 ms per 60 ms frame on this board (measured), and
|
||||
// it used to run inline on the capture task together with the ~56 ms of
|
||||
// AEC + wake-word work. Total ~104 ms against a 60 ms budget meant the I2S
|
||||
// RX DMA overflowed every frame: audio was lost, the AEC's microphone and
|
||||
// reference streams drifted apart, echo cancellation collapsed to ~9 dB and
|
||||
// the device's own TTS reached the server's ASR — it answered itself.
|
||||
//
|
||||
// The previous firmware never had this problem because encoding lived on a
|
||||
// separate task (audio_service.cc "Task 3: Opus 编码/解码"). Same split
|
||||
// here, pinned to the other core so it genuinely runs in parallel with
|
||||
// capture rather than merely interleaving with it.
|
||||
static constexpr size_t kEncodeSlots = 4;
|
||||
|
||||
static void TaskEntry(void* context);
|
||||
[[noreturn]] void Run();
|
||||
static void EncoderTaskEntry(void* context);
|
||||
[[noreturn]] void RunEncoder();
|
||||
void RecordTimings(uint32_t read_us, uint32_t convert_us,
|
||||
uint32_t process_us, uint32_t encode_us, uint32_t submit_us);
|
||||
bool CaptureEnabled(AudioPolicy policy) const;
|
||||
bool InputEnabled(AudioPolicy policy) const;
|
||||
|
||||
IAudioInput& input_;
|
||||
std::atomic<IEncodedAudioSink*> sink_ { nullptr };
|
||||
std::atomic<IVoiceFrameProcessor*> processor_ { nullptr };
|
||||
std::atomic<AudioTap*> audio_tap_ { nullptr };
|
||||
std::atomic<AudioPolicy> policy_ { AudioPolicy::kOff };
|
||||
OpusFrameEncoder encoder_;
|
||||
//Pcm24kTo16kConverter converter_;
|
||||
Pcm48kTo16kConverter converter_;
|
||||
|
||||
// Set by ApplyPolicy when leaving a capture policy; consumed by the
|
||||
// encoder task, which is the only task allowed to touch encoder_ state.
|
||||
std::atomic<bool> encoder_reset_pending_ { false };
|
||||
int16_t* encode_slots_ = nullptr;
|
||||
QueueHandle_t encode_ready_ = nullptr;
|
||||
QueueHandle_t encode_free_ = nullptr;
|
||||
TaskHandle_t encoder_task_ = nullptr;
|
||||
SemaphoreHandle_t encoder_stopped_signal_ = nullptr;
|
||||
StaticSemaphore_t encoder_stopped_signal_storage_ {};
|
||||
|
||||
int16_t* hardware_pcm_ = nullptr;
|
||||
int16_t* raw_protocol_pcm_ = nullptr;
|
||||
int16_t* processed_protocol_pcm_ = nullptr;
|
||||
uint8_t* encoded_ = nullptr;
|
||||
|
||||
mutable portMUX_TYPE metrics_lock_ = portMUX_INITIALIZER_UNLOCKED;
|
||||
CaptureMetrics metrics_ {};
|
||||
CaptureTimings timings_ {};
|
||||
TaskHandle_t task_ = nullptr;
|
||||
SemaphoreHandle_t stopped_signal_ = nullptr;
|
||||
StaticSemaphore_t stopped_signal_storage_ {};
|
||||
bool initialized_ = false;
|
||||
std::atomic<bool> running_ { false };
|
||||
};
|
||||
|
||||
} // namespace borla
|
||||
Reference in New Issue
Block a user