Code: Select all
#include <driver/i2s.h>
#define SIGNAL_LENGTH 100
#define KERNEL_LENGTH 10
//INMP441
#define I2S_WS 19
#define I2S_SD 21
#define I2S_SCK 18
// Uso del procesador 0 I2S
#define I2S_INPUT_PORT I2S_NUM_0
//WCMCU
#define I2S_BCK 27
#define I2S_LCK 26
#define I2S_DIN 25
#define I2S_OUT_NC -1
#define I2S_OUTPUT_PORT I2S_NUM_1
//Constantes compartidas de definición de protocolo I2S
#define I2S_SAMPLE_RATE 44100
#define I2S_BUFFER_COUNT 8
#define BITS_PER_SAMPLE 16
#define I2S_BUFFER_LEN 128
int samplesPerBuffer = (I2S_BUFFER_LEN / BITS_PER_SAMPLE);
int16_t inputSoundBuffer[I2S_BUFFER_LEN];
int16_t outputSoundBuffer[I2S_BUFFER_LEN];
void i2s_install() {
// Configuración del I2S
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX | I2S_MODE_TX ),
.sample_rate = I2S_SAMPLE_RATE,
.bits_per_sample = i2s_bits_per_sample_t(16),
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
// .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB),
//.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_PCM),
.communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = I2S_BUFFER_COUNT,
.dma_buf_len = I2S_BUFFER_LEN,
.use_apll = false,
.tx_desc_auto_clear = true,
.fixed_mclk = 0
};
const i2s_pin_config_t pin_config_o = {
.bck_io_num = I2S_BCK,
.ws_io_num = I2S_LCK,
.data_out_num = I2S_DIN,
.data_in_num = -1
};
const i2s_pin_config_t pin_config_i = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = -1,
.data_in_num = I2S_SD
};
i2s_driver_install(I2S_INPUT_PORT, &i2s_config, 0, NULL);
i2s_set_pin(I2S_INPUT_PORT, &pin_config_i);
i2s_driver_install(I2S_OUTPUT_PORT, &i2s_config, 0, NULL);
i2s_set_pin (I2S_OUTPUT_PORT, &pin_config_o);
}
void setup() {
// Configuración del monitor serial
Serial.begin(115200);
Serial.println(" ");
i2s_install();
delay(1000);
i2s_start(I2S_INPUT_PORT);
i2s_start(I2S_OUTPUT_PORT);
}
void loop() {
size_t bytesIn = 0;
esp_err_t result = i2s_read(I2S_INPUT_PORT, &inputSoundBuffer, I2S_BUFFER_LEN, &bytesIn, portMAX_DELAY);
if (result == ESP_OK)
{
if (bytesIn > 0) {
for (int16_t i = 0; i < bytesIn ; i++){ //samples; ++i) {
outputSoundBuffer[i] = (inputSoundBuffer[i]);
}
size_t transBytes = bytesIn;
i2s_write(I2S_OUTPUT_PORT, (char *) outputSoundBuffer, I2S_BUFFER_LEN, &transBytes, portMAX_DELAY);
}
}
}