此套框架提供了一种使用流、编解码器和音频处理函数等元件开发音频应用程序的方法。
该框架是通过将元件组合成管道来开发音频应用程序。如下图所示:
将MP3解码器和I2S流两个元件添加进音频管道,解码器的输入是MP3文件数据流,I2S流将解码后的音频数据输出到片外,各应用程序之间通过事件接口通信。
get-started / play_mp3是此框架的最基本的例程之一,使用了MP3解码器和I2S流两个元件,我们通过分析这个例程来学习这套框架的使用。
初次接触这套框架,只是大概有个思路,有错在所难免,望见谅
/*读mp3文件的回调函数*/
int mp3_music_read_cb(audio_element_handle_t el, char *buf, int len, TickType_t wait_time, void *ctx)
{
static int mp3_pos;
int read_size = adf_music_mp3_end - adf_music_mp3_start - mp3_pos;
if (read_size == 0) {
return AEL_IO_DONE;
} else if (len < read_size) {
read_size = len;
}
memcpy(buf, adf_music_mp3_start + mp3_pos, read_size);
mp3_pos += read_size;
return read_size;
}
void app_main(void)
{
//定义一个音频处理管道
audio_pipeline_handle_t pipeline;
//因为是实现本地播放,只需定义i2s流和MP3解码两个元件
audio_element_handle_t i2s_stream_writer, mp3_decoder;
//关闭非必要显示
esp_log_level_set("*", ESP_LOG_WARN);
esp_log_level_set(TAG, ESP_LOG_INFO);
ESP_LOGI(TAG, "[ 1 ] Start audio codec chip");
//根据不同硬件版本进行硬件初始化
#if (CONFIG_ESP_LYRAT_V4_3_BOARD || CONFIG_ESP_LYRAT_V4_2_BOARD)
audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ES8388_DEFAULT();
audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 0);
#endif
#if (CONFIG_ESP_LYRATD_MSC_V2_1_BOARD || CONFIG_ESP_LYRATD_MSC_V2_2_BOARD)
audio_hal_codec_config_t audio_hal_codec_cfg = AUDIO_HAL_ZL38063_DEFAULT();
audio_hal_handle_t hal = audio_hal_init(&audio_hal_codec_cfg, 2);
#endif
//打开解码驱动
audio_hal_ctrl_codec(hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);
ESP_LOGI(TAG, "[ 2 ] Create audio pipeline, add all elements to pipeline, and subscribe pipeline event");
//管道初始化
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 mp3 decoder to decode mp3 file and set custom read callback");
//解码器初始化
mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG();
mp3_decoder = mp3_decoder_init(&mp3_cfg);
//设置解码回调函数
audio_element_set_read_cb(mp3_decoder, mp3_music_read_cb, NULL);
ESP_LOGI(TAG, "[2.2] Create i2s stream to write data to codec chip");
//I2S流初始化
i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
i2s_cfg.type = AUDIO_STREAM_WRITER;
i2s_cfg.i2s_config.sample_rate = 48000;
i2s_stream_writer = i2s_stream_init(&i2s_cfg);
ESP_LOGI(TAG, "[2.3] Register all elements to audio pipeline");
//注册元件
audio_pipeline_register(pipeline, mp3_decoder, "mp3");
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s");
ESP_LOGI(TAG, "[2.4] Link it together [mp3_music_read_cb]-->mp3_decoder-->i2s_stream-->[codec_chip]");
#if (CONFIG_ESP_LYRAT_V4_3_BOARD || CONFIG_ESP_LYRAT_V4_2_BOARD)
//连接管内元件
audio_pipeline_link(pipeline, (const char *[]) {"mp3", "i2s"}, 2);
#endif
/**Zl38063 does not support 44.1KHZ frequency, so resample needs to be used to convert files to other rates.
* You can transfer to 16kHZ or 48kHZ.
*/
#if (CONFIG_ESP_LYRATD_MSC_V2_1_BOARD || CONFIG_ESP_LYRATD_MSC_V2_2_BOARD)
rsp_filter_cfg_t rsp_cfg = DEFAULT_RESAMPLE_FILTER_CONFIG();
rsp_cfg.src_rate = 44100;
rsp_cfg.src_ch = 2;
rsp_cfg.dest_rate = 48000;
rsp_cfg.dest_ch = 2;
rsp_cfg.type = AUDIO_CODEC_TYPE_DECODER;
audio_element_handle_t filter = rsp_filter_init(&rsp_cfg);
audio_pipeline_register(pipeline, filter, "filter");
audio_pipeline_link(pipeline, (const char *[]) {"mp3", "filter", "i2s"}, 3);
#endif
ESP_LOGI(TAG, "[ 3 ] 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);
ESP_LOGI(TAG, "[3.1] Listening event from all elements of pipeline");
//监听管道事件
audio_pipeline_set_listener(pipeline, evt);
ESP_LOGI(TAG, "[ 4 ] Start audio_pipeline");
//运行管道
audio_pipeline_run(pipeline);
/*
主循环有三个判断:
判断1:判断事件接口是否定义成功,若成功,ret应当有效。
判断2:判断当前是否处于解码阶段,获取解码器相关信息,并显示,然后设置i2s数据流相关参数。
判断3:判断是否处于i2s数据流阶段,并且data已停止。退出循环。
*/
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;
}
//获取MP3解码器的的消息指针,并在其后显示相关信息
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);
//根据上面读取到的信息,设置i2s流
audio_element_setinfo(i2s_stream_writer, &music_info);
/* Es8388 and es8374 use this function to set I2S and codec to the same frequency as the music file, and zl38063
* does not need this step because the data has been resampled.*/
#if (CONFIG_ESP_LYRAT_V4_3_BOARD || CONFIG_ESP_LYRAT_V4_2_BOARD)
//设置i2s流相关参数
i2s_stream_set_clk(i2s_stream_writer, music_info.sample_rates , music_info.bits, music_info.channels);
#endif
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) {
break;
}
}
ESP_LOGI(TAG, "[ 5 ] Stop audio_pipeline");
//停止运行管道
audio_pipeline_terminate(pipeline);
//解注册元件
audio_pipeline_unregister(pipeline, mp3_decoder);
audio_pipeline_unregister(pipeline, i2s_stream_writer);
//移除监听
/* Terminate the pipeline before removing the listener */
audio_pipeline_remove_listener(pipeline);
/* Make sure audio_pipeline_remove_listener is called before destroying event_iface */
//打断事件
audio_event_iface_destroy(evt);
/* Release all resources */
//再次解注册元件???
audio_pipeline_unregister(pipeline, i2s_stream_writer);
audio_pipeline_unregister(pipeline, mp3_decoder);
#if (CONFIG_ESP_LYRATD_MSC_V2_1_BOARD || CONFIG_ESP_LYRATD_MSC_V2_2_BOARD)
//解注册滤波器
audio_pipeline_unregister(pipeline, filter);
//解定义滤波器
audio_element_deinit(filter);
#endif
//解定义管道
audio_pipeline_deinit(pipeline);
//解定义元件
audio_element_deinit(i2s_stream_writer);
audio_element_deinit(mp3_decoder);
}