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

ChiShaoJun
Posts: 33
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时却触发不到按键的事件,是想请问下我代码的哪个步骤出了问题。

Code: Untitled.c Select all

/* Play music from Bluetooth device

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 "esp_log.h"
#include "nvs_flash.h"
#include "audio_element.h"
#include "audio_pipeline.h"
#include "audio_event_iface.h"
#include "audio_common.h"
#include "i2s_stream.h"
#include "esp_peripherals.h"
#include "periph_touch.h"
#include "audio_hal.h"
#include "board.h"
#include "bluetooth_service.h"

static const char *TAG = "BLUETOOTH_EXAMPLE";

#define LYRAT_TOUCH_SET TOUCH_PAD_NUM9
#define LYRAT_TOUCH_PLAY TOUCH_PAD_NUM8
#define LYRAT_TOUCH_VOLUP TOUCH_PAD_NUM7
#define LYRAT_TOUCH_VOLDWN TOUCH_PAD_NUM4

//状态切换
volatile uint8_t ex_audio_status = 1;

void app_main(void)
{
audio_pipeline_handle_t pipeline1, pipeline2;
audio_element_handle_t bt_stream_reader ,i2s_stream_reader, i2s_stream_writer1,i2s_stream_writer2;

//nvs flash初始化
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();
}

//日志级别打印
esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set(TAG, ESP_LOG_DEBUG);

//1.创建蓝牙服务
ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
bluetooth_service_cfg_t bt_cfg = {
.device_name = "ESP-ADF-SPEAKER",
.mode = BLUETOOTH_A2DP_SINK,
};
bluetooth_service_start(&bt_cfg);

//2.启动编解码器芯片
ESP_LOGI(TAG, "[ 2 ] Start codec chip");
audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ES8388_DEFAULT();
audio_hal_codec_cfg.i2s_iface.samples = AUDIO_HAL_44K_SAMPLES;
//音频输入设置把麦克风改成外部音频输入
audio_hal_codec_cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
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);

//3.创建音频管道以进行播放
ESP_LOGI(TAG, "[ 3 ] Create audio pipeline for playback");
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
pipeline1 = audio_pipeline_init(&pipeline_cfg);
pipeline2 = audio_pipeline_init(&pipeline_cfg);

//3.1.创建i2s流以将数据写入编解码器芯片
ESP_LOGI(TAG, "[3.1] Create i2s stream to read audio data from codec chip");
i2s_stream_cfg_t i2s_r_cfg = I2S_STREAM_CFG_DEFAULT();
i2s_r_cfg.type = AUDIO_STREAM_READER;
i2s_stream_reader = i2s_stream_init(&i2s_r_cfg);

//3.2.创建i2s流以将数据写入编解码器芯片
ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
i2s_stream_cfg_t i2s_w_cfg1 = I2S_STREAM_CFG_DEFAULT();
i2s_w_cfg1.type = AUDIO_STREAM_WRITER;
i2s_w_cfg1.i2s_config.sample_rate = 48000;
i2s_stream_writer1 = i2s_stream_init(&i2s_w_cfg1);

//3.2.1.创建i2s流以将数据写入编解码器芯片
ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
i2s_stream_cfg_t i2s_w_cfg2 = I2S_STREAM_CFG_DEFAULT();
i2s_w_cfg2.type = AUDIO_STREAM_WRITER;
i2s_w_cfg2.i2s_config.sample_rate = 48000;
i2s_stream_writer2 = i2s_stream_init(&i2s_w_cfg2);

//3.3.获取蓝牙流
ESP_LOGI(TAG, "[3.3] Get Bluetooth stream");
bt_stream_reader = bluetooth_service_create_stream();

//3.4.初始化外围设备
ESP_LOGI(TAG, "[ 3.4 ] Register all elements to audio pipeline");
audio_pipeline_register(pipeline1, bt_stream_reader, "bt");
audio_pipeline_register(pipeline1, i2s_stream_writer1, "i2sw1");

audio_pipeline_register(pipeline2, i2s_stream_reader, "i2sr");
audio_pipeline_register(pipeline2, i2s_stream_writer2, "i2sw2");

//3.5.把它链接在一起
ESP_LOGI(TAG, "[ 3.5 ] Link elements together");
audio_pipeline_link(pipeline1, (const char *[]) {"bt", "i2sw1"}, 2);
audio_pipeline_link(pipeline2, (const char *[]) {"i2sr", "i2sw2"}, 2);

//4.初始化外围设备
ESP_LOGI(TAG, "[ 4 ] Initialize peripherals");
esp_periph_config_t periph_cfg = { 0 };
esp_periph_init(&periph_cfg);

//4.1.初始化Touch外围设备
ESP_LOGI(TAG, "[4.1] Initialize Touch peripheral");
periph_touch_cfg_t touch_cfg = {
.touch_mask = TOUCH_SEL_SET | TOUCH_SEL_PLAY | TOUCH_SEL_VOLUP | TOUCH_SEL_VOLDWN,
.tap_threshold_percent = 70,
};
esp_periph_handle_t touch_periph = periph_touch_init(&touch_cfg);

//4.2.创建蓝牙外设
ESP_LOGI(TAG, "[4.2] Create Bluetooth peripheral");
esp_periph_handle_t bt_periph = bluetooth_service_create_periph();

//4.2.启动所有外围设备
ESP_LOGI(TAG, "[4.2] Start all peripherals");
esp_periph_start(touch_periph);
esp_periph_start(bt_periph);

//5.设置事件监听器
ESP_LOGI(TAG, "[ 5 ] Setup event listener");
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
audio_event_iface_handle_t evt1 = audio_event_iface_init(&evt_cfg);
audio_event_iface_handle_t evt2 = audio_event_iface_init(&evt_cfg);

ESP_LOGI(TAG, "[5.1] Listening event from all elements of pipeline");
//为此音频管道设置事件监听器,来自此管道的任何事件都可以通过`evt`监听
audio_pipeline_set_listener(pipeline1, evt1);
audio_pipeline_set_listener(pipeline2, evt2);

//5.2.外围设备的监听事件
ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
audio_event_iface_set_listener(esp_periph_get_event_iface(), evt1);
audio_event_iface_set_listener(esp_periph_get_event_iface(), evt2);

while(1)
{
switch(ex_audio_status)
{
case 0:
ESP_LOGI(TAG, "case 0");
//6.启动audio_pipeline
ESP_LOGI(TAG, "[ 6 ] Start audio_pipeline");
audio_pipeline_run(pipeline1);

//7.监听所有管道事件
ESP_LOGI(TAG, "[ 7 ] Listen for all pipeline events");
while (1)
{
audio_event_iface_msg_t msg;
esp_err_t ret1 = audio_event_iface_listen(evt1, &msg, portMAX_DELAY);
if (ret1 != ESP_OK) {
ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret1);
continue;
}

if (msg.cmd == AEL_MSG_CMD_ERROR) {
ESP_LOGE(TAG, "[ * ] Action command error: src_type:%d, source:%p cmd:%d, data:%p, data_len:%d",
msg.source_type, msg.source, msg.cmd, msg.data, msg.data_len);
}

if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) bt_stream_reader
&& msg.cmd == AEL_MSG_CMD_REPORT_MUSIC_INFO) {
audio_element_info_t music_info = {0};
audio_element_getinfo(bt_stream_reader, &music_info);

ESP_LOGI(TAG, "[ * ] Receive music info from Bluetooth, sample_rates=%d, bits=%d, ch=%d",
music_info.sample_rates, music_info.bits, music_info.channels);

audio_element_setinfo(i2s_stream_writer1, &music_info);
i2s_stream_set_clk(i2s_stream_writer1, music_info.sample_rates, music_info.bits, music_info.channels);
continue;
}

if (msg.source_type == PERIPH_ID_TOUCH
&& msg.cmd == PERIPH_TOUCH_TAP
&& msg.source == (void *)touch_periph) {

if ((int) msg.data == TOUCH_PLAY) {
ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
periph_bluetooth_play(bt_periph);
} else if ((int) msg.data == TOUCH_SET) {
ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
periph_bluetooth_pause(bt_periph);
} else if ((int) msg.data == TOUCH_VOLUP) {
if(audio_pipeline_pause(pipeline2) != ESP_OK)
{
ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
}
if(audio_pipeline_run(pipeline1) != ESP_OK)
{
ESP_LOGI(TAG, "[ * ] audio_pipeline_run err");
}
} else if ((int) msg.data == TOUCH_VOLDWN) {
ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
ex_audio_status = 1;
if(audio_pipeline_pause(pipeline1) != ESP_OK)
{
ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
}
break;
}
}

/* Stop when the Bluetooth is disconnected or suspended */
if (msg.source_type == PERIPH_ID_BLUETOOTH
&& msg.source == (void *)bt_periph) {
if (msg.cmd == PERIPH_BLUETOOTH_DISCONNECTED) {
ESP_LOGW(TAG, "[ * ] Bluetooth disconnected");
break;
}
}
/* 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_writer1
&& msg.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg.data == AEL_STATUS_STATE_STOPPED) {
ESP_LOGW(TAG, "[ * ] Stop event received");
break;
}
}
break;
case 1:
audio_pipeline_run(pipeline2);
while (1)
{
ESP_LOGI(TAG, "case 1");
audio_event_iface_msg_t msg2;
esp_err_t ret2 = audio_event_iface_listen(evt2, &msg2, portMAX_DELAY);
if (ret2 != ESP_OK) {
ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret2);
continue;
}

if (msg2.cmd == AEL_MSG_CMD_ERROR) {
ESP_LOGE(TAG, "[ * ] Action command error: src_type:%d, source:%p cmd:%d, data:%p, data_len:%d",
msg2.source_type, msg2.source, msg2.cmd, msg2.data, msg2.data_len);
}

if (msg2.source_type == PERIPH_ID_TOUCH
&& msg2.cmd == PERIPH_TOUCH_TAP
&& msg2.source == (void *)touch_periph) {

if ((int) msg2.data == TOUCH_PLAY) {
ESP_LOGI(TAG, "[ * ] [Play] touch tap event");
periph_bluetooth_play(bt_periph);
} else if ((int) msg2.data == TOUCH_SET) {
ESP_LOGI(TAG, "[ * ] [Set] touch tap event");
periph_bluetooth_pause(bt_periph);
} else if ((int) msg2.data == TOUCH_VOLUP) {
if(audio_pipeline_pause(pipeline2) != ESP_OK)
{
ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
}
} else if ((int) msg2.data == TOUCH_VOLDWN) {
ESP_LOGI(TAG, "[ * ] [Vol-] touch tap event");
ex_audio_status = 0;
if(audio_pipeline_pause(pipeline2) != ESP_OK)
{
ESP_LOGI(TAG, "[ * ] audio_pipeline_pause err");
}
break;
}
}

/* Stop when the last pipeline element (i2s_stream_writer in this case) receives stop event */
if (msg2.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg2.source == (void *) i2s_stream_writer2
&& msg2.cmd == AEL_MSG_CMD_REPORT_STATUS && (int) msg2.data == AEL_STATUS_STATE_STOPPED) {
ESP_LOGW(TAG, "[ * ] Stop event received");
break;
}
}
break;
}
}
}

