ESPHome  2024.12.2
speaker.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <cstddef>
4 #include <cstdint>
5 #include <vector>
6 
7 #ifdef USE_ESP32
8 #include <freertos/FreeRTOS.h>
9 #endif
10 
11 #include "esphome/core/defines.h"
12 
14 #ifdef USE_AUDIO_DAC
16 #endif
17 
18 namespace esphome {
19 namespace speaker {
20 
21 enum State : uint8_t {
26 };
27 
28 class Speaker {
29  public:
30 #ifdef USE_ESP32
31  virtual size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) {
38  return this->play(data, length);
39  };
40 #endif
41 
48  virtual size_t play(const uint8_t *data, size_t length) = 0;
49 
50  size_t play(const std::vector<uint8_t> &data) { return this->play(data.data(), data.size()); }
51 
52  virtual void start() = 0;
53  virtual void stop() = 0;
54  // In compare between *STOP()* and *FINISH()*; *FINISH()* will stop after emptying the play buffer,
55  // while *STOP()* will break directly.
56  // When finish() is not implemented on the platform component it should just do a normal stop.
57  virtual void finish() { this->stop(); }
58 
59  virtual bool has_buffered_data() const = 0;
60 
61  bool is_running() const { return this->state_ == STATE_RUNNING; }
62  bool is_stopped() const { return this->state_ == STATE_STOPPED; }
63 
64  // Volume control is handled by a configured audio dac component. Individual speaker components can
65  // override and implement in software if an audio dac isn't available.
66  virtual void set_volume(float volume) {
67  this->volume_ = volume;
68 #ifdef USE_AUDIO_DAC
69  if (this->audio_dac_ != nullptr) {
70  this->audio_dac_->set_volume(volume);
71  }
72 #endif
73  };
74  float get_volume() { return this->volume_; }
75 
76  virtual void set_mute_state(bool mute_state) {
77  this->mute_state_ = mute_state;
78 #ifdef USE_AUDIO_DAC
79  if (this->audio_dac_) {
80  if (mute_state) {
81  this->audio_dac_->set_mute_on();
82  } else {
83  this->audio_dac_->set_mute_off();
84  }
85  }
86 #endif
87  }
88  bool get_mute_state() { return this->mute_state_; }
89 
90 #ifdef USE_AUDIO_DAC
91  void set_audio_dac(audio_dac::AudioDac *audio_dac) { this->audio_dac_ = audio_dac; }
92 #endif
93 
94  void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info) {
95  this->audio_stream_info_ = audio_stream_info;
96  }
97 
98  protected:
101  float volume_{1.0f};
102  bool mute_state_{false};
103 
104 #ifdef USE_AUDIO_DAC
106 #endif
107 };
108 
109 } // namespace speaker
110 } // namespace esphome
bool is_running() const
Definition: speaker.h:61
virtual bool set_mute_off()=0
virtual bool set_mute_on()=0
virtual void set_volume(float volume)
Definition: speaker.h:66
virtual void finish()
Definition: speaker.h:57
virtual bool has_buffered_data() const =0
bool is_stopped() const
Definition: speaker.h:62
size_t play(const std::vector< uint8_t > &data)
Definition: speaker.h:50
void set_audio_stream_info(const audio::AudioStreamInfo &audio_stream_info)
Definition: speaker.h:94
void set_audio_dac(audio_dac::AudioDac *audio_dac)
Definition: speaker.h:91
virtual bool set_volume(float volume)=0
virtual void start()=0
audio_dac::AudioDac * audio_dac_
Definition: speaker.h:105
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
virtual size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait)
Plays the provided audio data.
Definition: speaker.h:37
virtual void stop()=0
audio::AudioStreamInfo audio_stream_info_
Definition: speaker.h:100
virtual void set_mute_state(bool mute_state)
Definition: speaker.h:76