ESPHome 2025.5.0
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
27namespace esphome {
28namespace audio {
29
30enum class AudioDecoderState : uint8_t {
31 DECODING = 0, // More data is available to decode
32 FINISHED, // All file data has been decoded and transferred
33 FAILED, // Encountered an error
34};
35
36// Only used within the AudioDecoder class; conveys the state of the particular file type decoder
37enum class FileDecoderState : uint8_t {
38 MORE_TO_PROCESS, // Successsfully read a file chunk and more data is available to decode
39 IDLE, // Not enough data to decode, waiting for more to be transferred
40 POTENTIALLY_FAILED, // Decoder encountered a potentially recoverable error if more file data is available
41 FAILED, // Decoder encoutnered an uncrecoverable error
42 END_OF_FILE, // The specific file decoder knows its the end of the file
43};
44
46 /*
47 * @brief Class that facilitates decoding an audio file.
48 * The audio file is read from a ring buffer source, decoded, and sent to an audio sink (ring buffer or speaker
49 * component).
50 * Supports wav, flac, and mp3 formats.
51 */
52 public:
56 AudioDecoder(size_t input_buffer_size, size_t output_buffer_size);
57
60
64 esp_err_t add_source(std::weak_ptr<RingBuffer> &input_ring_buffer);
65
69 esp_err_t add_sink(std::weak_ptr<RingBuffer> &output_ring_buffer);
70
71#ifdef USE_SPEAKER
75 esp_err_t add_sink(speaker::Speaker *speaker);
76#endif
77
82 esp_err_t start(AudioFileType audio_file_type);
83
88 AudioDecoderState decode(bool stop_gracefully);
89
93
96 uint32_t get_playback_ms() const { return this->playback_ms_; }
97
100 void set_pause_output_state(bool pause_state) { this->pause_output_ = pause_state; }
101
102 protected:
103 std::unique_ptr<esp_audio_libs::wav_decoder::WAVDecoder> wav_decoder_;
104#ifdef USE_AUDIO_FLAC_SUPPORT
106 std::unique_ptr<esp_audio_libs::flac::FLACDecoder> flac_decoder_;
107#endif
108#ifdef USE_AUDIO_MP3_SUPPORT
110 esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_;
111#endif
113
114 std::unique_ptr<AudioSourceTransferBuffer> input_transfer_buffer_;
115 std::unique_ptr<AudioSinkTransferBuffer> output_transfer_buffer_;
116
119
122
124 bool end_of_file_{false};
126
127 bool pause_output_{false};
128
130 uint32_t playback_ms_{0};
131};
132} // namespace audio
133} // namespace esphome
134
135#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_
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 and wav decoders are deallocated automatically)
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
FileDecoderState decode_flac_()
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.
std::unique_ptr< AudioSourceTransferBuffer > input_transfer_buffer_
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 input and output transfer buffers.
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7