I2S Receive bug causing garbage data?

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

I2S Receive bug causing garbage data?

Postby mgleason_3 » Wed Mar 22, 2017 6:57 am

Trying to receive audio via I2S from a single mic but there seems to be two issues:
1) With a single mic, you'd expect to see an interleaving of left and right channels, where one channel has data and the other is zeros . Instead, I'm seeing periods of data on both channels and then zeros on both channels; then data; then zeros. See "SAMPLE DATA" below.
2) When the data is averaged, the averaged should be similar for similar volume levels. Interestingly, during a single "run" the averages are similar. However, when the ESP32 is reset, the average changes. It's as though there is some timing issue that changes when the ESP32 is reset. See "AVERAGES FROM SAMPLE RUNS" below.

Maybe there's some additional setup required?

Code: Select all

static void init_i2s()
{
    i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_RX,                                  // Receive
        .sample_rate = SAMPLE_RATE,
        .bits_per_sample = BITS_PER_SAMPLE,
        .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
        .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,
        .dma_buf_count = 14,                                                    // number of buffers, 128 max.
        .dma_buf_len = 32*2,                                                    // size of each buffer
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1                                //Interrupt level 1
    };
    i2s_pin_config_t pin_config = {
        .bck_io_num = 26,
        .ws_io_num = 25,
        .data_in_num = 22,
        .data_out_num = I2S_PIN_NO_CHANGE                                       //was -1
    };

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

SAMPLE DATA
255 255 78 16 0 0 205 13 0 0 253 9 255 255 69 6 0 0 251 3 0 0 37 3 255 255 75 3 255 255 19 4 255 255 113 5 0 0 18 7 255 255 62 9 0 0 63 11 0 0 25 13 0 0 132 14 255 255 104 15 255 255 201 15 0 0 206 15 0 0 131 15 255 255 235 14 0 0 15 14 0 0 220 12 0 0 90 11 0 0 73 9 255 255 172 6 255 255 131 3 255 255 26 0 0 0 102 252 0 0 222 248 0 0 239 245 255 255 193 243 0 0 28 242 0 0 31 241 255 255 139 240 255 255 40 240 0 0 186 239 255 255 42 239 255 255 116 238 255 255 194 237 255 255 71 237 0 0 12 237 255 255 33 237 0 0 84 237 0 0 192 237 0 0 75 238 255 255 239 238 0 0 148 239 255 255 70 240 0 0 255 240 255 255 211 241 255 255 188 242 0 0 223 243 0 0 252 244 0 0 28 246 0 0 238 246 255 255 194 247 255 255 128 248 255 255 74 249 255 255 250 249 0 0 167 250 0 0 52 251 0 0 160 251 255 255 181 251 255 255 188 251 255 255 255 251 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 255 255 146 252 0 0 151 253 0 0 19 255 0 0 248 0 255 255 75 3 255 255 193 5 0 0 239 7 0 0 209 9 0 0 21 11 0 0 188 11 255 255 168 11 255 255 19 11 0 0 75 10 0 0 158 9 255 255 30 9 255 255 124 8 0 0 165 7 0 0 200 6 0 0 9 6 255 255 107 5 255 255 184 4 255 255 200 3 0 0 158 2 0 0 102 1 0 0 23 0 0 0 216 254 255 255 206 253 255 255 14 253 255 255 163 252 0 0 143 252 255 255 168 252 0 0 234 252 255 255 113 253 255 255 27 254 255 255 176 254 0 0 21 255 0 0 50 255 0 0 245 254 0 0 109 254 0 0 167 253 255 255 192 252 255 255 213 251 255 255 97 251 0 0 192 251 255 255 216 252 255 255 49 254 255 255 69 255 0 0 61 0 255 255 11 1 0 0 65 2 0 0 143 4 0 0 102 7 255 255 140 9 255 255 129 11 0 0 151 13 255 255 206 16 255 255 93 21 0 0 205 26 255 255 96 32 255 255 196 37 255 255 130 42 0 0 248 44 255 255 241 44 255 255 12 43 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 181 39 255 255 119 35 0 0 132 30 0 0 183 25 255 255 173 20 255 255 14 16 0 0 88 12 0 0 92 10 255 255 144 10 255 255 110 12 255 255 123 15 255 255 49 19 255 255 157 22 0 0 229 24 255 255 127 25 0 0 80 24 0 0 12 22 0 0 141 19 255 255 226 17 255 255 188 17 0 0 93 19 255 255 81 22 255 255 56 26 0 0 229 29 0 0 33 33 255 255 28 35 0 0 4 35 255 255 41 33 255 255 226 29 255 255 91 26 255 255 208 22 255 255 242 19 0 0 253 17 255 255 21 17 255 255 73 17 0 0 249 17 255 255 232 18 255 255 201 19 255 255 217 20 255 255 33 22 255 255 225 23 0 0 191 25 255 255 61 27 255 255 146 27 0 0 163 26 255 255 247 24 255 255 227 22 255 255 142 20 0 0 73 18 255 255 172 16 0 0 219 14 0 0 136 12 255 255 60 10 255 255 234 8 255 255 219 8 255 255 51 10 255 255 41 12 255 255 147 14 0 0 239 17 0 0 63 22 0 0 176 26 255 255 78 30 255 255 191 32 0 0 251 32 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 169 30 255 255 188 25 0 0 228 18 0 0 42 11 255 255 234 3 0 0 231 253 0 0 106 249 255 255 127 246 255 255 17 245 255 255 13 245 255 255 103 246 0 0 224 248 0 0 57 252 255 255 228 255 255 255 80 3 255 255 209 5 255 255 192 6 255 255 25 6 255 255 241 3 0 0 249 0 0 0 218 253 0 0 20 251 255 255 207 248 0 0 25 247 0 0 215 245 255 255 24 245 255 255 207 244 255 255 29 245 0 0 129 245 255 255 109 245 0 0 85 244 0 0 65 242 0 0 161 239 0 0 90 237 0 0 12 236 0 0 14 236 0 0 39 237 0 0 251 238 255 255 10 241 0 0 229 242 255 255 121 244 255 255 189 245 255 255 6 247 255 255 72 248 0 0 155 249 255 255 166 250 0 0 10 251 0 0 129 250 0 0 83 249 255 255 71 248 255 255 175 247 255 255 93 247 255 255 75 247 255 255 77 247 255 255 162 247 255 255 98 248 0 0 189 249 255 255 107 251 255 255 52 253 0 0 217 254 255 255 50 0 0 0 6 1 255 255 92 1 255 255 47 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
AVERAGES FROM SAMPLE RUNS
Run 1
Left AVG=1072910535, Right AVG=1051371210
Left AVG=1072910538, Right AVG=1051371213
Left AVG=1072910532, Right AVG=1051371207
Left AVG=1072910539, Right AVG=1051371215

