ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
audio_decoder.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
5#include "audio.h"
7
11
12#ifdef USE_SPEAKER
14#endif
15
16#include "esp_err.h"
17
18// micro-flac
19#ifdef USE_AUDIO_FLAC_SUPPORT
20#include <micro_flac/flac_decoder.h>
21#endif
22
23// micro-mp3
24#ifdef USE_AUDIO_MP3_SUPPORT
25#include <micro_mp3/mp3_decoder.h>
26#endif
27
28// micro-opus
29#ifdef USE_AUDIO_OPUS_SUPPORT
30#include <micro_opus/ogg_opus_decoder.h>
31#endif
32
33// micro-wav
34#ifdef USE_AUDIO_WAV_SUPPORT
35#include <micro_wav/wav_decoder.h>
36#endif
37
38namespace esphome::audio {
39
40enum class AudioDecoderState : uint8_t {
41 DECODING = 0, // More data is available to decode
42 FINISHED, // All file data has been decoded and transferred
43 FAILED, // Encountered an error
44};
45
46// Only used within the AudioDecoder class; conveys the state of the particular file type decoder
47enum class FileDecoderState : uint8_t {
48 MORE_TO_PROCESS, // Successsfully read a file chunk and more data is available to decode
49 IDLE, // Not enough data to decode, waiting for more to be transferred
50 POTENTIALLY_FAILED, // Decoder encountered a potentially recoverable error if more file data is available
51 FAILED, // Decoder encoutnered an uncrecoverable error
52 END_OF_FILE, // The specific file decoder knows its the end of the file
53};
54
56 /*
57 * @brief Class that facilitates decoding an audio file.
58 * The audio file is read from a source (ring buffer or const data pointer), decoded, and sent to an audio sink
59 * (ring buffer, speaker component, or callback).
60 * Supports flac, mp3, ogg opus, and wav formats (each enabled independently at compile time).
61 */
62 public:
66 AudioDecoder(size_t input_buffer_size, size_t output_buffer_size);
67
68 ~AudioDecoder() = default;
69
74 esp_err_t add_source(std::weak_ptr<ring_buffer::RingBuffer> &input_ring_buffer);
75
79 esp_err_t add_sink(std::weak_ptr<ring_buffer::RingBuffer> &output_ring_buffer);
80
81#ifdef USE_SPEAKER
85 esp_err_t add_sink(speaker::Speaker *speaker);
86#endif
87
92 esp_err_t add_source(const uint8_t *data_pointer, size_t length);
93
97 esp_err_t add_sink(AudioSinkCallback *callback);
98
103 esp_err_t start(AudioFileType audio_file_type);
104
109 AudioDecoderState decode(bool stop_gracefully);
110
113 const optional<audio::AudioStreamInfo> &get_audio_stream_info() const { return this->audio_stream_info_; }
114
117 uint32_t get_playback_ms() const { return this->playback_ms_; }
118
121 void set_pause_output_state(bool pause_state) { this->pause_output_ = pause_state; }
122
123 protected:
124#ifdef USE_AUDIO_FLAC_SUPPORT
126 std::unique_ptr<micro_flac::FLACDecoder> flac_decoder_;
127#endif
128#ifdef USE_AUDIO_MP3_SUPPORT
130 std::unique_ptr<micro_mp3::Mp3Decoder> mp3_decoder_;
131#endif
132#ifdef USE_AUDIO_OPUS_SUPPORT
134 std::unique_ptr<micro_opus::OggOpusDecoder> opus_decoder_;
135#endif
136#ifdef USE_AUDIO_WAV_SUPPORT
138 std::unique_ptr<micro_wav::WAVDecoder> wav_decoder_;
139#endif
140
141 std::unique_ptr<AudioReadableBuffer> input_buffer_;
142 std::unique_ptr<AudioSinkTransferBuffer> output_transfer_buffer_;
143
145 optional<AudioStreamInfo> audio_stream_info_{};
146
149
153
154 bool end_of_file_{false};
155
156 bool pause_output_{false};
157};
158} // namespace esphome::audio
159
160#endif
optional< AudioStreamInfo > audio_stream_info_
esp_err_t add_source(std::weak_ptr< ring_buffer::RingBuffer > &input_ring_buffer)
Adds a source ring buffer for raw file data.
uint32_t get_playback_ms() const
Returns the duration of audio (in milliseconds) decoded and sent to the sink.
esp_err_t start(AudioFileType audio_file_type)
Sets up decoding the file.
FileDecoderState decode_opus_()
std::unique_ptr< AudioReadableBuffer > input_buffer_
std::unique_ptr< micro_flac::FLACDecoder > flac_decoder_
const optional< audio::AudioStreamInfo > & get_audio_stream_info() const
Gets the audio stream information, if it has been decoded from the files header.
std::unique_ptr< micro_wav::WAVDecoder > wav_decoder_
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
esp_err_t add_sink(std::weak_ptr< ring_buffer::RingBuffer > &output_ring_buffer)
Adds a sink ring buffer for decoded audio.
FileDecoderState decode_flac_()
std::unique_ptr< micro_opus::OggOpusDecoder > opus_decoder_
void set_pause_output_state(bool pause_state)
Pauses sending resampled audio to the sink.
AudioDecoderState decode(bool stop_gracefully)
Decodes audio from the ring buffer source and writes to the sink.
std::unique_ptr< micro_mp3::Mp3Decoder > mp3_decoder_
AudioDecoder(size_t input_buffer_size, size_t output_buffer_size)
Allocates the output transfer buffer and stores the input buffer size for later use by add_source()
Abstract interface for writing decoded audio data to a sink.
static void uint32_t
uint16_t length
Definition tt21100.cpp:0