ESPHome  2024.4.2
Public Member Functions | Protected Member Functions | Protected Attributes
esphome::micro_wake_word::MicroWakeWord Class Reference

#include <micro_wake_word.h>

Inheritance diagram for esphome::micro_wake_word::MicroWakeWord:
esphome::Component

Public Member Functions

void setup () override
 
void loop () override
 
float get_setup_priority () const override
 
void dump_config () override
 
void start ()
 
void stop ()
 
bool is_running () const
 
bool initialize_models ()
 
std::string get_wake_word ()
 
void set_probability_cutoff (float probability_cutoff)
 
void set_sliding_window_average_size (size_t size)
 
void set_microphone (microphone::Microphone *microphone)
 
Trigger< std::string > * get_wake_word_detected_trigger () const
 
void set_model_start (const uint8_t *model_start)
 
void set_wake_word (const std::string &wake_word)
 
- Public Member Functions inherited from esphome::Component
float get_actual_setup_priority () const
 
void set_setup_priority (float priority)
 
virtual float get_loop_priority () const
 priority of loop(). More...
 
void call ()
 
virtual void on_shutdown ()
 
virtual void on_safe_shutdown ()
 
uint32_t get_component_state () const
 
virtual void mark_failed ()
 Mark this component as failed. More...
 
bool is_failed ()
 
bool is_ready ()
 
virtual bool can_proceed ()
 
bool status_has_warning () const
 
bool status_has_error () const
 
void status_set_warning (const char *message="unspecified")
 
void status_set_error (const char *message="unspecified")
 
void status_clear_warning ()
 
void status_clear_error ()
 
void status_momentary_warning (const std::string &name, uint32_t length=5000)
 
void status_momentary_error (const std::string &name, uint32_t length=5000)
 
bool has_overridden_loop () const
 
void set_component_source (const char *source)
 Set where this component was loaded from for some debug messages. More...
 
const char * get_component_source () const
 Get the integration where this component was declared as a string. More...
 

Protected Member Functions

void set_state_ (State state)
 
int read_microphone_ ()
 
bool detect_wake_word_ ()
 Detects if wake word has been said. More...
 
bool slice_available_ ()
 Returns true if there are enough audio samples in the buffer to generate another slice of features. More...
 
bool update_features_ ()
 Shifts previous feature slices over by one and generates a new slice of features. More...
 
bool generate_single_feature_ (const int16_t *audio_data, int audio_data_size, int8_t feature_output[PREPROCESSOR_FEATURE_SIZE])
 Generates features from audio samples. More...
 
float perform_streaming_inference_ ()
 Performs inference over the most recent feature slice with the streaming model. More...
 
bool stride_audio_samples_ (int16_t **audio_samples)
 Strides the audio samples by keeping the last 10 ms of the previous slice. More...
 
bool register_preprocessor_ops_ (tflite::MicroMutableOpResolver< 18 > &op_resolver)
 Returns true if successfully registered the preprocessor's TensorFlow operations. More...
 
bool register_streaming_ops_ (tflite::MicroMutableOpResolver< 17 > &op_resolver)
 Returns true if successfully registered the streaming model's TensorFlow operations. More...
 
- Protected Member Functions inherited from esphome::Component
virtual void call_loop ()
 
virtual void call_setup ()
 
virtual void call_dump_config ()
 
void set_interval (const std::string &name, uint32_t interval, std::function< void()> &&f)
 Set an interval function with a unique name. More...
 
void set_interval (uint32_t interval, std::function< void()> &&f)
 
bool cancel_interval (const std::string &name)
 Cancel an interval function. More...
 
