esphome-api-client 0.5.0
Modern C++17 client for the ESPHome native API (plaintext + Noise)
Loading...
Searching...
No Matches
voice_assistant.hpp
Go to the documentation of this file.
1#pragma once
2
5
9
10#include <cstdint>
11#include <functional>
12#include <string>
13#include <utility>
14#include <vector>
15
16namespace esphome::api {
17
21 std::uint32_t noise_suppression_level = 0;
22 std::uint32_t auto_gain = 0;
23 float volume_multiplier = 0.0F;
24};
25
35
39struct VoiceEvent {
41 std::vector<std::pair<std::string, std::string>> data;
42};
43
45struct VoiceAudio {
46 std::vector<std::uint8_t> data;
47 bool end = false;
48};
49
53 std::string timer_id;
54 std::string name;
55 std::uint32_t total_seconds = 0;
56 std::uint32_t seconds_left = 0;
57 bool is_active = false;
58};
59
63 std::string id;
64 std::string wake_word;
65 std::vector<std::string> trained_languages;
66};
67
71 std::vector<VoiceWakeWord> available_wake_words;
72 std::vector<std::string> active_wake_words;
73 std::uint32_t max_active_wake_words = 0;
74};
75
79class VoiceAssistant : public Subsystem {
80public:
81 using RequestHandler = std::function<void(const VoiceRequest&)>;
82 using EventHandler = std::function<void(const VoiceEvent&)>;
83 using AudioHandler = std::function<void(const VoiceAudio&)>;
84 using AnnounceFinishedHandler = std::function<void(bool /*success*/)>;
85 using ConfigurationHandler = std::function<void(const VoiceConfiguration&)>;
86 using TimerEventHandler = std::function<void(const VoiceTimerEvent&)>;
87
88 explicit VoiceAssistant(Client& client) : Subsystem(client) {}
89
93 void
95
97 void on_event(EventHandler handler);
98
101 void send_response(std::uint32_t port, bool error = false) const;
102
104 void on_audio(AudioHandler handler);
105
107 void send_audio(ByteView data, bool end = false) const;
108
111 void announce(const std::string& media_id,
112 const std::string& text = {},
113 const std::string& preannounce_media_id = {},
114 bool start_conversation = false) const;
115
118
122
124 void set_configuration(const std::vector<std::string>& active_wake_words) const;
125
128
129private:
130 RequestHandler request_handler_;
131 EventHandler event_handler_;
132 AudioHandler audio_handler_;
133 AnnounceFinishedHandler announce_finished_handler_;
134 ConfigurationHandler configuration_handler_;
135 TimerEventHandler timer_event_handler_;
136};
137
138} // namespace esphome::api
Byte buffer / view aliases and protobuf base-128 varint codec.
Non-owning view over a contiguous run of bytes (a C++17 stand-in for std::span<const std::uint8_t>).
Definition bytes.hpp:17
Async client for a single device.
Definition client.hpp:38
Shared base for the subsystems exposed by Client (log streaming, the various proxies,...
Definition subsystem.hpp:18
Drives the device's voice-assistant pipeline: subscribing to start/stop requests, relaying events and...
Definition voice_assistant.hpp:79
void set_configuration(const std::vector< std::string > &active_wake_words) const
Set the device's active wake words by id.
void subscribe(VoiceAssistantSubscribeFlag flags, RequestHandler handler, bool subscribe=true)
Subscribe to (or unsubscribe from) the device's voice-assistant requests.
std::function< void(const VoiceEvent &)> EventHandler
Definition voice_assistant.hpp:82
void send_audio(ByteView data, bool end=false) const
Stream an audio chunk to the device. Set end on the final chunk.
void on_announce_finished(AnnounceFinishedHandler handler)
Register a callback fired when an announcement finishes.
std::function< void(bool)> AnnounceFinishedHandler
Definition voice_assistant.hpp:84
void request_configuration(ConfigurationHandler handler)
Request the device's voice-assistant configuration.
std::function< void(const VoiceConfiguration &)> ConfigurationHandler
Definition voice_assistant.hpp:85
void on_event(EventHandler handler)
Register a callback for each pipeline event (VoiceAssistantEventResponse).
void send_response(std::uint32_t port, bool error=false) const
Reply to a start request, telling the device which UDP port to stream audio to (0 when streaming over...
std::function< void(const VoiceRequest &)> RequestHandler
Definition voice_assistant.hpp:81
std::function< void(const VoiceAudio &)> AudioHandler
Definition voice_assistant.hpp:83
void on_timer_event(TimerEventHandler handler)
Register a callback for timer events (VoiceAssistantTimerEventResponse).
void announce(const std::string &media_id, const std::string &text={}, const std::string &preannounce_media_id={}, bool start_conversation=false) const
Ask the device to announce media and/or text.
VoiceAssistant(Client &client)
Definition voice_assistant.hpp:88
std::function< void(const VoiceTimerEvent &)> TimerEventHandler
Definition voice_assistant.hpp:86
void on_audio(AudioHandler handler)
Register a callback for inbound audio chunks (VoiceAssistantAudio).
Typed mirrors of the ESPHome protobuf enums.
Definition bytes.hpp:10
VoiceAssistantEvent
Mirror of proto enum VoiceAssistantEvent.
Definition enums.hpp:311
VoiceAssistantTimerEvent
Mirror of proto enum VoiceAssistantTimerEvent.
Definition enums.hpp:331
VoiceAssistantRequestFlag
Mirror of proto enum VoiceAssistantRequestFlag.
Definition enums.hpp:304
VoiceAssistantSubscribeFlag
Mirror of proto enum VoiceAssistantSubscribeFlag.
Definition enums.hpp:298
Audio capture/processing settings the device requests for a pipeline run (api: VoiceAssistantRequest....
Definition voice_assistant.hpp:20
std::uint32_t auto_gain
Definition voice_assistant.hpp:22
float volume_multiplier
Definition voice_assistant.hpp:23
std::uint32_t noise_suppression_level
Definition voice_assistant.hpp:21
A chunk of audio streamed over the API (api: VoiceAssistantAudio).
Definition voice_assistant.hpp:45
std::vector< std::uint8_t > data
Definition voice_assistant.hpp:46
bool end
Definition voice_assistant.hpp:47
The device's voice-assistant configuration (api: VoiceAssistantConfigurationResponse).
Definition voice_assistant.hpp:70
std::vector< VoiceWakeWord > available_wake_words
Definition voice_assistant.hpp:71
std::vector< std::string > active_wake_words
Definition voice_assistant.hpp:72
std::uint32_t max_active_wake_words
Definition voice_assistant.hpp:73
A pipeline event emitted by the device (api: VoiceAssistantEventResponse).
Definition voice_assistant.hpp:39
std::vector< std::pair< std::string, std::string > > data
Definition voice_assistant.hpp:41
VoiceAssistantEvent event_type
Definition voice_assistant.hpp:40
The device asking the client to start or stop a voice pipeline (api: VoiceAssistantRequest).
Definition voice_assistant.hpp:28
std::string conversation_id
Definition voice_assistant.hpp:30
std::string wake_word_phrase
Definition voice_assistant.hpp:33
VoiceAudioSettings audio_settings
Definition voice_assistant.hpp:32
VoiceAssistantRequestFlag flags
Definition voice_assistant.hpp:31
bool start
Definition voice_assistant.hpp:29
A timer event surfaced by the device (api: VoiceAssistantTimerEventResponse).
Definition voice_assistant.hpp:51
std::uint32_t seconds_left
Definition voice_assistant.hpp:56
std::uint32_t total_seconds
Definition voice_assistant.hpp:55
VoiceAssistantTimerEvent event_type
Definition voice_assistant.hpp:52
bool is_active
Definition voice_assistant.hpp:57
std::string timer_id
Definition voice_assistant.hpp:53
std::string name
Definition voice_assistant.hpp:54
A wake word the device can be configured to listen for (api: VoiceAssistantWakeWord).
Definition voice_assistant.hpp:62
std::vector< std::string > trained_languages
Definition voice_assistant.hpp:65
std::string wake_word
Definition voice_assistant.hpp:64
std::string id
Definition voice_assistant.hpp:63
Common base for the Client-owned subsystems.