ESPHome 2025.5.0
Loading...
Searching...
No Matches
micro_wake_word.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP_IDF
4
6#include "streaming_model.h"
7
9
13
14#include <freertos/event_groups.h>
15
16#include <frontend.h>
17#include <frontend_util.h>
18
19namespace esphome {
20namespace micro_wake_word {
21
28
29class MicroWakeWord : public Component {
30 public:
31 void setup() override;
32 void loop() override;
33 float get_setup_priority() const override;
34 void dump_config() override;
35
36 void start();
37 void stop();
38
39 bool is_running() const { return this->state_ != State::STOPPED; }
40
41 void set_features_step_size(uint8_t step_size) { this->features_step_size_ = step_size; }
42
44 this->microphone_source_ = microphone_source;
45 }
46
47 void set_stop_after_detection(bool stop_after_detection) { this->stop_after_detection_ = stop_after_detection; }
48
50
52
53#ifdef USE_MICRO_WAKE_WORD_VAD
54 void add_vad_model(const uint8_t *model_start, uint8_t probability_cutoff, size_t sliding_window_size,
55 size_t tensor_arena_size);
56
57 // Intended for the voice assistant component to fetch VAD status
58 bool get_vad_state() { return this->vad_state_; }
59#endif
60
61 // Intended for the voice assistant component to access which wake words are available
62 // Since these are pointers to the WakeWordModel objects, the voice assistant component can enable or disable them
63 std::vector<WakeWordModel *> get_wake_words();
64
65 protected:
69
70 std::weak_ptr<RingBuffer> ring_buffer_;
71 std::vector<WakeWordModel *> wake_word_models_;
72
73#ifdef USE_MICRO_WAKE_WORD_VAD
74 std::unique_ptr<VADModel> vad_model_;
75 bool vad_state_{false};
76#endif
77
78 bool pending_start_{false};
79 bool pending_stop_{false};
80
82
84
85 // Audio frontend handles generating spectrogram features
86 struct FrontendConfig frontend_config_;
87 struct FrontendState frontend_state_;
88
89 // Handles managing the stop/state of the inference task
90 EventGroupHandle_t event_group_;
91
92 // Used to send messages about the models' states to the main loop
93 QueueHandle_t detection_queue_;
94
95 static void inference_task(void *params);
96 TaskHandle_t inference_task_handle_{nullptr};
97
99 void suspend_task_();
101 void resume_task_();
102
103 void set_state_(State state);
104
110 size_t generate_features_(int16_t *audio_buffer, size_t samples_available,
111 int8_t features_buffer[PREPROCESSOR_FEATURE_SIZE]);
112
116
118 void unload_models_();
119
123 bool update_model_probabilities_(const int8_t audio_features[PREPROCESSOR_FEATURE_SIZE]);
124};
125
126} // namespace micro_wake_word
127} // namespace esphome
128
129#endif // USE_ESP_IDF
void resume_task_()
Resumes the inference task.
microphone::MicrophoneSource * microphone_source_
void process_probabilities_()
Processes any new probabilities for each model.
Trigger< std::string > * get_wake_word_detected_trigger() const
void set_stop_after_detection(bool stop_after_detection)
std::weak_ptr< RingBuffer > ring_buffer_
Trigger< std::string > * wake_word_detected_trigger_
std::vector< WakeWordModel * > wake_word_models_
void suspend_task_()
Suspends the inference task.
void add_wake_word_model(WakeWordModel *model)
bool update_model_probabilities_(const int8_t audio_features[PREPROCESSOR_FEATURE_SIZE])
Runs an inference with each model using the new spectrogram features.
size_t generate_features_(int16_t *audio_buffer, size_t samples_available, int8_t features_buffer[PREPROCESSOR_FEATURE_SIZE])
Generates spectrogram features from an input buffer of audio samples.
std::unique_ptr< VADModel > vad_model_
void add_vad_model(const uint8_t *model_start, uint8_t probability_cutoff, size_t sliding_window_size, size_t tensor_arena_size)
void set_features_step_size(uint8_t step_size)
void set_microphone_source(microphone::MicrophoneSource *microphone_source)
void unload_models_()
Deletes each model's TFLite interpreters and frees tensor arena memory.
std::vector< WakeWordModel * > get_wake_words()
bool state
Definition fan.h:0
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7