wake_word_detect.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #ifndef WAKE_WORD_DETECT_H
  2. #define WAKE_WORD_DETECT_H
  3. #include <freertos/FreeRTOS.h>
  4. #include <freertos/task.h>
  5. #include <freertos/event_groups.h>
  6. #include <esp_afe_sr_models.h>
  7. #include <esp_nsn_models.h>
  8. #include <list>
  9. #include <string>
  10. #include <vector>
  11. #include <functional>
  12. #include <mutex>
  13. #include <condition_variable>
  14. class WakeWordDetect {
  15. public:
  16. WakeWordDetect();
  17. ~WakeWordDetect();
  18. void Initialize(int channels, bool reference);
  19. void Feed(const std::vector<int16_t>& data);
  20. void OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback);
  21. void OnVadStateChange(std::function<void(bool speaking)> callback);
  22. void StartDetection();
  23. void StopDetection();
  24. bool IsDetectionRunning();
  25. void EncodeWakeWordData();
  26. bool GetWakeWordOpus(std::vector<uint8_t>& opus);
  27. const std::string& GetLastDetectedWakeWord() const { return last_detected_wake_word_; }
  28. private:
  29. esp_afe_sr_data_t* afe_detection_data_ = nullptr;
  30. char* wakenet_model_ = NULL;
  31. std::vector<std::string> wake_words_;
  32. std::vector<int16_t> input_buffer_;
  33. EventGroupHandle_t event_group_;
  34. std::function<void(const std::string& wake_word)> wake_word_detected_callback_;
  35. std::function<void(bool speaking)> vad_state_change_callback_;
  36. bool is_speaking_ = false;
  37. int channels_;
  38. bool reference_;
  39. std::string last_detected_wake_word_;
  40. TaskHandle_t wake_word_encode_task_ = nullptr;
  41. StaticTask_t wake_word_encode_task_buffer_;
  42. StackType_t* wake_word_encode_task_stack_ = nullptr;
  43. std::list<std::vector<int16_t>> wake_word_pcm_;
  44. std::list<std::vector<uint8_t>> wake_word_opus_;
  45. std::mutex wake_word_mutex_;
  46. std::condition_variable wake_word_cv_;
  47. void StoreWakeWordData(uint16_t* data, size_t size);
  48. void AudioDetectionTask();
  49. };
  50. #endif