10 static const uint32_t DECODING_TIMEOUT_MS = 50;
11 static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
13 static const uint32_t MAX_POTENTIALLY_FAILED_COUNT = 10;
21 #ifdef USE_AUDIO_MP3_SUPPORT 23 esp_audio_libs::helix_decoder::MP3FreeDecoder(this->
mp3_decoder_);
33 return ESP_ERR_NO_MEM;
41 return ESP_ERR_NO_MEM;
50 return ESP_ERR_NO_MEM;
56 return ESP_ERR_NO_MEM;
65 #ifdef USE_AUDIO_FLAC_SUPPORT 67 this->
flac_decoder_ = make_unique<esp_audio_libs::flac::FLACDecoder>();
72 #ifdef USE_AUDIO_MP3_SUPPORT 74 this->
mp3_decoder_ = esp_audio_libs::helix_decoder::MP3InitDecoder();
75 this->free_buffer_required_ = 1152 *
sizeof(int16_t) * 2;
79 this->
wav_decoder_ = make_unique<esp_audio_libs::wav_decoder::WAVDecoder>();
81 this->free_buffer_required_ = 1024;
85 return ESP_ERR_NOT_SUPPORTED;
93 if (stop_gracefully) {
108 if (stop_gracefully) {
117 uint32_t decoding_start =
millis();
122 size_t bytes_written = this->
output_transfer_buffer_->transfer_data_to_sink(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS));
130 delay(READ_WRITE_TIMEOUT_MS);
135 (
millis() - decoding_start > DECODING_TIMEOUT_MS)) {
141 size_t bytes_read = this->
input_transfer_buffer_->transfer_data_from_source(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS));
158 #ifdef USE_AUDIO_FLAC_SUPPORT 163 #ifdef USE_AUDIO_MP3_SUPPORT 191 #ifdef USE_AUDIO_FLAC_SUPPORT 198 if (result == esp_audio_libs::flac::FLAC_DECODER_HEADER_OUT_OF_DATA) {
202 if (result != esp_audio_libs::flac::FLAC_DECODER_SUCCESS) {
207 size_t bytes_consumed = this->
flac_decoder_->get_bytes_index();
226 uint32_t output_samples = 0;
231 if (result == esp_audio_libs::flac::FLAC_DECODER_ERROR_OUT_OF_DATA) {
236 size_t bytes_consumed = this->
flac_decoder_->get_bytes_index();
239 if (result > esp_audio_libs::flac::FLAC_DECODER_ERROR_OUT_OF_DATA) {
248 if (result == esp_audio_libs::flac::FLAC_DECODER_NO_MORE_FRAMES) {
256 #ifdef USE_AUDIO_MP3_SUPPORT 261 esp_audio_libs::helix_decoder::MP3FindSyncWord(this->
input_transfer_buffer_->get_buffer_start(), buffer_length);
274 int err = esp_audio_libs::helix_decoder::MP3Decode(this->
mp3_decoder_, &buffer_start, &buffer_length,
282 case esp_audio_libs::helix_decoder::ERR_MP3_OUT_OF_MEMORY:
284 case esp_audio_libs::helix_decoder::ERR_MP3_NULL_POINTER:
293 esp_audio_libs::helix_decoder::MP3FrameInfo mp3_frame_info;
294 esp_audio_libs::helix_decoder::MP3GetLastFrameInfo(this->
mp3_decoder_, &mp3_frame_info);
295 if (mp3_frame_info.outputSamps > 0) {
296 int bytes_per_sample = (mp3_frame_info.bitsPerSample / 8);
314 esp_audio_libs::wav_decoder::WAVDecoderResult result = this->
wav_decoder_->decode_header(
317 if (result == esp_audio_libs::wav_decoder::WAV_DECODER_SUCCESS_IN_DATA) {
326 }
else if (result == esp_audio_libs::wav_decoder::WAV_DECODER_WARNING_INCOMPLETE_DATA) {
342 if (bytes_to_copy > 0) {
AudioFileType audio_file_type_
esp_err_t add_source(std::weak_ptr< RingBuffer > &input_ring_buffer)
Adds a source ring buffer for raw file data.
static std::unique_ptr< AudioSinkTransferBuffer > create(size_t buffer_size)
Creates a new sink transfer buffer.
std::unique_ptr< esp_audio_libs::flac::FLACDecoder > flac_decoder_
esp_err_t start(AudioFileType audio_file_type)
Sets up decoding the file.
uint32_t IRAM_ATTR HOT millis()
esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_
FileDecoderState decode_wav_()
esp_err_t add_sink(std::weak_ptr< RingBuffer > &output_ring_buffer)
Adds a sink ring buffer for decoded audio.
uint32_t potentially_failed_count_
~AudioDecoder()
Deallocates the MP3 decoder (the flac and wav decoders are deallocated automatically) ...
std::unique_ptr< AudioSinkTransferBuffer > output_transfer_buffer_
AudioDecoder(size_t input_buffer_size, size_t output_buffer_size)
Allocates the input and output transfer buffers.
static std::unique_ptr< AudioSourceTransferBuffer > create(size_t buffer_size)
Creates a new source transfer buffer.
std::unique_ptr< AudioSourceTransferBuffer > input_transfer_buffer_
std::unique_ptr< esp_audio_libs::wav_decoder::WAVDecoder > wav_decoder_
FileDecoderState decode_flac_()
size_t free_buffer_required_
Implementation of SPI Controller mode.
FileDecoderState decode_mp3_()
uint32_t accumulated_frames_written_
void IRAM_ATTR HOT delay(uint32_t ms)
AudioDecoderState decode(bool stop_gracefully)
Decodes audio from the ring buffer source and writes to the sink.
optional< AudioStreamInfo > audio_stream_info_