Add dummy audio processor

This commit is contained in:
Terrence
2025-04-29 18:17:08 +08:00
parent c201e5955f
commit f6334246ca
8 changed files with 171 additions and 93 deletions

View File

@@ -10,6 +10,12 @@
#include "iot/thing_manager.h"
#include "assets/lang_config.h"
#if CONFIG_USE_AUDIO_PROCESSOR
#include "afe_audio_processor.h"
#else
#include "dummy_audio_processor.h"
#endif
#include <cstring>
#include <esp_log.h>
#include <cJSON.h>
@@ -37,6 +43,12 @@ Application::Application() {
event_group_ = xEventGroupCreate();
background_task_ = new BackgroundTask(4096 * 8);
#if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_ = std::make_unique<AfeAudioProcessor>();
#else
audio_processor_ = std::make_unique<DummyAudioProcessor>();
#endif
esp_timer_create_args_t clock_timer_args = {
.callback = [](void* arg) {
Application* app = (Application*)arg;
@@ -502,9 +514,8 @@ void Application::Start() {
});
bool protocol_started = protocol_->Start();
#if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Initialize(codec, realtime_chat_enabled_);
audio_processor_.OnOutput([this](std::vector<int16_t>&& data) {
audio_processor_->Initialize(codec, realtime_chat_enabled_);
audio_processor_->OnOutput([this](std::vector<int16_t>&& data) {
background_task_->Schedule([this, data = std::move(data)]() mutable {
if (protocol_->IsAudioChannelBusy()) {
return;
@@ -520,7 +531,7 @@ void Application::Start() {
});
});
});
audio_processor_.OnVadStateChange([this](bool speaking) {
audio_processor_->OnVadStateChange([this](bool speaking) {
if (device_state_ == kDeviceStateListening) {
Schedule([this, speaking]() {
if (speaking) {
@@ -533,7 +544,6 @@ void Application::Start() {
});
}
});
#endif
#if CONFIG_USE_WAKE_WORD_DETECT
wake_word_detect_.Initialize(codec);
@@ -716,37 +726,16 @@ void Application::OnAudioInput() {
}
}
#endif
#if CONFIG_USE_AUDIO_PROCESSOR
if (audio_processor_.IsRunning()) {
if (audio_processor_->IsRunning()) {
std::vector<int16_t> data;
int samples = audio_processor_.GetFeedSize();
int samples = audio_processor_->GetFeedSize();
if (samples > 0) {
ReadAudio(data, 16000, samples);
audio_processor_.Feed(data);
audio_processor_->Feed(data);
return;
}
}
#else
if (device_state_ == kDeviceStateListening) {
std::vector<int16_t> data;
ReadAudio(data, 16000, 30 * 16000 / 1000);
background_task_->Schedule([this, data = std::move(data)]() mutable {
if (protocol_->IsAudioChannelBusy()) {
return;
}
opus_encoder_->Encode(std::move(data), [this](std::vector<uint8_t>&& opus) {
AudioStreamPacket packet;
packet.payload = std::move(opus);
packet.timestamp = last_output_timestamp_;
last_output_timestamp_ = 0;
Schedule([this, packet = std::move(packet)]() {
protocol_->SendAudio(packet);
});
});
});
return;
}
#endif
vTaskDelay(pdMS_TO_TICKS(30));
}
@@ -818,9 +807,7 @@ void Application::SetDeviceState(DeviceState state) {
case kDeviceStateIdle:
display->SetStatus(Lang::Strings::STANDBY);
display->SetEmotion("neutral");
#if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Stop();
#endif
audio_processor_->Stop();
#if CONFIG_USE_WAKE_WORD_DETECT
wake_word_detect_.StartDetection();
#endif
@@ -838,11 +825,7 @@ void Application::SetDeviceState(DeviceState state) {
UpdateIotStates();
// Make sure the audio processor is running
#if CONFIG_USE_AUDIO_PROCESSOR
if (!audio_processor_.IsRunning()) {
#else
if (true) {
#endif
if (!audio_processor_->IsRunning()) {
// Send the start listening command
protocol_->SendStartListening(listening_mode_);
if (listening_mode_ == kListeningModeAutoStop && previous_state == kDeviceStateSpeaking) {
@@ -853,18 +836,14 @@ void Application::SetDeviceState(DeviceState state) {
#if CONFIG_USE_WAKE_WORD_DETECT
wake_word_detect_.StopDetection();
#endif
#if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Start();
#endif
audio_processor_->Start();
}
break;
case kDeviceStateSpeaking:
display->SetStatus(Lang::Strings::SPEAKING);
if (listening_mode_ != kListeningModeRealtime) {
#if CONFIG_USE_AUDIO_PROCESSOR
audio_processor_.Stop();
#endif
audio_processor_->Stop();
#if CONFIG_USE_WAKE_WORD_DETECT
wake_word_detect_.StartDetection();
#endif