ESPHome 2026.5.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// esp-audio-libs
19#ifdef USE_AUDIO_MP3_SUPPORT
20#include <mp3_decoder.h>
21#endif
22#include <wav_decoder.h>
23
24// micro-flac
25#ifdef USE_AUDIO_FLAC_SUPPORT
26#include <micro_flac/flac_decoder.h>
27#endif
28
29// micro-opus
30#ifdef USE_AUDIO_OPUS_SUPPORT
31#include <micro_opus/ogg_opus_decoder.h>
32#endif
33
34namespace esphome {
35namespace audio {
36
37enum class AudioDecoderState : uint8_t {
38 DECODING = 0, // More data is available to decode
39 FINISHED, // All file data has been decoded and transferred
40 FAILED, // Encountered an error
41};
42
43// Only used within the AudioDecoder class; conveys the state of the particular file type decoder
44enum class FileDecoderState : uint8_t {
45 MORE_TO_PROCESS, // Successsfully read a file chunk and more data is available to decode
46 IDLE, // Not enough data to decode, waiting for more to be transferred
47 POTENTIALLY_FAILED, // Decoder encountered a potentially recoverable error if more file data is available
48 FAILED, // Decoder encoutnered an uncrecoverable error
49 END_OF_FILE, // The specific file decoder knows its the end of the file
50};
51
53 /*
54 * @brief Class that facilitates decoding an audio file.
55 * The audio file is read from a source (ring buffer or const data pointer), decoded, and sent to an audio sink
56 * (ring buffer, speaker component, or callback).
57 * Supports wav, flac, mp3, and ogg opus formats.
58 */
59 public:
63 AudioDecoder(size_t input_buffer_size, size_t output_buffer_size);
64
67
71 esp_err_t add_source(std::weak_ptr<RingBuffer> &input_ring_buffer);
72
76 esp_err_t add_sink(std::weak_ptr<RingBuffer> &output_ring_buffer);
77
78#ifdef USE_SPEAKER
82 esp_err_t add_sink(speaker::Speaker *speaker);
83#endif
84
89 esp_err_t add_source(const uint8_t *data_pointer, size_t length);
90
94 esp_err_t add_sink(AudioSinkCallback *callback);
95
100 esp_err_t start(AudioFileType audio_file_type);
101
106 AudioDecoderState decode(bool stop_gracefully);
107
110 const optional<audio::AudioStreamInfo> &get_audio_stream_info() const { return this->audio_stream_info_; }
111
114 uint32_t get_playback_ms() const { return this->playback_ms_; }
115
118 void set_pause_output_state(bool pause_state) { this->pause_output_ = pause_state; }
119
120 protected:
121 std::unique_ptr<esp_audio_libs::wav_decoder::WAVDecoder> wav_decoder_;
122#ifdef USE_AUDIO_FLAC_SUPPORT
124 std::unique_ptr<micro_flac::FLACDecoder> flac_decoder_;
125#endif
126#ifdef USE_AUDIO_MP3_SUPPORT
128 esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_;
129#endif
130#ifdef USE_AUDIO_OPUS_SUPPORT
132 std::unique_ptr<micro_opus::OggOpusDecoder> opus_decoder_;
133#endif
135
136 std::unique_ptr<AudioReadableBuffer> input_buffer_;
137 std::unique_ptr<AudioSinkTransferBuffer> output_transfer_buffer_;
138
140 optional<AudioStreamInfo> audio_stream_info_{};
141
145
149
150 bool end_of_file_{false};
152
154
155 bool pause_output_{false};
156};
157} // namespace audio
158} // namespace esphome
159
160#endif
optional< AudioStreamInfo > audio_stream_info_
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.
esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_
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.
~AudioDecoder()
Deallocates the MP3 decoder (the flac, opus, and wav decoders are deallocated automatically)
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
FileDecoderState decode_flac_()
std::unique_ptr< micro_opus::OggOpusDecoder > opus_decoder_
esp_err_t add_source(std::weak_ptr< RingBuffer > &input_ring_buffer)
Adds a source ring buffer for raw file data.
void set_pause_output_state(bool pause_state)
Pauses sending resampled audio to the sink.
std::unique_ptr< esp_audio_libs::wav_decoder::WAVDecoder > wav_decoder_
esp_err_t add_sink(std::weak_ptr< RingBuffer > &output_ring_buffer)
Adds a sink ring buffer for decoded audio.
AudioDecoderState decode(bool stop_gracefully)
Decodes audio from the ring buffer source and writes to the sink.
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.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
static void uint32_t
uint16_t length
Definition tt21100.cpp:0