切換mp3撥放來源時,音樂播放異常

DARTECH
Posts: 3
Joined: Tue Oct 11, 2022 5:29 am

切換mp3撥放來源時,音樂播放異常

Postby DARTECH » Wed Oct 19, 2022 9:51 am

目前我們的mp3音訊來源有兩個(http及spiffs),當我們撥放http時,切換至spiffs撥放音樂時(音樂為人聲),聽到聲音撥放是異常的(i2s sample rate是錯的),主因是沒有收到mp3_decoder的AEL_MSG_CMD_REPORT_MUSIC_INFO通知,造成採樣速度及播放音訊異常。
1.想請問該如何修改程式?或正確的切換方式?
2.play_mp3程式將永遠存在而不會刪除任務

以下是我們的程式:

void play_mp3(void)
{


esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG();
play_set = esp_periph_set_init(&periph_cfg);

ESP_LOGI(TAG, "[ 1 ] Mount spiffs");
// Initialize Spiffs peripheral
periph_spiffs_cfg_t spiffs_cfg = {
.root = "/spiffs",
.partition_label = NULL,
.max_files = 5,
.format_if_mount_failed = true
};
esp_periph_handle_t spiffs_handle = periph_spiffs_init(&spiffs_cfg);

// Start spiffs
esp_periph_start(play_set, spiffs_handle);

// Wait until spiffs is mounted
while (!periph_spiffs_is_mounted(spiffs_handle)) {
vTaskDelay(500 / portTICK_PERIOD_MS);
}

//ESP_LOGI(TAG, "[ 2 ] Start codec chip");
//audio_board_handle_t board_handle = audio_board_init();
//audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_DECODE, AUDIO_HAL_CTRL_START);

ESP_LOGI(TAG, "[3.0] Create audio pipeline for playback");
audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
pipeline = audio_pipeline_init(&pipeline_cfg);
AUDIO_NULL_CHECK(TAG, pipeline, return);

http_stream_cfg_t http_cfg = HTTP_STREAM_CFG_DEFAULT();
http_stream_reader = http_stream_init(&http_cfg);

ESP_LOGI(TAG, "[3.1] Create spiffs stream to read data from sdcard");
spiffs_stream_cfg_t flash_cfg = SPIFFS_STREAM_CFG_DEFAULT();
flash_cfg.type = AUDIO_STREAM_READER;
spiffs_stream_reader = spiffs_stream_init(&flash_cfg);

ESP_LOGI(TAG, "[3.2] Create i2s stream to write data to codec chip");
i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT();
i2s_cfg.type = AUDIO_STREAM_WRITER;
i2s_stream_writer = i2s_stream_init(&i2s_cfg);

ESP_LOGI(TAG, "[3.3] Create mp3 decoder to decode mp3 file");
mp3_decoder_cfg_t mp3_cfg = DEFAULT_MP3_DECODER_CONFIG();
mp3_decoder = mp3_decoder_init(&mp3_cfg);

ESP_LOGI(TAG, "[3.4] Register all elements to audio pipeline");
audio_pipeline_register(pipeline, http_stream_reader, "http");
audio_pipeline_register(pipeline, spiffs_stream_reader, "spiffs");
audio_pipeline_register(pipeline, mp3_decoder, "mp3");
audio_pipeline_register(pipeline, i2s_stream_writer, "i2s");

const char *link_tag[3] = {"http", "mp3", "i2s"};
audio_pipeline_link(pipeline, &link_tag[0], 3);

audio_element_set_uri(http_stream_reader, "");

ESP_LOGI(TAG, "[ 4 ] Set up event listener");
audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG();
play_evt = audio_event_iface_init(&evt_cfg);

ESP_LOGI(TAG, "[4.1] Listening event from all elements of pipeline");
audio_pipeline_set_listener(pipeline, play_evt);

//ESP_LOGI(TAG, "[4.2] Listening event from peripherals");
//audio_event_iface_set_listener(esp_periph_set_get_event_iface(play_set), play_evt);

while (1) {
audio_event_iface_msg_t msg;
esp_err_t ret = audio_event_iface_listen(play_evt, &msg, portMAX_DELAY);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "[ * ] Event interface error : %d", ret);
continue;
}


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

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

if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) i2s_stream_writer
&& msg.cmd == AEL_MSG_CMD_REPORT_STATUS){

if((int)msg.data == AEL_STATUS_STATE_FINISHED)
{
//audio_pipeline_resume(pipeline_record);
printf("AEL_STATUS_STATE_FINISHED\n");
}
else if((int)msg.data == AEL_STATUS_STATE_STOPPED)
{

printf("AEL_STATUS_STATE_STOPPED\n");
}
else if((int)msg.data == AEL_STATUS_NONE)
{
printf("AEL_STATUS_NONE\n");

}
}




}

ESP_LOGI(TAG, "[ 7 ] Stop audio_pipeline");
audio_pipeline_stop(pipeline);
audio_pipeline_wait_for_stop(pipeline);
audio_pipeline_terminate(pipeline);

