10static const uint32_t DECODING_TIMEOUT_MS = 50;
11static const uint32_t READ_WRITE_TIMEOUT_MS = 20;
13static 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
72#ifdef USE_AUDIO_MP3_SUPPORT
74 this->
mp3_decoder_ = esp_audio_libs::helix_decoder::MP3InitDecoder();
96 return ESP_ERR_NOT_SUPPORTED;
104 if (stop_gracefully) {
119 if (stop_gracefully) {
128 uint32_t decoding_start =
millis();
130 bool first_loop_iteration =
true;
132 size_t bytes_processed = 0;
133 size_t bytes_available_before_processing = 0;
139 size_t bytes_written =
149 delay(READ_WRITE_TIMEOUT_MS);
154 (
millis() - decoding_start > DECODING_TIMEOUT_MS)) {
161 size_t bytes_read = this->
input_transfer_buffer_->transfer_data_from_source(pdMS_TO_TICKS(READ_WRITE_TIMEOUT_MS),
162 first_loop_iteration);
189#ifdef USE_AUDIO_FLAC_SUPPORT
194#ifdef USE_AUDIO_MP3_SUPPORT
209 first_loop_iteration =
false;
225#ifdef USE_AUDIO_FLAC_SUPPORT
230 this->input_transfer_buffer_->available());
232 if (result == esp_audio_libs::flac::FLAC_DECODER_HEADER_OUT_OF_DATA) {
236 if (result != esp_audio_libs::flac::FLAC_DECODER_SUCCESS) {
241 size_t bytes_consumed = this->
flac_decoder_->get_bytes_index();
253 this->flac_decoder_->get_sample_rate());
258 uint32_t output_samples = 0;
261 reinterpret_cast<int16_t *
>(this->output_transfer_buffer_->get_buffer_end()), &output_samples);
263 if (result == esp_audio_libs::flac::FLAC_DECODER_ERROR_OUT_OF_DATA) {
268 size_t bytes_consumed = this->
flac_decoder_->get_bytes_index();
271 if (result > esp_audio_libs::flac::FLAC_DECODER_ERROR_OUT_OF_DATA) {
280 if (result == esp_audio_libs::flac::FLAC_DECODER_NO_MORE_FRAMES) {
288#ifdef USE_AUDIO_MP3_SUPPORT
293 esp_audio_libs::helix_decoder::MP3FindSyncWord(this->
input_transfer_buffer_->get_buffer_start(), buffer_length);
306 int err = esp_audio_libs::helix_decoder::MP3Decode(this->
mp3_decoder_, &buffer_start, &buffer_length,
314 case esp_audio_libs::helix_decoder::ERR_MP3_OUT_OF_MEMORY:
316 case esp_audio_libs::helix_decoder::ERR_MP3_NULL_POINTER:
325 esp_audio_libs::helix_decoder::MP3FrameInfo mp3_frame_info;
326 esp_audio_libs::helix_decoder::MP3GetLastFrameInfo(this->
mp3_decoder_, &mp3_frame_info);
327 if (mp3_frame_info.outputSamps > 0) {
328 int bytes_per_sample = (mp3_frame_info.bitsPerSample / 8);
346 esp_audio_libs::wav_decoder::WAVDecoderResult result = this->
wav_decoder_->decode_header(
349 if (result == esp_audio_libs::wav_decoder::WAV_DECODER_SUCCESS_IN_DATA) {
353 this->
wav_decoder_->bits_per_sample(), this->wav_decoder_->num_channels(), this->wav_decoder_->sample_rate());
358 }
else if (result == esp_audio_libs::wav_decoder::WAV_DECODER_WARNING_INCOMPLETE_DATA) {
374 if (bytes_to_copy > 0) {
optional< AudioStreamInfo > audio_stream_info_
uint32_t accumulated_frames_written_
esp_err_t start(AudioFileType audio_file_type)
Sets up decoding the file.
esp_audio_libs::helix_decoder::HMP3Decoder mp3_decoder_
uint32_t potentially_failed_count_
size_t free_buffer_required_
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_
AudioFileType audio_file_type_
FileDecoderState decode_flac_()
esp_err_t add_source(std::weak_ptr< RingBuffer > &input_ring_buffer)
Adds a source ring buffer for raw file data.
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_
FileDecoderState decode_wav_()
AudioDecoderState decode(bool stop_gracefully)
Decodes audio from the ring buffer source and writes to the sink.
FileDecoderState decode_mp3_()
AudioDecoder(size_t input_buffer_size, size_t output_buffer_size)
Allocates the input and output transfer buffers.
static std::unique_ptr< AudioSinkTransferBuffer > create(size_t buffer_size)
Creates a new sink transfer buffer.
static std::unique_ptr< AudioSourceTransferBuffer > create(size_t buffer_size)
Creates a new source transfer buffer.
Providing packet encoding functions for exchanging data with a remote host.
std::unique_ptr< T > make_unique(Args &&...args)
void IRAM_ATTR HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()