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?