ChiShaoJun
Posts: 33
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”吗?不清楚什么时候取消注册的

Code: Untitled.c Select all

/******************************
* 文件名称:play_bt_music_example.c
* 作 者:Kyle
* 日 期:20190319
* 描 述:bt与aux_in音频切换
******************************/

/* 头文件 --------------------------------------------------------------------*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "audio_element.h"
#include "audio_pipeline.h"
#include "audio_event_iface.h"
#include "audio_common.h"
#include "i2s_stream.h"
#include "esp_peripherals.h"
#include "periph_button.h"
#include "audio_hal.h"
#include "board.h"
#include "bluetooth_service.h"

/* 宏定义 --------------------------------------------------------------------*/
static const char *TAG = "BLUETOOTH_EXAMPLE";

/* 变量 ----------------------------------------------------------------------*/
//状态切换
bool ex_audio_status = false;
audio_pipeline_handle_t pipeline;
audio_element_handle_t bt_stream_reader, i2s_stream_reader, i2s_stream_writer;

/* 函数定义 ------------------------------------------------------------------*/
/******************************
* 函数名称:app_main
* 作 者:Kyle
* 日 期:20190319
* 描 述:主函数
* 输入参数:无
* 返 回 值:无
******************************/
void app_main(void)
{
//nvs flash初始化
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();
}