Run 2
Left AVG=1084774514, Right AVG=1046304599
Left AVG=1084774511, Right AVG=1046304599
Left AVG=1084774512, Right AVG=1046304598
Left AVG=1084774513, Right AVG=1046304598
Left AVG=1084774511, Right AVG=1046304597

Run 3
Left AVG=1096628631, Right AVG=1049588635
Left AVG=1096628631, Right AVG=1049588634
Left AVG=1096628631, Right AVG=1049588633
Left AVG=1096628636, Right AVG=1049588641
Left AVG=1096628629, Right AVG=1049588632

Run 4
Left AVG=1079489643, Right AVG=1028063152
Left AVG=1079489643, Right AVG=1028063154
Left AVG=1079489643, Right AVG=1028063151
Left AVG=1079489639, Right AVG=1028063150
Left AVG=1079489642, Right AVG=1028063153

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

Re: I2S Receive bug causing garbage data?

Postby mgleason_3 » Wed Mar 22, 2017 11:32 pm

Forgot to mention the data above is 32-bits - using following definitions:

Code: Select all

#define SAMPLE_RATE     (48000) 
#define I2S_NUM         (0)
#define BUFFER_SAMPLES  (2048)
#define BITS_PER_SAMPLE (32) 

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

Re: I2S Receive bug causing garbage data?

Postby mgleason_3 » Tue Sep 19, 2017 1:19 am

Hmmm, never received a reply so of 'course wondered if maybe it was a mistake/I'm crazy/don't understand - all of which is probably true :D. Went off on a few other issues, projects and wow, in a couple more months it'll have been a year!

But coming back to it now, even after pulling the latest IDF:
  • The data still doesn't look correct - was it tested? Did the data received match what was transmitted?
  • If there's only one microphone connected and that microphone only transmits on one channel (L or R), why is there data on both channels?
So, decided to try some different things to see if the problem could be clearer:
  1. Try testing on a raspberry pi
  2. Try printing the data in binary - to see if there's something obvious in the bit-pattern
  3. Try another microphone
The original testing was done on an ICS-43432 that I hand-soldered to a piece of perf board. The ICS-43432 is a digital mems microphone that outputs it's data via I2S. In the mean time a different digital mems I2S microphone arrived - this time a fully populated breakout from ADAFRUIT which uses the SPH0645LM4H.

The SPH0645 appears to output quite different data from the ICS-43432 (bias?), which may actually help in troubleshooting.

