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-&gt;setBCLK(16);
I2SOutputDevice-&gt;setDATA(18);
I2SOutputDevice-&gt;setBitsPerSample(I2S_BITS);
I2SOutputDevice-&gt;setFrequency(I2S_SAMPLE_RATE);
I2SOutputDevice-&gt;setSysClk(I2S_SAMPLE_RATE);
I2SOutputDevice-&gt;setBuffers(2, BUFFER_LENGTH);

pinMode(19,OUTPUT);
digitalWrite(19,HIGH);


I2SInputDevice = new I2S(INPUT);
I2SInputDevice-&gt;setBCLK(20);
I2SInputDevice-&gt;setDATA(22);
I2SInputDevice-&gt;setBitsPerSample(I2S_BITS);
I2SInputDevice-&gt;setFrequency(I2S_SAMPLE_RATE);
I2SInputDevice-&gt;setSysClk(I2S_SAMPLE_RATE);
I2SInputDevice-&gt;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-&gt;begin();
I2SInputDevice-&gt;begin();

}

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

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



// works

//int32_t L, R;
//I2SInputDevice-&gt;read24(&amp;L, &amp;R);
//I2SOutputDevice-&gt;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?

Allahjane
  • 101

0 Answers0