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;
}
}
}