ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
audio_file_media_source.h
Go to the documentation of this file.
1#pragma once
2
4
5#ifdef USE_ESP32
6
11
12#include <micro_decoder/decoder_source.h>
13#include <micro_decoder/types.h>
14
15#include <atomic>
16#include <memory>
17#include <string>
18
19namespace esphome::audio_file {
20
21// Inherits from two unrelated listener-style interfaces:
22// - media_source::MediaSource: this source reports state and writes audio *to* an orchestrator
23// (the orchestrator calls set_listener() on us with a MediaSourceListener*).
24// - micro_decoder::DecoderListener: the underlying decoder calls back *into* us with decoded
25// audio and state changes (we call decoder_->set_listener(this) in setup()).
26class AudioFileMediaSource : public Component, public media_source::MediaSource, public micro_decoder::DecoderListener {
27 public:
28 void setup() override;
29 void loop() override;
30 void dump_config() override;
31
32 void set_task_stack_in_psram(bool task_stack_in_psram) { this->decoder_task_stack_in_psram_ = task_stack_in_psram; }
33
34 // MediaSource interface implementation
35 bool play_uri(const std::string &uri) override;
37 bool can_handle(const std::string &uri) const override;
38
39 // DecoderListener interface implementation
40 size_t on_audio_write(const uint8_t *data, size_t length, uint32_t timeout_ms) override;
41 void on_stream_info(const micro_decoder::AudioStreamInfo &info) override;
42 void on_state_change(micro_decoder::DecoderState state) override;
43
44 protected:
45 std::unique_ptr<micro_decoder::DecoderSource> decoder_;
48
49 // Written from the main loop in handle_command(), read from the decoder task in
50 // on_audio_write(). Must be atomic to avoid a data race.
51 std::atomic<bool> pause_{false};
53};
54
55} // namespace esphome::audio_file
56
57#endif // USE_ESP32
bool can_handle(const std::string &uri) const override
void on_state_change(micro_decoder::DecoderState state) override
void handle_command(media_source::MediaSourceCommand command) override
bool play_uri(const std::string &uri) override
std::unique_ptr< micro_decoder::DecoderSource > decoder_
size_t on_audio_write(const uint8_t *data, size_t length, uint32_t timeout_ms) override
void on_stream_info(const micro_decoder::AudioStreamInfo &info) override
void set_task_stack_in_psram(bool task_stack_in_psram)
Abstract base class for media sources MediaSource provides audio data to an orchestrator via the Medi...
bool state
Definition fan.h:2
MediaSourceCommand
Commands that are sent from the orchestrator to a media source.
static void uint32_t
uint16_t length
Definition tt21100.cpp:0