Code: Select all

SPH0645

                   LEFT                                        RIGHT
 Decimal Value:Binary Value                     | Decimal Value:Binary Value
-------------- --------------------------------   ------------- --------------------------------
Raspberry - 32-bit quiet
             0:00000000000000000000000000000000 |    -112721920:11111001010010000000000000000000 
             0:00000000000000000000000000000000 |    -112689152:11111001010010001000000000000000 
             0:00000000000000000000000000000000 |    -112885760:11111001010001011000000000000000 

ESP32 - 32-bit quiet
             1:00000000000000000000000000000001 |    -223051776:11110010101101001000000000000000 
             1:00000000000000000000000000000001 |    -222855168:11110010101101111000000000000000 
             1:00000000000000000000000000000001 |    -222756864:11110010101110010000000000000000 
             
Above is the data captured from the SPH0645 on both a Raspberry and the ESP32. Take a look at the left-most bits in the Binary Value of the RIGHT channel. Note there are 5-ones in a row on the Raspberry and only 4 on the ESP32.

Now keeping in mind that the left channel should have no data because the microphone is transmitting on the right channel, look at the right-most bits in the Binary Value of the LEFT channel. On the Raspberry, it's a 0, but on the ESP32 it a 1. It's as though a bit has been moved to the wrong channel.

BuddyCasino
Posts: 263
Joined: Sun Jun 19, 2016 12:00 am

Re: I2S Receive bug causing garbage data?

Postby BuddyCasino » Tue Sep 19, 2017 8:57 am

I have both the ICS-43434 and the SPH0645, and also printed the values in binary. Can't remember to have observed this phenomenon. Can you attach your code so I can re-check?

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

Re: I2S Receive bug causing garbage data?

Postby mgleason_3 » Tue Sep 19, 2017 6:12 pm

@ BuddyCasino
>> Can you attach your code so I can re-check?

Here you go!

NOTE: I've used different pins than the example - if you want to use a different configuration, be sure to edit the "pin_config = {..."

Code: Select all

#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/i2s.h"

#define BITS_PER_SAMPLE     (32)        // 8, 16, 24, 32
#define SAMPLE_RATE         (48000)
#define I2S_NUM             (0)
#define FRAMES_REQUESTED    (32)

#if BITS_PER_SAMPLE==32
  typedef int BUFFER_TYPE;
#elif BITS_PER_SAMPLE==24
  typedef int BUFFER_TYPE;
#elif BITS_PER_SAMPLE==16
  typedef short BUFFER_TYPE;
#elif BITS_PER_SAMPLE==8
  typedef byte BUFFER_TYPE;
#endif


