Hello Everyone,
I am working with INMP441 for recording the audio and speaker with max98357A to play the audio samples. I don't want to use the sd card. Here is the code:
#include <driver/i2s.h>
#define I2S_PORT_MIC I2S_NUM_0 // Microphone I2S port
#define I2S_PORT_SPEAKER I2S_NUM_1 // Speaker I2S port
// Pin definitions for the MAX98357A (speaker output)
#define I2S_SPEAKER_BCLK 26
#define I2S_SPEAKER_LRCLK 25
#define I2S_SPEAKER_DOUT 22
// Connections for the INMP441 microphone
#define I2S_MIC_BCLK 33
#define I2S_MIC_WS 19
#define I2S_MIC_DOUT 32
// Define input buffer length
#define bufferLen 64
int16_t sBuffer[bufferLen];
void i2s_install_microphone() {
// I2S configuration for the microphone
const i2s_config_t i2s_config_mic = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 44100, // Sampling rate for mic
.bits_per_sample = i2s_bits_per_sample_t(16), // 16-bit per sample
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // Mono input
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = bufferLen,
.use_apll = false
};
i2s_driver_install(I2S_PORT_MIC, &i2s_config_mic, 0, NULL);
// Pin configuration for the INMP441 microphone
const i2s_pin_config_t pin_config_mic = {
.bck_io_num = I2S_MIC_BCLK,
.ws_io_num = I2S_MIC_WS,
.data_out_num = -1, // No data output for the mic
.data_in_num = I2S_MIC_DOUT
};
i2s_set_pin(I2S_PORT_MIC, &pin_config_mic);
}
void i2s_install_speaker() {
// I2S configuration for the speaker (MAX98357A)
const i2s_config_t i2s_config_speaker = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_TX), // Transmit mode
.sample_rate = 44100, // Same sample rate
.bits_per_sample = i2s_bits_per_sample_t(16), // 16-bit per sample
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT, // Mono output
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = 0,
.dma_buf_count = 8,
.dma_buf_len = bufferLen,
.use_apll = false
};
i2s_driver_install(I2S_PORT_SPEAKER, &i2s_config_speaker, 0, NULL);
// Pin configuration for the MAX98357A speaker output
const i2s_pin_config_t pin_config_speaker = {
.bck_io_num = I2S_SPEAKER_BCLK,
.ws_io_num = I2S_SPEAKER_LRCLK,
.data_out_num = I2S_SPEAKER_DOUT,
.data_in_num = -1 // No data input for the speaker
};
i2s_set_pin(I2S_PORT_SPEAKER, &pin_config_speaker);
}
void setup() {
Serial.begin(115200);
// Install and configure I2S for microphone
i2s_install_microphone();
i2s_start(I2S_PORT_MIC);
// Install and configure I2S for speaker (MAX98357A)
i2s_install_speaker();
i2s_start(I2S_PORT_SPEAKER);
delay(500);
}
void loop() {
size_t bytesIn = 0;
// Capture audio data from the microphone
esp_err_t result = i2s_read(I2S_PORT_MIC, &sBuffer, bufferLen * sizeof(int16_t), &bytesIn, portMAX_DELAY);
if (result == ESP_OK && bytesIn > 0) {
// Play the captured audio using the MAX98357A
size_t bytesWritten = 0;
i2s_write(I2S_PORT_SPEAKER, &sBuffer, bytesIn, &bytesWritten, portMAX_DELAY);
// Optionally print the data to serial (for debugging purposes)
for (int i = 0; i < bytesIn / sizeof(int16_t); ++i) {
Serial.print("Sample ");
Serial.print(i);
Serial.print(": ");
Serial.println(sBuffer[i]);
}
}
}
Result of the code:
Noisy output