ESPHome  2024.7.2
voice_assistant.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "esphome/core/defines.h"
4 
5 #ifdef USE_VOICE_ASSISTANT
6 
9 #include "esphome/core/helpers.h"
11 
15 #ifdef USE_SPEAKER
17 #endif
18 #ifdef USE_MEDIA_PLAYER
20 #endif
22 
23 #ifdef USE_ESP_ADF
24 #include <esp_vad.h>
25 #endif
26 
27 #include <unordered_map>
28 #include <vector>
29 
30 namespace esphome {
31 namespace voice_assistant {
32 
33 // Version 1: Initial version
34 // Version 2: Adds raw speaker support
35 static const uint32_t LEGACY_INITIAL_VERSION = 1;
36 static const uint32_t LEGACY_SPEAKER_SUPPORT = 2;
37 
38 enum VoiceAssistantFeature : uint32_t {
40  FEATURE_SPEAKER = 1 << 1,
42  FEATURE_TIMERS = 1 << 3,
43 };
44 
45 enum class State {
46  IDLE,
59 };
60 
61 enum AudioMode : uint8_t {
64 };
65 
66 struct Timer {
67  std::string id;
68  std::string name;
69  uint32_t total_seconds;
70  uint32_t seconds_left;
71  bool is_active;
72 
73  std::string to_string() const {
74  return str_sprintf("Timer(id=%s, name=%s, total_seconds=%" PRIu32 ", seconds_left=%" PRIu32 ", is_active=%s)",
75  this->id.c_str(), this->name.c_str(), this->total_seconds, this->seconds_left,
76  YESNO(this->is_active));
77  }
78 };
79 
80 class VoiceAssistant : public Component {
81  public:
82  void setup() override;
83  void loop() override;
84  float get_setup_priority() const override;
85  void start_streaming();
86  void start_streaming(struct sockaddr_storage *addr, uint16_t port);
87  void failed_to_start();
88 
89  void set_microphone(microphone::Microphone *mic) { this->mic_ = mic; }
90 #ifdef USE_SPEAKER
91  void set_speaker(speaker::Speaker *speaker) {
92  this->speaker_ = speaker;
93  this->local_output_ = true;
94  }
95 #endif
96 #ifdef USE_MEDIA_PLAYER
98  this->media_player_ = media_player;
99  this->local_output_ = true;
100  }
101 #endif
102 
103  uint32_t get_legacy_version() const {
104 #ifdef USE_SPEAKER
105  if (this->speaker_ != nullptr) {
106  return LEGACY_SPEAKER_SUPPORT;
107  }
108 #endif
109  return LEGACY_INITIAL_VERSION;
110  }
111 
112  uint32_t get_feature_flags() const {
113  uint32_t flags = 0;
116 #ifdef USE_SPEAKER
117  if (this->speaker_ != nullptr) {
119  }
120 #endif
121 
122  if (this->has_timers_) {
124  }
125 
126  return flags;
127  }
128 
129  void request_start(bool continuous, bool silence_detection);
130  void request_stop();
131 
132  void on_event(const api::VoiceAssistantEventResponse &msg);
133  void on_audio(const api::VoiceAssistantAudio &msg);
134  void on_timer_event(const api::VoiceAssistantTimerEventResponse &msg);
135 
136  bool is_running() const { return this->state_ != State::IDLE; }
137  void set_continuous(bool continuous) { this->continuous_ = continuous; }
138  bool is_continuous() const { return this->continuous_; }
139 
140  void set_use_wake_word(bool use_wake_word) { this->use_wake_word_ = use_wake_word; }
141 #ifdef USE_ESP_ADF
142  void set_vad_threshold(uint8_t vad_threshold) { this->vad_threshold_ = vad_threshold; }
143 #endif
144 
145  void set_noise_suppression_level(uint8_t noise_suppression_level) {
146  this->noise_suppression_level_ = noise_suppression_level;
147  }
148  void set_auto_gain(uint8_t auto_gain) { this->auto_gain_ = auto_gain; }
149  void set_volume_multiplier(float volume_multiplier) { this->volume_multiplier_ = volume_multiplier; }
150 
151  Trigger<> *get_intent_end_trigger() const { return this->intent_end_trigger_; }
152  Trigger<> *get_intent_start_trigger() const { return this->intent_start_trigger_; }
153  Trigger<> *get_listening_trigger() const { return this->listening_trigger_; }
154  Trigger<> *get_end_trigger() const { return this->end_trigger_; }
155  Trigger<> *get_start_trigger() const { return this->start_trigger_; }
156  Trigger<> *get_stt_vad_end_trigger() const { return this->stt_vad_end_trigger_; }
157  Trigger<> *get_stt_vad_start_trigger() const { return this->stt_vad_start_trigger_; }
158 #ifdef USE_SPEAKER
159  Trigger<> *get_tts_stream_start_trigger() const { return this->tts_stream_start_trigger_; }
160  Trigger<> *get_tts_stream_end_trigger() const { return this->tts_stream_end_trigger_; }
161 #endif
162  Trigger<> *get_wake_word_detected_trigger() const { return this->wake_word_detected_trigger_; }
163  Trigger<std::string> *get_stt_end_trigger() const { return this->stt_end_trigger_; }
164  Trigger<std::string> *get_tts_end_trigger() const { return this->tts_end_trigger_; }
165  Trigger<std::string> *get_tts_start_trigger() const { return this->tts_start_trigger_; }
166  Trigger<std::string, std::string> *get_error_trigger() const { return this->error_trigger_; }
167  Trigger<> *get_idle_trigger() const { return this->idle_trigger_; }
168 
169  Trigger<> *get_client_connected_trigger() const { return this->client_connected_trigger_; }
170  Trigger<> *get_client_disconnected_trigger() const { return this->client_disconnected_trigger_; }
171 
172  void client_subscription(api::APIConnection *client, bool subscribe);
173  api::APIConnection *get_api_connection() const { return this->api_client_; }
174 
175  void set_wake_word(const std::string &wake_word) { this->wake_word_ = wake_word; }
176 
177  Trigger<Timer> *get_timer_started_trigger() const { return this->timer_started_trigger_; }
178  Trigger<Timer> *get_timer_updated_trigger() const { return this->timer_updated_trigger_; }
179  Trigger<Timer> *get_timer_cancelled_trigger() const { return this->timer_cancelled_trigger_; }
180  Trigger<Timer> *get_timer_finished_trigger() const { return this->timer_finished_trigger_; }
181  Trigger<std::vector<Timer>> *get_timer_tick_trigger() const { return this->timer_tick_trigger_; }
182  void set_has_timers(bool has_timers) { this->has_timers_ = has_timers; }
183  const std::unordered_map<std::string, Timer> &get_timers() const { return this->timers_; }
184 
185  protected:
186  bool allocate_buffers_();
187  void clear_buffers_();
188  void deallocate_buffers_();
189 
190  int read_microphone_();
191  void set_state_(State state);
192  void set_state_(State state, State desired_state);
193  void signal_stop_();
194 
195  std::unique_ptr<socket::Socket> socket_ = nullptr;
196  struct sockaddr_storage dest_addr_;
197 
198  Trigger<> *intent_end_trigger_ = new Trigger<>();
199  Trigger<> *intent_start_trigger_ = new Trigger<>();
200  Trigger<> *listening_trigger_ = new Trigger<>();
201  Trigger<> *end_trigger_ = new Trigger<>();
202  Trigger<> *start_trigger_ = new Trigger<>();
203  Trigger<> *stt_vad_start_trigger_ = new Trigger<>();
204  Trigger<> *stt_vad_end_trigger_ = new Trigger<>();
205 #ifdef USE_SPEAKER
206  Trigger<> *tts_stream_start_trigger_ = new Trigger<>();
207  Trigger<> *tts_stream_end_trigger_ = new Trigger<>();
208 #endif
209  Trigger<> *wake_word_detected_trigger_ = new Trigger<>();
210  Trigger<std::string> *stt_end_trigger_ = new Trigger<std::string>();
211  Trigger<std::string> *tts_end_trigger_ = new Trigger<std::string>();
212  Trigger<std::string> *tts_start_trigger_ = new Trigger<std::string>();
214  Trigger<> *idle_trigger_ = new Trigger<>();
215 
216  Trigger<> *client_connected_trigger_ = new Trigger<>();
217  Trigger<> *client_disconnected_trigger_ = new Trigger<>();
218 
219  api::APIConnection *api_client_{nullptr};
220 
221  std::unordered_map<std::string, Timer> timers_;
222  void timer_tick_();
223  Trigger<Timer> *timer_started_trigger_ = new Trigger<Timer>();
224  Trigger<Timer> *timer_finished_trigger_ = new Trigger<Timer>();
225  Trigger<Timer> *timer_updated_trigger_ = new Trigger<Timer>();
226  Trigger<Timer> *timer_cancelled_trigger_ = new Trigger<Timer>();
228  bool has_timers_{false};
229  bool timer_tick_running_{false};
230 
231  microphone::Microphone *mic_{nullptr};
232 #ifdef USE_SPEAKER
233  void write_speaker_();
234  speaker::Speaker *speaker_{nullptr};
235  uint8_t *speaker_buffer_;
236  size_t speaker_buffer_index_{0};
237  size_t speaker_buffer_size_{0};
238  size_t speaker_bytes_received_{0};
239  bool wait_for_stream_end_{false};
240  bool stream_ended_{false};
241 #endif
242 #ifdef USE_MEDIA_PLAYER
243  media_player::MediaPlayer *media_player_{nullptr};
244 #endif
245 
246  bool local_output_{false};
247 
248  std::string conversation_id_{""};
249 
250  std::string wake_word_{""};
251 
253 
254 #ifdef USE_ESP_ADF
255  vad_handle_t vad_instance_;
256  uint8_t vad_threshold_{5};
257  uint8_t vad_counter_{0};
258 #endif
259  std::unique_ptr<RingBuffer> ring_buffer_;
260 
263  uint8_t auto_gain_;
265 
266  uint8_t *send_buffer_;
267  int16_t *input_buffer_;
268 
269  bool continuous_{false};
271 
273  State desired_state_{State::IDLE};
274 
275  AudioMode audio_mode_{AUDIO_MODE_UDP};
276  bool udp_socket_running_{false};
277  bool start_udp_socket_();
278 };
279 
280 template<typename... Ts> class StartAction : public Action<Ts...>, public Parented<VoiceAssistant> {
281  TEMPLATABLE_VALUE(std::string, wake_word);
282 
283  public:
284  void play(Ts... x) override {
285  this->parent_->set_wake_word(this->wake_word_.value(x...));
286  this->parent_->request_start(false, this->silence_detection_);
287  }
288 
289  void set_silence_detection(bool silence_detection) { this->silence_detection_ = silence_detection; }
290 
291  protected:
293 };
294 
295 template<typename... Ts> class StartContinuousAction : public Action<Ts...>, public Parented<VoiceAssistant> {
296  public:
297  void play(Ts... x) override { this->parent_->request_start(true, true); }
298 };
299 
300 template<typename... Ts> class StopAction : public Action<Ts...>, public Parented<VoiceAssistant> {
301  public:
302  void play(Ts... x) override { this->parent_->request_stop(); }
303 };
304 
305 template<typename... Ts> class IsRunningCondition : public Condition<Ts...>, public Parented<VoiceAssistant> {
306  public:
307  bool check(Ts... x) override { return this->parent_->is_running() || this->parent_->is_continuous(); }
308 };
309 
310 template<typename... Ts> class ConnectedCondition : public Condition<Ts...>, public Parented<VoiceAssistant> {
311  public:
312  bool check(Ts... x) override { return this->parent_->get_api_connection() != nullptr; }
313 };
314 
315 extern VoiceAssistant *global_voice_assistant; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
316 
317 } // namespace voice_assistant
318 } // namespace esphome
319 
320 #endif // USE_VOICE_ASSISTANT
void setup()
Trigger< Timer > * get_timer_finished_trigger() const
void loop()
void set_microphone(microphone::Microphone *mic)
Trigger< Timer > * get_timer_started_trigger() const
std::unordered_map< std::string, Timer > timers_
HighFrequencyLoopRequester high_freq_
VoiceAssistant * global_voice_assistant
Trigger< Timer > * get_timer_cancelled_trigger() const
uint16_t x
Definition: tt21100.cpp:17
Helper class to request loop() to be called as fast as possible.
Definition: helpers.h:609
api::APIConnection * get_api_connection() const
const std::unordered_map< std::string, Timer > & get_timers() const
Trigger< std::vector< Timer > > * get_timer_tick_trigger() const
void set_wake_word(const std::string &wake_word)
void set_noise_suppression_level(uint8_t noise_suppression_level)
void set_volume_multiplier(float volume_multiplier)
Base class for all automation conditions.
Definition: automation.h:74
Trigger< std::string > * get_tts_start_trigger() const
std::string str_sprintf(const char *fmt,...)
Definition: helpers.cpp:312
const uint32_t flags
Definition: stm32flash.h:85
Trigger< std::string, std::string > * get_error_trigger() const
std::unique_ptr< RingBuffer > ring_buffer_
void set_speaker(speaker::Speaker *speaker)
void set_vad_threshold(uint8_t vad_threshold)
Trigger< std::string > * get_tts_end_trigger() const
void set_use_wake_word(bool use_wake_word)
void set_media_player(media_player::MediaPlayer *media_player)
void set_silence_detection(bool silence_detection)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
Trigger< std::string > * get_stt_end_trigger() const
Trigger< Timer > * get_timer_updated_trigger() const
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521
bool state
Definition: fan.h:34