ESPHome  2025.2.0
audio_pipeline.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP_IDF
4 
9 
11 
12 #include "esp_err.h"
13 
14 #include <freertos/FreeRTOS.h>
15 #include <freertos/event_groups.h>
16 #include <freertos/queue.h>
17 
18 namespace esphome {
19 namespace speaker {
20 
21 // Internal sink/source buffers for reader and decoder
22 static const size_t DEFAULT_TRANSFER_BUFFER_SIZE = 24 * 1024;
23 
24 enum class AudioPipelineType : uint8_t {
25  MEDIA,
27 };
28 
29 enum class AudioPipelineState : uint8_t {
32  PLAYING,
33  STOPPING,
34  STOPPED,
35  PAUSED,
38 };
39 
40 enum class InfoErrorSource : uint8_t {
41  READER = 0,
42  DECODER,
43 };
44 
45 enum class DecodingError : uint8_t {
46  FAILED_HEADER = 0,
49 };
50 
51 // Used to pass information from each task.
58 };
59 
61  public:
67  AudioPipeline(speaker::Speaker *speaker, size_t buffer_size, bool task_stack_in_psram, std::string base_name,
68  UBaseType_t priority);
69 
73  void start_url(const std::string &uri);
74 
78  void start_file(audio::AudioFile *audio_file);
79 
82  esp_err_t stop();
83 
87  AudioPipelineState process_state();
88 
90  void suspend_tasks();
92  void resume_tasks();
93 
94  uint32_t get_playback_ms() { return this->playback_ms_; }
95 
96  void set_pause_state(bool pause_state);
97 
98  protected:
101  esp_err_t allocate_communications_();
102 
105  esp_err_t start_tasks_();
106 
108  void delete_tasks_();
109 
110  std::string base_name_;
111  UBaseType_t priority_;
112 
113  uint32_t playback_ms_{0};
114 
115  bool hard_stop_{false};
116  bool is_playing_{false};
117  bool pause_state_{false};
119 
120  // Pending file start state used to ensure the pipeline fully stops before attempting to start the next file
121  bool pending_url_{false};
122  bool pending_file_{false};
123 
124  speaker::Speaker *speaker_{nullptr};
125 
126  std::string current_uri_{};
127  audio::AudioFile *current_audio_file_{nullptr};
128 
131 
132  size_t buffer_size_; // Ring buffer between reader and decoder
133  size_t transfer_buffer_size_; // Internal source/sink buffers for the audio reader and decoder
134 
135  std::weak_ptr<RingBuffer> raw_file_ring_buffer_;
136 
137  // Handles basic control/state of the three tasks
138  EventGroupHandle_t event_group_{nullptr};
139 
140  // Receives detailed info (file type, stream info, resampling info) or specific errors from the three tasks
141  QueueHandle_t info_error_queue_{nullptr};
142 
143  // Handles reading the media file from flash or a url
144  static void read_task(void *params);
145  TaskHandle_t read_task_handle_{nullptr};
146  StaticTask_t read_task_stack_;
147  StackType_t *read_task_stack_buffer_{nullptr};
148 
149  // Decodes the media file into PCM audio
150  static void decode_task(void *params);
151  TaskHandle_t decode_task_handle_{nullptr};
152  StaticTask_t decode_task_stack_;
153  StackType_t *decode_task_stack_buffer_{nullptr};
154 };
155 
156 } // namespace speaker
157 } // namespace esphome
158 
159 #endif
optional< DecodingError > decoding_err
optional< audio::AudioStreamInfo > audio_stream_info
audio::AudioStreamInfo current_audio_stream_info_
audio::AudioFileType current_audio_file_type_
std::weak_ptr< RingBuffer > raw_file_ring_buffer_
optional< audio::AudioFileType > file_type
uint8_t priority
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7