static void init_i2s()
{
    i2s_config_t i2s_config = {
        .mode = I2S_MODE_MASTER | I2S_MODE_RX,                                  // Receive
        .sample_rate = SAMPLE_RATE,
        .bits_per_sample = BITS_PER_SAMPLE,
        .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
        .communication_format = I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB,  
        .dma_buf_count = 32,
        .dma_buf_len = 32*2,
        .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1
    };
   
    i2s_pin_config_t pin_config = {
        .ws_io_num = 23,
        .bck_io_num = 22,
        .data_in_num = 21,
      .data_out_num = I2S_PIN_NO_CHANGE
  };

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


void printBits(size_t const size, void const * const ptr)
{
    unsigned char *b = (unsigned char*) ptr;
    unsigned char byte;
    int i, j;

    for (i=size-1;i>=0;i--)
    {
        for (j=7;j>=0;j--)
        {
            byte = (b[i] >> j) & 1;
            printf("%u", byte);
        }
    }
    printf(" ");
}

void print_buffer_as_binary(BUFFER_TYPE *p, int len)
{
    int i;
    BUFFER_TYPE frame = 0;

    for (i = 0; i < len; i++) {
        if (i%2 == 0) printf("\n");
        frame = p[i];
        printf("%14d:", frame);
        printBits(sizeof(frame), &frame);
        if (i%2 == 0) printf("|");
    }
    printf("\n");
}

void print_bytes(void *p, size_t len)
{
    size_t i;
    printf("(");
    for (i = 0; i < len; ++i) {
        printf("%d", ((char*)p)[i]);
        if (i + 1 < len)
          printf(" ");
    }
    printf(")");
}

void app_main()
{
    unsigned int i;
    int frames_read = 0, buffer_size_in_bytes = 0;

    TickType_t ticks_to_wait = 1000;

    buffer_size_in_bytes = FRAMES_REQUESTED*sizeof(BUFFER_TYPE);
    char *buffer = (char*)malloc(buffer_size_in_bytes);

    // nvs_flash_init();
    init_i2s();
  
    // Skip initial frames
    for(i = 0; i < 6; i++) {
        frames_read = i2s_read_bytes(I2S_NUM, buffer, FRAMES_REQUESTED, 0) / sizeof(BUFFER_TYPE);
    }

    // Main loop
    for(i = 0; i < 100; i++) {
        frames_read = i2s_read_bytes(I2S_NUM, buffer, FRAMES_REQUESTED, ticks_to_wait) / sizeof(BUFFER_TYPE);

        print_buffer_as_binary(buffer, frames_read);   
    }
    printf("\n\n");
    printf("Done! \n");
}
Last edited by mgleason_3 on Tue Sep 19, 2017 7:00 pm, edited 1 time in total.

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

Re: I2S Receive bug causing garbage data?

Postby mgleason_3 » Tue Sep 19, 2017 6:58 pm

Running a test with the ICS-43432 gives interesting results with BITS_PER_SAMPLE (16) -

NOTE: the data on the left channel on the esp32 (which I presume should be all 0's).

Code: Select all

Raspberry - 16-bit quiet
             0:0000000000000000 |            -3:1111111111111101 
             0:0000000000000000 |            -2:1111111111111110 
             0:0000000000000000 |             4:0000000000000100 

ESP32 - 16-bit quiet
            16:0000000000010000 |             0:0000000000000000 
            15:0000000000001111 |             0:0000000000000000 
            15:0000000000001111 |            -2:1111111111111110 
            

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

Re: I2S Receive bug causing garbage data?

Postby mgleason_3 » Thu Sep 21, 2017 6:25 am

Here's some data with BITS_PER_SAMPLE set to 24-bit (the buffer is 32-bit). Again, note the left channel which should be silent.

Raspberry - 24-bit quiet
0:00000000000000000000000000000000 | 16776961:00000000111111111111111100000001
0:00000000000000000000000000000000 | 123:00000000000000000000000001111011
0:00000000000000000000000000000000 | 707:00000000000000000000001011000011


ESP32 - 24-bit quiet
-512:11111111111111111111111000000000 | 655104:00000000000010011111111100000000
-512:11111111111111111111111000000000 | 641024:00000000000010011100100000000000
0:00000000000000000000000000000000 | 579584:00000000000010001101100000000000

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

Re: I2S Receive bug causing garbage data?

Postby mgleason_3 » Fri Sep 22, 2017 7:11 am

Also tried capturing data while playing a louder sound. Again, note the left channel should be silent, but instead we have times where it's all 1's.

As though the louder sound is "Pushing" more bits to the left channel...

Code: Select all

Raspberry - 32-bit Loud
             0:00000000000000000000000000000000 |    -966652416:11000110011000100000111000000000 
             0:00000000000000000000000000000000 |    -223758464:11110010101010011011011110000000 
             0:00000000000000000000000000000000 |     454887424:00011011000111010000100000000000 

ESP32 - 32-bit Loud
             0:00000000000000000000000000000000 |     190961663:00001011011000011101011111111111 
            -1:11111111111111111111111111111111 |    -920545793:11001001001000011001010111111111 
            -1:11111111111111111111111111111111 |     381301248:00010110101110100011001000000000 
            

mgleason_3
Posts: 44
Joined: Mon Nov 07, 2016 5:04 pm

Re: I2S Receive bug causing garbage data?

Postby mgleason_3 » Tue Sep 26, 2017 9:45 pm

@ BuddyCasino
Hey - just wondering if you had a chance to test it?

BuddyCasino
Posts: 263
Joined: Sun Jun 19, 2016 12:00 am

Re: I2S Receive bug causing garbage data?

Postby BuddyCasino » Fri Sep 29, 2017 6:17 pm

Sorry, was quite busy. Just did a run quickly, not sure it makes sense. I took your code and only changed the pins.


SPH0645:

Code: Select all

    -182616001:11110101000111011000000000111111 |            -1:11111111111111111111111111111111
    -182026177:11110101001001101000000000111111 |            -1:11111111111111111111111111111111
    -181436353:11110101001011111000000000111111 |            -1:11111111111111111111111111111111
    -180944833:11110101001101110000000000111111 |            -1:11111111111111111111111111111111
ICS43434:

Code: Select all

        239231:00000000000000111010011001111111 |            -1:11111111111111111111111111111111
        671871:00000000000010100100000001111111 |            -1:11111111111111111111111111111111
       1137279:00000000000100010101101001111111 |            -1:11111111111111111111111111111111
       1008255:00000000000011110110001001111111 |            -1:11111111111111111111111111111111

Who is online

Users browsing this forum: No registered users and 7 guests