1010#include " iot/thing_manager.h"
1111#include " assets/lang_config.h"
1212
13+ #if CONFIG_USE_AUDIO_PROCESSOR
14+ #include " afe_audio_processor.h"
15+ #else
16+ #include " dummy_audio_processor.h"
17+ #endif
18+
1319#include < cstring>
1420#include < esp_log.h>
1521#include < cJSON.h>
@@ -37,6 +43,12 @@ Application::Application() {
3743 event_group_ = xEventGroupCreate ();
3844 background_task_ = new BackgroundTask (4096 * 8 );
3945
46+ #if CONFIG_USE_AUDIO_PROCESSOR
47+ audio_processor_ = std::make_unique<AfeAudioProcessor>();
48+ #else
49+ audio_processor_ = std::make_unique<DummyAudioProcessor>();
50+ #endif
51+
4052 esp_timer_create_args_t clock_timer_args = {
4153 .callback = [](void * arg) {
4254 Application* app = (Application*)arg;
@@ -502,9 +514,8 @@ void Application::Start() {
502514 });
503515 bool protocol_started = protocol_->Start ();
504516
505- #if CONFIG_USE_AUDIO_PROCESSOR
506- audio_processor_.Initialize (codec, realtime_chat_enabled_);
507- audio_processor_.OnOutput ([this ](std::vector<int16_t >&& data) {
517+ audio_processor_->Initialize (codec, realtime_chat_enabled_);
518+ audio_processor_->OnOutput ([this ](std::vector<int16_t >&& data) {
508519 background_task_->Schedule ([this , data = std::move (data)]() mutable {
509520 if (protocol_->IsAudioChannelBusy ()) {
510521 return ;
@@ -520,7 +531,7 @@ void Application::Start() {
520531 });
521532 });
522533 });
523- audio_processor_. OnVadStateChange ([this ](bool speaking) {
534+ audio_processor_-> OnVadStateChange ([this ](bool speaking) {
524535 if (device_state_ == kDeviceStateListening ) {
525536 Schedule ([this , speaking]() {
526537 if (speaking) {
@@ -533,7 +544,6 @@ void Application::Start() {
533544 });
534545 }
535546 });
536- #endif
537547
538548#if CONFIG_USE_WAKE_WORD_DETECT
539549 wake_word_detect_.Initialize (codec);
@@ -716,37 +726,16 @@ void Application::OnAudioInput() {
716726 }
717727 }
718728#endif
719- #if CONFIG_USE_AUDIO_PROCESSOR
720- if (audio_processor_.IsRunning ()) {
729+ if (audio_processor_->IsRunning ()) {
721730 std::vector<int16_t > data;
722- int samples = audio_processor_. GetFeedSize ();
731+ int samples = audio_processor_-> GetFeedSize ();
723732 if (samples > 0 ) {
724733 ReadAudio (data, 16000 , samples);
725- audio_processor_. Feed (data);
734+ audio_processor_-> Feed (data);
726735 return ;
727736 }
728737 }
729- #else
730- if (device_state_ == kDeviceStateListening ) {
731- std::vector<int16_t > data;
732- ReadAudio (data, 16000 , 30 * 16000 / 1000 );
733- background_task_->Schedule ([this , data = std::move (data)]() mutable {
734- if (protocol_->IsAudioChannelBusy ()) {
735- return ;
736- }
737- opus_encoder_->Encode (std::move (data), [this ](std::vector<uint8_t >&& opus) {
738- AudioStreamPacket packet;
739- packet.payload = std::move (opus);
740- packet.timestamp = last_output_timestamp_;
741- last_output_timestamp_ = 0 ;
742- Schedule ([this , packet = std::move (packet)]() {
743- protocol_->SendAudio (packet);
744- });
745- });
746- });
747- return ;
748- }
749- #endif
738+
750739 vTaskDelay (pdMS_TO_TICKS (30 ));
751740}
752741
@@ -818,9 +807,7 @@ void Application::SetDeviceState(DeviceState state) {
818807 case kDeviceStateIdle :
819808 display->SetStatus (Lang::Strings::STANDBY);
820809 display->SetEmotion (" neutral" );
821- #if CONFIG_USE_AUDIO_PROCESSOR
822- audio_processor_.Stop ();
823- #endif
810+ audio_processor_->Stop ();
824811#if CONFIG_USE_WAKE_WORD_DETECT
825812 wake_word_detect_.StartDetection ();
826813#endif
@@ -838,11 +825,7 @@ void Application::SetDeviceState(DeviceState state) {
838825 UpdateIotStates ();
839826
840827 // Make sure the audio processor is running
841- #if CONFIG_USE_AUDIO_PROCESSOR
842- if (!audio_processor_.IsRunning ()) {
843- #else
844- if (true ) {
845- #endif
828+ if (!audio_processor_->IsRunning ()) {
846829 // Send the start listening command
847830 protocol_->SendStartListening (listening_mode_);
848831 if (listening_mode_ == kListeningModeAutoStop && previous_state == kDeviceStateSpeaking ) {
@@ -853,18 +836,14 @@ void Application::SetDeviceState(DeviceState state) {
853836#if CONFIG_USE_WAKE_WORD_DETECT
854837 wake_word_detect_.StopDetection ();
855838#endif
856- #if CONFIG_USE_AUDIO_PROCESSOR
857- audio_processor_.Start ();
858- #endif
839+ audio_processor_->Start ();
859840 }
860841 break ;
861842 case kDeviceStateSpeaking :
862843 display->SetStatus (Lang::Strings::SPEAKING);
863844
864845 if (listening_mode_ != kListeningModeRealtime ) {
865- #if CONFIG_USE_AUDIO_PROCESSOR
866- audio_processor_.Stop ();
867- #endif
846+ audio_processor_->Stop ();
868847#if CONFIG_USE_WAKE_WORD_DETECT
869848 wake_word_detect_.StartDetection ();
870849#endif
0 commit comments