ESPHome 2025.5.0
Loading...
Searching...
No Matches
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
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.
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
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
125
126 std::string current_uri_{};
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
static void read_task(void *params)
void suspend_tasks()
Suspends any running tasks.
void set_pause_state(bool pause_state)
void delete_tasks_()
Resets the task related pointers and deallocates their stacks.
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
optional< DecodingError > decoding_err
optional< audio::AudioFileType > file_type
optional< audio::AudioStreamInfo > audio_stream_info