0

I'm using a raspberry pi pico W and testing audio loopback (microphone to speaker in a loop) using INMP441 microphone and MAX98357 amp board.

Followed functions from this page https://arduino-pico.readthedocs.io/en/latest/i2s.html

I arrived at this code:

#include <I2S.h>

I2S* I2SOutputDevice = nullptr;
I2S* I2SInputDevice = nullptr;
#define I2S_SAMPLE_RATE 44100
#define I2S_BITS 24
#define BUFFER_LENGTH 256
#define I2S_BUFFER_SIZE 1024


uint8_t* data;


// the setup function runs once when you press reset or power the board
void setup() {

    I2SOutputDevice = new I2S(OUTPUT);
    I2SOutputDevice->setBCLK(16);
    I2SOutputDevice->setDATA(18);
    I2SOutputDevice->setBitsPerSample(I2S_BITS);
    I2SOutputDevice->setFrequency(I2S_SAMPLE_RATE);
    I2SOutputDevice->setSysClk(I2S_SAMPLE_RATE);
    I2SOutputDevice->setBuffers(2, BUFFER_LENGTH);
    
    pinMode(19,OUTPUT);
    digitalWrite(19,HIGH);


    I2SInputDevice = new I2S(INPUT);
    I2SInputDevice->setBCLK(20);
    I2SInputDevice->setDATA(22);
    I2SInputDevice->setBitsPerSample(I2S_BITS);
    I2SInputDevice->setFrequency(I2S_SAMPLE_RATE);
    I2SInputDevice->setSysClk(I2S_SAMPLE_RATE);
    I2SInputDevice->setBuffers(2, BUFFER_LENGTH);

    pinMode(LED_BUILTIN,OUTPUT);

    data = new uint8_t[I2S_BUFFER_SIZE];
    digitalWrite(LED_BUILTIN, HIGH);
    delay(1000);
    digitalWrite(LED_BUILTIN, LOW);
    delay(1000);

    I2SOutputDevice->begin();
    I2SInputDevice->begin();
}


// the loop function runs over and over again until power down or reset
void loop() {


    // does not work
    size_t actuallyRead = I2SInputDevice->readBytes(data, I2S_BUFFER_SIZE);
    I2SOutputDevice->write(data, actuallyRead);
    


    // works
    
    //int32_t L, R;
    //I2SInputDevice->read24(&L, &R);
    //I2SOutputDevice->write24(L, R);

}

As indicated in the code reading one sample at a time and writing works fine I can hear on speaker what I speak into the microphone.

however if I go the buffer read and write route all I get is noise form the speaker that does not react or correspond to any microphone input .

If I add a serial print to see how many bytes were read then it is a positive number the same as size of the buffer, so it is reading data but playback is missing in this case

what am I doing wrong?

Bonus question: INMP441 seem to have very low sensitivity , you have to speak very close to it which is nearly impractical , is this normal?

2
  • Please extend your test code to print the number of read bytes in each loop. Are you aware that both I²C transfers and serial transmissions need time, and therefore the playback can have another sampling rate? Commented Sep 14, 2023 at 5:43
  • I did print the read bytes and its always the full size of the buffer Commented Sep 14, 2023 at 15:58

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.