ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
audio_pipeline.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
10
12
13#include "esp_err.h"
14
15#include <freertos/event_groups.h>
16#include <freertos/queue.h>
17
18namespace esphome::speaker {
19
20// Internal sink/source buffers for reader and decoder
21static const size_t DEFAULT_TRANSFER_BUFFER_SIZE = 24 * 1024;
22
23enum class AudioPipelineType : uint8_t {
24 MEDIA,
26};
27
28enum class AudioPipelineState : uint8_t {
31 PLAYING,
33 STOPPED,
34 PAUSED,
37};
38
39enum class InfoErrorSource : uint8_t {
40 READER = 0,
41 DECODER,
42};
43
49
50// Used to pass information from each task.
53 optional<esp_err_t> err;
54 optional<audio::AudioFileType> file_type;
55 optional<audio::AudioStreamInfo> audio_stream_info;
56 optional<DecodingError> decoding_err;
57};
58
59class AudioPipeline final {
60 public:
66 AudioPipeline(speaker::Speaker *speaker, size_t buffer_size, bool task_stack_in_psram, const char *base_name,
67 UBaseType_t priority);
68
73 void start_url(const std::string &uri, bool is_announcement = false);
74
79 void start_file(const audio::AudioFile *audio_file, bool is_announcement = false);
80
83 esp_err_t stop();
84
89
91 void suspend_tasks();
93 void resume_tasks();
94
96
97 void set_pause_state(bool pause_state);
98
99 protected:
102 esp_err_t allocate_communications_();
103
106 esp_err_t start_tasks_();
107
108 const char *base_name_;
109 UBaseType_t priority_;
110
112
113 bool hard_stop_{false};
114 bool is_playing_{false};
115 bool is_finishing_{false};
116 bool pause_state_{false};
118
119 // Pending file start state used to ensure the pipeline fully stops before attempting to start the next file
120 bool pending_url_{false};
121 bool pending_file_{false};
124
126
127 std::string current_uri_{};
129
132
133 size_t buffer_size_; // Ring buffer between reader and decoder
134 size_t transfer_buffer_size_; // Internal source/sink buffers for the audio reader and decoder
135
136 std::weak_ptr<ring_buffer::RingBuffer> raw_file_ring_buffer_;
137
138 // Handles basic control/state of the three tasks
139 EventGroupHandle_t event_group_{nullptr};
140
141 // Receives detailed info (file type, stream info, resampling info) or specific errors from the three tasks
142 QueueHandle_t info_error_queue_{nullptr};
143
144 // Handles reading the media file from flash or a url
145 static void read_task(void *params);
147
148 // Decodes the media file into PCM audio
149 static void decode_task(void *params);
151};
152
153} // namespace esphome::speaker
154
155#endif
Helper for FreeRTOS static task management.
Definition static_task.h:18
const audio::AudioFile * current_audio_file_
static void read_task(void *params)
void suspend_tasks()
Suspends any running tasks.
void start_file(const audio::AudioFile *audio_file, bool is_announcement=false)
Starts an audio pipeline given a AudioFile pointer.
void set_pause_state(bool pause_state)
esp_err_t allocate_communications_()
Allocates the event group and info error queue.
esp_err_t start_tasks_()
Common start code for the pipeline, regardless if the source is a file or url.
audio::AudioStreamInfo current_audio_stream_info_
esp_err_t stop()
Stops the pipeline.
AudioPipeline(speaker::Speaker *speaker, size_t buffer_size, bool task_stack_in_psram, const char *base_name, UBaseType_t priority)
void start_url(const std::string &uri, bool is_announcement=false)
Starts an audio pipeline given a media url.
static void decode_task(void *params)
void resume_tasks()
Resumes any running tasks.
audio::AudioFileType current_audio_file_type_
std::weak_ptr< ring_buffer::RingBuffer > raw_file_ring_buffer_
AudioPipelineState process_state()
Processes the state of the audio pipeline based on the info_error_queue_ and event_group_.
uint8_t priority
static void uint32_t
optional< DecodingError > decoding_err
optional< audio::AudioFileType > file_type
optional< audio::AudioStreamInfo > audio_stream_info