My project is to make a voice amplifier with a mic INMP441 and a speaker on a esp32 card.
So I use the i2s protocol to collect the data from the mic and I convert the data to a 8-bits integer to send it to the DAC and then send it to the speaker (with a PAM8403 between).
The problem is that I can't transmit the sound from my microphone correctly to the speaker, there is only an unpleasant noise coming out of it.
If you have any suggestion, it would really help me.
I join my code there :
- #include <driver/i2s.h>
- #include<cmath>
- #include <driver/dac.h>
- #define I2S_WS 15
- #define I2S_SD 32
- #define I2S_SCK 14
- #define I2S_PORT I2S_NUM_0
- void setup() {
- Serial.begin(115200);
- Serial.println("Setup I2S ...");
- delay(1000);
- i2s_install();
- i2s_setpin();
- i2s_start(I2S_PORT);
- delay(500);
- }
- void loop() {
- int16_t sample = 0;
- int bytes = i2s_pop_sample(I2S_PORT, (char*)&sample, portMAX_DELAY);
- if(bytes > 0){
- int partieEntiere =(int)floor((sample+3276-1)/256); // convert to a 8-bit integer
- Serial.println(partieEntiere);
- dacWrite(25,partieEntiere); //send to the DAC
- }
- }
- // config the i2s protocol
- void i2s_install(){
- const i2s_config_t i2s_config = {
- .mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
- .sample_rate = 44100,
- .bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
- .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
- .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
- .intr_alloc_flags = 0, // default interrupt priority
- .dma_buf_count = 8,
- .dma_buf_len = 64,
- .use_apll = false
- };
- i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
- }
- void i2s_setpin(){
- const i2s_pin_config_t pin_config = {
- .bck_io_num = I2S_SCK,
- .ws_io_num = I2S_WS,
- .data_out_num = -1,
- .data_in_num = I2S_SD
- };
- i2s_set_pin(I2S_PORT, &pin_config);
- }