esp_log_level_set("*", ESP_LOG_INFO);
esp_log_level_set(TAG, ESP_LOG_DEBUG);

//1.创建蓝牙服务
ESP_LOGI(TAG, "[ 1 ] Create Bluetooth service");
bluetooth_service_cfg_t bt_cfg = {
.device_name = "ESP-ADF-SPEAKER",
.mode = BLUETOOTH_A2DP_SINK,
};
bluetooth_service_start(&bt_cfg);

//2.启动编解码器芯片
ESP_LOGI(TAG, "[ 2 ] Start codec chip");
audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ES8388_DEFAULT();
audio_hal_codec_cfg.i2s_iface.samples = AUDIO_HAL_44K_SAMPLES;
audio_hal_codec_cfg.adc_input = AUDIO_HAL_ADC_INPUT_LINE2;
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);

//3.创建音频管道以进行播放
ESP_LOGI(TAG, "[ 3 ] Create audio pipeline for playback");
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
pipeline = audio_pipeline_init(&pipeline_cfg);

//3.1.创建i2s流以将数据从编解码器芯片读取
ESP_LOGI(TAG, "[3.1] Create i2s stream to read data to codec chip");
i2s_stream_cfg_t i2sr_cfg = I2S_STREAM_CFG_DEFAULT();
i2sr_cfg.type = AUDIO_STREAM_READER;
i2s_stream_reader = i2s_stream_init(&i2sr_cfg);

