text: /* Play an MP3 file from HTTP and save it to SD card
  
     This example code is in the Public Domain (or CC0 licensed, at your option.)
  
     Unless required by applicable law or agreed to in writing, this
     software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
     CONDITIONS OF ANY KIND, either express or implied.
  */
  
  #include <string.h>
  #include "freertos/FreeRTOS.h"
  #include "freertos/task.h"
  #include "freertos/event_groups.h"
  #include "esp_log.h"
  #include "esp_wifi.h"
  #include "nvs_flash.h"
  #include "audio_element.h"
  #include "audio_pipeline.h"
  #include "audio_event_iface.h"
  #include "audio_common.h"
  #include "http_stream.h"
  #include "i2s_stream.h"
  #include "mp3_decoder.h"
  #include "fatfs_stream.h"
  #include "raw_stream.h"
  
  #include "esp_peripherals.h"
  #include "periph_wifi.h"
  #include "periph_sdcard.h"
  #include "board.h"
  
  static const char *TAG = "PLAY_AND_SAVE_FILE";
  
  void app_main(void)
  {
      esp_err_t err = nvs_flash_init();
      if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
          // NVS partition was truncated and needs to be erased
          // Retry nvs_flash_init
          ESP_ERROR_CHECK(nvs_flash_erase());
          err = nvs_flash_init();
      }
      tcpip_adapter_init();
  
      audio_pipeline_handle_t pipeline;
      audio_element_handle_t http_stream_reader, i2s_stream_writer, mp3_decoder;
  
      esp_log_level_set("*", ESP_LOG_INFO);
      esp_log_level_set(TAG, ESP_LOG_DEBUG);
  
      ESP_LOGI(TAG, "[ 1 ] Start audio codec chip");
      audio_board_handle_t board_handle = audio_board_init();
      audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
  
      ESP_LOGI(TAG, "[2.0] Create audio pipeline for playback");
      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 http stream to read data");
      http_stream_cfg_t http_cfg = HTTP_STREAM_CFG_DEFAULT();
      http_stream_reader = http_stream_init(&http_cfg);
  
      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] Create mp3 decoder to decode mp3 file");
      mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG();
      mp3_decoder = mp3_decoder_init(&mp3_cfg);
  
      ESP_LOGI(TAG, "[2.4] Register all elements to audio pipeline");
      audio_pipeline_register(pipeline, http_stream_reader, "http");
      audio_pipeline_register(pipeline, mp3_decoder,        "mp3");
      audio_pipeline_register(pipeline, i2s_stream_writer,  "i2s");
  
      ESP_LOGI(TAG, "[2.5] Link elements together http_stream-->mp3_decoder-->i2s_stream-->[codec_chip]");
      audio_pipeline_link(pipeline, (const char *[]) {"http", "mp3", "i2s"}, 3);
      ESP_LOGI(TAG, "[2.6] Set up  uri (http as http_stream, mp3 as mp3 decoder, and default output is i2s)");
      audio_element_set_uri(http_stream_reader, "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3");
  
      ESP_LOGI(TAG, "[3.0] Create fatfs stream to save file");
      fatfs_stream_cfg_t fatfs_cfg = FATFS_STREAM_CFG_DEFAULT();
      fatfs_cfg.type = AUDIO_STREAM_WRITER;
      audio_element_handle_t el_fatfs_wr_stream = fatfs_stream_init(&fatfs_cfg);
  
      ESP_LOGI(TAG, "[3.1] Create raw stream to read data");
      raw_stream_cfg_t raw_cfg = RAW_STREAM_CFG_DEFAULT();
      raw_cfg.type = AUDIO_STREAM_WRITER;
      audio_element_handle_t el_raw_read = raw_stream_init(&raw_cfg);
  
      ESP_LOGI(TAG, "[3.2] Create pipeline to save audio file");
      audio_pipeline_cfg_t pipeline_save_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
      audio_pipeline_handle_t pipeline_save = audio_pipeline_init(&pipeline_save_cfg);
  
      ESP_LOGI(TAG, "[3.3] Set up  uri for save file name");
      audio_element_set_uri(el_fatfs_wr_stream, "/sdcard/test_output.mp3");
  
      ESP_LOGI(TAG, "[3.4] Register all elements to pipeline_save");
      audio_pipeline_register(pipeline_save, el_raw_read, "raw");
      audio_pipeline_register(pipeline_save, el_fatfs_wr_stream, "file");
  
      ESP_LOGI(TAG, "[3.5] Link elements together raw_stream-->fatfs_stream");
      audio_pipeline_link(pipeline_save, (const char *[]) {"raw", "file"}, 2);
  
      ESP_LOGI(TAG, "[3.6] Connect input ringbuffer of pipeline_save to http stream multi output");
      ringbuf_handle_t rb = audio_element_get_output_ringbuf(el_raw_read);
      audio_element_set_multi_output_ringbuf(http_stream_reader, rb, 0);
  
      ESP_LOGI(TAG, "[4.0] Start and wait for Wi-Fi network");
      esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
      esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
      periph_wifi_cfg_t wifi_cfg = {
          .ssid = CONFIG_WIFI_SSID,
          .password = CONFIG_WIFI_PASSWORD,
      };
      esp_periph_handle_t wifi_handle = periph_wifi_init(&wifi_cfg);
      esp_periph_start(set, wifi_handle);
      periph_wifi_wait_for_connected(wifi_handle, portMAX_DELAY);
  
      ESP_LOGI(TAG, "[4.1] Start and wait for SDCARD to mount");
      periph_sdcard_cfg_t sdcard_cfg = {
          .root = "/sdcard",
          .card_detect_pin = get_sdcard_intr_gpio(), // GPIO_NUM_34
      };
      esp_periph_handle_t sdcard_handle = periph_sdcard_init(&sdcard_cfg);
      esp_periph_start(set, sdcard_handle);
      while (!periph_sdcard_is_mounted(sdcard_handle)) {
          vTaskDelay(100 / portTICK_PERIOD_MS);
      }
  
      ESP_LOGI(TAG, "[ 5 ] Set up  event listener");
      audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
      audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  
      ESP_LOGI(TAG, "[5.1] Listening event from all elements of audio pipeline");
      audio_pipeline_set_listener(pipeline, evt);
  
      ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
      audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);
  
      ESP_LOGI(TAG, "[ 6 ] Start pipelines");
      audio_pipeline_run(pipeline);
      audio_pipeline_run(pipeline_save);
  
      while (1) {
          audio_event_iface_msg_t msg;
          esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
          if (ret != ESP_OK) {
              ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
              continue;
          }
  
          if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT
              && msg.source == (void *) mp3_decoder
              && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
              audio_element_info_t music_info = {0};
              audio_element_getinfo(mp3_decoder, &music_info);
  
              ESP_LOGI(TAG, "[ * ] Receive music info from mp3 decoder, sample_rates=%d, bits=%d, ch=%d",
                       music_info.sample_rates, music_info.bits, music_info.channels);
  
              audio_element_setinfo(i2s_stream_writer, &music_info);
              i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
              continue;
          }
  
          /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
          if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
              && msg.cmd == AEL_MSG_CMD_REPORT_STATUS
              && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) {
              ESP_LOGW(TAG, "[ * ] Stop event received");
              break;
          }
      }
  
      ESP_LOGI(TAG, "[ 7 ] Stop pipelines");
      audio_pipeline_terminate(pipeline);
      audio_pipeline_unregister_more(pipeline, http_stream_reader,
                                     mp3_decoder, i2s_stream_writer, NULL);
      audio_pipeline_unregister_more(pipeline_save, el_raw_read, el_fatfs_wr_stream, NULL);
      audio_pipeline_remove_listener(pipeline);
  
      /* Stop all peripherals before removing the listener */
      esp_periph_set_stop_all(set);
      audio_event_iface_remove_listener(esp_periph_set_get_event_iface(set), evt);
  
      /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
      audio_event_iface_destroy(evt);
  
      /* Release all resources */
      audio_pipeline_deinit(pipeline);
      audio_element_deinit(http_stream_reader);
      audio_element_deinit(i2s_stream_writer);
      audio_element_deinit(mp3_decoder);
  
      audio_pipeline_deinit(pipeline_save);
      audio_element_deinit(el_raw_read);
      audio_element_deinit(el_fatfs_wr_stream);
      esp_periph_set_destroy(set);
  }
  