wangzhibo
2026-10-08 220e4fab27b7f419b3e89e2f2d28ae789aac09d7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#ifndef ESP_WAKE_WORD_H
#define ESP_WAKE_WORD_H
 
#include <esp_wn_iface.h>
#include <esp_wn_models.h>
#include <model_path.h>
 
#include <string>
#include <vector>
#include <functional>
#include <atomic>
#include <mutex>
 
#include "audio_codec.h"
#include "wake_word.h"
 
class EspWakeWord : public WakeWord {
public:
    EspWakeWord();
    ~EspWakeWord();
 
    bool Initialize(AudioCodec* codec, srmodel_list_t* models_list);
    void Feed(const std::vector<int16_t>& data);
    void OnWakeWordDetected(std::function<void(const std::string& wake_word)> callback);
    void OnOfflineCommandDetected(std::function<void(const std::string& command)> callback);
    void Start();
    void Stop();
    size_t GetFeedSize();
    void EncodeWakeWordData();
    bool GetWakeWordOpus(std::vector<uint8_t>& opus);
    const std::string& GetLastDetectedWakeWord() const { return last_detected_wake_word_; }
 
private:
    esp_wn_iface_t *wakenet_iface_ = nullptr;
    model_iface_data_t *wakenet_data_ = nullptr;
    srmodel_list_t *wakenet_model_ = nullptr;
    AudioCodec* codec_ = nullptr;
    std::atomic<bool> running_ = false;
 
    std::function<void(const std::string& wake_word)> wake_word_detected_callback_;
    std::function<void(const std::string& command)> offline_command_detected_callback_;
    std::string last_detected_wake_word_;
    std::vector<int16_t> input_buffer_;
    std::mutex input_buffer_mutex_;
};
 
#endif