Upgrade Playlist Features
This commit is contained in:
@@ -1,189 +1,187 @@
|
||||
#include "afe_audio_processor.h"
|
||||
#include <esp_log.h>
|
||||
|
||||
#define PROCESSOR_RUNNING 0x01
|
||||
|
||||
#define TAG "AfeAudioProcessor"
|
||||
|
||||
AfeAudioProcessor::AfeAudioProcessor()
|
||||
: afe_data_(nullptr) {
|
||||
event_group_ = xEventGroupCreate();
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) {
|
||||
codec_ = codec;
|
||||
frame_samples_ = frame_duration_ms * 16000 / 1000;
|
||||
|
||||
// Pre-allocate output buffer capacity
|
||||
output_buffer_.reserve(frame_samples_);
|
||||
|
||||
int ref_num = codec_->input_reference() ? 1 : 0;
|
||||
|
||||
std::string input_format;
|
||||
for (int i = 0; i < codec_->input_channels() - ref_num; i++) {
|
||||
input_format.push_back('M');
|
||||
}
|
||||
for (int i = 0; i < ref_num; i++) {
|
||||
input_format.push_back('R');
|
||||
}
|
||||
|
||||
srmodel_list_t *models;
|
||||
if (models_list == nullptr) {
|
||||
models = esp_srmodel_init("model");
|
||||
} else {
|
||||
models = models_list;
|
||||
}
|
||||
|
||||
char* ns_model_name = esp_srmodel_filter(models, ESP_NSNET_PREFIX, NULL);
|
||||
char* vad_model_name = esp_srmodel_filter(models, ESP_VADN_PREFIX, NULL);
|
||||
|
||||
afe_config_t* afe_config = afe_config_init(input_format.c_str(), NULL, AFE_TYPE_VC, AFE_MODE_HIGH_PERF);
|
||||
afe_config->aec_mode = AEC_MODE_VOIP_HIGH_PERF;
|
||||
afe_config->vad_mode = VAD_MODE_0;
|
||||
afe_config->vad_min_noise_ms = 100;
|
||||
if (vad_model_name != nullptr) {
|
||||
afe_config->vad_model_name = vad_model_name;
|
||||
}
|
||||
|
||||
if (ns_model_name != nullptr) {
|
||||
afe_config->ns_init = true;
|
||||
afe_config->ns_model_name = ns_model_name;
|
||||
afe_config->afe_ns_mode = AFE_NS_MODE_NET;
|
||||
} else {
|
||||
afe_config->ns_init = false;
|
||||
}
|
||||
|
||||
afe_config->afe_perferred_core = 1;
|
||||
afe_config->afe_perferred_priority = 1;
|
||||
afe_config->agc_init = false;
|
||||
afe_config->memory_alloc_mode = AFE_MEMORY_ALLOC_MORE_PSRAM;
|
||||
|
||||
#ifdef CONFIG_USE_DEVICE_AEC
|
||||
afe_config->aec_init = true;
|
||||
afe_config->vad_init = false;
|
||||
#else
|
||||
afe_config->aec_init = false;
|
||||
afe_config->vad_init = true;
|
||||
#endif
|
||||
|
||||
afe_iface_ = esp_afe_handle_from_config(afe_config);
|
||||
afe_data_ = afe_iface_->create_from_config(afe_config);
|
||||
|
||||
xTaskCreate([](void* arg) {
|
||||
auto this_ = (AfeAudioProcessor*)arg;
|
||||
this_->AudioProcessorTask();
|
||||
vTaskDelete(NULL);
|
||||
}, "audio_communication", 4096, this, 3, NULL);
|
||||
}
|
||||
|
||||
AfeAudioProcessor::~AfeAudioProcessor() {
|
||||
if (afe_data_ != nullptr) {
|
||||
afe_iface_->destroy(afe_data_);
|
||||
}
|
||||
vEventGroupDelete(event_group_);
|
||||
}
|
||||
|
||||
size_t AfeAudioProcessor::GetFeedSize() {
|
||||
if (afe_data_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return afe_iface_->get_feed_chunksize(afe_data_);
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Feed(std::vector<int16_t>&& data) {
|
||||
if (afe_data_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
afe_iface_->feed(afe_data_, data.data());
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Start() {
|
||||
xEventGroupSetBits(event_group_, PROCESSOR_RUNNING);
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Stop() {
|
||||
xEventGroupClearBits(event_group_, PROCESSOR_RUNNING);
|
||||
if (afe_data_ != nullptr) {
|
||||
afe_iface_->reset_buffer(afe_data_);
|
||||
}
|
||||
}
|
||||
|
||||
bool AfeAudioProcessor::IsRunning() {
|
||||
return xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING;
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) {
|
||||
output_callback_ = callback;
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::OnVadStateChange(std::function<void(bool speaking)> callback) {
|
||||
vad_state_change_callback_ = callback;
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::AudioProcessorTask() {
|
||||
auto fetch_size = afe_iface_->get_fetch_chunksize(afe_data_);
|
||||
auto feed_size = afe_iface_->get_feed_chunksize(afe_data_);
|
||||
ESP_LOGI(TAG, "Audio communication task started, feed size: %d fetch size: %d",
|
||||
feed_size, fetch_size);
|
||||
|
||||
while (true) {
|
||||
xEventGroupWaitBits(event_group_, PROCESSOR_RUNNING, pdFALSE, pdTRUE, portMAX_DELAY);
|
||||
|
||||
auto res = afe_iface_->fetch_with_delay(afe_data_, portMAX_DELAY);
|
||||
if ((xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING) == 0) {
|
||||
continue;
|
||||
}
|
||||
if (res == nullptr || res->ret_value == ESP_FAIL) {
|
||||
if (res != nullptr) {
|
||||
ESP_LOGI(TAG, "Error code: %d", res->ret_value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// VAD state change
|
||||
if (vad_state_change_callback_) {
|
||||
if (res->vad_state == VAD_SPEECH && !is_speaking_) {
|
||||
is_speaking_ = true;
|
||||
vad_state_change_callback_(true);
|
||||
} else if (res->vad_state == VAD_SILENCE && is_speaking_) {
|
||||
is_speaking_ = false;
|
||||
vad_state_change_callback_(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (output_callback_) {
|
||||
size_t samples = res->data_size / sizeof(int16_t);
|
||||
|
||||
// Add data to buffer
|
||||
output_buffer_.insert(output_buffer_.end(), res->data, res->data + samples);
|
||||
|
||||
// Output complete frames when buffer has enough data
|
||||
while (output_buffer_.size() >= frame_samples_) {
|
||||
if (output_buffer_.size() == frame_samples_) {
|
||||
// If buffer size equals frame size, move the entire buffer
|
||||
output_callback_(std::move(output_buffer_));
|
||||
output_buffer_.clear();
|
||||
output_buffer_.reserve(frame_samples_);
|
||||
} else {
|
||||
// If buffer size exceeds frame size, copy one frame and remove it
|
||||
output_callback_(std::vector<int16_t>(output_buffer_.begin(), output_buffer_.begin() + frame_samples_));
|
||||
output_buffer_.erase(output_buffer_.begin(), output_buffer_.begin() + frame_samples_);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::EnableDeviceAec(bool enable) {
|
||||
if (enable) {
|
||||
#if CONFIG_USE_DEVICE_AEC
|
||||
afe_iface_->disable_vad(afe_data_);
|
||||
afe_iface_->enable_aec(afe_data_);
|
||||
#else
|
||||
ESP_LOGE(TAG, "Device AEC is not supported");
|
||||
#endif
|
||||
} else {
|
||||
afe_iface_->disable_aec(afe_data_);
|
||||
afe_iface_->enable_vad(afe_data_);
|
||||
}
|
||||
}
|
||||
#include "afe_audio_processor.h"
|
||||
#include <esp_log.h>
|
||||
|
||||
#define PROCESSOR_RUNNING 0x01
|
||||
|
||||
#define TAG "AfeAudioProcessor"
|
||||
|
||||
AfeAudioProcessor::AfeAudioProcessor()
|
||||
: afe_data_(nullptr) {
|
||||
event_group_ = xEventGroupCreate();
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) {
|
||||
codec_ = codec;
|
||||
frame_samples_ = frame_duration_ms * 16000 / 1000;
|
||||
|
||||
// Pre-allocate output buffer capacity
|
||||
output_buffer_.reserve(frame_samples_);
|
||||
|
||||
int ref_num = codec_->input_reference() ? 1 : 0;
|
||||
|
||||
std::string input_format;
|
||||
for (int i = 0; i < codec_->input_channels() - ref_num; i++) {
|
||||
input_format.push_back('M');
|
||||
}
|
||||
for (int i = 0; i < ref_num; i++) {
|
||||
input_format.push_back('R');
|
||||
}
|
||||
|
||||
srmodel_list_t *models;
|
||||
if (models_list == nullptr) {
|
||||
models = esp_srmodel_init("model");
|
||||
} else {
|
||||
models = models_list;
|
||||
}
|
||||
|
||||
char* ns_model_name = esp_srmodel_filter(models, ESP_NSNET_PREFIX, NULL);
|
||||
char* vad_model_name = esp_srmodel_filter(models, ESP_VADN_PREFIX, NULL);
|
||||
|
||||
afe_config_t* afe_config = afe_config_init(input_format.c_str(), NULL, AFE_TYPE_VC, AFE_MODE_HIGH_PERF);
|
||||
afe_config->aec_mode = AEC_MODE_VOIP_HIGH_PERF;
|
||||
afe_config->vad_mode = VAD_MODE_0;
|
||||
afe_config->vad_min_noise_ms = 100;
|
||||
if (vad_model_name != nullptr) {
|
||||
afe_config->vad_model_name = vad_model_name;
|
||||
}
|
||||
|
||||
if (ns_model_name != nullptr) {
|
||||
afe_config->ns_init = true;
|
||||
afe_config->ns_model_name = ns_model_name;
|
||||
afe_config->afe_ns_mode = AFE_NS_MODE_NET;
|
||||
} else {
|
||||
afe_config->ns_init = false;
|
||||
}
|
||||
|
||||
afe_config->agc_init = false;
|
||||
afe_config->memory_alloc_mode = AFE_MEMORY_ALLOC_MORE_PSRAM;
|
||||
|
||||
#ifdef CONFIG_USE_DEVICE_AEC
|
||||
afe_config->aec_init = true;
|
||||
afe_config->vad_init = false;
|
||||
#else
|
||||
afe_config->aec_init = false;
|
||||
afe_config->vad_init = true;
|
||||
#endif
|
||||
|
||||
afe_iface_ = esp_afe_handle_from_config(afe_config);
|
||||
afe_data_ = afe_iface_->create_from_config(afe_config);
|
||||
|
||||
xTaskCreate([](void* arg) {
|
||||
auto this_ = (AfeAudioProcessor*)arg;
|
||||
this_->AudioProcessorTask();
|
||||
vTaskDelete(NULL);
|
||||
}, "audio_communication", 4096, this, 3, NULL);
|
||||
}
|
||||
|
||||
AfeAudioProcessor::~AfeAudioProcessor() {
|
||||
if (afe_data_ != nullptr) {
|
||||
afe_iface_->destroy(afe_data_);
|
||||
}
|
||||
vEventGroupDelete(event_group_);
|
||||
}
|
||||
|
||||
size_t AfeAudioProcessor::GetFeedSize() {
|
||||
if (afe_data_ == nullptr) {
|
||||
return 0;
|
||||
}
|
||||
return afe_iface_->get_feed_chunksize(afe_data_);
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Feed(std::vector<int16_t>&& data) {
|
||||
if (afe_data_ == nullptr) {
|
||||
return;
|
||||
}
|
||||
afe_iface_->feed(afe_data_, data.data());
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Start() {
|
||||
xEventGroupSetBits(event_group_, PROCESSOR_RUNNING);
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::Stop() {
|
||||
xEventGroupClearBits(event_group_, PROCESSOR_RUNNING);
|
||||
if (afe_data_ != nullptr) {
|
||||
afe_iface_->reset_buffer(afe_data_);
|
||||
}
|
||||
}
|
||||
|
||||
bool AfeAudioProcessor::IsRunning() {
|
||||
return xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING;
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) {
|
||||
output_callback_ = callback;
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::OnVadStateChange(std::function<void(bool speaking)> callback) {
|
||||
vad_state_change_callback_ = callback;
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::AudioProcessorTask() {
|
||||
auto fetch_size = afe_iface_->get_fetch_chunksize(afe_data_);
|
||||
auto feed_size = afe_iface_->get_feed_chunksize(afe_data_);
|
||||
ESP_LOGI(TAG, "Audio communication task started, feed size: %d fetch size: %d",
|
||||
feed_size, fetch_size);
|
||||
|
||||
while (true) {
|
||||
xEventGroupWaitBits(event_group_, PROCESSOR_RUNNING, pdFALSE, pdTRUE, portMAX_DELAY);
|
||||
|
||||
auto res = afe_iface_->fetch_with_delay(afe_data_, portMAX_DELAY);
|
||||
if ((xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING) == 0) {
|
||||
continue;
|
||||
}
|
||||
if (res == nullptr || res->ret_value == ESP_FAIL) {
|
||||
if (res != nullptr) {
|
||||
ESP_LOGI(TAG, "Error code: %d", res->ret_value);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
// VAD state change
|
||||
if (vad_state_change_callback_) {
|
||||
if (res->vad_state == VAD_SPEECH && !is_speaking_) {
|
||||
is_speaking_ = true;
|
||||
vad_state_change_callback_(true);
|
||||
} else if (res->vad_state == VAD_SILENCE && is_speaking_) {
|
||||
is_speaking_ = false;
|
||||
vad_state_change_callback_(false);
|
||||
}
|
||||
}
|
||||
|
||||
if (output_callback_) {
|
||||
size_t samples = res->data_size / sizeof(int16_t);
|
||||
|
||||
// Add data to buffer
|
||||
output_buffer_.insert(output_buffer_.end(), res->data, res->data + samples);
|
||||
|
||||
// Output complete frames when buffer has enough data
|
||||
while (output_buffer_.size() >= frame_samples_) {
|
||||
if (output_buffer_.size() == frame_samples_) {
|
||||
// If buffer size equals frame size, move the entire buffer
|
||||
output_callback_(std::move(output_buffer_));
|
||||
output_buffer_.clear();
|
||||
output_buffer_.reserve(frame_samples_);
|
||||
} else {
|
||||
// If buffer size exceeds frame size, copy one frame and remove it
|
||||
output_callback_(std::vector<int16_t>(output_buffer_.begin(), output_buffer_.begin() + frame_samples_));
|
||||
output_buffer_.erase(output_buffer_.begin(), output_buffer_.begin() + frame_samples_);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AfeAudioProcessor::EnableDeviceAec(bool enable) {
|
||||
if (enable) {
|
||||
#if CONFIG_USE_DEVICE_AEC
|
||||
afe_iface_->disable_vad(afe_data_);
|
||||
afe_iface_->enable_aec(afe_data_);
|
||||
#else
|
||||
ESP_LOGE(TAG, "Device AEC is not supported");
|
||||
#endif
|
||||
} else {
|
||||
afe_iface_->disable_aec(afe_data_);
|
||||
afe_iface_->enable_vad(afe_data_);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,45 +1,45 @@
|
||||
#ifndef AFE_AUDIO_PROCESSOR_H
|
||||
#define AFE_AUDIO_PROCESSOR_H
|
||||
|
||||
#include <esp_afe_sr_models.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/event_groups.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "audio_processor.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
class AfeAudioProcessor : public AudioProcessor {
|
||||
public:
|
||||
AfeAudioProcessor();
|
||||
~AfeAudioProcessor();
|
||||
|
||||
void Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) override;
|
||||
void Feed(std::vector<int16_t>&& data) override;
|
||||
void Start() override;
|
||||
void Stop() override;
|
||||
bool IsRunning() override;
|
||||
void OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) override;
|
||||
void OnVadStateChange(std::function<void(bool speaking)> callback) override;
|
||||
size_t GetFeedSize() override;
|
||||
void EnableDeviceAec(bool enable) override;
|
||||
|
||||
private:
|
||||
EventGroupHandle_t event_group_ = nullptr;
|
||||
esp_afe_sr_iface_t* afe_iface_ = nullptr;
|
||||
esp_afe_sr_data_t* afe_data_ = nullptr;
|
||||
std::function<void(std::vector<int16_t>&& data)> output_callback_;
|
||||
std::function<void(bool speaking)> vad_state_change_callback_;
|
||||
AudioCodec* codec_ = nullptr;
|
||||
int frame_samples_ = 0;
|
||||
bool is_speaking_ = false;
|
||||
std::vector<int16_t> output_buffer_;
|
||||
|
||||
void AudioProcessorTask();
|
||||
};
|
||||
|
||||
#ifndef AFE_AUDIO_PROCESSOR_H
|
||||
#define AFE_AUDIO_PROCESSOR_H
|
||||
|
||||
#include <esp_afe_sr_models.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/event_groups.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "audio_processor.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
class AfeAudioProcessor : public AudioProcessor {
|
||||
public:
|
||||
AfeAudioProcessor();
|
||||
~AfeAudioProcessor();
|
||||
|
||||
void Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) override;
|
||||
void Feed(std::vector<int16_t>&& data) override;
|
||||
void Start() override;
|
||||
void Stop() override;
|
||||
bool IsRunning() override;
|
||||
void OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) override;
|
||||
void OnVadStateChange(std::function<void(bool speaking)> callback) override;
|
||||
size_t GetFeedSize() override;
|
||||
void EnableDeviceAec(bool enable) override;
|
||||
|
||||
private:
|
||||
EventGroupHandle_t event_group_ = nullptr;
|
||||
esp_afe_sr_iface_t* afe_iface_ = nullptr;
|
||||
esp_afe_sr_data_t* afe_data_ = nullptr;
|
||||
std::function<void(std::vector<int16_t>&& data)> output_callback_;
|
||||
std::function<void(bool speaking)> vad_state_change_callback_;
|
||||
AudioCodec* codec_ = nullptr;
|
||||
int frame_samples_ = 0;
|
||||
bool is_speaking_ = false;
|
||||
std::vector<int16_t> output_buffer_;
|
||||
|
||||
void AudioProcessorTask();
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,68 +1,68 @@
|
||||
#include "audio_debugger.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
#include <esp_log.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
#define TAG "AudioDebugger"
|
||||
|
||||
|
||||
AudioDebugger::AudioDebugger() {
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
udp_sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (udp_sockfd_ >= 0) {
|
||||
// 解析配置的服务器地址 "IP:PORT"
|
||||
std::string server_addr = CONFIG_AUDIO_DEBUG_UDP_SERVER;
|
||||
size_t colon_pos = server_addr.find(':');
|
||||
|
||||
if (colon_pos != std::string::npos) {
|
||||
std::string ip = server_addr.substr(0, colon_pos);
|
||||
int port = std::stoi(server_addr.substr(colon_pos + 1));
|
||||
|
||||
memset(&udp_server_addr_, 0, sizeof(udp_server_addr_));
|
||||
udp_server_addr_.sin_family = AF_INET;
|
||||
udp_server_addr_.sin_port = htons(port);
|
||||
inet_pton(AF_INET, ip.c_str(), &udp_server_addr_.sin_addr);
|
||||
|
||||
ESP_LOGI(TAG, "Initialized server address: %s", CONFIG_AUDIO_DEBUG_UDP_SERVER);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Invalid server address: %s, should be IP:PORT", CONFIG_AUDIO_DEBUG_UDP_SERVER);
|
||||
close(udp_sockfd_);
|
||||
udp_sockfd_ = -1;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to create UDP socket: %d", errno);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
AudioDebugger::~AudioDebugger() {
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
if (udp_sockfd_ >= 0) {
|
||||
close(udp_sockfd_);
|
||||
ESP_LOGI(TAG, "Closed UDP socket");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void AudioDebugger::Feed(const std::vector<int16_t>& data) {
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
if (udp_sockfd_ >= 0) {
|
||||
ssize_t sent = sendto(udp_sockfd_, data.data(), data.size() * sizeof(int16_t), 0,
|
||||
(struct sockaddr*)&udp_server_addr_, sizeof(udp_server_addr_));
|
||||
if (sent < 0) {
|
||||
ESP_LOGW(TAG, "Failed to send audio data to %s: %d", CONFIG_AUDIO_DEBUG_UDP_SERVER, errno);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Sent %d bytes audio data to %s", sent, CONFIG_AUDIO_DEBUG_UDP_SERVER);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
#include "audio_debugger.h"
|
||||
#include "sdkconfig.h"
|
||||
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
#include <esp_log.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#endif
|
||||
|
||||
#define TAG "AudioDebugger"
|
||||
|
||||
|
||||
AudioDebugger::AudioDebugger() {
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
udp_sockfd_ = socket(AF_INET, SOCK_DGRAM, 0);
|
||||
if (udp_sockfd_ >= 0) {
|
||||
// 解析配置的服务器地址 "IP:PORT"
|
||||
std::string server_addr = CONFIG_AUDIO_DEBUG_UDP_SERVER;
|
||||
size_t colon_pos = server_addr.find(':');
|
||||
|
||||
if (colon_pos != std::string::npos) {
|
||||
std::string ip = server_addr.substr(0, colon_pos);
|
||||
int port = std::stoi(server_addr.substr(colon_pos + 1));
|
||||
|
||||
memset(&udp_server_addr_, 0, sizeof(udp_server_addr_));
|
||||
udp_server_addr_.sin_family = AF_INET;
|
||||
udp_server_addr_.sin_port = htons(port);
|
||||
inet_pton(AF_INET, ip.c_str(), &udp_server_addr_.sin_addr);
|
||||
|
||||
ESP_LOGI(TAG, "Initialized server address: %s", CONFIG_AUDIO_DEBUG_UDP_SERVER);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Invalid server address: %s, should be IP:PORT", CONFIG_AUDIO_DEBUG_UDP_SERVER);
|
||||
close(udp_sockfd_);
|
||||
udp_sockfd_ = -1;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to create UDP socket: %d", errno);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
AudioDebugger::~AudioDebugger() {
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
if (udp_sockfd_ >= 0) {
|
||||
close(udp_sockfd_);
|
||||
ESP_LOGI(TAG, "Closed UDP socket");
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void AudioDebugger::Feed(const std::vector<int16_t>& data) {
|
||||
#if CONFIG_USE_AUDIO_DEBUGGER
|
||||
if (udp_sockfd_ >= 0) {
|
||||
ssize_t sent = sendto(udp_sockfd_, data.data(), data.size() * sizeof(int16_t), 0,
|
||||
(struct sockaddr*)&udp_server_addr_, sizeof(udp_server_addr_));
|
||||
if (sent < 0) {
|
||||
ESP_LOGW(TAG, "Failed to send audio data to %s: %d", CONFIG_AUDIO_DEBUG_UDP_SERVER, errno);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Sent %d bytes audio data to %s", sent, CONFIG_AUDIO_DEBUG_UDP_SERVER);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
#ifndef AUDIO_DEBUGGER_H
|
||||
#define AUDIO_DEBUGGER_H
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
class AudioDebugger {
|
||||
public:
|
||||
AudioDebugger();
|
||||
~AudioDebugger();
|
||||
|
||||
void Feed(const std::vector<int16_t>& data);
|
||||
|
||||
private:
|
||||
int udp_sockfd_ = -1;
|
||||
struct sockaddr_in udp_server_addr_;
|
||||
};
|
||||
|
||||
#ifndef AUDIO_DEBUGGER_H
|
||||
#define AUDIO_DEBUGGER_H
|
||||
|
||||
#include <vector>
|
||||
#include <cstdint>
|
||||
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
class AudioDebugger {
|
||||
public:
|
||||
AudioDebugger();
|
||||
~AudioDebugger();
|
||||
|
||||
void Feed(const std::vector<int16_t>& data);
|
||||
|
||||
private:
|
||||
int udp_sockfd_ = -1;
|
||||
struct sockaddr_in udp_server_addr_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -1,59 +1,59 @@
|
||||
#include "no_audio_processor.h"
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "NoAudioProcessor"
|
||||
|
||||
void NoAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) {
|
||||
codec_ = codec;
|
||||
frame_samples_ = frame_duration_ms * 16000 / 1000;
|
||||
}
|
||||
|
||||
void NoAudioProcessor::Feed(std::vector<int16_t>&& data) {
|
||||
if (!is_running_ || !output_callback_) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
#include "no_audio_processor.h"
|
||||
#include <esp_log.h>
|
||||
|
||||
#define TAG "NoAudioProcessor"
|
||||
|
||||
void NoAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) {
|
||||
codec_ = codec;
|
||||
frame_samples_ = frame_duration_ms * 16000 / 1000;
|
||||
}
|
||||
|
||||
void NoAudioProcessor::Feed(std::vector<int16_t>&& data) {
|
||||
if (!is_running_ || !output_callback_) {
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,33 +1,33 @@
|
||||
#ifndef DUMMY_AUDIO_PROCESSOR_H
|
||||
#define DUMMY_AUDIO_PROCESSOR_H
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "audio_processor.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
class NoAudioProcessor : public AudioProcessor {
|
||||
public:
|
||||
NoAudioProcessor() = default;
|
||||
~NoAudioProcessor() = default;
|
||||
|
||||
void Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) override;
|
||||
void Feed(std::vector<int16_t>&& data) override;
|
||||
void Start() override;
|
||||
void Stop() override;
|
||||
bool IsRunning() override;
|
||||
void OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) override;
|
||||
void OnVadStateChange(std::function<void(bool speaking)> callback) override;
|
||||
size_t GetFeedSize() override;
|
||||
void EnableDeviceAec(bool enable) override;
|
||||
|
||||
private:
|
||||
AudioCodec* codec_ = nullptr;
|
||||
int frame_samples_ = 0;
|
||||
std::function<void(std::vector<int16_t>&& data)> output_callback_;
|
||||
std::function<void(bool speaking)> vad_state_change_callback_;
|
||||
bool is_running_ = false;
|
||||
};
|
||||
|
||||
#ifndef DUMMY_AUDIO_PROCESSOR_H
|
||||
#define DUMMY_AUDIO_PROCESSOR_H
|
||||
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
|
||||
#include "audio_processor.h"
|
||||
#include "audio_codec.h"
|
||||
|
||||
class NoAudioProcessor : public AudioProcessor {
|
||||
public:
|
||||
NoAudioProcessor() = default;
|
||||
~NoAudioProcessor() = default;
|
||||
|
||||
void Initialize(AudioCodec* codec, int frame_duration_ms, srmodel_list_t* models_list) override;
|
||||
void Feed(std::vector<int16_t>&& data) override;
|
||||
void Start() override;
|
||||
void Stop() override;
|
||||
bool IsRunning() override;
|
||||
void OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) override;
|
||||
void OnVadStateChange(std::function<void(bool speaking)> callback) override;
|
||||
size_t GetFeedSize() override;
|
||||
void EnableDeviceAec(bool enable) override;
|
||||
|
||||
private:
|
||||
AudioCodec* codec_ = nullptr;
|
||||
int frame_samples_ = 0;
|
||||
std::function<void(std::vector<int16_t>&& data)> output_callback_;
|
||||
std::function<void(bool speaking)> vad_state_change_callback_;
|
||||
bool is_running_ = false;
|
||||
};
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user