2025-05-31 22:21:03 +08:00
|
|
|
#include "no_audio_processor.h"
|
2025-04-29 18:17:08 +08:00
|
|
|
#include <esp_log.h>
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
#define TAG "NoAudioProcessor"
|
2025-04-29 18:17:08 +08:00
|
|
|
|
2025-07-20 07:53:52 +08:00
|
|
|
void NoAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms) {
|
|
|
|
|
codec_ = codec;
|
|
|
|
|
frame_samples_ = frame_duration_ms * 16000 / 1000;
|
2025-04-29 18:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-07-20 03:57:36 +08:00
|
|
|
void NoAudioProcessor::Feed(std::vector<int16_t>&& data) {
|
2025-04-29 18:17:08 +08:00
|
|
|
if (!is_running_ || !output_callback_) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2025-07-20 03:57:36 +08:00
|
|
|
|
|
|
|
|
if (codec_->input_channels() == 2) {
|
|
|
|
|
// If input channels is 2, we need to fetch the left channel data
|
|
|
|
|
auto mono_data = std::vector<int16_t>(data.size() / 2);
|
|
|
|
|
for (size_t i = 0, j = 0; i < mono_data.size(); ++i, j += 2) {
|
|
|
|
|
mono_data[i] = data[j];
|
|
|
|
|
}
|
|
|
|
|
output_callback_(std::move(mono_data));
|
|
|
|
|
} else {
|
|
|
|
|
output_callback_(std::move(data));
|
|
|
|
|
}
|
2025-04-29 18:17:08 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
void NoAudioProcessor::Start() {
|
2025-04-29 18:17:08 +08:00
|
|
|
is_running_ = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
void NoAudioProcessor::Stop() {
|
2025-04-29 18:17:08 +08:00
|
|
|
is_running_ = false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
bool NoAudioProcessor::IsRunning() {
|
2025-04-29 18:17:08 +08:00
|
|
|
return is_running_;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
void NoAudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) {
|
2025-04-29 18:17:08 +08:00
|
|
|
output_callback_ = callback;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
void NoAudioProcessor::OnVadStateChange(std::function<void(bool speaking)> callback) {
|
2025-04-29 18:17:08 +08:00
|
|
|
vad_state_change_callback_ = callback;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
size_t NoAudioProcessor::GetFeedSize() {
|
2025-04-29 18:17:08 +08:00
|
|
|
if (!codec_) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2025-07-20 03:57:36 +08:00
|
|
|
return frame_samples_;
|
2025-05-27 14:58:49 +08:00
|
|
|
}
|
|
|
|
|
|
2025-05-31 22:21:03 +08:00
|
|
|
void NoAudioProcessor::EnableDeviceAec(bool enable) {
|
2025-05-27 14:58:49 +08:00
|
|
|
if (enable) {
|
|
|
|
|
ESP_LOGE(TAG, "Device AEC is not supported");
|
|
|
|
|
}
|
|
|
|
|
}
|