when i select different modes from streaming modes am seeaing an error

reventer
Posts: 1
Joined: Sun Jul 30, 2023 10:59 am

when i select different modes from streaming modes am seeaing an error

Postby reventer » Tue Aug 15, 2023 12:12 pm

  1. [code]
  2. #include <inttypes.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "freertos/queue.h"
  6. #include "esp_log.h"
  7. #include "esp_http_client.h"
  8. #include "esp_peripherals.h"
  9. #include "periph_sdcard.h"
  10. #include "audio_pipeline.h"
  11. #include "audio_element.h"
  12. #include "audio_event_iface.h"
  13. #include "audio_common.h"
  14. #include "fatfs_stream.h"
  15. #include "i2s_stream.h"
  16. #include "http_stream.h"
  17. #include "esp_adc/adc_oneshot.h"
  18. #include "periph_wifi.h"
  19. #include "raw_stream.h"
  20.  
  21. #include "mp3_decoder.h"
  22. #include "audio_mem.h"
  23. #include "driver/gpio.h"
  24. #include <string.h>
  25. #include "board.h"
  26. #include "driver/i2s_std.h"
  27. #include "sdkconfig.h"
  28. #include "wav_encoder.h"
  29. #include "wav_decoder.h"
  30. #include "esp_wifi.h"
  31. #include "wifi_service.h"
  32. #include "periph_adc_button.h"
  33. #include "input_key_service.h"
  34. #include "esp_idf_version.h"
  35. #include "esp_err.h"
  36. #include "nvs_flash.h"
  37. #include <esp_sleep.h>
  38. #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0))
  39. #include "esp_netif.h"
  40. #endif
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47. #define RECORD_TIME_SECONDS (120) // 2 minutes in seconds
  48. #define HTTP_MAX_TIME_SECONDS (300) // 5 minutes in seconds
  49. #define HTTP_MAX_SIZE_BYTES (15 * 1024 * 1024) // 15 MB in bytes
  50. #define AUDIO_SAMPLE_RATE  (16000)
  51.  
  52. static const char *TAG = "MODE_SELECTION";
  53. #define CONFIG_WIFI_SSID "Pixel_2261"
  54. #define CONFIG_WIFI_PASSWORD "123412341234"
  55.  
  56.  
  57. #define MODE_SELECTION_TIMEOUT_SECONDS (5 * 60)  // 5 minutes in seconds
  58. #define SLEEP_TIMEOUT_SECONDS (5 * 60)          // 5 minutes in seconds
  59.  
  60.  
  61.  audio_element_handle_t i2s_stream_reader, fatfs_stream_writer, wav_encoder, fatfs_stream_reader, http_stream_reader,i2s_stream_writer,wav_decoder,mp3_decoder;
  62. esp_periph_set_handle_t set;
  63.  audio_event_iface_msg_t msg;
  64. esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
  65. static bool is_recording = false;
  66. static bool is_playing_sd = false;
  67. static bool is_playing_http = false;
  68.  
  69. bool toogle_switch=false;
  70. bool r= false;
  71. int mode = 1;
  72. const char *filename;
  73. const char *pipeline;
  74. const char *element;
  75. enum wifi_service_event_t;
  76.  
  77.  
  78. static void record_audio(void);
  79. static void play_from_http(void);
  80. static void play_from_sd_card(const char *filename);
  81.  void wait_for_mode_selection(void);
  82.  
  83. //**********************************************************************
  84. const int NUM_MODES = 3;
  85. //**********************************************************************
  86. void app_main(void)
  87. {
  88.  
  89.     esp_log_level_set("*", ESP_LOG_WARN);
  90.     esp_log_level_set(TAG, ESP_LOG_INFO);
  91.  
  92.     ESP_LOGI(TAG, "[ 1 ] nvs flash");
  93.     esp_err_t err = nvs_flash_init();
  94.     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  95.         ESP_ERROR_CHECK(nvs_flash_erase());
  96.         err = nvs_flash_init();
  97.     }
  98.     #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(4, 1, 0))
  99.     ESP_ERROR_CHECK(esp_netif_init());
  100.     #endif
  101.  
  102.  
  103.     ESP_LOGI(TAG, "[ 2 ] Start codec chip");
  104.     audio_board_handle_t board_handle = audio_board_init();
  105.     audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);
  106.  
  107.     esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
  108.     esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
  109.  
  110.     ESP_LOGI(TAG, "[3.1] Initialize keys on board");
  111.     audio_board_key_init(set);
  112.  
  113.     ESP_LOGI(TAG, "[ 3 ] Start and wait for Wi-Fi network");
  114.    
  115.      periph_wifi_cfg_t wifi_cfg = {
  116.         .ssid = CONFIG_WIFI_SSID,
  117.         .password = CONFIG_WIFI_PASSWORD,
  118.     };
  119.     esp_periph_handle_t wifi_handle = periph_wifi_init(&wifi_cfg);;
  120.     esp_periph_start(set, wifi_handle);
  121.     periph_wifi_wait_for_connected(wifi_handle,1000/portMAX_DELAY);
  122.     if (periph_wifi_is_connected(wifi_handle) == PERIPH_WIFI_CONNECTED) {
  123.         r=true;}
  124.         else{r=false;}
  125.  
  126.     ESP_LOGI(TAG, "[4] Mount sdcard");
  127.     // Initialize SD Card peripheral
  128.     audio_board_sdcard_init(set, SD_MODE_1_LINE);
  129.  
  130.      ESP_LOGI(TAG, "[ 5 ] Create and start input key service");
  131.      audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();  
  132.     audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  133.     audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);
  134.     audio_event_iface_msg_t msg;
  135.     ESP_LOGI(TAG, "[ 5 ] mode selection");
  136.      if (audio_event_iface_listen(evt, &msg, portMAX_DELAY) == ESP_OK) {
  137.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && (msg.cmd == PERIPH_ADC_BUTTON_PRESSED || msg.cmd == PERIPH_ADC_BUTTON_PRESSED)){        
  138.                   // Cycle through modes
  139.                  // Update mode based on current mode index
  140.                 mode = (mode + 1) % NUM_MODES;
  141.                switch (mode) {
  142.                 case 1:if (r==true) {
  143.                             is_recording = false;
  144.                             is_playing_sd = false;
  145.                             is_playing_http = true;
  146.                             play_from_http();
  147.                             ESP_LOGI(TAG, "Selected mode: Play from HTTP");
  148.                         } else {
  149.                             is_recording = false;
  150.                             is_playing_sd = true;
  151.                             is_playing_http = false;
  152.                             play_from_sd_card( "/sdcard/httpmusic.mp3"); // Replace with the appropriate SD card filename
  153.                             ESP_LOGI(TAG, "Selected mode: Play from SD Card (Wi-Fi not connected)");
  154.                         }
  155.                         break;
  156.                 case 2:
  157.                         is_recording = true;
  158.                         is_playing_sd = false;
  159.                         is_playing_http = false;
  160.                         record_audio();
  161.                         ESP_LOGI(TAG, "Selected mode: Audio Recording");
  162.                         break;
  163.                 case 3:
  164.                         is_recording = false;
  165.                         is_playing_sd = true;
  166.                         is_playing_http = false;
  167.                         play_from_sd_card("/sdcard/rec.wav");
  168.                         ESP_LOGI(TAG, "Selected mode: Play from SD Card");
  169.                         break;
  170.                 default:
  171.                         is_recording = false;
  172.                         is_playing_sd = true;
  173.                         is_playing_http = false;
  174.                         play_from_sd_card("/sdcard/rec.wav");
  175.                         ESP_LOGI(TAG, "Selected mode: Play from SD Card");
  176.                         break;
  177.            
  178.                         }
  179.         }
  180.     }
  181.  
  182.  
  183.  
  184.     wait_for_mode_selection();
  185.  
  186.     // Release all resources
  187.     ESP_LOGI(TAG, "[7] terminating everything");
  188.    
  189.     audio_element_deinit(i2s_stream_reader);
  190.     audio_element_deinit(i2s_stream_writer);
  191.     audio_element_deinit(http_stream_reader);
  192.     audio_element_deinit(mp3_decoder);
  193.     audio_element_deinit(wav_encoder);
  194.     audio_element_deinit(wav_decoder);
  195.     audio_element_deinit(fatfs_stream_writer);
  196.     audio_element_deinit(fatfs_stream_reader);
  197. }
  198. //**********************************************************************
  199. //**********************************************************************
  200. static void record_audio(void) {
  201.   is_recording = true;
  202.  
  203.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  204.     audio_pipeline_handle_t rec_pipeline = audio_pipeline_init(&pipeline_cfg);
  205.     mem_assert(rec_pipeline);
  206.  // Create I2S stream to read audio data from codec chip
  207.   i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
  208.   i2s_cfg.type = AUDIO_STREAM_READER;
  209.   i2s_cfg.i2s_config.sample_rate = AUDIO_SAMPLE_RATE;
  210.   i2s_stream_reader = i2s_stream_init(&i2s_cfg);
  211.  
  212.  
  213.  // Create WAV encoder to encode audio data in WAV format
  214.   wav_encoder_cfg_t wav_cfg = DEFAULT_WAV_ENCODER_CONFIG(); // You need to replace this with the actual function for initializing the WAV encoder
  215.   audio_element_handle_t wav_encoder = wav_encoder_init(&wav_cfg);
  216.  
  217. // Create FATFS stream to write WAV data to SD card
  218.   ESP_LOGI(TAG, "[3.1] Create fatfs stream to write data to sdcard");
  219.   fatfs_stream_cfg_t fatfs_cfg = FATFS_STREAM_CFG_DEFAULT();
  220.   fatfs_cfg.type = AUDIO_STREAM_WRITER;
  221.   fatfs_stream_writer = fatfs_stream_init(&fatfs_cfg);
  222.  
  223. // Register all elements to the audio pipeline
  224.   audio_pipeline_register(rec_pipeline, i2s_stream_reader, "i2s");
  225.   audio_pipeline_register(rec_pipeline, wav_encoder, "wav"); // Update the tag to "wav"
  226.   audio_pipeline_register(rec_pipeline, fatfs_stream_writer, "file");
  227.  
  228. // Link elements together: [codec_chip]-->i2s_stream-->filter-->wav_encoder-->fatfs_stream-->[sdcard]
  229.   const char *link_tag[4] = {"i2s", "wav", "file"}; // Update the tag to "wav"
  230.   audio_pipeline_link(rec_pipeline, &link_tag[0], 3);
  231.  
  232.  
  233.  ESP_LOGI(TAG, "[3.6] Set music info to fatfs");
  234.  audio_element_info_t music_info = {0};
  235.   audio_element_getinfo(i2s_stream_reader, &music_info);
  236.   ESP_LOGI(TAG, "[ * ] Save the recording info to the fatfs stream writer, sample_rates=%d, bits=%d, ch=%d",
  237.   music_info.sample_rates, music_info.bits, music_info.channels);
  238.   audio_element_setinfo(fatfs_stream_writer, &music_info);
  239.  
  240.     audio_element_set_uri(fatfs_stream_writer, "/sdcard/rec.wav");
  241.  
  242. ESP_LOGI(TAG, "[ 1] Set up  event listener");
  243. audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  244. audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  245.  
  246. ESP_LOGI(TAG, "[1.1] Listening event from pipeline");
  247. audio_pipeline_set_listener(rec_pipeline, evt);
  248.  
  249. ESP_LOGI(TAG, "[1.2] Listening event from peripherals");
  250. audio_event_iface_set_listener(esp_periph_set_get_event_iface( set), evt);
  251.  
  252. while (true) {
  253.        
  254.         esp_err_t ret = audio_event_iface_listen(evt, &msg, 0);  // Non-blocking read
  255.         if (ret != ESP_OK) {
  256.             ESP_LOGE(TAG, "Error reading event from queue: %d", ret);
  257.             break;
  258.         }
  259.     }
  260.     ESP_LOGI(TAG, "Event listener queue cleared");
  261.  
  262. // Start audio pipeline
  263. audio_pipeline_run(rec_pipeline);
  264.  
  265.  
  266.  ESP_LOGI(TAG, "[ 1.3 ] Listen for all pipeline events, record for %d Seconds", RECORD_TIME_SECONDS);
  267. int second_recorded = 0;
  268.     while (1) {
  269.         if (audio_event_iface_listen(evt, &msg, 1000 / portTICK_RATE_MS) != ESP_OK) {
  270.             second_recorded ++;
  271.             ESP_LOGI(TAG, "[ * ] Recording ... %d", second_recorded);
  272.             if (second_recorded >= RECORD_TIME_SECONDS) {
  273.                 audio_element_set_ringbuf_done(i2s_stream_reader);
  274.             }
  275.             continue;
  276.         }
  277.         /* Stop when the last pipeline element (fatfs_stream_writer in this case) receives stop event */
  278.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) fatfs_stream_writer
  279.             && msg.cmd == AEL_MSG_CMD_REPORT_STATUS
  280.             && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED)
  281.                 || ((int)msg.data == AEL_STATUS_ERROR_OPEN))) {
  282.             ESP_LOGW(TAG, "[ * ] Stop event received");
  283.             break;
  284.         }
  285.         /*pause or play button*/
  286.          if (((int)msg.data == get_input_play_id()) && (msg.cmd == PERIPH_ADC_BUTTON_PRESSED || msg.cmd == PERIPH_ADC_BUTTON_PRESSED)) {
  287.             toogle_switch = !toogle_switch;
  288.             audio_pipeline_pause(rec_pipeline);
  289.             ESP_LOGE(TAG,  "");
  290.             if (toogle_switch) {
  291.                audio_pipeline_pause(rec_pipeline);
  292.             } else {
  293.                 audio_pipeline_resume(rec_pipeline);
  294.             }
  295.             audio_pipeline_run(rec_pipeline);
  296.             ESP_LOGE(TAG, "[ 4.1 ] Start playback new pipeline");
  297.         }
  298.     }
  299.  
  300.  is_recording = false;
  301.     audio_pipeline_stop(rec_pipeline);
  302.     audio_pipeline_wait_for_stop(rec_pipeline);
  303.     audio_pipeline_terminate(rec_pipeline);
  304.      audio_pipeline_unregister(rec_pipeline, i2s_stream_reader);
  305.     audio_pipeline_unregister(rec_pipeline, wav_encoder);
  306.     audio_pipeline_unregister(rec_pipeline, fatfs_stream_writer);
  307.     audio_pipeline_remove_listener(rec_pipeline);
  308.     audio_event_iface_remove_listener(esp_periph_set_get_event_iface(set), evt);
  309.     audio_event_iface_destroy(evt);
  310.     audio_pipeline_deinit(rec_pipeline);
  311.     vTaskDelete(NULL);
  312. }
  313. //**********************************************************************
  314. static void play_from_sd_card(const char *filename){
  315.  
  316.  is_playing_sd = true;
  317. ESP_LOGI(TAG, "[3.1] Create fatfs stream to read data from SD card");
  318. audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  319. audio_pipeline_handle_t sd_pipeline = audio_pipeline_init(&pipeline_cfg);
  320. mem_assert(sd_pipeline);
  321.  
  322. ESP_LOGI(TAG, "[3.1] Create fatfs stream to read data from SD card");
  323. fatfs_stream_cfg_t fatfs_cfg = FATFS_STREAM_CFG_DEFAULT();
  324. fatfs_cfg.type = AUDIO_STREAM_READER;
  325. fatfs_stream_reader = fatfs_stream_init(&fatfs_cfg);
  326.  
  327.  
  328. ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
  329. i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
  330. i2s_cfg.type = AUDIO_STREAM_WRITER;
  331. i2s_stream_writer = i2s_stream_init(&i2s_cfg);
  332.  
  333. ESP_LOGI(TAG, "[3.3] Create wav decoder");
  334. wav_decoder_cfg_t  wav_dec_cfg  = DEFAULT_WAV_DECODER_CONFIG();
  335. audio_element_handle_t wav_decoder = wav_decoder_init(&wav_dec_cfg);
  336.  
  337. ESP_LOGI(TAG, "[3.4] Register all elements to audio pipeline");
  338. audio_pipeline_register(sd_pipeline, fatfs_stream_reader, "file");
  339. audio_pipeline_register(sd_pipeline, wav_decoder, "wav");
  340. audio_pipeline_register(sd_pipeline, i2s_stream_writer, "i2s");
  341.  
  342. ESP_LOGI(TAG, "[3.5] Link elements together: [sdcard]-->fatfs_stream-->wav_decoder-->i2s_stream-->[codec_chip]");
  343. const char *link_tag[3] = {"file", "wav", "i2s"};
  344. audio_pipeline_link(sd_pipeline, &link_tag[0], 3);
  345.  
  346.  ESP_LOGI(TAG, "[3.6] Set up uri:test.wav");
  347.     audio_element_set_uri(fatfs_stream_reader, filename);
  348.  
  349. ESP_LOGI(TAG, "[3.6] Set up event listener");
  350. audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  351. audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  352. audio_pipeline_set_listener(sd_pipeline, evt);
  353. ESP_LOGI(TAG, "[1.2] Listening event from peripherals");
  354. audio_event_iface_set_listener(esp_periph_set_get_event_iface( set), evt);
  355.  
  356.  
  357.  
  358. ESP_LOGI(TAG, "[3.7] Start audio pipeline");
  359. audio_pipeline_run(sd_pipeline);
  360.  
  361.  
  362.  ESP_LOGI(TAG, "[ 6 ] Listen for all pipeline events");
  363.     while (1) {
  364.         audio_event_iface_msg_t msg;
  365.         esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
  366.         if (ret != ESP_OK) {
  367.             ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
  368.             continue;
  369.         }
  370.  
  371.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) wav_decoder
  372.             && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
  373.             audio_element_info_t music_info = {0};
  374.             audio_element_getinfo(wav_decoder, &music_info);
  375.  
  376.             ESP_LOGI(TAG, "[ * ] Receive music info from mp3 decoder, sample_rates=%d, bits=%d, ch=%d",
  377.                      music_info.sample_rates, music_info.bits, music_info.channels);
  378.  
  379.             audio_element_setinfo(i2s_stream_writer, &music_info);
  380.             i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
  381.             continue;
  382.         }
  383.  
  384.         /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  385.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
  386.             && msg.cmd == AEL_MSG_CMD_REPORT_STATUS
  387.             && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) {
  388.             ESP_LOGW(TAG, "[ * ] Stop event received");
  389.             break;
  390.         }
  391.         /*play puase button*/
  392.  
  393.          if (((int)msg.data == get_input_play_id()) && (msg.cmd == PERIPH_ADC_BUTTON_PRESSED || msg.cmd == PERIPH_ADC_BUTTON_PRESSED)) {
  394.             toogle_switch = !toogle_switch;
  395.             audio_pipeline_pause(sd_pipeline);
  396.             ESP_LOGE(TAG,  "");
  397.             if (toogle_switch) {
  398.                audio_pipeline_pause(sd_pipeline);
  399.             } else {
  400.                 audio_pipeline_resume(sd_pipeline);
  401.             }
  402.             audio_pipeline_run(sd_pipeline);
  403.             ESP_LOGE(TAG, "[ 4.1 ] Start playback new pipeline");
  404.         }
  405.     }
  406.  
  407.     is_playing_sd = false;
  408.     ESP_LOGI(TAG, "[4.0] Stop audio pipeline");
  409.     audio_pipeline_stop(sd_pipeline);
  410.     audio_pipeline_wait_for_stop(sd_pipeline);
  411.     audio_pipeline_terminate(sd_pipeline);
  412.     audio_pipeline_unregister(sd_pipeline, fatfs_stream_reader);
  413.     audio_pipeline_unregister(sd_pipeline,wav_decoder);
  414.     audio_pipeline_unregister(sd_pipeline, i2s_stream_writer);
  415.     audio_pipeline_remove_listener(sd_pipeline);
  416.    
  417.     audio_event_iface_remove_listener(esp_periph_set_get_event_iface(set), evt);
  418.     /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
  419.     audio_event_iface_destroy(evt);
  420.     audio_pipeline_deinit(sd_pipeline);
  421.     vTaskDelete(NULL);
  422. }
  423. //**********************************************************************
  424. static void play_from_http(void) {
  425.   is_playing_http = true;
  426.        
  427.     ESP_LOGI(TAG, "[2.0] Create audio pipeline for playback");
  428.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  429.    audio_pipeline_handle_t http_pipeline = audio_pipeline_init(&pipeline_cfg);
  430.     mem_assert(http_pipeline);
  431.  
  432.  
  433.     ESP_LOGI(TAG, "[2.1] Create http stream to read data");
  434.     http_stream_cfg_t http_cfg = HTTP_STREAM_CFG_DEFAULT();
  435.     http_cfg.multi_out_num = 1;
  436.     http_cfg.out_rb_size = 1024 * 1024;
  437.     http_stream_reader = http_stream_init(&http_cfg);
  438.  
  439.     ESP_LOGI(TAG, "[2.2] Create i2s stream to write data to codec chip");
  440.     i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
  441.     i2s_cfg.type = AUDIO_STREAM_WRITER;
  442.     i2s_stream_writer = i2s_stream_init(&i2s_cfg);
  443.  
  444.     ESP_LOGI(TAG, "[2.3] Create mp3 decoder to decode mp3 file");
  445.     mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG();
  446.     mp3_decoder = mp3_decoder_init(&mp3_cfg);
  447.  
  448.     ESP_LOGI(TAG, "[2.4] Register all elements to audio pipeline");
  449.     audio_pipeline_register(http_pipeline, http_stream_reader, "http");
  450.     audio_pipeline_register(http_pipeline, mp3_decoder,        "mp3");
  451.     audio_pipeline_register(http_pipeline, i2s_stream_writer,  "i2s");
  452.  
  453.     ESP_LOGI(TAG, "[2.5] Link elements together http_stream-->mp3_decoder-->i2s_stream-->[codec_chip]");
  454.     const char *link_tag[5] = {"http", "mp3", "i2s","wav", "file"};
  455.     audio_pipeline_link(http_pipeline, &link_tag[0],5 );
  456.     ESP_LOGI(TAG, "[2.6] Set up  uri (http as http_stream, mp3 as mp3 decoder, and default output is i2s)");
  457.     audio_element_set_uri(http_stream_reader, "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3");
  458.  
  459.     ESP_LOGI(TAG, "[3.0] Create fatfs stream to save file");
  460.     fatfs_stream_cfg_t fatfs_cfg = FATFS_STREAM_CFG_DEFAULT();
  461.     fatfs_cfg.type = AUDIO_STREAM_WRITER;
  462.     audio_element_handle_t fatfs_stream_writer = fatfs_stream_init(&fatfs_cfg);
  463.  
  464.     ESP_LOGI(TAG, "[3.1] Create raw stream to read data");
  465.     i2s_stream_reader = i2s_stream_init(&i2s_cfg);
  466.     wav_encoder_cfg_t wav_cfg = DEFAULT_WAV_ENCODER_CONFIG();
  467.     wav_encoder = wav_encoder_init(&wav_cfg);
  468.  
  469.     ESP_LOGI(TAG, "[3.2] Create pipeline to save audio file");
  470.     audio_pipeline_cfg_t pipeline_save_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  471.     audio_pipeline_handle_t pipeline_save = audio_pipeline_init(&pipeline_save_cfg);
  472.  
  473.     ESP_LOGI(TAG, "[3.4] Register all elements to pipeline_save");
  474.     audio_pipeline_register(pipeline_save, wav_encoder, "wav");
  475.     audio_pipeline_register(pipeline_save, fatfs_stream_writer, "file");
  476.  
  477.     ESP_LOGI(TAG, "[3.5] Link elements together raw_stream-->fatfs_stream");
  478.     //const char *link_tag[3] = {"wav", "file",};
  479.     //audio_pipeline_link(pipeline, &link_tag[0], 3);
  480.  
  481.     ESP_LOGI(TAG, "[3.6] Connect input ringbuffer of pipeline_save to http stream multi output");
  482.     ringbuf_handle_t rb = audio_element_get_output_ringbuf(wav_encoder);
  483.     audio_element_set_multi_output_ringbuf(http_stream_reader, rb, 0);
  484.  
  485.     ESP_LOGI(TAG, "[3.6] Set music info to fatfs");
  486.     audio_element_info_t music_info = {0};
  487.     audio_element_getinfo(i2s_stream_reader, &music_info);
  488.     ESP_LOGI(TAG, "[ * ] Save the recording info to the fatfs stream writer, sample_rates=%d, bits=%d, ch=%d",
  489.                 music_info.sample_rates, music_info.bits, music_info.channels);
  490.     audio_element_setinfo(fatfs_stream_writer, &music_info);
  491.  
  492.     audio_element_set_uri(fatfs_stream_writer, "/sdcard/http.wav");
  493.  
  494.     ESP_LOGI(TAG, "[ 5 ] Set up  event listener");
  495.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  496.     audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  497.  
  498.     ESP_LOGI(TAG, "[5.1] Listening event from all elements of audio pipeline");
  499.     audio_pipeline_set_listener(http_pipeline, evt);
  500.     audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);
  501.  
  502.  
  503.     ESP_LOGI(TAG, "[ 6 ] Start pipelines");
  504.     audio_pipeline_run(http_pipeline);
  505.     audio_pipeline_run(pipeline_save);
  506.  
  507.     while (1) {
  508.         audio_event_iface_msg_t msg;
  509.         esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
  510.         if (ret != ESP_OK) {
  511.             ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
  512.             continue;
  513.         }
  514.  
  515.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT
  516.             && msg.source == (void *) mp3_decoder
  517.             && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
  518.             audio_element_info_t music_info = {0};
  519.             audio_element_getinfo(mp3_decoder, &music_info);
  520.  
  521.             ESP_LOGI(TAG, "[ * ] Receive music info from mp3 decoder, sample_rates=%d, bits=%d, ch=%d",
  522.                      music_info.sample_rates, music_info.bits, music_info.channels);
  523.  
  524.             i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels);
  525.             continue;
  526.         }
  527.  
  528.         /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  529.         if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
  530.             && msg.cmd == AEL_MSG_CMD_REPORT_STATUS
  531.             && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) {
  532.             ESP_LOGW(TAG, "[ * ] Stop event received");
  533.             break;
  534.         }
  535.         /*play pause button*/
  536.  
  537.          if (((int)msg.data == get_input_play_id()) && (msg.cmd == PERIPH_ADC_BUTTON_PRESSED || msg.cmd == PERIPH_ADC_BUTTON_PRESSED)) {
  538.             toogle_switch = !toogle_switch;
  539.             audio_pipeline_pause(http_pipeline);
  540.             ESP_LOGE(TAG,  "");
  541.             if (toogle_switch) {
  542.                audio_pipeline_pause(http_pipeline);
  543.             } else {
  544.                 audio_pipeline_resume(http_pipeline);
  545.             }
  546.             audio_pipeline_run(http_pipeline);
  547.             ESP_LOGE(TAG, "[ 4.1 ] Start playback new pipeline");
  548.         }
  549.     }
  550.  
  551.  
  552.  is_playing_http = false;
  553.  
  554.     ESP_LOGI(TAG, "[5.0] Finished");
  555.     audio_pipeline_stop(http_pipeline);
  556.     audio_pipeline_wait_for_stop(http_pipeline);
  557.     audio_pipeline_terminate(http_pipeline);
  558.     audio_pipeline_unregister_more(http_pipeline, http_stream_reader,  mp3_decoder ,i2s_stream_writer,NULL );
  559.     audio_pipeline_unregister_more(pipeline_save,wav_encoder, fatfs_stream_writer,NULL);
  560.     audio_pipeline_remove_listener(http_pipeline);
  561.     audio_event_iface_destroy(evt);
  562.     audio_pipeline_deinit(http_pipeline);
  563.     }
  564. //**********************************************************************
  565. void wait_for_mode_selection() {
  566.     ESP_LOGI(TAG, "Waiting for mode selection for %d seconds...", MODE_SELECTION_TIMEOUT_SECONDS);
  567.     vTaskDelay(MODE_SELECTION_TIMEOUT_SECONDS * 1000 / portTICK_PERIOD_MS);
  568.  
  569.     // If no mode is selected, put the device to sleep
  570.     if (!is_recording && !is_playing_sd && !is_playing_http) {
  571.         ESP_LOGI(TAG, "No mode selected, entering sleep mode for %d seconds...", SLEEP_TIMEOUT_SECONDS);
  572.  
  573.         // You can replace the sleep mode depending on your device's sleep capabilities
  574.         esp_sleep_enable_timer_wakeup(SLEEP_TIMEOUT_SECONDS * 1000000);
  575.         esp_deep_sleep_start();
  576.     }
  577. }[/code]
---- Opened the serial port COM5 ----
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x1f (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:7432
load:0x40078000,len:16564
load:0x40080400,len:4
load:0x40080404,len:4284
entry 0x40080668
I (29) boot: ESP-IDF v5.1-dirty 2nd stage bootloader
I (29) boot: compile time Aug 15 2023 10:02:55
I (29) boot: Multicore bootloader
I (34) boot: chip revision: v3.0
I (38) qio_mode: Enabling default flash chip QIO
I (43) boot.esp32: SPI Speed : 80MHz
I (48) boot.esp32: SPI Mode : QIO
I (52) boot.esp32: SPI Flash Size : 8MB
I (57) boot: Enabling RNG early entropy source...
I (62) boot: Partition Table:
I (66) boot: ## Label Usage Type ST Offset Length
I (73) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (80) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (88) boot: 2 factory factory app 00 00 00010000 00500000
I (95) boot: End of partition table
I (100) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=649f4h (412148) map
I (157) esp_image: segment 1: paddr=00074a1c vaddr=3ff80066 size=00008h ( 8) load
I (158) esp_image: segment 2: paddr=00074a2c vaddr=3ffb0000 size=04390h ( 17296) load
I (166) esp_image: segment 3: paddr=00078dc4 vaddr=40080000 size=07254h ( 29268) load
I (177) esp_image: segment 4: paddr=00080020 vaddr=400d0020 size=d7148h (880968) map
I (286) esp_image: segment 5: paddr=00157170 vaddr=40087254 size=16de8h ( 93672) load
I (302) esp_image: segment 6: paddr=0016df60 vaddr=400c0000 size=00068h ( 104) load
I (317) boot: Loaded app from partition at offset 0x10000
I (317) boot: Disabling RNG early entropy source...
I (329) cpu_start: Multicore app
I (329) quad_psram: This chip is ESP32-D0WD
I (330) esp_psram: Found 8MB PSRAM device
I (331) esp_psram: Speed: 80MHz
I (335) esp_psram: PSRAM initialized, cache is in low/high (2-core) mode.
W (342) esp_psram: Virtual address not enough for PSRAM, map as much as we can. 4MB is mapped
I (352) cpu_start: Pro cpu up.
I (355) cpu_start: Starting app cpu, entry point is 0x40081ec8
I (0) cpu_start: App cpu up.
I (876) esp_psram: SPI SRAM memory test OK
I (884) cpu_start: Pro cpu start user code
I (884) cpu_start: cpu freq: 240000000 Hz
I (884) cpu_start: Application information:
I (887) cpu_start: Project name: toy1
I (891) cpu_start: App version: 1
I (896) cpu_start: Compile time: Aug 15 2023 10:01:28
I (902) cpu_start: ELF file SHA256: 063fe588ef174378...
I (908) cpu_start: ESP-IDF: v5.1-dirty
I (913) cpu_start: Min chip rev: v0.0
I (918) cpu_start: Max chip rev: v3.99
I (923) cpu_start: Chip rev: v3.0
I (927) heap_init: Initializing. RAM available for dynamic allocation:
I (935) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (941) heap_init: At 3FFB94B0 len 00026B50 (154 KiB): DRAM
I (947) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (953) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (960) heap_init: At 4009E03C len 00001FC4 (7 KiB): IRAM
I (966) esp_psram: Adding pool of 4096K of PSRAM memory to heap allocator
I (974) spi_flash: detected chip: gd
I (977) spi_flash: flash io: qio
W (981) ADC: legacy driver is deprecated, please migrate to `esp_adc/adc_oneshot.h`
W (990) i2s(legacy): legacy i2s driver is deprecated, please migrate to use driver/i2s_std.h, driver/i2s_pdm.h or driver/i2s_tdm.h
I (1002) app_start: Starting scheduler on CPU0
I (1007) app_start: Starting scheduler on CPU1
I (1007) main_task: Started on CPU0
I (1017) esp_psram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (1025) main_task: Calling app_main()
I (1029) MODE_SELECTION: [ 1 ] nvs flash
I (1062) MODE_SELECTION: [ 2 ] Start codec chip
E (1083) gpio: gpio_install_isr_service(498): GPIO isr service already installed
I (1083) MODE_SELECTION: [3.1] Initialize keys on board
I (1086) MODE_SELECTION: [ 3 ] Start and wait for Wi-Fi network
I (1093) MODE_SELECTION: [4] Mount sdcard
W (1112) PERIPH_TOUCH: _touch_init
I (1742) MODE_SELECTION: [ 5 ] Create and start input key service
I (1742) MODE_SELECTION: [ 5 ] mode selection
W (3078) PERIPH_WIFI: WiFi Event cb, Unhandle event_base:WIFI_EVENT, event_id:4
I (4579) MODE_SELECTION: Waiting for mode selection for 300 seconds...
W (17175) AUDIO_EVT: There is no space in external queue

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 115 guests