forked from xiaozhi/xiaozhi-esp32
66 lines
1.7 KiB
C++
66 lines
1.7 KiB
C++
#include "no_audio_processor.h"
|
|
#include <esp_log.h>
|
|
|
|
#define TAG "NoAudioProcessor"
|
|
|
|
void NoAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms) :
|
|
codec_(codec),
|
|
frame_duration_ms_(frame_duration_ms) {
|
|
frame_samples_ = frame_duration_ms_ * codec_->input_sample_rate() / 1000;
|
|
}
|
|
|
|
void NoAudioProcessor::Feed(std::vector<int16_t>&& data) {
|
|
if (!is_running_ || !output_callback_) {
|
|
return;
|
|
}
|
|
|
|
if (data.size() != frame_samples_) {
|
|
ESP_LOGE(TAG, "Feed data size is not equal to frame size, feed size: %u, frame size: %u", data.size(), frame_samples_);
|
|
return;
|
|
}
|
|
|
|
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));
|
|
}
|
|
}
|
|
|
|
void NoAudioProcessor::Start() {
|
|
is_running_ = true;
|
|
}
|
|
|
|
void NoAudioProcessor::Stop() {
|
|
is_running_ = false;
|
|
}
|
|
|
|
bool NoAudioProcessor::IsRunning() {
|
|
return is_running_;
|
|
}
|
|
|
|
void NoAudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) {
|
|
output_callback_ = callback;
|
|
}
|
|
|
|
void NoAudioProcessor::OnVadStateChange(std::function<void(bool speaking)> callback) {
|
|
vad_state_change_callback_ = callback;
|
|
}
|
|
|
|
size_t NoAudioProcessor::GetFeedSize() {
|
|
if (!codec_) {
|
|
return 0;
|
|
}
|
|
return frame_samples_;
|
|
}
|
|
|
|
void NoAudioProcessor::EnableDeviceAec(bool enable) {
|
|
if (enable) {
|
|
ESP_LOGE(TAG, "Device AEC is not supported");
|
|
}
|
|
}
|