afe_wake_word.h 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #ifndef AFE_WAKE_WORD_H
  2. #define AFE_WAKE_WORD_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. #include "audio_codec.h"
  15. #include "wake_word.h"
  16. class AfeWakeWord : public WakeWord {
  17. public:
  18. AfeWakeWord();
  19. ~AfeWakeWord();
  20. bool Initialize(AudioCodec* codec);
  21. void Feed(const std::vector<int16_t>& data);
  22. void OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback);
  23. void Start();
  24. void Stop();
  25. size_t GetFeedSize();
  26. void EncodeWakeWordData();
  27. bool GetWakeWordOpus(std::vector<uint8_t>& opus);
  28. const std::string& GetLastDetectedWakeWord() const { return last_detected_wake_word_; }
  29. private:
  30. esp_afe_sr_iface_t* afe_iface_ = nullptr;
  31. esp_afe_sr_data_t* afe_data_ = nullptr;
  32. char* wakenet_model_ = NULL;
  33. std::vector<std::string> wake_words_;
  34. EventGroupHandle_t event_group_;
  35. std::function<void(const std::string& wake_word)> wake_word_detected_callback_;
  36. AudioCodec* codec_ = nullptr;
  37. std::string last_detected_wake_word_;
  38. TaskHandle_t wake_word_encode_task_ = nullptr;
  39. StaticTask_t wake_word_encode_task_buffer_;
  40. StackType_t* wake_word_encode_task_stack_ = nullptr;
  41. std::list<std::vector<int16_t>> wake_word_pcm_;
  42. std::list<std::vector<uint8_t>> wake_word_opus_;
  43. std::mutex wake_word_mutex_;
  44. std::condition_variable wake_word_cv_;
  45. void StoreWakeWordData(const int16_t* data, size_t size);
  46. void AudioDetectionTask();
  47. };
  48. #endif