void app_main(void) { audio_pipeline_handle_t pipeline; audio_element_handle_t i2s_stream_writer, mp3_decoder; esp_log_level_set("*", ESP_LOG_WARN); esp_log_level_set(TAG, ESP_LOG_INFO); ESP_LOGI(TAG, "[ 1 ] Start audio codec chip"); audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ES8388_DEFAULT(); audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 0); audio_hal_ctrl_codec(hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START); ESP_LOGI(TAG, "[ 2 ] Create audio pipeline, add all elements to pipeline, and subscribe pipeline event"); audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG(); pipeline = audio_pipeline_init(&pipeline_cfg); mem_assert(pipeline); ESP_LOGI(TAG, "[2.1] Create mp3 decoder to decode mp3 file and set custom read callback"); mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG(); mp3_decoder = mp3_decoder_init(&mp3_cfg); audio_element_set_read_cb(mp3_decoder, mp3_music_read_cb, NULL); ESP_LOGI(TAG, "[2.2] Create i2s stream to write data to codec chip"); i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT(); i2s_cfg.type = AUDIO_STREAM_WRITER; i2s_stream_writer = i2s_stream_init(&i2s_cfg); ESP_LOGI(TAG, "[2.3] Register all elements to audio pipeline"); audio_pipeline_register(pipeline, mp3_decoder, "mp3"); audio_pipeline_register(pipeline, i2s_stream_writer, "i2s"); ESP_LOGI(TAG, "[2.4] Link it together [mp3_music_read_cb]-->mp3_decoder-->i2s_stream-->[codec_chip]"); audio_pipeline_link(pipeline, (const char *[]) {"mp3", "i2s"}, 2);
struct mp3_decoder* mp3_decoder_create() { struct mp3_decoder* decoder; /* allocate object */ decoder = (struct mp3_decoder*) rt_malloc (sizeof(struct mp3_decoder)); if (decoder != RT_NULL) { mp3_decoder_init(decoder); } return decoder; }