ESPHome 2026.3.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_FLAC_SUPPORT
20#include <flac_decoder.h>
21#endif
22#ifdef USE_AUDIO_MP3_SUPPORT
23#include <mp3_decoder.h>
24#endif
25#include <wav_decoder.h>
26
27// micro-opus
28#ifdef USE_AUDIO_OPUS_SUPPORT
29#include <micro_opus/ogg_opus_decoder.h>
30#endif
31
32namespace esphome {
33namespace audio {
34
35enum class AudioDecoderState : uint8_t {
36 DECODING = 0, // More data is available to decode
37 FINISHED, // All file data has been decoded and transferred
38 FAILED, // Encountered an error
39};
40
41// Only used within the AudioDecoder class; conveys the state of the particular file type decoder
42enum class FileDecoderState : uint8_t {
43 MORE_TO_PROCESS, // Successsfully read a file chunk and more data is available to decode
44 IDLE, // Not enough data to decode, waiting for more to be transferred
45 POTENTIALLY_FAILED, // Decoder encountered a potentially recoverable error if more file data is available
46 FAILED, // Decoder encoutnered an uncrecoverable error
47 END_OF_FILE, // The specific file decoder knows its the end of the file
48};
49
51 /*
52 * @brief Class that facilitates decoding an audio file.
53 * The audio file is read from a source (ring buffer or const data pointer), decoded, and sent to an audio sink
54 * (ring buffer, speaker component, or callback).
55 * Supports wav, flac, mp3, and ogg opus formats.
56 */
57 public:
61 AudioDecoder(size_t input_buffer_size, size_t output_buffer_size);
62
65
69 esp_err_t add_source(std::weak_ptr<RingBuffer> &input_ring_buffer);
70
74 esp_err_t add_sink(std::weak_ptr<RingBuffer> &output_ring_buffer);
75
76#ifdef USE_SPEAKER
80 esp_err_t add_sink(speaker::Speaker *speaker);
81#endif
82
87 esp_err_t add_source(const uint8_t *data_pointer, size_t length);
88
92 esp_err_t add_sink(AudioSinkCallback *callback);
93
98 esp_err_t start(AudioFileType audio_file_type);
99
104 AudioDecoderState decode(bool stop_gracefully);
105
109
112 uint32_t get_playback_ms() const { return this->playback_ms_; }
113
116 void set_pause_output_state(bool pause_state) { this->pause_output_ = pause_state; }
117
118 protected:
119 std::unique_ptr<esp_audio_libs::wav_decoder::WAVDecoder> wav_decoder_;
120#ifdef USE_AUDIO_FLAC_SUPPORT
122 std::unique_ptr<esp_audio_libs::flac::FLACDecoder> flac_decoder_;
123#endif
124#ifdef USE_AUDIO_MP3_SUPPORT
126 esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_;
127#endif
128#ifdef USE_AUDIO_OPUS_SUPPORT
130 std::unique_ptr<micro_opus::OggOpusDecoder> opus_decoder_;
131#endif
133
134 std::unique_ptr<AudioReadableBuffer> input_buffer_;
135 std::unique_ptr<AudioSinkTransferBuffer> output_transfer_buffer_;
136
139
143
146 uint32_t playback_ms_{0};
147
148 bool end_of_file_{false};
150
152
153 bool pause_output_{false};
154};
155} // namespace audio
156} // namespace esphome
157
158#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_
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< esp_audio_libs::flac::FLACDecoder > flac_decoder_
~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
uint16_t length
Definition tt21100.cpp:0