audio_processor.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_communication_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. afe_config_t afe_config = {
  14. .aec_init = true,
  15. .se_init = true,
  16. .vad_init = false,
  17. .wakenet_init = false,
  18. .voice_communication_init = true,
  19. .voice_communication_agc_init = true,
  20. .voice_communication_agc_gain = 10,
  21. .vad_mode = VAD_MODE_3,
  22. .wakenet_model_name = NULL,
  23. .wakenet_model_name_2 = NULL,
  24. .wakenet_mode = DET_MODE_90,
  25. .afe_mode = SR_MODE_HIGH_PERF,
  26. .afe_perferred_core = 1,
  27. .afe_perferred_priority = 1,
  28. .afe_ringbuf_size = 50,
  29. .memory_alloc_mode = AFE_MEMORY_ALLOC_MORE_PSRAM,
  30. .afe_linear_gain = 1.0,
  31. .agc_mode = AFE_MN_PEAK_AGC_MODE_2,
  32. .pcm_config = {
  33. .total_ch_num = channels_,
  34. .mic_num = channels_ - ref_num,
  35. .ref_num = ref_num,
  36. .sample_rate = 16000,
  37. },
  38. .debug_init = false,
  39. .debug_hook = {{ AFE_DEBUG_HOOK_MASE_TASK_IN, NULL }, { AFE_DEBUG_HOOK_FETCH_TASK_IN, NULL }},
  40. .afe_ns_mode = NS_MODE_SSP,
  41. .afe_ns_model_name = NULL,
  42. .fixed_first_channel = true,
  43. };
  44. afe_communication_data_ = esp_afe_vc_v1.create_from_config(&afe_config);
  45. xTaskCreate([](void* arg) {
  46. auto this_ = (AudioProcessor*)arg;
  47. this_->AudioProcessorTask();
  48. vTaskDelete(NULL);
  49. }, "audio_communication", 4096 * 2, this, 2, NULL);
  50. }
  51. AudioProcessor::~AudioProcessor() {
  52. if (afe_communication_data_ != nullptr) {
  53. esp_afe_vc_v1.destroy(afe_communication_data_);
  54. }
  55. vEventGroupDelete(event_group_);
  56. }
  57. void AudioProcessor::Input(const std::vector<int16_t>& data) {
  58. input_buffer_.insert(input_buffer_.end(), data.begin(), data.end());
  59. auto feed_size = esp_afe_vc_v1.get_feed_chunksize(afe_communication_data_) * channels_;
  60. while (input_buffer_.size() >= feed_size) {
  61. auto chunk = input_buffer_.data();
  62. esp_afe_vc_v1.feed(afe_communication_data_, chunk);
  63. input_buffer_.erase(input_buffer_.begin(), input_buffer_.begin() + feed_size);
  64. }
  65. }
  66. void AudioProcessor::Start() {
  67. xEventGroupSetBits(event_group_, PROCESSOR_RUNNING);
  68. }
  69. void AudioProcessor::Stop() {
  70. xEventGroupClearBits(event_group_, PROCESSOR_RUNNING);
  71. }
  72. bool AudioProcessor::IsRunning() {
  73. return xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING;
  74. }
  75. void AudioProcessor::OnOutput(std::function<void(std::vector<int16_t>&& data)> callback) {
  76. output_callback_ = callback;
  77. }
  78. void AudioProcessor::AudioProcessorTask() {
  79. auto fetch_size = esp_afe_sr_v1.get_fetch_chunksize(afe_communication_data_);
  80. auto feed_size = esp_afe_sr_v1.get_feed_chunksize(afe_communication_data_);
  81. ESP_LOGI(TAG, "Audio communication task started, feed size: %d fetch size: %d",
  82. feed_size, fetch_size);
  83. while (true) {
  84. xEventGroupWaitBits(event_group_, PROCESSOR_RUNNING, pdFALSE, pdTRUE, portMAX_DELAY);
  85. auto res = esp_afe_vc_v1.fetch(afe_communication_data_);
  86. if ((xEventGroupGetBits(event_group_) & PROCESSOR_RUNNING) == 0) {
  87. continue;
  88. }
  89. if (res == nullptr || res->ret_value == ESP_FAIL) {
  90. if (res != nullptr) {
  91. ESP_LOGI(TAG, "Error code: %d", res->ret_value);
  92. }
  93. continue;
  94. }
  95. if (output_callback_) {
  96. output_callback_(std::vector<int16_t>(res->data, res->data + res->data_size / sizeof(int16_t)));
  97. }
  98. }
  99. }