#include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_log.h" #include "esp_err.h" #include "esp_timer.h" #include "bsp_board.h" #include "tca9555_driver.h" #include "LED.h" #define SAMPLE_RATE 16000 #define DELAY_SECONDS 1 #define DELAY_SAMPLES (SAMPLE_RATE * DELAY_SECONDS) static int16_t mono[256]; static int16_t stereo[512]; static int16_t delay_buffer[DELAY_SAMPLES]; static uint32_t write_idx = 0; static uint32_t read_idx = DELAY_SAMPLES/2; static void mic_task(void *arg) { int16_t buffer[1024]; while(1) { esp_get_feed_data(true, buffer, sizeof(buffer)); int level = get_mic_level(); for(int i = 0; i < 256; i++) { mono[i] = buffer[i*4+3]; } for(int i = 0; i < 256; i++) { delay_buffer[write_idx] = mono[i]; write_idx++; if(write_idx >= DELAY_SAMPLES) write_idx = 0; } static int16_t delayed_mono[256]; for(int i = 0; i < 256; i++) { delayed_mono[i] = delay_buffer[read_idx]; read_idx++; if(read_idx >= DELAY_SAMPLES) read_idx = 0; } for(int i = 0; i < 256; i++) { stereo[i*2] = delayed_mono[i]; stereo[i*2+1] = delayed_mono[i]; } esp_audio_play( stereo, sizeof(stereo), portMAX_DELAY ); if(level > 70) { led_strip_set_pixel( led_strip, 0, 5, 5, 5 ); led_strip_refresh(led_strip); } else { led_strip_clear(led_strip); } } } void app_main() { ESP_ERROR_CHECK(esp_board_init(16000, 2, 16)); gpio_set_level(GPIO_PWR_CTRL, 1); esp_audio_set_play_vol(60); tca9555_driver_init(); led_strip = configure_led(); xTaskCreate( mic_task, "mic_task", 8192, NULL, 5, NULL ); }