audio_pipeline_unregister(pipeline, spiffs_stream_reader);
audio_pipeline_unregister(pipeline, i2s_stream_writer);
audio_pipeline_unregister(pipeline, mp3_decoder);

/* Terminal the pipeline before removing the listener */
audio_pipeline_remove_listener(pipeline);

/* Stop all periph before removing the listener */
esp_periph_set_stop_all(play_set);
audio_event_iface_remove_listener(esp_periph_set_get_event_iface(play_set), play_evt);

/* Make sure audio_pipeline_remove_listener & audio_event_iface_remove_listener are called before destroying event_iface */
audio_event_iface_destroy(play_evt);


/* Release all resources */
audio_pipeline_deinit(pipeline);
audio_element_deinit(spiffs_stream_reader);
audio_element_deinit(i2s_stream_writer);
audio_element_deinit(mp3_decoder);
esp_periph_set_destroy(play_set);

//xEventGroupSetBits(test_evt,BIT1);

//audio_pipeline_resume(pipeline_record);

vTaskDelete(NULL);
}

void test_evt_task(void)
{
EventBits_t bits = NULL;
while(true){
bits= xEventGroupWaitBits(test_evt,
BIT1 | BIT0,
pdFALSE,
pdFALSE,
portMAX_DELAY);
if (bits & BIT0) {

printf("event1\n");

audio_pipeline_pause(pipeline);
audio_pipeline_unlink(pipeline);
audio_pipeline_link(pipeline, (const char *[]) {"http", "mp3", "i2s"}, 3);
audio_element_set_uri(http_stream_reader, "https://dl.espressif.com/dl/audio/ff-16b-2c-44100hz.mp3");
audio_pipeline_set_listener(pipeline, play_evt);
audio_pipeline_reset_ringbuffer(pipeline);
audio_pipeline_reset_elements(pipeline);
audio_pipeline_change_state(pipeline,AEL_STATE_INIT);
audio_pipeline_run(pipeline);

xEventGroupClearBits(test_evt,BIT0);
}else if (bits & BIT1) {
printf("event2\n");

audio_pipeline_terminate(pipeline);
audio_pipeline_change_state(pipeline,AEL_STATE_STOPPED);
audio_pipeline_unlink(pipeline);
audio_pipeline_link(pipeline, (const char *[]) {"spiffs", "mp3", "i2s"}, 3);
audio_element_set_uri(spiffs_stream_reader, "/spiffs/start_ok.mp3");
audio_pipeline_set_listener(pipeline, play_evt);
audio_pipeline_reset_ringbuffer(pipeline);
audio_pipeline_reset_elements(pipeline);
audio_pipeline_change_state(pipeline,AEL_STATE_INIT);
audio_pipeline_run(pipeline);

xEventGroupClearBits(test_evt,BIT1);
}else if (bits & BIT2) {

xEventGroupClearBits(test_evt,BIT2);
}
}
vTaskDelete(NULL);
}

void task1(void){

while(1)
{

vTaskDelay(10/portTICK_RATE_MS );
}
vTaskDelete(NULL);
}

void task2(void){

while(1)
{

vTaskDelay(10/portTICK_RATE_MS );
}
vTaskDelete(NULL);
}

void task3(void){

while(1)
{

vTaskDelay(10/portTICK_RATE_MS );
}
vTaskDelete(NULL);
}


void app_main()
{
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

wifi_init_sta();

test_evt = xEventGroupCreate();

audio_board_handle_t board_handle = audio_board_init();
audio_hal_ctrl_codec(board_handle->audio_hal, AUDIO_HAL_CODEC_MODE_BOTH, AUDIO_HAL_CTRL_START);

xTaskCreate(&play_mp3,"play_mp3",4096,NULL,15,&play_mp3_task);
xTaskCreate(&task1,"task1",4096,NULL,13,&test1_task);//dont care
xTaskCreate(&task2,"task2",4096,NULL,13,&test2_task);//dont care
xTaskCreate(&task3,"task3",4096,NULL,13,&test3_task);//dont care
xTaskCreate(&test_evt_task,"test_evt_task",4096,NULL,10,NULL);

vTaskDelay(5000/portTICK_PERIOD_MS );
printf("BIT0\n");
xEventGroupSetBits(test_evt,BIT0);

vTaskDelay(5000/portTICK_PERIOD_MS );
printf("BIT1\n");
xEventGroupSetBits(test_evt,BIT1);
}

ESP_William
Posts: 124
Joined: Tue Apr 24, 2018 5:54 am

Re: 切換mp3撥放來源時,音樂播放異常

Postby ESP_William » Thu Nov 10, 2022 11:32 am

如果音频源变化并且音频源的采样率也不同的应用,如果不熟悉 pipeline 的切换操作的话建议用高级接口 audio_play(), 可以参考 CLI 的音频实现方式(可切换各种音频源播放,用 console 的方式演示出来):https://github.com/espressif/esp-adf/tr ... amples/cli

Who is online

Users browsing this forum: No registered users and 56 guests