Skip to content

algorithm example not working on lyrat mini v1.2 (AUD-6461) #1472

@keeturan

Description

@keeturan

Environment

  • Audio development kit: [ESP32-LyraT-Mini|]
  • Audio kit version (fESP32-LyraT-Mini]
  • [Required] Module or chip used: [ESP32-WROVER-E]
  • [Required] IDF version: v5.4
  • [Required] ADF version: v2.7-105-g4200c64d
  • Build system: [CMake]
  • [Required] Running log:

`--- Quit: Ctrl+] | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
ets Jul 29 2019 12:21:46

rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0030,len:6592
load:0x40078000,len:16664
load:0x40080400,len:4
--- 0x40080400: _init at ??:?

ho 8 tail 4 room 4
load:0x40080404,len:4268
entry 0x40080658
I (31) boot: ESP-IDF v5.4-dirty 2nd stage bootloader
I (31) boot: compile time Jun 20 2025 08:12:10
I (31) boot: Multicore bootloader
I (33) boot: chip revision: v3.1
I (36) qio_mode: Enabling default flash chip QIO
I (40) boot.esp32: SPI Speed : 80MHz
I (44) boot.esp32: SPI Mode : QIO
I (47) boot.esp32: SPI Flash Size : 8MB
I (51) boot: Enabling RNG early entropy source...
I (55) boot: Partition Table:
I (58) boot: ## Label Usage Type ST Offset Length
I (64) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (71) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (77) boot: 2 factory factory app 00 00 00010000 00200000
I (84) boot: 3 model Unknown data 01 82 00210000 00200000
I (90) boot: End of partition table
I (94) esp_image: segment 0: paddr=00010020 vaddr=3f400020 size=1e314h (123668) map
I (133) esp_image: segment 1: paddr=0002e33c vaddr=3ff80000 size=0001ch ( 28) load
I (133) esp_image: segment 2: paddr=0002e360 vaddr=3ffb0000 size=01cb8h ( 7352) load
I (139) esp_image: segment 3: paddr=00030020 vaddr=400d0020 size=43e04h (278020) map
I (215) esp_image: segment 4: paddr=00073e2c vaddr=3ffb1cb8 size=020d0h ( 8400) load
I (218) esp_image: segment 5: paddr=00075f04 vaddr=40080000 size=13534h ( 79156) load
I (252) boot: Loaded app from partition at offset 0x10000
I (252) boot: Disabling RNG early entropy source...
I (262) quad_psram: This chip is ESP32-D0WD
I (263) esp_psram: Found 8MB PSRAM device
I (263) esp_psram: Speed: 80MHz
I (263) esp_psram: PSRAM initialized, cache is in low/high (2-core) mode.
W (270) esp_psram: Virtual address not enough for PSRAM, map as much as we can. 4MB is mapped
I (278) cpu_start: Multicore app
I (795) esp_psram: SPI SRAM memory test OK
I (803) cpu_start: Pro cpu start user code
I (803) cpu_start: cpu freq: 240000000 Hz
I (803) app_init: Application information:
I (803) app_init: Project name: algorithm_examples
I (808) app_init: App version: 1
I (811) app_init: Compile time: Jun 20 2025 08:15:10
I (816) app_init: ELF file SHA256: 5b7789baa...
I (820) app_init: ESP-IDF: v5.4-dirty
I (825) efuse_init: Min chip rev: v0.0
I (828) efuse_init: Max chip rev: v3.99
I (832) efuse_init: Chip rev: v3.1
I (836) heap_init: Initializing. RAM available for dynamic allocation:
I (843) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (848) heap_init: At 3FFB4A28 len 0002B5D8 (173 KiB): DRAM
I (853) heap_init: At 3FFE0440 len 00003AE0 (14 KiB): D/IRAM
I (858) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (864) heap_init: At 40093534 len 0000CACC (50 KiB): IRAM
I (869) esp_psram: Adding pool of 4096K of PSRAM memory to heap allocator
I (876) spi_flash: detected chip: gd
I (879) spi_flash: flash io: qio
W (882) ADC: legacy driver is deprecated, please migrate to esp_adc/adc_oneshot.h
I (890) main_task: Started on CPU0
I (893) esp_psram: Reserving pool of 32K of internal memory for DMA/internal allocations
I (900) main_task: Calling app_main()
I (904) ALGORITHM_EXAMPLES: [2.0] Start codec chip
W (909) i2c_bus_v2: I2C master handle is NULL, will create new one
I (920) DRV8311: ES8311 in Slave mode
I (931) gpio: GPIO[21]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (977) AUDIO_HAL: Codec mode is 3, Ctrl:1
I (984) ALGORITHM_EXAMPLES: [3.0] Create audio pipeline_rec for recording
I (984) ALGORITHM_EXAMPLES: [3.1] Create algorithm stream for aec
I (991) MODEL_LOADER: The storage free size is 3968 KB
I (991) MODEL_LOADER: The partition size is 2048 KB
I (1000) MODEL_LOADER: Successfully load srmodels
I (1000) ALGORITHM_EXAMPLES: [3.4] Register all elements to audio pipeline_rec
I (1006) ALGORITHM_EXAMPLES: [3.5] Link it together [codec_chip]-->aec-->wav_encoder-->fatfs_stream-->[sdcard]
I (1016) ALGORITHM_EXAMPLES: [5.0] Set up event listener
I (1021) ALGORITHM_EXAMPLES: [5.2] Listening event from peripherals
I (1027) ALGORITHM_EXAMPLES: [6.0] Start audio_pipeline
I (1032) AUDIO_THREAD: The aec task allocate stack on external memory
I (1039) AUDIO_ELEMENT: [aec-0x3f8018d0] Element task created
I (1043) AUDIO_PIPELINE: Func:audio_pipeline_run, Line:359, MEM Total:4425168 Bytes, Inter:332975 Bytes, Dram:281919 Bytes, Dram largest free:118784Bytes

I (1057) AUDIO_ELEMENT: [aec] AEL_MSG_CMD_RESUME,state:1
I (1123) AFE: AFE Version: (1MIC_V250121)
I (1123) AFE: Input PCM Config: total 2 channels(1 microphone, 1 playback), sample rate:16000
I (1126) AFE: AFE Pipeline: [input] -> |AEC(VOIP_LOW_COST)| -> |NS(WebRTC)| -> [output]
I (1135) AUDIO_THREAD: The algo_fetch task allocate stack on external memory
I (1160) AUDIO_PIPELINE: Pipeline started
I (1161) ALGORITHM_EXAMPLES: [7.0] Listen for all pipeline events`

  • Operating system: [Windows]
  • Using an IDE?: espressif ide
  • Power supply: [USB]

