ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
speaker_media_player.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include "audio_pipeline.h"
6
11
16
17#ifdef USE_OTA_STATE_LISTENER
19#endif
20
21#include <deque>
22#include <freertos/FreeRTOS.h>
23#include <freertos/queue.h>
24
25namespace esphome::speaker {
26
28 optional<media_player::MediaPlayerCommand> command;
29 optional<float> volume;
30 optional<bool> announce;
31 // The caller asked for an announcement; `announce` is also forced on in single-pipeline mode
32 bool is_announcement{false};
33 optional<std::string *> url; // Must be manually deleted after receiving this struct from a queue
34 optional<const audio::AudioFile *> file;
35 optional<bool> enqueue;
36};
37
39 optional<std::string> url;
40 optional<const audio::AudioFile *> file;
41 bool is_announcement{false};
42};
43
45 float volume;
47};
48
49class SpeakerMediaPlayer final : public Component,
51#ifdef USE_OTA_STATE_LISTENER
52 ,
54#endif
55{
56 public:
57 float get_setup_priority() const override { return esphome::setup_priority::PROCESSOR; }
58 void setup() override;
59 void loop() override;
60
61#ifdef USE_OTA_STATE_LISTENER
62 void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override;
63#endif
64
65 // MediaPlayer implementations
67 bool is_muted() const override { return this->is_muted_; }
68
69 void set_buffer_size(size_t buffer_size) { this->buffer_size_ = buffer_size; }
70 void set_task_stack_in_psram(bool task_stack_in_psram) { this->task_stack_in_psram_ = task_stack_in_psram; }
71
72 // Percentage to increase or decrease the volume for volume up or volume down commands
73 void set_volume_increment(float volume_increment) { this->volume_increment_ = volume_increment; }
74
75 // Volume used initially on first boot when no volume had been previously saved
76 void set_volume_initial(float volume_initial) { this->volume_initial_ = volume_initial; }
77
78 void set_volume_max(float volume_max) { this->volume_max_ = volume_max; }
79 void set_volume_min(float volume_min) { this->volume_min_ = volume_min; }
80
82 void set_announcement_volume(float announcement_volume);
83
84 void set_announcement_speaker(Speaker *announcement_speaker) { this->announcement_speaker_ = announcement_speaker; }
86 this->announcement_format_ = announcement_format;
87 }
88 void set_media_speaker(Speaker *media_speaker) { this->media_speaker_ = media_speaker; }
90 this->media_format_ = media_format;
91 }
92
96
97 void play_file(const audio::AudioFile *media_file, bool announcement, bool enqueue);
98
99 void set_playlist_delay_ms(AudioPipelineType pipeline_type, uint32_t delay_ms);
100
101 protected:
102 // Receives commands from HA or from the voice assistant component
103 // Sends commands to the media_control_commanda_queue_
105
107 void set_volume_(float volume, bool publish = true);
108
111
114 return [this](float volume) { this->apply_speaker_volume_(volume); };
115 }
116
120 void set_mute_state_(bool mute_state);
121
124
127 inline bool single_pipeline_() { return (this->media_speaker_ == nullptr); }
128
131
132 // Processes commands from media_control_command_queue_.
134
135 std::unique_ptr<AudioPipeline> announcement_pipeline_;
136 std::unique_ptr<AudioPipeline> media_pipeline_;
139
140 optional<media_player::MediaPlayerSupportedFormat> media_format_;
142 bool media_repeat_one_{false};
143 // Set when the media pipeline reports an error, consumed when it stops, so the failed item is dropped
146
147 optional<media_player::MediaPlayerSupportedFormat> announcement_format_;
150 // Set when the announcement pipeline reports an error, consumed when it stops, so the failed item is dropped
153
155
156 std::deque<PlaylistItem> announcement_playlist_;
157 std::deque<PlaylistItem> media_playlist_;
158
160
162
163 bool is_paused_{false};
164 bool is_muted_{false};
165#ifdef USE_SPEAKER_MEDIA_PLAYER_ON_OFF
166 bool is_turn_off_{false};
167#endif
170
171 // The amount to change the volume on volume up/down commands
173
174 // The initial volume used by Setup when no previous volume was saved
176
179
181
182 // Used to save volume/mute state for restoration on reboot
184
188};
189
190} // namespace esphome::speaker
191
192#endif
Listener interface for global OTA state changes (includes OTA component pointer).
Tracks a separate volume for announcements that start while nothing else is playing.
optional< media_player::MediaPlayerSupportedFormat > announcement_format_
void set_announcement_format(const media_player::MediaPlayerSupportedFormat &announcement_format)
void save_volume_restore_state_()
Saves the current volume and mute state to the flash for restoration.
std::deque< PlaylistItem > announcement_playlist_
void set_volume_increment(float volume_increment)
void set_volume_(float volume, bool publish=true)
Updates this->volume and saves volume/mute state to flash for restortation if publish is true.
void set_task_stack_in_psram(bool task_stack_in_psram)
void play_file(const audio::AudioFile *media_file, bool announcement, bool enqueue)
void set_announcement_volume(float announcement_volume)
Sets the volume for announcements that start while nothing else is playing.
void set_mute_state_(bool mute_state)
Sets the mute state.
void set_media_speaker(Speaker *media_speaker)
std::deque< PlaylistItem > media_playlist_
std::unique_ptr< AudioPipeline > media_pipeline_
void stop_and_unpause_media_()
Stops the media pipeline and polls until stopped to unpause it, avoiding an audible glitch.
void set_volume_initial(float volume_initial)
optional< media_player::MediaPlayerSupportedFormat > media_format_
void control(const media_player::MediaPlayerCall &call) override
void apply_speaker_volume_(float volume)
Remaps a volume to the configured limits and sends it to the speakers.
void set_media_format(const media_player::MediaPlayerSupportedFormat &media_format)
void set_playlist_delay_ms(AudioPipelineType pipeline_type, uint32_t delay_ms)
void on_ota_global_state(ota::OTAState state, float progress, uint8_t error, ota::OTAComponent *comp) override
void set_announcement_speaker(Speaker *announcement_speaker)
std::unique_ptr< AudioPipeline > announcement_pipeline_
auto speaker_volume_applier_()
Returns a callable that sends a volume to the speakers, for announcement_volume_ to apply its changes...
media_player::MediaPlayerTraits get_traits() override
bool single_pipeline_()
Returns true if the media player has only the announcement pipeline defined, false if both the announ...
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:47
static void uint32_t
optional< media_player::MediaPlayerCommand > command
optional< const audio::AudioFile * > file
optional< const audio::AudioFile * > file