Reading from INMP441 with ESP32-WROOM-32results in loud noise
Posted: Mon Aug 16, 2021 10:48 am
Hello,
I am currently desperatly trying to get a NMP441 microphone properly working with a ESP32.
The code I am using seems to work but when I send the data off to my server hosted on a rpi the audio sounds broken and is just a very loud noise. My guess is that I am parsing the data from the microphone wrong. To setup the code and trying to understand what using the I2S interface means I used this doku: https://docs.espressif.com/projects/esp ... s/i2s.htmland this github repo: https://github.com/atomic14/esp32_audio
This is the code for setting up the mic:
This the code for reading from the i2s peripheral:
while recording the output looks like this:
```
ec ff ef ff e7 ff ec ff
e3 ff e7 ff df ff e3 ff
db ff df ff d7 ff db ff
d4 ff d7 ff d0 ff d4 ff
cb ff d0 ff c8 ff cb ff
c4 ff c8 ff bf ff c4 ff
```
the server code receiving the buffer looks like this:
and outputs this:
```
Got 180224 I2S bytes
<Buffer 02 00 02 00 02 00 02 00 02 00 02 00 02 00 02 00 03 00 03 00 04 00 04 00 03 00 03 00 03 00 03 00 04 00 04 00 05 00 05 00 05 00 05 00 05 00 05 00 04 00 ... 180174 more bytes>
```
But importing and reading this audio.raw into audacity results in just loud noise which shows that something is broken.
Does anybody have an idea what could be wrong?
Regards,
Robert
I am currently desperatly trying to get a NMP441 microphone properly working with a ESP32.
The code I am using seems to work but when I send the data off to my server hosted on a rpi the audio sounds broken and is just a very loud noise. My guess is that I am parsing the data from the microphone wrong. To setup the code and trying to understand what using the I2S interface means I used this doku: https://docs.espressif.com/projects/esp ... s/i2s.htmland this github repo: https://github.com/atomic14/esp32_audio
This is the code for setting up the mic:
Code: Select all
#define I2S_PORT (i2s_port_t)(0) // set main I2S port to I2S_NUM_0
// i2s config
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX), // receive
.sample_rate = 44100,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT, // also tried 32 bit before
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // use right channel
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // interrupt level 1
.dma_buf_count = 64, // number of buffers
.dma_buf_len = 512
};
// pin config
const i2s_pin_config_t pin_config = {
.bck_io_num = 33, // serial clock, sck
.ws_io_num = 32, // word select, ws
.data_out_num = I2S_PIN_NO_CHANGE, // only used for speakers
.data_in_num = 34 // serial data, sd
};
Serial.println("-----Mic Controller-----");
// config i2s driver and pins
// fct must be called before any read/write
esp_err_t err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
if (err != ESP_OK)
{
Serial.printf("Failed installing the driver: %d\n", err);
}
err = i2s_set_pin(I2S_PORT, &pin_config);
if (err != ESP_OK)
{
Serial.printf("Failed setting pin: %d\n", err);
}
Serial.println("I2S driver installed! :-)");
Serial.println("------------------------");
Code: Select all
// 44,1KHz (SAMPLE_RATE) * Byte per sample * time in seconds = total size in bytes
const size_t recordSize = (44100 * I2S_BITS_PER_SAMPLE_16BIT/ 8) * recordTime; //recordTime = 2s
// size in bytes
size_t totalReadSize = 0;
// 16 bits per sample set in config * 1024 samples per buffers = total bits per buffer
const uint16_t totalBitsPerBuffer = 44100 * I2S_BITS_PER_SAMPLE_16BIT;
char *samples = (char *)calloc(totalBitsPerBuffer, sizeof(char));
// number of bytes read
size_t bytesRead;
Serial.println("Start recording...");
// read until wanted size is reached
while (totalReadSize < recordSize)
{
// read to buffer
esp_err_t err = i2s_read(I2S_PORT, (void *)samples, totalBitsPerBuffer, &bytesRead, portMAX_DELAY);
// check if error occurd, if so stop recording
if (err != ESP_OK)
{
Serial.println("Error while recording!");
break;
}
printf("=======\n");
for (int i = 0; i < bytesRead; i++)
{
printf("%02x ", (uint8_t)samples[i]);
if ((i + 1) % 8 == 0)
{
printf("\n");
}
}
printf("=======\n");
// add read size to total read size
totalReadSize += bytesRead;
}
HttpController httpController("http://192.168.2.100:8080");
httpController.postRequest("/esp/audio", "application/octet-stream", "", (uint8_t *)samples, totalReadSize);
```
ec ff ef ff e7 ff ec ff
e3 ff e7 ff df ff e3 ff
db ff df ff d7 ff db ff
d4 ff d7 ff d0 ff d4 ff
cb ff d0 ff c8 ff cb ff
c4 ff c8 ff bf ff c4 ff
```
the server code receiving the buffer looks like this:
Code: Select all
app.post('/esp/audio', (req, res) => {
console.log(`Got ${req.body.length} I2S bytes`);
fs.writeFile('audio.raw', req.body, () => {
res.sendStatus(200);
});
});
```
Got 180224 I2S bytes
<Buffer 02 00 02 00 02 00 02 00 02 00 02 00 02 00 02 00 03 00 03 00 04 00 04 00 03 00 03 00 03 00 03 00 04 00 04 00 05 00 05 00 05 00 05 00 05 00 05 00 04 00 ... 180174 more bytes>
```
But importing and reading this audio.raw into audacity results in just loud noise which shows that something is broken.
Does anybody have an idea what could be wrong?
Regards,
Robert