audio_processor.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "audio_processor.h"
  2. #include <esp_log.h>
  3. #define PROCESSOR_RUNNING 0x01
  4. static const char* TAG = "AudioProcessor";
  5. AudioProcessor::AudioProcessor()
  6. : afe_data_(nullptr) {
  7. event_group_ = xEventGroupCreate();
  8. }
  9. void AudioProcessor::Initialize(int channels, bool reference) {
  10. channels_ = channels;
  11. reference_ = reference;
  12. int ref_num = reference_ ? 1 : 0;
  13. std::string input_format;
  14. for (int i = 0; i < channels_ - ref_num; i++) {
  15. input_format.push_back('M');
  16. }
  17. for (int i = 0; i < ref_num; i++) {
  18. input_format.push_back('R');
  19. }
  20. afe_config_t* afe_config = afe_config_init(input_format.c_str(), NULL, AFE_TYPE_VC, AFE_MODE_HIGH_PERF);
  21. afe_config->aec_init = false;
  22. afe_config->aec_mode = AEC_MODE_VOIP_HIGH_PERF;
  23. afe_config->ns_init = true;
  24. afe_config->vad_init = true;
  25. afe_config->vad_mode = VAD_MODE_0;
  26. afe_config->vad_min_noise_ms = 100;
  27. afe_config->afe_perferred_core = 1;
  28. afe_config->afe_perferred_priority = 1;
  29. afe_config->agc_init = true;
  30. afe_config->agc_mode = AFE_AGC_MODE_WEBRTC;
  31. afe_config->agc_compression_gain_db = 10;
  32. afe_config->memory_alloc_mode = AFE_MEMORY_ALLOC_MORE_PSRAM;
  33. afe_iface_ = esp_afe_handle_from_config(afe_config);
  34. afe_data_ = afe_iface_->create_from_config(afe_config);
  35. xTaskCreate([](void* arg) {
  36. auto this_ = (AudioProcessor*)arg;
  37. this_->AudioProcessorTask();
  38. vTaskDelete(NULL);
  39. }, "audio_communication", 4096, this, 3, NULL);
  40. }
  41. AudioProcessor::~AudioProcessor() {
  42. if (afe_data_ != nullptr) {
  43. afe_iface_->destroy(afe_data_);
  44. }
  45. vEventGroupDelete(event_group_);
  46. }
  47. void AudioProcessor::Input(const std::vector<int16_t>& data) {
  48. input_buffer_.insert(input_buffer_.end(), data.begin(), data.end());
  49. auto feed_size = afe_iface_->get_feed_chunksize(afe_data_) * channels_;
  50. while (input_buffer_.size() >= feed_size) {
  51. auto chunk = input_buffer_.data();
  52. afe_iface_->feed(afe_data_, chunk);
  53. input_buffer_.erase(input_buffer_.begin(), input_buffer_.begin() + feed_size);
  54. }
  55. }
  56. void AudioProcessor::Start() {
  57. xEventGroupSetBits(event_group_, PROCESSOR_RUNNING);
  58. }
  59. void AudioProcessor::Stop() {
  60. xEventGroupClearBits(event_group_, PROCESSOR_RUNNING);
  61. afe_iface_->reset_buffer(afe_data_);
  62. }
  63. bool AudioProcessor::IsRunning() {
  64. return xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING;
  65. }
  66. void AudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) {
  67. output_callback_ = callback;
  68. }
  69. void AudioProcessor::OnVadStateChange(std::function<void(bool speaking)> callback) {
  70. vad_state_change_callback_ = callback;
  71. }
  72. void AudioProcessor::AudioProcessorTask() {
  73. auto fetch_size = afe_iface_->get_fetch_chunksize(afe_data_);
  74. auto feed_size = afe_iface_->get_feed_chunksize(afe_data_);
  75. ESP_LOGI(TAG, "Audio communication task started, feed size: %d fetch size: %d",
  76. feed_size, fetch_size);
  77. while (true) {
  78. xEventGroupWaitBits(event_group_, PROCESSOR_RUNNING, pdFALSE, pdTRUE, portMAX_DELAY);
  79. auto res = afe_iface_->fetch_with_delay(afe_data_, portMAX_DELAY);
  80. if ((xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING) == 0) {
  81. continue;
  82. }
  83. if (res == nullptr || res->ret_value == ESP_FAIL) {
  84. if (res != nullptr) {
  85. ESP_LOGI(TAG, "Error code: %d", res->ret_value);
  86. }
  87. continue;
  88. }
  89. // VAD state change
  90. if (vad_state_change_callback_) {
  91. if (res->vad_state == VAD_SPEECH && !is_speaking_) {
  92. is_speaking_ = true;
  93. vad_state_change_callback_(true);
  94. } else if (res->vad_state == VAD_SILENCE && is_speaking_) {
  95. is_speaking_ = false;
  96. vad_state_change_callback_(false);
  97. }
  98. }
  99. if (output_callback_) {
  100. output_callback_(std::vector<int16_t>(res->data, res->data + res->data_size / sizeof(int16_t)));
  101. }
  102. }
  103. }