//3.2.创建i2s流以将数据写入编解码器芯片
ESP_LOGI(TAG, "[3.1] Create i2s stream to write data to codec chip");
i2s_stream_cfg_t i2sw_cfg = I2S_STREAM_CFG_DEFAULT();
i2sw_cfg.type = AUDIO_STREAM_WRITER;
i2s_stream_writer = i2s_stream_init(&i2sw_cfg);

//3.3.获取蓝牙流
ESP_LOGI(TAG, "[3.2] Get Bluetooth stream");
bt_stream_reader = bluetooth_service_create_stream();

//3.2.将所有元素注册到音频管道
ESP_LOGI(TAG, "[3.2] Register all elements to audio pipeline");
audio_pipeline_register(pipeline, i2s_stream_reader, "i2sr");
audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
audio_pipeline_register(pipeline, bt_stream_reader, "bt");

//3.3.把它链接在一起
ESP_LOGI(TAG, "[3.3] Link it together [Bluetooth]-->bt_stream_reader-->i2s_stream_writer-->[codec_chip]");
audio_pipeline_link(pipeline, (const char *[]) {"i2sr", "i2sw"}, 2);

//4.初始化外围设备
ESP_LOGI(TAG, "[ 4 ] Initialize peripherals");
esp_periph_config_t periph_cfg = { 0 };
esp_periph_init(&periph_cfg);

//4.1.初始化button外围设备
ESP_LOGI(TAG, "[4.1] Initialize button peripheral");
periph_button_cfg_t btn_cfg = {
.gpio_mask = GPIO_SEL_36 | GPIO_SEL_39, // REC BTN & MODE BTN
};
esp_periph_handle_t button_periph = periph_button_init(&btn_cfg);