Problem Description

I made loopback by connecting the algorithm input to the speaker output.
I want to hear the echo cancellation performance live.
There is noisy sound in the algorithm output. The microphone is not working properly.
RECORD_HARDWARE_AEC variable is set to true.

Code to Reproduce This Issue

/* Algorithm Example

   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 "driver/i2s.h"
#include "audio_element.h"
#include "audio_pipeline.h"
#include "board.h"
#include "es8311.h"
#ifdef CONFIG_ESP32_S3_KORVO2_V3_BOARD
#include "es7210.h"
#endif  /* CONFIG_ESP32_S3_KORVO2_V3_BOARD */
#include "fatfs_stream.h"
#include "i2s_stream.h"
#include "algorithm_stream.h"
#include "wav_encoder.h"
#include "mp3_decoder.h"
#include "filter_resample.h"
#include "audio_mem.h"
#include "audio_sys.h"
#include "audio_idf_version.h"

static const char *TAG = "ALGORITHM_EXAMPLES";

/* Debug original input data for AEC feature*/
// #define DEBUG_ALGO_INPUT

#define I2S_SAMPLE_RATE     16000
#if CONFIG_ESP_LYRAT_MINI_V1_1_BOARD || CONFIG_ESP32_S3_KORVO2L_V1_BOARD
#define I2S_CHANNELS        I2S_CHANNEL_FMT_RIGHT_LEFT
#else
#define I2S_CHANNELS        I2S_CHANNEL_FMT_ONLY_LEFT 
#endif
#define I2S_BITS            CODEC_ADC_BITS_PER_SAMPLE

/* The AEC internal buffering mechanism requires that the recording signal
   is delayed by around 0 - 10 ms compared to the corresponding reference (playback) signal. */
#define DEFAULT_REF_DELAY_MS    0
#define ESP_RING_BUFFER_SIZE    256

extern const uint8_t adf_music_mp3_start[] asm("_binary_test_mp3_start");
extern const uint8_t adf_music_mp3_end[]   asm("_binary_test_mp3_end");

#if !RECORD_HARDWARE_AEC
static ringbuf_handle_t ringbuf_ref;
#endif
static audio_element_handle_t i2s_stream_reader;
static audio_element_handle_t i2s_stream_writter;



static int i2s_read_cb(audio_element_handle_t el, char *buf, int len, TickType_t wait_time, void *ctx)
{

    size_t bytes_read = audio_element_input(i2s_stream_reader, buf, len);
    if (bytes_read > 0) {
#if (CONFIG_IDF_TARGET_ESP32 && !RECORD_HARDWARE_AEC)
        algorithm_mono_fix((uint8_t *)buf, bytes_read);
#endif
    } else {
        ESP_LOGE(TAG, "i2s read failed");
    }

    return bytes_read;
}

static int i2s_write_cb(audio_element_handle_t el, char *buf, int len, TickType_t wait_time, void *ctx)
{
    int bytes_write = 0;

#if !RECORD_HARDWARE_AEC
    if (rb_write(ringbuf_ref, buf, len, wait_time) <= 0) {
        ESP_LOGW(TAG, "ringbuf write timeout");
    }
#endif

#if (CONFIG_IDF_TARGET_ESP32 && !RECORD_HARDWARE_AEC)
    algorithm_mono_fix((uint8_t *)buf, len);
#endif
    bytes_write = audio_element_output(i2s_stream_writter, buf, len);
    if (bytes_write < 0) {
        ESP_LOGE(TAG, "i2s write failed");
    }
    return bytes_write;
}

