ESPHome  2025.2.0
audio_decoder.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
5 #include "audio.h"
7 
8 #include "esphome/core/defines.h"
9 #include "esphome/core/helpers.h"
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 namespace esphome {
28 namespace audio {
29 
30 enum 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
37 enum 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 
45 class AudioDecoder {
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 
59  ~AudioDecoder();
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
72  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 
92  const optional<audio::AudioStreamInfo> &get_audio_stream_info() const { return this->audio_stream_info_; }
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
105  FileDecoderState decode_flac_();
106  std::unique_ptr<esp_audio_libs::flac::FLACDecoder> flac_decoder_;
107 #endif
108 #ifdef USE_AUDIO_MP3_SUPPORT
109  FileDecoderState decode_mp3_();
110  esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_;
111 #endif
112  FileDecoderState decode_wav_();
113 
114  std::unique_ptr<AudioSourceTransferBuffer> input_transfer_buffer_;
115  std::unique_ptr<AudioSinkTransferBuffer> output_transfer_buffer_;
116 
118  optional<AudioStreamInfo> audio_stream_info_{};
119 
120  size_t free_buffer_required_{0};
121  size_t wav_bytes_left_{0};
122 
123  uint32_t potentially_failed_count_{0};
124  bool end_of_file_{false};
125  bool wav_has_known_end_{false};
126 
127  bool pause_output_{false};
128 
129  uint32_t accumulated_frames_written_{0};
130  uint32_t playback_ms_{0};
131 };
132 } // namespace audio
133 } // namespace esphome
134 
135 #endif
void set_pause_output_state(bool pause_state)
Pauses sending resampled audio to the sink.
std::unique_ptr< esp_audio_libs::flac::FLACDecoder > flac_decoder_
esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
const optional< audio::AudioStreamInfo > & get_audio_stream_info() const
Gets the audio stream information, if it has been decoded from the files header.
Definition: audio_decoder.h:92
std::unique_ptr< AudioSourceTransferBuffer > input_transfer_buffer_
std::unique_ptr< esp_audio_libs::wav_decoder::WAVDecoder > wav_decoder_
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
uint32_t get_playback_ms() const
Returns the duration of audio (in milliseconds) decoded and sent to the sink.
Definition: audio_decoder.h:96