//4.2.创建蓝牙外设
ESP_LOGI(TAG, "[4.2] Create Bluetooth peripheral");
esp_periph_handle_t bt_periph = bluetooth_service_create_periph();

//4.2.启动所有外围设备
ESP_LOGI(TAG, "[4.2] Start all peripherals");
esp_periph_start(button_periph);
esp_periph_start(bt_periph);

//5.设置事件监听器
ESP_LOGI(TAG, "[ 5 ] Setup 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);

//5.1.来自管道的所有元素的监听事件
ESP_LOGI(TAG, "[5.1] Listening event from all elements of pipeline");
audio_pipeline_set_listener(pipeline, evt);

//5.2.外围设备的监听事件
ESP_LOGI(TAG, "[5.2] Listening event from peripherals");
audio_event_iface_set_listener(esp_periph_get_event_iface(), evt);

//6.启动audio_pipeline
ESP_LOGI(TAG, "[ 6 ] Start audio_pipeline");
audio_pipeline_run(pipeline);

//7.监听所有管道事件
ESP_LOGI(TAG, "[ 7 ] Listen for all pipeline events");
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 != PERIPH_ID_BUTTON) {
// audio_element_handle_t el = (audio_element_handle_t)msg.source;
// ESP_LOGI(TAG, "Element tag:[%s],src_type:%x, cmd:%d, data_len:%d, data:%p",
// audio_element_get_tag(el), msg.source_type, msg.cmd, msg.data_len, msg.data);
continue;
}
if (((int)msg.data == GPIO_MODE) && (msg.cmd == PERIPH_BUTTON_PRESSED)){
ex_audio_status = !ex_audio_status;
if (!ex_audio_status) {
periph_bluetooth_pause(bt_periph);
}

audio_pipeline_stop(pipeline);
audio_pipeline_wait_for_stop(pipeline);
ESP_LOGE(TAG, "Changing music to %s", ex_audio_status ? "bt" : "i2sr");
if (ex_audio_status) {
audio_pipeline_breakup_elements(pipeline, i2s_stream_reader);
//蓝牙
audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
audio_pipeline_relink(pipeline, (const char *[]) {"bt", "i2sw"}, 2);
audio_pipeline_set_listener(pipeline, evt);
} else {
audio_pipeline_breakup_elements(pipeline, bt_stream_reader);
//外部
audio_pipeline_register(pipeline, i2s_stream_writer, "i2sw");
audio_pipeline_relink(pipeline, (const char *[]) {"i2sr", "i2sw"}, 2);
audio_pipeline_set_listener(pipeline, evt);
}
audio_pipeline_run(pipeline);
audio_pipeline_resume(pipeline);

if (ex_audio_status) {
periph_bluetooth_play(bt_periph);
}
}
}

//8.停止audio_pipeline
ESP_LOGI(TAG, "[ 8 ] Stop audio_pipeline");
audio_pipeline_terminate(pipeline);

audio_pipeline_unregister(pipeline, bt_stream_reader);
audio_pipeline_unregister(pipeline, i2s_stream_reader);
audio_pipeline_unregister(pipeline, i2s_stream_writer);

//在删除侦听器之前终止管道
/* Terminate the pipeline before removing the listener */
audio_pipeline_remove_listener(pipeline);

//在删除侦听器之前停止所有外围设备
/* Stop all peripherals before removing the listener */
esp_periph_stop_all();
audio_event_iface_remove_listener(esp_periph_get_event_iface(), evt);

//确保在销毁event_iface之前调用audio_pipeline_remove_listener和audio_event_iface_remove_listener
/* 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(bt_stream_reader);
audio_element_deinit(i2s_stream_reader);
audio_element_deinit(i2s_stream_writer);
esp_periph_destroy();
bluetooth_service_destroy();
}

Who is online

Users browsing this forum: No registered users and 1 guest