From accdee6652d731d6af63ac2b167581c8fb8f6b99 Mon Sep 17 00:00:00 2001 From: X-Ryl669 Date: Sat, 16 Mar 2024 18:54:16 +0100 Subject: [PATCH] Try to fix non-working microphone pipeline --- esphome/components/esp_adf/__init__.py | 3 + .../esp_adf/microphone/esp_adf_microphone.cpp | 88 ++++++++++++++++++- 2 files changed, 87 insertions(+), 4 deletions(-) diff --git a/esphome/components/esp_adf/__init__.py b/esphome/components/esp_adf/__init__.py index 52b8737b76..9a91394da8 100644 --- a/esphome/components/esp_adf/__init__.py +++ b/esphome/components/esp_adf/__init__.py @@ -91,6 +91,9 @@ async def to_code(config): cg.add_define("USE_ESP_ADF_BOARD") esp32.add_idf_sdkconfig_option(SUPPORTED_BOARDS[board], True) + #esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_BOOT_INIT", True) + #esp32.add_idf_sdkconfig_option("CONFIG_SPIRAM_ALLOW_STACK_EXTERNAL_MEMORY", True) + esp32.add_extra_script( "pre", diff --git a/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp b/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp index aa0cbfbb11..1912dc021b 100644 --- a/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp +++ b/esphome/components/esp_adf/microphone/esp_adf_microphone.cpp @@ -75,12 +75,14 @@ void ESPADFMicrophone::read_task(void *params) { event.type = TaskEventType::STARTING; xQueueSend(this_mic->read_event_queue_, &event, portMAX_DELAY); - +/* audio_pipeline_cfg_t pipeline_cfg = { .rb_size = 8 * 1024, }; + */ + audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG(); audio_pipeline_handle_t pipeline = audio_pipeline_init(&pipeline_cfg); - +/* i2s_driver_config_t i2s_config = {}; i2s_config.mode = (i2s_mode_t) (I2S_MODE_MASTER | I2S_MODE_RX); i2s_config.sample_rate = 16000; @@ -111,9 +113,37 @@ void ESPADFMicrophone::read_task(void *params) { i2s_cfg.uninstall_drv = true; i2s_cfg.need_expand = false; i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT; - +*/ + //i2s_stream_cfg_t i2s_cfg = I2S_STREAM_CFG_DEFAULT_WITH_PARA(CODEC_ADC_I2S_PORT, 48000, 16, AUDIO_STREAM_READER); + i2s_stream_cfg_t i2s_cfg = {}; + memset(&i2s_cfg, 0, sizeof(i2s_cfg)); + i2s_cfg.type = AUDIO_STREAM_READER; + i2s_cfg.i2s_port = (i2s_port_t)CODEC_ADC_I2S_PORT; + i2s_cfg.use_alc = false; + i2s_cfg.volume = 0; + i2s_cfg.out_rb_size = I2S_STREAM_RINGBUFFER_SIZE; + i2s_cfg.task_stack = I2S_STREAM_TASK_STACK; + i2s_cfg.task_core = I2S_STREAM_TASK_CORE; + i2s_cfg.task_prio = I2S_STREAM_TASK_PRIO; + i2s_cfg.stack_in_ext = false; + i2s_cfg.multi_out_num = 0; + i2s_cfg.uninstall_drv = true; + i2s_cfg.need_expand = false; + i2s_cfg.expand_src_bits = I2S_BITS_PER_SAMPLE_16BIT; + i2s_cfg.buffer_len = I2S_STREAM_BUF_SIZE; + i2s_cfg.i2s_config.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_RX); + i2s_cfg.i2s_config.sample_rate = 16000; + i2s_cfg.i2s_config.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT; + i2s_cfg.i2s_config.channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT; + i2s_cfg.i2s_config.communication_format = I2S_COMM_FORMAT_STAND_I2S; + i2s_cfg.i2s_config.intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM; + i2s_cfg.i2s_config.dma_buf_count = 3; + i2s_cfg.i2s_config.dma_buf_len = 300; + i2s_cfg.i2s_config.use_apll = true; + i2s_cfg.i2s_config.tx_desc_auto_clear = true; + i2s_cfg.i2s_config.fixed_mclk = 0; audio_element_handle_t i2s_stream_reader = i2s_stream_init(&i2s_cfg); - +/* rsp_filter_cfg_t rsp_cfg = { .src_rate = 16000, .src_ch = 2, @@ -134,6 +164,27 @@ void ESPADFMicrophone::read_task(void *params) { .task_prio = RSP_FILTER_TASK_PRIO, .stack_in_ext = true, }; + */ + rsp_filter_cfg_t rsp_cfg = {}; + memset(&rsp_cfg, 0, sizeof(rsp_cfg)); //DEFAULT_RESAMPLE_FILTER_CONFIG(); + rsp_cfg.src_rate = 16000; + rsp_cfg.src_ch = 2; + rsp_cfg.src_bits = 16; + rsp_cfg.dest_rate = 16000; + rsp_cfg.dest_ch = 1; + rsp_cfg.dest_bits = 16; + rsp_cfg.mode = RESAMPLE_DECODE_MODE; + rsp_cfg.max_indata_bytes = RSP_FILTER_BUFFER_BYTE; + rsp_cfg.out_len_bytes = RSP_FILTER_BUFFER_BYTE; + rsp_cfg.type = ESP_RESAMPLE_TYPE_AUTO; + rsp_cfg.complexity = 2; + rsp_cfg.down_ch_idx = 0; + rsp_cfg.prefer_flag = ESP_RSP_PREFER_TYPE_SPEED; + rsp_cfg.out_rb_size = RSP_FILTER_RINGBUFFER_SIZE; + rsp_cfg.task_stack = RSP_FILTER_TASK_STACK; + rsp_cfg.task_core = RSP_FILTER_TASK_CORE; + rsp_cfg.task_prio = RSP_FILTER_TASK_PRIO; + rsp_cfg.stack_in_ext = true; audio_element_handle_t filter = rsp_filter_init(&rsp_cfg); algorithm_stream_cfg_t algo_cfg = { @@ -179,6 +230,19 @@ void ESPADFMicrophone::read_task(void *params) { }; audio_pipeline_link(pipeline, &link_tag[0], 3); +/* // Set the event listener for the pipeline, else it'll fill itself and crash + audio_event_iface_cfg_t evt_cfg = AUDIO_EVENT_IFACE_DEFAULT_CFG(); + audio_event_iface_handle_t evt = audio_event_iface_init(&evt_cfg); + audio_pipeline_set_listener(pipeline, evt); + esp_periph_config_t periph_cfg = DEFAULT_ESP_PERIPH_SET_CONFIG(); + esp_periph_set_handle_t set = esp_periph_set_init(&periph_cfg); + + // Initialize SD Card peripheral +// audio_board_sdcard_init(set, SD_MODE_1_LINE); + audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt); + ESP_LOGW(TAG, "audio pipeline listener installed"); +*/ + audio_pipeline_run(pipeline); event.type = TaskEventType::STARTED; @@ -215,6 +279,22 @@ void ESPADFMicrophone::read_task(void *params) { rb_read(this_mic->ring_buffer_, nullptr, bytes_read - available, 0); } rb_write(this_mic->ring_buffer_, (char *) buffer, bytes_read, 0); + +/* // Pipeline listener for events too + audio_event_iface_msg_t msg; + esp_err_t ret = audio_event_iface_listen(evt, &msg, 0); + if (ret == ESP_OK) { + ESP_LOGW(TAG, "msg srct:%d, src:%p, cmd:%d, data:%d", msg.source_type, msg.source, msg.cmd, (int)msg.data); + + // Stop when the last pipeline element (raw_read in this case) receives stop event + if (msg.source_type == AUDIO_ELEMENT_TYPE_ELEMENT && msg.source == (void *) raw_read + && msg.cmd == AEL_MSG_CMD_REPORT_STATUS + && (((int)msg.data == AEL_STATUS_STATE_STOPPED) || ((int)msg.data == AEL_STATUS_STATE_FINISHED))) { + // Stop from the microphone (upon error?) + break; + } + } +*/ } allocator.deallocate(buffer, BUFFER_SIZE / sizeof(int16_t));