custom_wake_word.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. #include "custom_wake_word.h"
  2. #include "application.h"
  3. #include <esp_log.h>
  4. #include <model_path.h>
  5. #include <arpa/inet.h>
  6. #include "esp_wn_iface.h"
  7. #include "esp_wn_models.h"
  8. #include "esp_afe_sr_iface.h"
  9. #include "esp_afe_sr_models.h"
  10. #include "esp_mn_iface.h"
  11. #include "esp_mn_models.h"
  12. #include "esp_mn_speech_commands.h"
  13. #include <sstream>
  14. #define DETECTION_RUNNING_EVENT 1
  15. #define TAG "CustomWakeWord"
  16. CustomWakeWord::CustomWakeWord()
  17. : afe_data_(nullptr),
  18. wake_word_pcm_(),
  19. wake_word_opus_() {
  20. event_group_ = xEventGroupCreate();
  21. }
  22. CustomWakeWord::~CustomWakeWord() {
  23. if (afe_data_ != nullptr) {
  24. afe_iface_->destroy(afe_data_);
  25. }
  26. // 清理 multinet 资源
  27. if (multinet_model_data_ != nullptr && multinet_ != nullptr) {
  28. multinet_->destroy(multinet_model_data_);
  29. multinet_model_data_ = nullptr;
  30. }
  31. if (wake_word_encode_task_stack_ != nullptr) {
  32. heap_caps_free(wake_word_encode_task_stack_);
  33. }
  34. vEventGroupDelete(event_group_);
  35. }
  36. bool CustomWakeWord::Initialize(AudioCodec* codec) {
  37. codec_ = codec;
  38. models = esp_srmodel_init("model");
  39. if (models == nullptr || models->num == -1) {
  40. ESP_LOGE(TAG, "Failed to initialize wakenet model");
  41. return false;
  42. }
  43. // 初始化 multinet (命令词识别)
  44. mn_name_ = esp_srmodel_filter(models, ESP_MN_PREFIX, ESP_MN_CHINESE);
  45. if (mn_name_ == nullptr) {
  46. ESP_LOGE(TAG, "Failed to initialize multinet, mn_name is nullptr");
  47. ESP_LOGI(TAG, "Please refer to https://pcn7cs20v8cr.feishu.cn/wiki/CpQjwQsCJiQSWSkYEvrcxcbVnwh to add custom wake word");
  48. return false;
  49. }
  50. ESP_LOGI(TAG, "multinet:%s", mn_name_);
  51. multinet_ = esp_mn_handle_from_name(mn_name_);
  52. multinet_model_data_ = multinet_->create(mn_name_, 2000); // 2秒超时
  53. multinet_->set_det_threshold(multinet_model_data_, 0.5);
  54. esp_mn_commands_clear();
  55. esp_mn_commands_add(1, CONFIG_CUSTOM_WAKE_WORD); // 添加自定义唤醒词作为命令词
  56. esp_mn_commands_update();
  57. // 打印所有的命令词
  58. multinet_->print_active_speech_commands(multinet_model_data_);
  59. ESP_LOGI(TAG, "Custom wake word: %s", CONFIG_CUSTOM_WAKE_WORD);
  60. // 初始化 afe
  61. int ref_num = codec_->input_reference() ? 1 : 0;
  62. std::string input_format;
  63. for (int i = 0; i < codec_->input_channels() - ref_num; i++) {
  64. input_format.push_back('M');
  65. }
  66. for (int i = 0; i < ref_num; i++) {
  67. input_format.push_back('R');
  68. }
  69. afe_config_t* afe_config = afe_config_init(input_format.c_str(), models, AFE_TYPE_SR, AFE_MODE_HIGH_PERF);
  70. afe_config->aec_init = codec_->input_reference();
  71. afe_config->aec_mode = AEC_MODE_SR_HIGH_PERF;
  72. afe_config->afe_perferred_core = 1;
  73. afe_config->afe_perferred_priority = 1;
  74. afe_config->memory_alloc_mode = AFE_MEMORY_ALLOC_MORE_PSRAM;
  75. afe_iface_ = esp_afe_handle_from_config(afe_config);
  76. afe_data_ = afe_iface_->create_from_config(afe_config);
  77. xTaskCreate([](void* arg) {
  78. auto this_ = (CustomWakeWord*)arg;
  79. this_->AudioDetectionTask();
  80. vTaskDelete(NULL);
  81. }, "audio_detection", 16384, this, 3, nullptr);
  82. return true;
  83. }
  84. void CustomWakeWord::OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback) {
  85. wake_word_detected_callback_ = callback;
  86. }
  87. void CustomWakeWord::Start() {
  88. xEventGroupSetBits(event_group_, DETECTION_RUNNING_EVENT);
  89. }
  90. void CustomWakeWord::Stop() {
  91. xEventGroupClearBits(event_group_, DETECTION_RUNNING_EVENT);
  92. if (afe_data_ != nullptr) {
  93. afe_iface_->reset_buffer(afe_data_);
  94. }
  95. }
  96. void CustomWakeWord::Feed(const std::vector<int16_t>& data) {
  97. if (afe_data_ == nullptr) {
  98. return;
  99. }
  100. afe_iface_->feed(afe_data_, data.data());
  101. }
  102. size_t CustomWakeWord::GetFeedSize() {
  103. if (afe_data_ == nullptr) {
  104. return 0;
  105. }
  106. return afe_iface_->get_feed_chunksize(afe_data_) * codec_->input_channels();
  107. }
  108. void CustomWakeWord::AudioDetectionTask() {
  109. auto fetch_size = afe_iface_->get_fetch_chunksize(afe_data_);
  110. auto feed_size = afe_iface_->get_feed_chunksize(afe_data_);
  111. // 检查 multinet 是否已正确初始化
  112. if (multinet_ == nullptr || multinet_model_data_ == nullptr) {
  113. ESP_LOGE(TAG, "Multinet not initialized properly");
  114. return;
  115. }
  116. int mu_chunksize = multinet_->get_samp_chunksize(multinet_model_data_);
  117. assert(mu_chunksize == feed_size);
  118. ESP_LOGI(TAG, "Audio detection task started, feed size: %d fetch size: %d", feed_size, fetch_size);
  119. // 禁用wakenet,直接使用multinet检测自定义唤醒词
  120. afe_iface_->disable_wakenet(afe_data_);
  121. while (true) {
  122. xEventGroupWaitBits(event_group_, DETECTION_RUNNING_EVENT, pdFALSE, pdTRUE, portMAX_DELAY);
  123. auto res = afe_iface_->fetch_with_delay(afe_data_, portMAX_DELAY);
  124. if (res == nullptr || res->ret_value == ESP_FAIL) {
  125. ESP_LOGW(TAG, "Fetch failed, continue");
  126. continue;
  127. }
  128. // 存储音频数据用于语音识别
  129. StoreWakeWordData(res->data, res->data_size / sizeof(int16_t));
  130. // 直接使用multinet检测自定义唤醒词
  131. esp_mn_state_t mn_state = multinet_->detect(multinet_model_data_, res->data);
  132. if (mn_state == ESP_MN_STATE_DETECTING) {
  133. // 仍在检测中,继续
  134. continue;
  135. } else if (mn_state == ESP_MN_STATE_DETECTED) {
  136. // 检测到自定义唤醒词
  137. esp_mn_results_t *mn_result = multinet_->get_results(multinet_model_data_);
  138. ESP_LOGI(TAG, "Custom wake word detected: command_id=%d, string=%s, prob=%f",
  139. mn_result->command_id[0], mn_result->string, mn_result->prob[0]);
  140. if (mn_result->command_id[0] == 1) { // 自定义唤醒词
  141. ESP_LOGI(TAG, "Custom wake word '%s' detected successfully!", CONFIG_CUSTOM_WAKE_WORD);
  142. // 停止检测
  143. Stop();
  144. last_detected_wake_word_ = CONFIG_CUSTOM_WAKE_WORD_DISPLAY;
  145. // 调用回调
  146. if (wake_word_detected_callback_) {
  147. wake_word_detected_callback_(last_detected_wake_word_);
  148. }
  149. // 清理multinet状态,准备下次检测
  150. multinet_->clean(multinet_model_data_);
  151. ESP_LOGI(TAG, "Ready for next detection");
  152. }
  153. } else if (mn_state == ESP_MN_STATE_TIMEOUT) {
  154. // 超时,清理状态继续检测
  155. ESP_LOGD(TAG, "Command word detection timeout, cleaning state");
  156. multinet_->clean(multinet_model_data_);
  157. continue;
  158. }
  159. }
  160. ESP_LOGI(TAG, "Audio detection task ended");
  161. }
  162. void CustomWakeWord::StoreWakeWordData(const int16_t* data, size_t samples) {
  163. // store audio data to wake_word_pcm_
  164. wake_word_pcm_.emplace_back(std::vector<int16_t>(data, data + samples));
  165. // keep about 2 seconds of data, detect duration is 30ms (sample_rate == 16000, chunksize == 512)
  166. while (wake_word_pcm_.size() > 2000 / 30) {
  167. wake_word_pcm_.pop_front();
  168. }
  169. }
  170. void CustomWakeWord::EncodeWakeWordData() {
  171. wake_word_opus_.clear();
  172. if (wake_word_encode_task_stack_ == nullptr) {
  173. wake_word_encode_task_stack_ = (StackType_t*)heap_caps_malloc(4096 * 8, MALLOC_CAP_SPIRAM);
  174. }
  175. wake_word_encode_task_ = xTaskCreateStatic([](void* arg) {
  176. auto this_ = (CustomWakeWord*)arg;
  177. {
  178. auto start_time = esp_timer_get_time();
  179. auto encoder = std::make_unique<OpusEncoderWrapper>(16000, 1, OPUS_FRAME_DURATION_MS);
  180. encoder->SetComplexity(0); // 0 is the fastest
  181. int packets = 0;
  182. for (auto& pcm: this_->wake_word_pcm_) {
  183. encoder->Encode(std::move(pcm), [this_](std::vector<uint8_t>&& opus) {
  184. std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
  185. this_->wake_word_opus_.emplace_back(std::move(opus));
  186. this_->wake_word_cv_.notify_all();
  187. });
  188. packets++;
  189. }
  190. this_->wake_word_pcm_.clear();
  191. auto end_time = esp_timer_get_time();
  192. ESP_LOGI(TAG, "Encode wake word opus %d packets in %ld ms", packets, (long)((end_time - start_time) / 1000));
  193. std::lock_guard<std::mutex> lock(this_->wake_word_mutex_);
  194. this_->wake_word_opus_.push_back(std::vector<uint8_t>());
  195. this_->wake_word_cv_.notify_all();
  196. }
  197. vTaskDelete(NULL);
  198. }, "encode_detect_packets", 4096 * 8, this, 2, wake_word_encode_task_stack_, &wake_word_encode_task_buffer_);
  199. }
  200. bool CustomWakeWord::GetWakeWordOpus(std::vector<uint8_t>& opus) {
  201. std::unique_lock<std::mutex> lock(wake_word_mutex_);
  202. wake_word_cv_.wait(lock, [this]() {
  203. return !wake_word_opus_.empty();
  204. });
  205. opus.swap(wake_word_opus_.front());
  206. wake_word_opus_.pop_front();
  207. return !opus.empty();
  208. }