Glitches when sendind I2S through Wi-Fi

Hello everyone! I have problem.
I getting audio chunks from INPM411 mic though i2s interface and send to little web server (where I join chunks to one audio file).
And everything OK, but on the end of chunks I have glitches - for example, if I have 8096 samples to read from mic and send, I have glitch approximately one per second.
If I read 512 samples - I have little glitches, approximately few tens in second
I tried independent tasks, two queues, ring queue - but I hear glitches anyway.
And if I set 512 samples, may be it’s OK, but my S3 overheat after 5-7 minutes, cause a lot of little chunks.
For me enough chunks with any size, I need to remove that glitches

#include <WiFi.h>
#include <WiFiUdp.h>
#include "driver/i2s.h"
#include "driver/rtc_io.h"

// === Wi-Fi / UDP ===
const char* ssid = "*";
const char* password = "*";
WiFiUDP udp;
const char* udpAddress = "192.168.1.221";
const int udpPort = 5011;

// === I2S ===
#define SAMPLE_RATE   16000
#define I2S_WS        8
#define I2S_SD        43
#define I2S_SCK       7
// #define BLOCK_SIZE    32768 
#define BLOCK_SIZE    8096
// #define BLOCK_SIZE    4048  
// #define BLOCK_SIZE    2048

int32_t i2sBuffer[BLOCK_SIZE];
int16_t sendBuffer[BLOCK_SIZE];

void setupI2S() {
    i2s_config_t i2s_config = {
        .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
        .sample_rate = SAMPLE_RATE,
        .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
        .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
        .communication_format = I2S_COMM_FORMAT_I2S,
        .intr_alloc_flags = 1,
        .dma_buf_count = 40,
        .dma_buf_len = 1024,
        .use_apll = true
    };

    i2s_pin_config_t pin_config = {
        .bck_io_num = I2S_SCK,
        .ws_io_num = I2S_WS,
        .data_out_num = I2S_PIN_NO_CHANGE,
        .data_in_num = I2S_SD
    };

    i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
    i2s_set_pin(I2S_NUM_0, &pin_config);
}

void setup() {
    Serial.begin(115200);

    Serial.println("Device started, waiting for button press...");

    // Wi-Fi
    WiFi.begin(ssid, password);
    Serial.print("Connecting to WiFi");
    while (WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("\nWiFi connected!");
    Serial.println(WiFi.localIP());

    udp.begin(udpPort);
    setupI2S();
}

void loop() {
    size_t bytesRead;
    i2s_read(I2S_NUM_0, i2sBuffer, sizeof(i2sBuffer), &bytesRead, portMAX_DELAY);
    int samplesRead = bytesRead / 4;

    for (int i = 0; i < samplesRead; i++) {
        int32_t sample32 = i2sBuffer[i];
        int32_t s = sample32 >> 13;
        if (s > 32767) s = 32767;
        if (s < -32768) s = -32768;
        sendBuffer[i] = (int16_t)s;
    }

    udp.beginPacket(udpAddress, udpPort);
    udp.write((uint8_t*)sendBuffer, samplesRead * 2);
    udp.endPacket();
}

Hi there,

Sounds like the rtos is inducing a delay and you get a glitch in the sound when recording.
I have seen this before, on here so check other I2S threads, you’ll find it.
:crossed_fingers:

HTH
GL :slight_smile: PJ :v:

not sure if the fix was the code or the sampling though, it’s been a while.

Yeah, I have tried to find, but almost all posts - about wifi or l2s, but dont’ together.
I need to understand why I hear glitches on the end of sended chunks - chatgpt said, that it ok, cause blocking operations and switching buffers and etc, and I need to know, how I can do it correctly

Hi there,

Ok, Yes… That sounds correct, Look for the “sending in Chunks” that was one a user figured it out in. they are scattered about, Check out also the Youtube video on the Lost KeyFinder , there is a section on the Sound (i2S) that may add some insight.
Good topic though and users have used the Max chip amp and dac to get very solid results.

HTH
GL :slight_smile: PJ :v: