Add audio debugger

This commit is contained in:
Terrence
2025-06-06 20:24:00 +08:00
parent 24ede22197
commit 7bc5f7bb0c
11 changed files with 170 additions and 5 deletions

View File

@@ -155,18 +155,19 @@ void AfeWakeWord::EncodeWakeWordData() {
auto encoder = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
encoder->SetComplexity(0); // 0 is the fastest
int packets = 0;
for (auto& pcm: this_->wake_word_pcm_) {
encoder->Encode(std::move(pcm), [this_](std::vector<uint8_t>&& opus) {
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
this_->wake_word_opus_.emplace_back(std::move(opus));
this_->wake_word_cv_.notify_all();
});
packets++;
}
this_->wake_word_pcm_.clear();
auto end_time = esp_timer_get_time();
ESP_LOGI(TAG, "Encode wake word opus %u packets in %lld ms",
this_->wake_word_opus_.size(), (end_time - start_time) / 1000);
ESP_LOGI(TAG, "Encode wake word opus %d packets in %ld ms", packets, (long)((end_time - start_time) / 1000));
std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
this_->wake_word_opus_.push_back(std::vector<uint8_t>());

View File

@@ -0,0 +1,64 @@
#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 (udp_sockfd_ >= 0) {
close(udp_sockfd_);
ESP_LOGI(TAG, "Closed UDP socket");
}
}
void AudioDebugger::Feed(const std::vector<int16_t>& data) {
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);
}
}
}

View File

@@ -0,0 +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_;
};
#endif