Open zsjlovestudy opened 1 month ago
我将这两个例程的主要部分写成了函数如下: ` void playmp3(const char *url) { audio_pipeline_handle_t pipeline; audio_element_handle_t spiffs_stream_reader, i2s_stream_writer, mp3_decoder;
// Initialize peripherals management esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG(); esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
// ESP_LOGI(TAG, "[ 1 ] Mount spiffs"); // 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_BOTH, AUDIO_HAL_CTRL_START); // es8311_set_mic_gain(0x22); // audio_hal_set_volume(board_handle->audio_hal, g_sysCFG.voice_volume); // Set voice volume (0-100) // es8311_set_mic_gain(g_sysCFG.pa_gain); // 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); // 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, spiffs_stream_reader, "spiffs"); audio_pipeline_register(pipeline, mp3_decoder, "mp3"); audio_pipeline_register(pipeline, i2s_stream_writer, "i2s"); // ESP_LOGI(TAG, "[3.5] Link it together [flash]-->spiffs-->mp3_decoder-->i2s_stream-->[codec_chip]"); const char *link_tag[3] = {"spiffs", "mp3", "i2s"}; audio_pipeline_link(pipeline, &link_tag[0], 3); // ESP_LOGI(TAG, "[3.6] Set up uri (file as spiffs, mp3 as mp3 decoder, and default output is i2s)"); audio_element_set_uri(spiffs_stream_reader, url); // ESP_LOGI(TAG, "[ 4 ] Set up 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, "[4.1] Listening event from all elements of pipeline"); audio_pipeline_set_listener(pipeline, evt); // ESP_LOGI(TAG, "[4.2] Listening event from peripherals"); audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt); // ESP_LOGI(TAG, "[ 5 ] Start audio_pipeline"); audio_pipeline_run(pipeline); // ESP_LOGI(TAG, "[ 6 ] 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 == 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_stream_set_clk(i2s_stream_writer, music_info.sample_rates, music_info.bits, music_info.channels); 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) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) { // ESP_LOGW(TAG, "[ * ] Stop event received"); break; } } // 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(set); audio_event_iface_remove_listener(esp_periph_set_get_event_iface(set), evt); /* 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(spiffs_stream_reader); audio_element_deinit(i2s_stream_writer); audio_element_deinit(mp3_decoder); esp_periph_set_destroy(set);
} ` codec和spiffs我已经提前初始化和挂载,然后可以实现多次重复运行这个函数,但是每次运行的时候都会在刚开始有一个破音,不知道是哪里的原因,我已经将codec初始化在初始化时只允许一次了,但是每次调用这个函数还是有破音。
不知道有什么解决方案,或者使用方法,如果只在初始化第一次播放音频时有破音,后面播放和录音都没有声音这样还可以接受。
你用的开发板和IDF ADF版本分别是?
idf的版本是V5.1.2 adf的版本是v2.6-108-g1f275b50
目前发现每次调用录音或者播放函数时,会有一下破音。然后再结束时也有一声。
您用的开发板是哪款呢?我们进行测试一下
我将这两个例程的主要部分写成了函数如下: ` void playmp3(const char *url) { audio_pipeline_handle_t pipeline; audio_element_handle_t spiffs_stream_reader, i2s_stream_writer, mp3_decoder;
// Initialize peripherals management esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG(); esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg);
} ` codec和spiffs我已经提前初始化和挂载,然后可以实现多次重复运行这个函数,但是每次运行的时候都会在刚开始有一个破音,不知道是哪里的原因,我已经将codec初始化在初始化时只允许一次了,但是每次调用这个函数还是有破音。
不知道有什么解决方案,或者使用方法,如果只在初始化第一次播放音频时有破音,后面播放和录音都没有声音这样还可以接受。