ESPHome  2024.12.2
i2s_audio_speaker.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include "../i2s_audio.h"
6 
7 #include <driver/i2s.h>
8 
9 #include <freertos/event_groups.h>
10 #include <freertos/queue.h>
11 #include <freertos/FreeRTOS.h>
12 
15 
16 #include "esphome/core/component.h"
17 #include "esphome/core/gpio.h"
18 #include "esphome/core/helpers.h"
20 
21 namespace esphome {
22 namespace i2s_audio {
23 
24 class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Component {
25  public:
26  float get_setup_priority() const override { return esphome::setup_priority::PROCESSOR; }
27 
28  void setup() override;
29  void loop() override;
30 
31  void set_buffer_duration(uint32_t buffer_duration_ms) { this->buffer_duration_ms_ = buffer_duration_ms; }
32  void set_timeout(uint32_t ms) { this->timeout_ = ms; }
33  void set_dout_pin(uint8_t pin) { this->dout_pin_ = pin; }
34 #if SOC_I2S_SUPPORTS_DAC
35  void set_internal_dac_mode(i2s_dac_mode_t mode) { this->internal_dac_mode_ = mode; }
36 #endif
37  void set_i2s_comm_fmt(i2s_comm_format_t mode) { this->i2s_comm_fmt_ = mode; }
38 
39  void start() override;
40  void stop() override;
41  void finish() override;
42 
49  size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) override;
50  size_t play(const uint8_t *data, size_t length) override { return play(data, length, 0); }
51 
52  bool has_buffered_data() const override;
53 
58  void set_volume(float volume) override;
59 
64  void set_mute_state(bool mute_state) override;
65 
66  protected:
75  static void speaker_task(void *params);
76 
79  void stop_(bool wait_on_empty);
80 
84  bool send_esp_err_to_event_group_(esp_err_t err);
85 
91  esp_err_t allocate_buffers_(size_t data_buffer_size, size_t ring_buffer_size);
92 
102  esp_err_t start_i2s_driver_(audio::AudioStreamInfo &audio_stream_info);
103 
108  void delete_task_(size_t buffer_size);
109 
110  TaskHandle_t speaker_task_handle_{nullptr};
111  EventGroupHandle_t event_group_{nullptr};
112 
113  QueueHandle_t i2s_event_queue_;
114 
115  uint8_t *data_buffer_;
116  std::shared_ptr<RingBuffer> audio_ring_buffer_;
117 
119 
121  uint8_t dout_pin_;
122 
123  bool task_created_{false};
124 
125  int16_t q15_volume_factor_{INT16_MAX};
126 
127 #if SOC_I2S_SUPPORTS_DAC
128  i2s_dac_mode_t internal_dac_mode_{I2S_DAC_CHANNEL_DISABLE};
129 #endif
130  i2s_comm_format_t i2s_comm_fmt_;
131 };
132 
133 } // namespace i2s_audio
134 } // namespace esphome
135 
136 #endif // USE_ESP32
size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) override
Plays the provided audio data.
void stop_(bool wait_on_empty)
Sends a stop command to the speaker task via event_group_.
bool send_esp_err_to_event_group_(esp_err_t err)
Sets the corresponding ERR_ESP event group bits.
esp_err_t allocate_buffers_(size_t data_buffer_size, size_t ring_buffer_size)
Allocates the data buffer and ring buffer.
static void speaker_task(void *params)
Function for the FreeRTOS task handling audio output.
void set_i2s_comm_fmt(i2s_comm_format_t mode)
void set_volume(float volume) override
Sets the volume of the speaker.
void set_buffer_duration(uint32_t buffer_duration_ms)
esp_err_t start_i2s_driver_(audio::AudioStreamInfo &audio_stream_info)
Starts the ESP32 I2S driver.
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:183
void set_mute_state(bool mute_state) override
Mutes or unmute the speaker.
size_t play(const uint8_t *data, size_t length) override
const float PROCESSOR
For components that use data from sensors like displays.
Definition: component.cpp:20
float get_setup_priority() const override
void delete_task_(size_t buffer_size)
Deletes the speaker&#39;s task.
std::shared_ptr< RingBuffer > audio_ring_buffer_
uint16_t length
Definition: tt21100.cpp:12
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_internal_dac_mode(i2s_dac_mode_t mode)