void set_retry (const std::string &name, uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
 Set an retry function with a unique name. More...
 
void set_retry (uint32_t initial_wait_time, uint8_t max_attempts, std::function< RetryResult(uint8_t)> &&f, float backoff_increase_factor=1.0f)
 
bool cancel_retry (const std::string &name)
 Cancel a retry function. More...
 
void set_timeout (const std::string &name, uint32_t timeout, std::function< void()> &&f)
 Set a timeout function with a unique name. More...
 
void set_timeout (uint32_t timeout, std::function< void()> &&f)
 
bool cancel_timeout (const std::string &name)
 Cancel a timeout function. More...
 
void defer (const std::string &name, std::function< void()> &&f)
 Defer a callback to the next loop() call. More...
 
void defer (std::function< void()> &&f)
 Defer a callback to the next loop() call. More...
 
bool cancel_defer (const std::string &name)
 Cancel a defer callback using the specified name, name must not be empty. More...
 

Protected Attributes

const uint8_t * model_start_
 
std::string wake_word_
 
microphone::Microphonemicrophone_ {nullptr}
 
Trigger< std::string > * wake_word_detected_trigger_ = new Trigger<std::string>()
 
State state_ {State::IDLE}
 
HighFrequencyLoopRequester high_freq_
 
std::unique_ptr< RingBufferring_buffer_
 
int16_t * input_buffer_
 
const tflite::Model * preprocessor_model_ {nullptr}
 
const tflite::Model * streaming_model_ {nullptr}
 
tflite::MicroInterpreter * streaming_interpreter_ {nullptr}
 
tflite::MicroInterpreter * preprocessor_interperter_ {nullptr}
 
std::vector< float > recent_streaming_probabilities_
 
size_t last_n_index_ {0}
 
float probability_cutoff_ {0.5}
 
size_t sliding_window_average_size_ {10}
 
int16_t ignore_windows_ {-MIN_SLICES_BEFORE_DETECTION}
 
uint8_t * streaming_var_arena_ {nullptr}
 
uint8_t * streaming_tensor_arena_ {nullptr}
 
uint8_t * preprocessor_tensor_arena_ {nullptr}
 
int8_t * new_features_data_ {nullptr}
 
tflite::MicroResourceVariables * mrv_ {nullptr}
 
int16_t * preprocessor_audio_buffer_
 
bool detected_ {false}
 
- Protected Attributes inherited from esphome::Component
uint32_t component_state_ {0x0000}
 State of this component. More...
 
float setup_priority_override_ {NAN}
 
const char * component_source_ {nullptr}
 

Detailed Description

Definition at line 64 of file micro_wake_word.h.

Member Function Documentation

◆ detect_wake_word_()

bool esphome::micro_wake_word::MicroWakeWord::detect_wake_word_ ( )
protected

Detects if wake word has been said.

If enough audio samples are available, it will generate one slice of new features. If the streaming model predicts the wake word, then the nonstreaming model confirms it.

Parameters
ring_BufferRing buffer containing raw audio samples
Returns
True if the wake word is detected, false otherwise

Definition at line 326 of file micro_wake_word.cpp.

◆ dump_config()

void esphome::micro_wake_word::MicroWakeWord::dump_config ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 56 of file micro_wake_word.cpp.

◆ generate_single_feature_()

bool esphome::micro_wake_word::MicroWakeWord::generate_single_feature_ ( const int16_t *  audio_data,
int  audio_data_size,
int8_t  feature_output[PREPROCESSOR_FEATURE_SIZE] 
)
protected

Generates features from audio samples.

Adapted from TFLite micro speech example

Parameters
audio_dataPointer to array with the audio samples
audio_data_sizeThe number of samples to use as input to the preprocessor model
feature_outputArray that will store the features
Returns
True if successful, false otherwise.

Definition at line 407 of file micro_wake_word.cpp.

◆ get_setup_priority()

float esphome::micro_wake_word::MicroWakeWord::get_setup_priority ( ) const
overridevirtual

Reimplemented from esphome::Component.

Definition at line 35 of file micro_wake_word.cpp.

◆ get_wake_word()

std::string esphome::micro_wake_word::MicroWakeWord::get_wake_word ( )
inline

Definition at line 78 of file micro_wake_word.h.

◆ get_wake_word_detected_trigger()

Trigger<std::string>* esphome::micro_wake_word::MicroWakeWord::get_wake_word_detected_trigger ( ) const
inline

Definition at line 86 of file micro_wake_word.h.

◆ initialize_models()

bool esphome::micro_wake_word::MicroWakeWord::initialize_models ( )

Definition at line 181 of file micro_wake_word.cpp.

◆ is_running()

bool esphome::micro_wake_word::MicroWakeWord::is_running ( ) const
inline

Definition at line 74 of file micro_wake_word.h.

◆ loop()

void esphome::micro_wake_word::MicroWakeWord::loop ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 110 of file micro_wake_word.cpp.

◆ perform_streaming_inference_()

float esphome::micro_wake_word::MicroWakeWord::perform_streaming_inference_ ( )
protected

Performs inference over the most recent feature slice with the streaming model.

Returns
Probability of the wake word between 0.0 and 1.0

Definition at line 304 of file micro_wake_word.cpp.

◆ read_microphone_()

int esphome::micro_wake_word::MicroWakeWord::read_microphone_ ( )
protected

Definition at line 90 of file micro_wake_word.cpp.

◆ register_preprocessor_ops_()

bool esphome::micro_wake_word::MicroWakeWord::register_preprocessor_ops_ ( tflite::MicroMutableOpResolver< 18 > &  op_resolver)
protected

Returns true if successfully registered the preprocessor's TensorFlow operations.

Definition at line 422 of file micro_wake_word.cpp.

◆ register_streaming_ops_()

bool esphome::micro_wake_word::MicroWakeWord::register_streaming_ops_ ( tflite::MicroMutableOpResolver< 17 > &  op_resolver)
protected

Returns true if successfully registered the streaming model's TensorFlow operations.

Definition at line 463 of file micro_wake_word.cpp.

◆ set_microphone()

void esphome::micro_wake_word::MicroWakeWord::set_microphone ( microphone::Microphone microphone)
inline

Definition at line 84 of file micro_wake_word.h.

◆ set_model_start()

void esphome::micro_wake_word::MicroWakeWord::set_model_start ( const uint8_t *  model_start)
inline

Definition at line 88 of file micro_wake_word.h.

◆ set_probability_cutoff()

void esphome::micro_wake_word::MicroWakeWord::set_probability_cutoff ( float  probability_cutoff)
inline

Definition at line 81 of file micro_wake_word.h.

◆ set_sliding_window_average_size()

void esphome::micro_wake_word::MicroWakeWord::set_sliding_window_average_size ( size_t  size)

Definition at line 369 of file micro_wake_word.cpp.

◆ set_state_()

void esphome::micro_wake_word::MicroWakeWord::set_state_ ( State  state)
protected

Definition at line 175 of file micro_wake_word.cpp.

◆ set_wake_word()

void esphome::micro_wake_word::MicroWakeWord::set_wake_word ( const std::string &  wake_word)
inline

Definition at line 89 of file micro_wake_word.h.

◆ setup()

void esphome::micro_wake_word::MicroWakeWord::setup ( )
overridevirtual

Reimplemented from esphome::Component.

Definition at line 63 of file micro_wake_word.cpp.

◆ slice_available_()

bool esphome::micro_wake_word::MicroWakeWord::slice_available_ ( )
protected

Returns true if there are enough audio samples in the buffer to generate another slice of features.

Definition at line 374 of file micro_wake_word.cpp.

◆ start()

void esphome::micro_wake_word::MicroWakeWord::start ( )

Definition at line 151 of file micro_wake_word.cpp.

◆ stop()

void esphome::micro_wake_word::MicroWakeWord::stop ( )

Definition at line 163 of file micro_wake_word.cpp.

◆ stride_audio_samples_()

bool esphome::micro_wake_word::MicroWakeWord::stride_audio_samples_ ( int16_t **  audio_samples)
protected

Strides the audio samples by keeping the last 10 ms of the previous slice.

Adapted from the TFLite micro speech example

Parameters
ring_bufferRing buffer containing raw audio samples
audio_samplesPointer to an array that will store the strided audio samples
Returns
True if successful, false otherwise

Definition at line 380 of file micro_wake_word.cpp.

◆ update_features_()

bool esphome::micro_wake_word::MicroWakeWord::update_features_ ( )
protected

Shifts previous feature slices over by one and generates a new slice of features.

Parameters
ring_bufferring buffer containing raw audio samples
Returns
True if a new slice of features was generated, false otherwise

Definition at line 289 of file micro_wake_word.cpp.

Field Documentation

◆ detected_

bool esphome::micro_wake_word::MicroWakeWord::detected_ {false}
protected

Definition at line 132 of file micro_wake_word.h.

◆ high_freq_

HighFrequencyLoopRequester esphome::micro_wake_word::MicroWakeWord::high_freq_
protected

Definition at line 101 of file micro_wake_word.h.

◆ ignore_windows_

int16_t esphome::micro_wake_word::MicroWakeWord::ignore_windows_ {-MIN_SLICES_BEFORE_DETECTION}
protected

Definition at line 120 of file micro_wake_word.h.

◆ input_buffer_

int16_t* esphome::micro_wake_word::MicroWakeWord::input_buffer_
protected

Definition at line 105 of file micro_wake_word.h.

◆ last_n_index_

size_t esphome::micro_wake_word::MicroWakeWord::last_n_index_ {0}
protected

Definition at line 113 of file micro_wake_word.h.

◆ microphone_

microphone::Microphone* esphome::micro_wake_word::MicroWakeWord::microphone_ {nullptr}
protected

Definition at line 98 of file micro_wake_word.h.

◆ model_start_

const uint8_t* esphome::micro_wake_word::MicroWakeWord::model_start_
protected

Definition at line 95 of file micro_wake_word.h.

◆ mrv_

tflite::MicroResourceVariables* esphome::micro_wake_word::MicroWakeWord::mrv_ {nullptr}
protected

Definition at line 127 of file micro_wake_word.h.

◆ new_features_data_

int8_t* esphome::micro_wake_word::MicroWakeWord::new_features_data_ {nullptr}
protected

Definition at line 125 of file micro_wake_word.h.

◆ preprocessor_audio_buffer_

int16_t* esphome::micro_wake_word::MicroWakeWord::preprocessor_audio_buffer_
protected

Definition at line 130 of file micro_wake_word.h.

◆ preprocessor_interperter_

tflite::MicroInterpreter* esphome::micro_wake_word::MicroWakeWord::preprocessor_interperter_ {nullptr}
protected

Definition at line 110 of file micro_wake_word.h.

◆ preprocessor_model_

const tflite::Model* esphome::micro_wake_word::MicroWakeWord::preprocessor_model_ {nullptr}
protected

Definition at line 107 of file micro_wake_word.h.

◆ preprocessor_tensor_arena_

uint8_t* esphome::micro_wake_word::MicroWakeWord::preprocessor_tensor_arena_ {nullptr}
protected

Definition at line 124 of file micro_wake_word.h.

◆ probability_cutoff_

float esphome::micro_wake_word::MicroWakeWord::probability_cutoff_ {0.5}
protected

Definition at line 115 of file micro_wake_word.h.

◆ recent_streaming_probabilities_

std::vector<float> esphome::micro_wake_word::MicroWakeWord::recent_streaming_probabilities_
protected

Definition at line 112 of file micro_wake_word.h.

◆ ring_buffer_

std::unique_ptr<RingBuffer> esphome::micro_wake_word::MicroWakeWord::ring_buffer_
protected

Definition at line 103 of file micro_wake_word.h.

◆ sliding_window_average_size_

size_t esphome::micro_wake_word::MicroWakeWord::sliding_window_average_size_ {10}
protected

Definition at line 116 of file micro_wake_word.h.

◆ state_

State esphome::micro_wake_word::MicroWakeWord::state_ {State::IDLE}
protected

Definition at line 100 of file micro_wake_word.h.

◆ streaming_interpreter_

tflite::MicroInterpreter* esphome::micro_wake_word::MicroWakeWord::streaming_interpreter_ {nullptr}
protected

Definition at line 109 of file micro_wake_word.h.

◆ streaming_model_

const tflite::Model* esphome::micro_wake_word::MicroWakeWord::streaming_model_ {nullptr}
protected

Definition at line 108 of file micro_wake_word.h.

◆ streaming_tensor_arena_

uint8_t* esphome::micro_wake_word::MicroWakeWord::streaming_tensor_arena_ {nullptr}
protected

Definition at line 123 of file micro_wake_word.h.

◆ streaming_var_arena_

uint8_t* esphome::micro_wake_word::MicroWakeWord::streaming_var_arena_ {nullptr}
protected

Definition at line 122 of file micro_wake_word.h.

◆ wake_word_

std::string esphome::micro_wake_word::MicroWakeWord::wake_word_
protected

Definition at line 96 of file micro_wake_word.h.

◆ wake_word_detected_trigger_

Trigger<std::string>* esphome::micro_wake_word::MicroWakeWord::wake_word_detected_trigger_ = new Trigger<std::string>()
protected

Definition at line 99 of file micro_wake_word.h.


The documentation for this class was generated from the following files: