建立双管道时候,按键外设的问题

ChiShaoJun
Posts: 32
Joined: Mon Sep 17, 2018 3:24 am

建立双管道时候,按键外设的问题

Postby ChiShaoJun » Wed Mar 13, 2019 7:56 am

我这边做了一个简单的双管道demo,想通过按键的事件进行切换。管道1是连接bt播放音乐,管道2是连接AUX_IN播放音乐,硬件为ESP32_LyraT v4.3。但是使用管道2时却触发不到按键的事件,是想请问下我代码的哪个步骤出了问题。
  1. /* Play music from Bluetooth device
  2.  
  3.    This example code is in the Public Domain (or CC0 licensed, at your option.)
  4.  
  5.    Unless required by applicable law or agreed to in writing, this
  6.    software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  7.    CONDITIONS OF ANY KIND, either express or implied.
  8. */
  9. #include <string.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "nvs_flash.h"
  14. #include "audio_element.h"
  15. #include "audio_pipeline.h"
  16. #include "audio_event_iface.h"
  17. #include "audio_common.h"
  18. #include "i2s_stream.h"
  19. #include "esp_peripherals.h"
  20. #include "periph_touch.h"
  21. #include "audio_hal.h"
  22. #include "board.h"
  23. #include "bluetooth_service.h"
  24.  
  25. static const char *TAG = "BLUETOOTH_EXAMPLE";
  26.  
  27. #define LYRAT_TOUCH_SET     TOUCH_PAD_NUM9
  28. #define LYRAT_TOUCH_PLAY    TOUCH_PAD_NUM8
  29. #define LYRAT_TOUCH_VOLUP   TOUCH_PAD_NUM7
  30. #define LYRAT_TOUCH_VOLDWN  TOUCH_PAD_NUM4
  31.  
  32. //状态切换
  33. volatile uint8_t ex_audio_status = 1;
  34.  
  35. void app_main(void)
  36. {
  37.     audio_pipeline_handle_t pipeline1, pipeline2;
  38.     audio_element_handle_t bt_stream_reader ,i2s_stream_reader, i2s_stream_writer1,i2s_stream_writer2;
  39.  
  40.     //nvs flash初始化
  41.     esp_err_t err = nvs_flash_init();
  42.     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  43.         // NVS partition was truncated and needs to be erased
  44.         // Retry nvs_flash_init
  45.         ESP_ERROR_CHECK(nvs_flash_erase());
  46.         err = nvs_flash_init();
  47.     }
  48.  
  49.     //日志级别打印
  50.     esp_log_level_set("*", ESP_LOG_INFO);
  51.     esp_log_level_set(TAG, ESP_LOG_DEBUG);
  52.  
  53.     //1.创建蓝牙服务
  54.     ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
  55.     bluetooth_service_cfg_t bt_cfg = {
  56.         .device_name = "ESP-ADF-SPEAKER",
  57.         .mode = BLUETOOTH_A2DP_SINK,
  58.     };
  59.     bluetooth_service_start(&bt_cfg);
  60.  
  61.     //2.启动编解码器芯片
  62.     ESP_LOGI(TAG, "[ 2 ] Start codec chip");
  63.     audio_hal_codec_config_t audio_hal_codec_cfg =  AUDIO_HAL_ES8388_DEFAULT();
  64.     audio_hal_codec_cfg.i2s_iface.samples = AUDIO_HAL_44K_SAMPLES;
  65.     //音频输入设置把麦克风改成外部音频输入
  66.     audio_hal_codec_cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
  67.     audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 0);
  68.     audio_hal_ctrl_codec(hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
  69.  
  70.     //3.创建音频管道以进行播放
  71.     ESP_LOGI(TAG, "[ 3 ] Create audio pipeline for playback");
  72.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  73.     pipeline1 = audio_pipeline_init(&pipeline_cfg);
  74.     pipeline2 = audio_pipeline_init(&pipeline_cfg);
  75.  
  76.     //3.1.创建i2s流以将数据写入编解码器芯片
  77.     ESP_LOGI(TAG, "[3.1] Create i2s stream to read audio data from codec chip");
  78.     i2s_stream_cfg_t i2s_r_cfg = I2S_STREAM_CFG_DEFAULT();
  79.     i2s_r_cfg.type = AUDIO_STREAM_READER;
  80.     i2s_stream_reader = i2s_stream_init(&i2s_r_cfg);
  81.  
  82.     //3.2.创建i2s流以将数据写入编解码器芯片
  83.     ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
  84.     i2s_stream_cfg_t i2s_w_cfg1 = I2S_STREAM_CFG_DEFAULT();
  85.     i2s_w_cfg1.type = AUDIO_STREAM_WRITER;
  86.     i2s_w_cfg1.i2s_config.sample_rate = 48000;
  87.     i2s_stream_writer1 = i2s_stream_init(&i2s_w_cfg1);
  88.  
  89.     //3.2.1.创建i2s流以将数据写入编解码器芯片
  90.     ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
  91.     i2s_stream_cfg_t i2s_w_cfg2 = I2S_STREAM_CFG_DEFAULT();
  92.     i2s_w_cfg2.type = AUDIO_STREAM_WRITER;
  93.     i2s_w_cfg2.i2s_config.sample_rate = 48000;
  94.     i2s_stream_writer2 = i2s_stream_init(&i2s_w_cfg2);
  95.  
  96.     //3.3.获取蓝牙流
  97.     ESP_LOGI(TAG, "[3.3] Get Bluetooth stream");
  98.     bt_stream_reader = bluetooth_service_create_stream();
  99.  
  100.     //3.4.初始化外围设备
  101.     ESP_LOGI(TAG, "[ 3.4 ] Register all elements to audio pipeline");
  102.     audio_pipeline_register(pipeline1, bt_stream_reader, "bt");
  103.     audio_pipeline_register(pipeline1, i2s_stream_writer1, "i2sw1");
  104.  
  105.     audio_pipeline_register(pipeline2, i2s_stream_reader, "i2sr");
  106.     audio_pipeline_register(pipeline2, i2s_stream_writer2, "i2sw2");
  107.  
  108.     //3.5.把它链接在一起
  109.     ESP_LOGI(TAG, "[ 3.5 ] Link elements together");
  110.     audio_pipeline_link(pipeline1, (const char *[]) {"bt", "i2sw1"}, 2);
  111.     audio_pipeline_link(pipeline2, (const char *[]) {"i2sr", "i2sw2"}, 2);
  112.  
  113.     //4.初始化外围设备
  114.     ESP_LOGI(TAG, "[ 4 ] Initialize peripherals");
  115.     esp_periph_config_t periph_cfg = { 0 };
  116.     esp_periph_init(&periph_cfg);
  117.  
  118.     //4.1.初始化Touch外围设备
  119.     ESP_LOGI(TAG, "[4.1] Initialize Touch peripheral");
  120.     periph_touch_cfg_t touch_cfg = {
  121.         .touch_mask = TOUCH_SEL_SET | TOUCH_SEL_PLAY | TOUCH_SEL_VOLUP | TOUCH_SEL_VOLDWN,
  122.         .tap_threshold_percent = 70,
  123.     };
  124.     esp_periph_handle_t touch_periph = periph_touch_init(&touch_cfg);
  125.  
  126.     //4.2.创建蓝牙外设
  127.     ESP_LOGI(TAG, "[4.2] Create Bluetooth peripheral");
  128.     esp_periph_handle_t bt_periph = bluetooth_service_create_periph();
  129.  
  130.     //4.2.启动所有外围设备
  131.     ESP_LOGI(TAG, "[4.2] Start all peripherals");
  132.     esp_periph_start(touch_periph);
  133.     esp_periph_start(bt_periph);
  134.  
  135.     //5.设置事件监听器
  136.     ESP_LOGI(TAG, "[ 5 ] Setup event listener");
  137.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  138.     audio_event_iface_handle_t evt1 = audio_event_iface_init(&evt_cfg);
  139.     audio_event_iface_handle_t evt2 = audio_event_iface_init(&evt_cfg);
  140.  
  141.     ESP_LOGI(TAG, "[5.1] Listening event from all elements of pipeline");
  142.     //为此音频管道设置事件监听器,来自此管道的任何事件都可以通过`evt`监听
  143.     audio_pipeline_set_listener(pipeline1, evt1);
  144.     audio_pipeline_set_listener(pipeline2, evt2);
  145.  
  146.     //5.2.外围设备的监听事件
  147.     ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
  148.     audio_event_iface_set_listener(esp_periph_get_event_iface(), evt1);
  149.     audio_event_iface_set_listener(esp_periph_get_event_iface(), evt2);
  150.  
  151.     while(1)
  152.     {
  153.         switch(ex_audio_status)
  154.         {
  155.         case 0:
  156.             ESP_LOGI(TAG, "case 0");
  157.             //6.启动audio_pipeline
  158.             ESP_LOGI(TAG, "[ 6 ] Start audio_pipeline");
  159.             audio_pipeline_run(pipeline1);
  160.  
  161.             //7.监听所有管道事件
  162.             ESP_LOGI(TAG, "[ 7 ] Listen for all pipeline events");
  163.             while (1)
  164.             {
  165.                 audio_event_iface_msg_t msg;
  166.                 esp_err_t ret1 = audio_event_iface_listen(evt1, &msg, portMAX_DELAY);
  167.                 if (ret1 != ESP_OK) {
  168.                     ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret1);
  169.                     continue;
  170.                 }
  171.  
  172.                 if (msg.cmd == AEL_MSG_CMD_ERROR) {
  173.                     ESP_LOGE(TAG, "[ * ] Action command error: src_type:%d, source:%p cmd:%d, data:%p, data_len:%d",
  174.                              msg.source_type, msg.source, msg.cmd, msg.data, msg.data_len);
  175.                 }
  176.  
  177.                 if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) bt_stream_reader
  178.                     && msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
  179.                     audio_element_info_t music_info = {0};
  180.                     audio_element_getinfo(bt_stream_reader, &music_info);
  181.  
  182.                     ESP_LOGI(TAG, "[ * ] Receive music info from Bluetooth, sample_rates=%d, bits=%d, ch=%d",
  183.                              music_info.sample_rates, music_info.bits, music_info.channels);
  184.  
  185.                     audio_element_setinfo(i2s_stream_writer1, &music_info);
  186.                     i2s_stream_set_clk(i2s_stream_writer1, music_info.sample_rates, music_info.bits, music_info.channels);
  187.                     continue;
  188.                 }
  189.  
  190.                 if (msg.source_type == PERIPH_ID_TOUCH
  191.                     && msg.cmd == PERIPH_TOUCH_TAP
  192.                     && msg.source == (void *)touch_periph) {
  193.  
  194.                     if ((int) msg.data == TOUCH_PLAY) {
  195.                         ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
  196.                         periph_bluetooth_play(bt_periph);
  197.                     } else if ((int) msg.data == TOUCH_SET) {
  198.                         ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
  199.                         periph_bluetooth_pause(bt_periph);
  200.                     } else if ((int) msg.data == TOUCH_VOLUP) {
  201.                         if(audio_pipeline_pause(pipeline2) != ESP_OK)
  202.                         {
  203.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  204.                         }
  205.                         if(audio_pipeline_run(pipeline1) != ESP_OK)
  206.                         {
  207.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_run err");
  208.                         }
  209.                     } else if ((int) msg.data == TOUCH_VOLDWN) {
  210.                         ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
  211.                         ex_audio_status = 1;
  212.                         if(audio_pipeline_pause(pipeline1) != ESP_OK)
  213.                         {
  214.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  215.                         }
  216.                         break;
  217.                     }
  218.                 }
  219.  
  220.                 /* Stop when the Bluetooth is disconnected or suspended */
  221.                 if (msg.source_type == PERIPH_ID_BLUETOOTH
  222.                     && msg.source == (void *)bt_periph) {
  223.                     if (msg.cmd == PERIPH_BLUETOOTH_DISCONNECTED) {
  224.                         ESP_LOGW(TAG, "[ * ] Bluetooth disconnected");
  225.                         break;
  226.                     }
  227.                 }
  228.                 /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  229.                 if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer1
  230.                     && msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg.data == AEL_STATUS_STATE_STOPPED) {
  231.                     ESP_LOGW(TAG, "[ * ] Stop event received");
  232.                     break;
  233.                 }
  234.             }
  235.         break;
  236.         case 1:
  237.             audio_pipeline_run(pipeline2);
  238.             while (1)
  239.             {
  240.                 ESP_LOGI(TAG, "case 1");
  241.                 audio_event_iface_msg_t msg2;
  242.                 esp_err_t ret2 = audio_event_iface_listen(evt2, &msg2, portMAX_DELAY);
  243.                 if (ret2 != ESP_OK) {
  244.                     ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret2);
  245.                     continue;
  246.                 }
  247.  
  248.                 if (msg2.cmd == AEL_MSG_CMD_ERROR) {
  249.                     ESP_LOGE(TAG, "[ * ] Action command error: src_type:%d, source:%p cmd:%d, data:%p, data_len:%d",
  250.                              msg2.source_type, msg2.source, msg2.cmd, msg2.data, msg2.data_len);
  251.                 }
  252.  
  253.                 if (msg2.source_type == PERIPH_ID_TOUCH
  254.                     && msg2.cmd == PERIPH_TOUCH_TAP
  255.                     && msg2.source == (void *)touch_periph) {
  256.  
  257.                     if ((int) msg2.data == TOUCH_PLAY) {
  258.                         ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
  259.                         periph_bluetooth_play(bt_periph);
  260.                     } else if ((int) msg2.data == TOUCH_SET) {
  261.                         ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
  262.                         periph_bluetooth_pause(bt_periph);
  263.                     } else if ((int) msg2.data == TOUCH_VOLUP) {
  264.                         if(audio_pipeline_pause(pipeline2) != ESP_OK)
  265.                         {
  266.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  267.                         }
  268.                     } else if ((int) msg2.data == TOUCH_VOLDWN) {
  269.                         ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
  270.                         ex_audio_status = 0;
  271.                         if(audio_pipeline_pause(pipeline2) != ESP_OK)
  272.                         {
  273.                             ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
  274.                         }
  275.                         break;
  276.                     }
  277.                 }
  278.  
  279.                 /* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
  280.                 if (msg2.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg2.source == (void *) i2s_stream_writer2
  281.                     && msg2.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg2.data == AEL_STATUS_STATE_STOPPED) {
  282.                     ESP_LOGW(TAG, "[ * ] Stop event received");
  283.                     break;
  284.                 }
  285.             }
  286.             break;
  287.         }
  288.     }
  289. }

ChiShaoJun
Posts: 32
Joined: Mon Sep 17, 2018 3:24 am

Re: 建立双管道时候,按键外设的问题

Postby ChiShaoJun » Wed Mar 20, 2019 2:45 am

我根据了“flexible_pipeline”例程的逻辑重新改了代码,如下。
发现几个问题
1、audio_pipeline_pause得换成audio_pipeline_stop+audio_pipeline_wait_for_stop才可用
2、得重新注册i2s_stream_writer——audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
但是audio_pipeline_stop注释不是说了“The link state of the elements in the pipeline is kept”吗?不清楚什么时候取消注册的
  1. /******************************
  2. * 文件名称:play_bt_music_example.c
  3. * 作    者:Kyle
  4. * 日    期:20190319
  5. * 描    述:bt与aux_in音频切换
  6. ******************************/
  7.  
  8. /* 头文件 --------------------------------------------------------------------*/
  9. #include <string.h>
  10. #include "freertos/FreeRTOS.h"
  11. #include "freertos/task.h"
  12. #include "esp_log.h"
  13. #include "nvs_flash.h"
  14. #include "audio_element.h"
  15. #include "audio_pipeline.h"
  16. #include "audio_event_iface.h"
  17. #include "audio_common.h"
  18. #include "i2s_stream.h"
  19. #include "esp_peripherals.h"
  20. #include "periph_button.h"
  21. #include "audio_hal.h"
  22. #include "board.h"
  23. #include "bluetooth_service.h"
  24.  
  25. /* 宏定义 --------------------------------------------------------------------*/
  26. static const char *TAG = "BLUETOOTH_EXAMPLE";
  27.  
  28. /* 变量 ----------------------------------------------------------------------*/
  29. //状态切换
  30. bool ex_audio_status = false;
  31. audio_pipeline_handle_t pipeline;
  32. audio_element_handle_t bt_stream_reader, i2s_stream_reader, i2s_stream_writer;
  33.  
  34. /* 函数定义 ------------------------------------------------------------------*/
  35. /******************************
  36. * 函数名称:app_main
  37. * 作    者:Kyle
  38. * 日    期:20190319
  39. * 描    述:主函数
  40. * 输入参数:无
  41. * 返 回 值:无
  42. ******************************/
  43. void app_main(void)
  44. {
  45.     //nvs flash初始化
  46.     esp_err_t err = nvs_flash_init();
  47.     if (err == ESP_ERR_NVS_NO_FREE_PAGES) {
  48.         // NVS partition was truncated and needs to be erased
  49.         // Retry nvs_flash_init
  50.         ESP_ERROR_CHECK(nvs_flash_erase());
  51.         err = nvs_flash_init();
  52.     }
  53.  
  54.     esp_log_level_set("*", ESP_LOG_INFO);
  55.     esp_log_level_set(TAG, ESP_LOG_DEBUG);
  56.  
  57.     //1.创建蓝牙服务
  58.     ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
  59.     bluetooth_service_cfg_t bt_cfg = {
  60.         .device_name = "ESP-ADF-SPEAKER",
  61.         .mode = BLUETOOTH_A2DP_SINK,
  62.     };
  63.     bluetooth_service_start(&bt_cfg);
  64.  
  65.     //2.启动编解码器芯片
  66.     ESP_LOGI(TAG, "[ 2 ] Start codec chip");
  67.     audio_hal_codec_config_t audio_hal_codec_cfg =  AUDIO_HAL_ES8388_DEFAULT();
  68.     audio_hal_codec_cfg.i2s_iface.samples = AUDIO_HAL_44K_SAMPLES;
  69.     audio_hal_codec_cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
  70.     audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 0);
  71.     audio_hal_ctrl_codec(hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
  72.  
  73.     //3.创建音频管道以进行播放
  74.     ESP_LOGI(TAG, "[ 3 ] Create audio pipeline for playback");
  75.     audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
  76.     pipeline = audio_pipeline_init(&pipeline_cfg);
  77.  
  78.     //3.1.创建i2s流以将数据从编解码器芯片读取
  79.     ESP_LOGI(TAG, "[3.1] Create i2s stream to read data to codec chip");
  80.     i2s_stream_cfg_t i2sr_cfg = I2S_STREAM_CFG_DEFAULT();
  81.     i2sr_cfg.type = AUDIO_STREAM_READER;
  82.     i2s_stream_reader = i2s_stream_init(&i2sr_cfg);
  83.  
  84.     //3.2.创建i2s流以将数据写入编解码器芯片
  85.     ESP_LOGI(TAG, "[3.1] Create i2s stream to write data to codec chip");
  86.     i2s_stream_cfg_t i2sw_cfg = I2S_STREAM_CFG_DEFAULT();
  87.     i2sw_cfg.type = AUDIO_STREAM_WRITER;
  88.     i2s_stream_writer = i2s_stream_init(&i2sw_cfg);
  89.  
  90.     //3.3.获取蓝牙流
  91.     ESP_LOGI(TAG, "[3.2] Get Bluetooth stream");
  92.     bt_stream_reader = bluetooth_service_create_stream();
  93.  
  94.     //3.2.将所有元素注册到音频管道
  95.     ESP_LOGI(TAG, "[3.2] Register all elements to audio pipeline");
  96.     audio_pipeline_register(pipeline, i2s_stream_reader, "i2sr");
  97.     audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
  98.     audio_pipeline_register(pipeline, bt_stream_reader, "bt");
  99.  
  100.     //3.3.把它链接在一起
  101.     ESP_LOGI(TAG, "[3.3] Link it together [Bluetooth]-->bt_stream_reader-->i2s_stream_writer-->[codec_chip]");
  102.     audio_pipeline_link(pipeline, (const char *[]) {"i2sr", "i2sw"}, 2);
  103.  
  104.     //4.初始化外围设备
  105.     ESP_LOGI(TAG, "[ 4 ] Initialize peripherals");
  106.     esp_periph_config_t periph_cfg = { 0 };
  107.     esp_periph_init(&periph_cfg);
  108.  
  109.     //4.1.初始化button外围设备
  110.     ESP_LOGI(TAG, "[4.1] Initialize button peripheral");
  111.     periph_button_cfg_t btn_cfg = {
  112.         .gpio_mask = GPIO_SEL_36 | GPIO_SEL_39, // REC BTN & MODE BTN
  113.     };
  114.     esp_periph_handle_t button_periph = periph_button_init(&btn_cfg);
  115.  
  116.     //4.2.创建蓝牙外设
  117.     ESP_LOGI(TAG, "[4.2] Create Bluetooth peripheral");
  118.     esp_periph_handle_t bt_periph = bluetooth_service_create_periph();
  119.  
  120.     //4.2.启动所有外围设备
  121.     ESP_LOGI(TAG, "[4.2] Start all peripherals");
  122.     esp_periph_start(button_periph);
  123.     esp_periph_start(bt_periph);
  124.  
  125.     //5.设置事件监听器
  126.     ESP_LOGI(TAG, "[ 5 ] Setup event listener");
  127.     audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
  128.     audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg);
  129.  
  130.     //5.1.来自管道的所有元素的监听事件
  131.     ESP_LOGI(TAG, "[5.1] Listening event from all elements of pipeline");
  132.     audio_pipeline_set_listener(pipeline, evt);
  133.  
  134.     //5.2.外围设备的监听事件
  135.     ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
  136.     audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);
  137.  
  138.     //6.启动audio_pipeline
  139.     ESP_LOGI(TAG, "[ 6 ] Start audio_pipeline");
  140.     audio_pipeline_run(pipeline);
  141.  
  142.     //7.监听所有管道事件
  143.     ESP_LOGI(TAG, "[ 7 ] Listen for all pipeline events");
  144.     while (1) {
  145.         audio_event_iface_msg_t msg;
  146.         esp_err_t ret = audio_event_iface_listen(evt, &msg, portMAX_DELAY);
  147.         if (ret != ESP_OK) {
  148.             ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
  149.             continue;
  150.         }
  151.         if (msg.source_type != PERIPH_ID_BUTTON) {
  152. //            audio_element_handle_t el = (audio_element_handle_t)msg.source;
  153. //            ESP_LOGI(TAG, "Element tag:[%s],src_type:%x, cmd:%d, data_len:%d, data:%p",
  154. //                     audio_element_get_tag(el), msg.source_type, msg.cmd, msg.data_len, msg.data);
  155.             continue;
  156.         }
  157.         if (((int)msg.data == GPIO_MODE) && (msg.cmd == PERIPH_BUTTON_PRESSED)){
  158.             ex_audio_status = !ex_audio_status;
  159.             if (!ex_audio_status) {
  160.                 periph_bluetooth_pause(bt_periph);
  161.             }
  162.  
  163.             audio_pipeline_stop(pipeline);
  164.             audio_pipeline_wait_for_stop(pipeline);
  165.             ESP_LOGE(TAG, "Changing music to %s", ex_audio_status ? "bt" : "i2sr");
  166.             if (ex_audio_status) {
  167.                 audio_pipeline_breakup_elements(pipeline, i2s_stream_reader);
  168.                 //蓝牙
  169.                 audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
  170.                 audio_pipeline_relink(pipeline, (const char *[]) {"bt", "i2sw"}, 2);
  171.                 audio_pipeline_set_listener(pipeline, evt);
  172.             } else {
  173.                 audio_pipeline_breakup_elements(pipeline, bt_stream_reader);
  174.                 //外部
  175.                 audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
  176.                 audio_pipeline_relink(pipeline, (const char *[]) {"i2sr", "i2sw"}, 2);
  177.                 audio_pipeline_set_listener(pipeline, evt);
  178.             }
  179.             audio_pipeline_run(pipeline);
  180.             audio_pipeline_resume(pipeline);
  181.  
  182.             if (ex_audio_status) {
  183.                 periph_bluetooth_play(bt_periph);
  184.             }
  185.         }
  186.     }
  187.  
  188.     //8.停止audio_pipeline
  189.     ESP_LOGI(TAG, "[ 8 ] Stop audio_pipeline");
  190.     audio_pipeline_terminate(pipeline);
  191.  
  192.     audio_pipeline_unregister(pipeline, bt_stream_reader);
  193.     audio_pipeline_unregister(pipeline, i2s_stream_reader);
  194.     audio_pipeline_unregister(pipeline, i2s_stream_writer);
  195.  
  196.     //在删除侦听器之前终止管道
  197.     /* Terminate the pipeline before removing the listener */
  198.     audio_pipeline_remove_listener(pipeline);
  199.  
  200.     //在删除侦听器之前停止所有外围设备
  201.     /* Stop all peripherals before removing the listener */
  202.     esp_periph_stop_all();
  203.     audio_event_iface_remove_listener(esp_periph_get_event_iface(), evt);
  204.  
  205.     //确保在销毁event_iface之前调用audio_pipeline_remove_listener和audio_event_iface_remove_listener
  206.     /* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
  207.     audio_event_iface_destroy(evt);
  208.  
  209.     //释放所有资源
  210.     /* Release all resources */
  211.     audio_pipeline_deinit(pipeline);
  212.     audio_element_deinit(bt_stream_reader);
  213.     audio_element_deinit(i2s_stream_reader);
  214.     audio_element_deinit(i2s_stream_writer);
  215.     esp_periph_destroy();
  216.     bluetooth_service_destroy();
  217. }

Who is online

Users browsing this forum: No registered users and 96 guests