afe_audio_processor.cc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "afe_audio_processor.h"
  2. #include <esp_log.h>
  3. #define PROCESSOR_RUNNING 0x01
  4. #define TAG "AfeAudioProcessor"
  5. AfeAudioProcessor::AfeAudioProcessor()
  6. : afe_data_(nullptr) {
  7. event_group_ = xEventGroupCreate();
  8. }
  9. void AfeAudioProcessor::Initialize(AudioCodec* codec, int frame_duration_ms) {
  10. codec_ = codec;
  11. frame_samples_ = frame_duration_ms * 16000 / 1000;
  12. // Pre-allocate output buffer capacity
  13. output_buffer_.reserve(frame_samples_);
  14. int ref_num = codec_->input_reference() ? 1 : 0;
  15. std::string input_format;
  16. for (int i = 0; i < codec_->input_channels() - ref_num; i++) {
  17. input_format.push_back('M');
  18. }
  19. for (int i = 0; i < ref_num; i++) {
  20. input_format.push_back('R');
  21. }
  22. srmodel_list_t *models = esp_srmodel_init("model");
  23. char* ns_model_name = esp_srmodel_filter(models, ESP_NSNET_PREFIX, NULL);
  24. char* vad_model_name = esp_srmodel_filter(models, ESP_VADN_PREFIX, NULL);
  25. afe_config_t* afe_config = afe_config_init(input_format.c_str(), NULL, AFE_TYPE_VC, AFE_MODE_HIGH_PERF);
  26. afe_config->aec_mode = AEC_MODE_VOIP_HIGH_PERF;
  27. afe_config->vad_mode = VAD_MODE_0;
  28. afe_config->vad_min_noise_ms = 100;
  29. if (vad_model_name != nullptr) {
  30. afe_config->vad_model_name = vad_model_name;
  31. }
  32. if (ns_model_name != nullptr) {
  33. afe_config->ns_init = true;
  34. afe_config->ns_model_name = ns_model_name;
  35. afe_config->afe_ns_mode = AFE_NS_MODE_NET;
  36. } else {
  37. afe_config->ns_init = false;
  38. }
  39. afe_config->afe_perferred_core = 1;
  40. afe_config->afe_perferred_priority = 1;
  41. afe_config->agc_init = false;
  42. afe_config->memory_alloc_mode = AFE_MEMORY_ALLOC_MORE_PSRAM;
  43. #ifdef CONFIG_USE_DEVICE_AEC
  44. afe_config->aec_init = true;
  45. afe_config->vad_init = false;
  46. #else
  47. afe_config->aec_init = false;
  48. afe_config->vad_init = true;
  49. #endif
  50. afe_iface_ = esp_afe_handle_from_config(afe_config);
  51. afe_data_ = afe_iface_->create_from_config(afe_config);
  52. xTaskCreate([](void* arg) {
  53. auto this_ = (AfeAudioProcessor*)arg;
  54. this_->AudioProcessorTask();
  55. vTaskDelete(NULL);
  56. }, "audio_communication", 4096, this, 3, NULL);
  57. }
  58. AfeAudioProcessor::~AfeAudioProcessor() {
  59. if (afe_data_ != nullptr) {
  60. afe_iface_->destroy(afe_data_);
  61. }
  62. vEventGroupDelete(event_group_);
  63. }
  64. size_t AfeAudioProcessor::GetFeedSize() {
  65. if (afe_data_ == nullptr) {
  66. return 0;
  67. }
  68. return afe_iface_->get_feed_chunksize(afe_data_) * codec_->input_channels();
  69. }
  70. void AfeAudioProcessor::Feed(std::vector<int16_t>&& data) {
  71. if (afe_data_ == nullptr) {
  72. return;
  73. }
  74. afe_iface_->feed(afe_data_, data.data());
  75. }
  76. void AfeAudioProcessor::Start() {
  77. xEventGroupSetBits(event_group_, PROCESSOR_RUNNING);
  78. }
  79. void AfeAudioProcessor::Stop() {
  80. xEventGroupClearBits(event_group_, PROCESSOR_RUNNING);
  81. if (afe_data_ != nullptr) {
  82. afe_iface_->reset_buffer(afe_data_);
  83. }
  84. }
  85. bool AfeAudioProcessor::IsRunning() {
  86. return xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING;
  87. }
  88. void AfeAudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) {
  89. output_callback_ = callback;
  90. }
  91. void AfeAudioProcessor::OnVadStateChange(std::function<void(bool speaking)> callback) {
  92. vad_state_change_callback_ = callback;
  93. }
  94. void AfeAudioProcessor::AudioProcessorTask() {
  95. auto fetch_size = afe_iface_->get_fetch_chunksize(afe_data_);
  96. auto feed_size = afe_iface_->get_feed_chunksize(afe_data_);
  97. ESP_LOGI(TAG, "Audio communication task started, feed size: %d fetch size: %d",
  98. feed_size, fetch_size);
  99. while (true) {
  100. xEventGroupWaitBits(event_group_, PROCESSOR_RUNNING, pdFALSE, pdTRUE, portMAX_DELAY);
  101. auto res = afe_iface_->fetch_with_delay(afe_data_, portMAX_DELAY);
  102. if ((xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING) == 0) {
  103. continue;
  104. }
  105. if (res == nullptr || res->ret_value == ESP_FAIL) {
  106. if (res != nullptr) {
  107. ESP_LOGI(TAG, "Error code: %d", res->ret_value);
  108. }
  109. continue;
  110. }
  111. // VAD state change
  112. if (vad_state_change_callback_) {
  113. if (res->vad_state == VAD_SPEECH && !is_speaking_) {
  114. is_speaking_ = true;
  115. vad_state_change_callback_(true);
  116. } else if (res->vad_state == VAD_SILENCE && is_speaking_) {
  117. is_speaking_ = false;
  118. vad_state_change_callback_(false);
  119. }
  120. }
  121. if (output_callback_) {
  122. size_t samples = res->data_size / sizeof(int16_t);
  123. // Add data to buffer
  124. output_buffer_.insert(output_buffer_.end(), res->data, res->data + samples);
  125. // Output complete frames when buffer has enough data
  126. while (output_buffer_.size() >= frame_samples_) {
  127. if (output_buffer_.size() == frame_samples_) {
  128. // If buffer size equals frame size, move the entire buffer
  129. output_callback_(std::move(output_buffer_));
  130. output_buffer_.clear();
  131. output_buffer_.reserve(frame_samples_);
  132. } else {
  133. // If buffer size exceeds frame size, copy one frame and remove it
  134. output_callback_(std::vector<int16_t>(output_buffer_.begin(), output_buffer_.begin() + frame_samples_));
  135. output_buffer_.erase(output_buffer_.begin(), output_buffer_.begin() + frame_samples_);
  136. }
  137. }
  138. }
  139. }
  140. }
  141. void AfeAudioProcessor::EnableDeviceAec(bool enable) {
  142. if (enable) {
  143. #if CONFIG_USE_DEVICE_AEC
  144. afe_iface_->disable_vad(afe_data_);
  145. afe_iface_->enable_aec(afe_data_);
  146. #else
  147. ESP_LOGE(TAG, "Device AEC is not supported");
  148. #endif
  149. } else {
  150. afe_iface_->disable_aec(afe_data_);
  151. afe_iface_->enable_vad(afe_data_);
  152. }
  153. }