void app_main()
{
    esp_log_level_set("*", ESP_LOG_INFO); 
    esp_log_level_set(TAG, ESP_LOG_INFO);


    // 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, "[2.0] Start codec chip");
    i2s_stream_cfg_t i2s_w_cfg = I2S_STREAM_CFG_DEFAULT_WITH_PARA(I2S_NUM_0, I2S_SAMPLE_RATE, I2S_BITS, AUDIO_STREAM_WRITER);
    i2s_w_cfg.task_stack = -1;
    i2s_w_cfg.need_expand = (16 != I2S_BITS);
    i2s_stream_set_channel_type(&i2s_w_cfg, I2S_CHANNELS);
    i2s_stream_writter = i2s_stream_init(&i2s_w_cfg);

    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);
    audio_hal_set_volume(board_handle->audio_hal, 80);


    i2s_stream_cfg_t i2s_r_cfg = I2S_STREAM_CFG_DEFAULT_WITH_PARA(CODEC_ADC_I2S_PORT, I2S_SAMPLE_RATE, I2S_BITS, AUDIO_STREAM_READER);
    i2s_r_cfg.task_stack = -1;
    i2s_stream_set_channel_type(&i2s_r_cfg, I2S_CHANNELS);
    i2s_stream_reader = i2s_stream_init(&i2s_r_cfg);

    ESP_LOGI(TAG, "[3.0] Create audio pipeline_rec for recording");
    audio_pipeline_cfg_t pipeline_cfg = DEFAULT_AUDIO_PIPELINE_CONFIG();
    audio_pipeline_handle_t pipeline_rec = audio_pipeline_init(&pipeline_cfg);
    mem_assert(pipeline_rec);

    ESP_LOGI(TAG, "[3.1] Create algorithm stream for aec");
    algorithm_stream_cfg_t algo_config = ALGORITHM_STREAM_CFG_DEFAULT();

    algo_config.input_format = "RM";
 
#if !RECORD_HARDWARE_AEC 
    algo_config.input_type = ALGORITHM_STREAM_INPUT_TYPE2;
#endif
#if CONFIG_ESP_LYRAT_MINI_V1_1_BOARD
    algo_config.ref_linear_factor = 3;
#endif
#ifdef DEBUG_ALGO_INPUT
    algo_config.debug_input = true;
#endif
    algo_config.sample_rate = I2S_SAMPLE_RATE;
    algo_config.out_rb_size = ESP_RING_BUFFER_SIZE;
    audio_element_handle_t element_algo = algo_stream_init(&algo_config);
    audio_element_set_music_info(element_algo, I2S_SAMPLE_RATE, 1, ALGORITHM_STREAM_DEFAULT_SAMPLE_BIT);
    audio_element_set_read_cb(element_algo, i2s_read_cb, NULL);
    audio_element_set_input_timeout(element_algo, portMAX_DELAY);

    audio_element_set_write_cb(element_algo, i2s_write_cb, NULL);
    audio_element_set_output_timeout(element_algo, portMAX_DELAY); 

    ESP_LOGI(TAG, "[3.4] Register all elements to audio pipeline_rec");
    audio_pipeline_register(pipeline_rec, element_algo, "aec");  

    ESP_LOGI(TAG, "[3.5] Link it together [codec_chip]-->aec-->wav_encoder-->fatfs_stream-->[sdcard]");
    const char *link_rec[1] = {"aec"};
    audio_pipeline_link(pipeline_rec, &link_rec[0], 1); 



#if !RECORD_HARDWARE_AEC
    // Please reference the way of ALGORITHM_STREAM_INPUT_TYPE2 in "algorithm_stream.h"
    ringbuf_ref = rb_create(ALGORITHM_STREAM_RINGBUFFER_SIZE, 1);
    audio_element_set_multi_input_ringbuf(element_algo, ringbuf_ref, 0);

    /* When the playback signal far ahead of the recording signal,
        the playback signal needs to be delayed */
    algo_stream_set_delay(element_algo, ringbuf_ref, DEFAULT_REF_DELAY_MS);
#endif

    ESP_LOGI(TAG, "[5.0] 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, "[5.2] Listening event from peripherals");
    audio_event_iface_set_listener(esp_periph_set_get_event_iface(set), evt);

    ESP_LOGI(TAG, "[6.0] Start audio_pipeline");




    audio_pipeline_run(pipeline_rec);

    ESP_LOGI(TAG, "[7.0] 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;
        }




    }

    ESP_LOGI(TAG, "[8.0] Stop audio_pipeline");

    audio_pipeline_stop(pipeline_rec);
    audio_pipeline_wait_for_stop(pipeline_rec);
    audio_pipeline_deinit(pipeline_rec);


    audio_element_deinit(i2s_stream_reader);
    audio_element_deinit(i2s_stream_writter);

    /* Terminate the pipeline before removing the listener */


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

    esp_periph_set_destroy(set);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions