ESPHome 2026.6.0-dev
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
60 public:
66 AudioPipeline(speaker::Speaker *speaker, size_t buffer_size, bool task_stack_in_psram, std::string base_name,
67 UBaseType_t priority);
68
72 void start_url(const std::string &uri);
73
77 void start_file(audio::AudioFile *audio_file);
78
81 esp_err_t stop();
82
87
89 void suspend_tasks();
91 void resume_tasks();
92
94
95 void set_pause_state(bool pause_state);
96
97 protected:
100 esp_err_t allocate_communications_();
101
104 esp_err_t start_tasks_();
105
106 std::string base_name_;
107 UBaseType_t priority_;
108
110
111 bool hard_stop_{false};
112 bool is_playing_{false};
113 bool is_finishing_{false};
114 bool pause_state_{false};
116
117 // Pending file start state used to ensure the pipeline fully stops before attempting to start the next file
118 bool pending_url_{false};
119 bool pending_file_{false};
120
122
123 std::string current_uri_{};
125
128
129 size_t buffer_size_; // Ring buffer between reader and decoder
130 size_t transfer_buffer_size_; // Internal source/sink buffers for the audio reader and decoder
131
132 std::weak_ptr<ring_buffer::RingBuffer> raw_file_ring_buffer_;
133
134 // Handles basic control/state of the three tasks
135 EventGroupHandle_t event_group_{nullptr};
136
137 // Receives detailed info (file type, stream info, resampling info) or specific errors from the three tasks
138 QueueHandle_t info_error_queue_{nullptr};
139
140 // Handles reading the media file from flash or a url
141 static void read_task(void *params);
143
144 // Decodes the media file into PCM audio
145 static void decode_task(void *params);
147};
148
149} // namespace esphome::speaker
150
151#endif
Helper for FreeRTOS static task management.
Definition static_task.h:15
static void read_task(void *params)
void suspend_tasks()
Suspends any running tasks.
void set_pause_state(bool pause_state)
void start_url(const std::string &uri)
Starts an audio pipeline given a media url.
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_
void start_file(audio::AudioFile *audio_file)
Starts an audio pipeline given a AudioFile pointer.
esp_err_t stop()
Stops the pipeline.
static void decode_task(void *params)
AudioPipeline(speaker::Speaker *speaker, size_t buffer_size, bool task_stack_in_psram, std::string base_name, UBaseType_t priority)
void resume_tasks()
Resumes any running tasks.
audio::AudioFile * current_audio_file_
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