2024-10-03 06:39:22 +08:00
|
|
|
#ifndef WAKE_WORD_DETECT_H
|
|
|
|
|
#define WAKE_WORD_DETECT_H
|
|
|
|
|
|
|
|
|
|
#include <freertos/FreeRTOS.h>
|
|
|
|
|
#include <freertos/task.h>
|
|
|
|
|
#include <freertos/event_groups.h>
|
|
|
|
|
|
2024-11-05 20:15:00 +08:00
|
|
|
#include <esp_afe_sr_models.h>
|
|
|
|
|
#include <esp_nsn_models.h>
|
|
|
|
|
|
2024-10-03 06:39:22 +08:00
|
|
|
#include <list>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <functional>
|
2024-11-14 23:15:43 +08:00
|
|
|
#include <mutex>
|
|
|
|
|
#include <condition_variable>
|
2024-10-03 06:39:22 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class WakeWordDetect {
|
|
|
|
|
public:
|
|
|
|
|
WakeWordDetect();
|
|
|
|
|
~WakeWordDetect();
|
|
|
|
|
|
2024-10-24 09:53:08 +08:00
|
|
|
void Initialize(int channels, bool reference);
|
|
|
|
|
void Feed(std::vector<int16_t>& data);
|
2024-11-25 00:59:03 +08:00
|
|
|
void OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback);
|
2024-10-03 06:39:22 +08:00
|
|
|
void OnVadStateChange(std::function<void(bool speaking)> callback);
|
|
|
|
|
void StartDetection();
|
|
|
|
|
void StopDetection();
|
|
|
|
|
bool IsDetectionRunning();
|
|
|
|
|
void EncodeWakeWordData();
|
2024-11-14 23:15:43 +08:00
|
|
|
bool GetWakeWordOpus(std::string& opus);
|
2024-11-25 00:59:03 +08:00
|
|
|
const std::string& GetLastDetectedWakeWord() const { return last_detected_wake_word_; }
|
2024-10-03 06:39:22 +08:00
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
esp_afe_sr_data_t* afe_detection_data_ = nullptr;
|
|
|
|
|
char* wakenet_model_ = NULL;
|
2024-11-25 00:59:03 +08:00
|
|
|
std::vector<std::string> wake_words_;
|
2024-10-03 06:39:22 +08:00
|
|
|
std::vector<int16_t> input_buffer_;
|
|
|
|
|
EventGroupHandle_t event_group_;
|
2024-11-25 00:59:03 +08:00
|
|
|
std::function<void(const std::string& wake_word)> wake_word_detected_callback_;
|
2024-10-03 06:39:22 +08:00
|
|
|
std::function<void(bool speaking)> vad_state_change_callback_;
|
|
|
|
|
bool is_speaking_ = false;
|
2024-10-24 09:53:08 +08:00
|
|
|
int channels_;
|
|
|
|
|
bool reference_;
|
2024-11-25 00:59:03 +08:00
|
|
|
std::string last_detected_wake_word_;
|
2024-10-03 06:39:22 +08:00
|
|
|
|
|
|
|
|
TaskHandle_t wake_word_encode_task_ = nullptr;
|
|
|
|
|
StaticTask_t wake_word_encode_task_buffer_;
|
|
|
|
|
StackType_t* wake_word_encode_task_stack_ = nullptr;
|
|
|
|
|
std::list<std::vector<int16_t>> wake_word_pcm_;
|
2024-11-14 23:15:43 +08:00
|
|
|
std::list<std::string> wake_word_opus_;
|
|
|
|
|
std::mutex wake_word_mutex_;
|
|
|
|
|
std::condition_variable wake_word_cv_;
|
2024-10-03 06:39:22 +08:00
|
|
|
|
|
|
|
|
void StoreWakeWordData(uint16_t* data, size_t size);
|
|
|
|
|
void AudioDetectionTask();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
#endif
|