ESPHome 2026.5.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
9
12
13#include "esp_err.h"
14
15#include <freertos/event_groups.h>
16#include <freertos/queue.h>
17
18namespace esphome {
19namespace speaker {
20
21// Internal sink/source buffers for reader and decoder
22static const size_t DEFAULT_TRANSFER_BUFFER_SIZE = 24 * 1024;
23
24enum class AudioPipelineType : uint8_t {
25 MEDIA,
27};
28
29enum class AudioPipelineState : uint8_t {
32 PLAYING,
34 STOPPED,
35 PAUSED,
38};
39
40enum class InfoErrorSource : uint8_t {
41 READER = 0,
42 DECODER,
43};
44
50
51// Used to pass information from each task.
54 optional<esp_err_t> err;
55 optional<audio::AudioFileType> file_type;
56 optional<audio::AudioStreamInfo> audio_stream_info;
57 optional<DecodingError> decoding_err;
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
88
90 void suspend_tasks();
92 void resume_tasks();
93
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
107 std::string base_name_;
108 UBaseType_t priority_;
109
111
112 bool hard_stop_{false};
113 bool is_playing_{false};
114 bool is_finishing_{false};
115 bool pause_state_{false};
117
118 // Pending file start state used to ensure the pipeline fully stops before attempting to start the next file
119 bool pending_url_{false};
120 bool pending_file_{false};
121
123
124 std::string current_uri_{};
126
129
130 size_t buffer_size_; // Ring buffer between reader and decoder
131 size_t transfer_buffer_size_; // Internal source/sink buffers for the audio reader and decoder
132
133 std::weak_ptr<RingBuffer> raw_file_ring_buffer_;
134
135 // Handles basic control/state of the three tasks
136 EventGroupHandle_t event_group_{nullptr};
137
138 // Receives detailed info (file type, stream info, resampling info) or specific errors from the three tasks
139 QueueHandle_t info_error_queue_{nullptr};
140
141 // Handles reading the media file from flash or a url
142 static void read_task(void *params);
144
145 // Decodes the media file into PCM audio
146 static void decode_task(void *params);
148};
149
150} // namespace speaker
151} // namespace esphome
152
153#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)
std::weak_ptr< RingBuffer > raw_file_ring_buffer_
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_
AudioPipelineState process_state()
Processes the state of the audio pipeline based on the info_error_queue_ and event_group_.
uint8_t priority
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
optional< DecodingError > decoding_err
optional< audio::AudioFileType > file_type
optional< audio::AudioStreamInfo > audio_stream_info