ESP32 s3 | Help needed to simultaneously speak and listen to the audio from a microphone and a speaker.

BidhantGurung
Posts: 1
Joined: Tue Sep 26, 2023 9:52 am

ESP32 s3 | Help needed to simultaneously speak and listen to the audio from a microphone and a speaker.

Postby BidhantGurung » Tue Sep 26, 2023 10:03 am

I am trying to speak and listen to my voice simulatenously using the MEMS digital microphone and a MAX98357 audio codec amplifier.

The "i2s input data" of the digital microphone is connected to the esp32 s3. The "i2s output data" is the connected from the esp to the amplifier. For some reason, the esp is able to read my voice but it is not able to write to the amplifier/speaker. i can only hear a faint buzzing noise. What could be the problem. I have attched the code below for the reference. All the hardware seems to be working fine so I think it is a problem with the code itself. I feel like we cannot read and write simultaneoulsy so please help me in resolving this problem.

Code: Select all

#include <stdint.h>
#include <stdlib.h>
#include "sdkconfig.h"

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

#include "driver/i2s_std.h"
#include "driver/gpio.h"
#include "esp_check.h"
#include "driver/i2s_tdm.h"


#define MICROPHONE_DATA_IN          GPIO_NUM_10 

#define I2S_BCLK                    GPIO_NUM_12     
#define I2S_LRCLK                   GPIO_NUM_13

#define AMPLIFIER_DATA_OUT          GPIO_NUM_11

#define I2S_MIC_LR                   GPIO_NUM_14

#define EXAMPLE_BUFF_SIZE               2048

static i2s_chan_handle_t                tx_chan;        
static i2s_chan_handle_t                rx_chan;        

static void i2s_audio_task(void *args)
{
    uint8_t *r_buf = (uint8_t *)calloc(1, EXAMPLE_BUFF_SIZE);
    assert(r_buf); 

    size_t r_bytes = 0;
    size_t w_bytes = 0;

    ESP_ERROR_CHECK(i2s_channel_enable(rx_chan));
    ESP_ERROR_CHECK(i2s_channel_enable(tx_chan));

    esp_err_t ret = ESP_OK; 
    int count = 0;

    do {
        ret = i2s_channel_read(rx_chan, r_buf, EXAMPLE_BUFF_SIZE, &r_bytes, 100);
        if (ret == ESP_OK){
            printf("Read was successful...\n");
            printf("[0] %d [1] %d [2] %d [3] %d\n[4] %d [5] %d [6] %d [7] %d\n\n",
                   r_buf[0], r_buf[1], r_buf[2], r_buf[3], r_buf[4], r_buf[5], r_buf[6], r_buf[7]);
        }
        ret = i2s_channel_write(tx_chan, r_buf, EXAMPLE_BUFF_SIZE, &w_bytes, 100);
        if(ret == ESP_OK){
            printf("Write was successful...\n");
            printf("[0] %d [1] %d [2] %d [3] %d\n[4] %d [5] %d [6] %d [7] %d\n\n",
                   r_buf[0], r_buf[1], r_buf[2], r_buf[3], r_buf[4], r_buf[5], r_buf[6], r_buf[7]);
        } 

        count = count +1;
        vTaskDelay(100);
    }while (count <20);

    free(r_buf);
    vTaskDelete(NULL);
}

static void i2s_std_duplex(void)
{
    i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_AUTO, I2S_ROLE_MASTER);
    ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_chan, &rx_chan));
    i2s_std_config_t std_cfg = {
        .clk_cfg  = I2S_STD_CLK_DEFAULT_CONFIG(44100),
        .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_MONO),
        .gpio_cfg = {
            .mclk = I2S_GPIO_UNUSED,    
            .bclk = I2S_BCLK,
            .ws   = I2S_LRCLK,
            .dout = AMPLIFIER_DATA_OUT,
            .din  = MICROPHONE_DATA_IN, 
            .invert_flags = {
                .mclk_inv = false,
                .bclk_inv = false,
                .ws_inv   = false,
            },
        },
    };

    ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_chan, &std_cfg));
    ESP_ERROR_CHECK(i2s_channel_init_std_mode(rx_chan, &std_cfg));
}



void app_main(void)
{
    i2s_std_duplex();

    xTaskCreate(i2s_audio_task, "i2s_audio_task", 4096, NULL, 5, NULL);
}


Who is online

Users browsing this forum: Baidu [Spider], ESP_Sprite and 115 guests