Re: I2S microphone (RX)
Posted: Wed Sep 06, 2017 7:36 pm
Bumping for inactivity
nic_ru wrote:Hi all!
I'm trying to store in a buffer array (with no luck) the voice captured from my I2S mic (https://www.adafruit.com/product/3421) in order to execute a speech to text recognition on my server. I wrote some code (after reading your comments) but I'm probably missing something because the audio file stored on the cloud is basically "empty".
This is my code (I'm a super beginner in this world):Any ideas?Code: Select all
#include "driver/i2s.h" #define SAMPLES 128 // make it a power of two for best DMA performance #define SPEECH_BUFFER_SIZE 40960 uint32_t speech_buffer[SPEECH_BUFFER_SIZE]; int last_position_written = 0; void i2s_config() { // input i2s_config_t i2s_in_config = { mode: (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX), sample_rate: 16000, bits_per_sample: (i2s_bits_per_sample_t)32, channel_format: I2S_CHANNEL_FMT_RIGHT_LEFT, communication_format: (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB), intr_alloc_flags: ESP_INTR_FLAG_LEVEL1, dma_buf_count: 14, dma_buf_len: 64 }; i2s_pin_config_t i2s_in_pin_config = { bck_io_num: 16, ws_io_num: 4, data_out_num: -1, //Not used data_in_num: 17 }; pinMode(17, INPUT); pinMode(16, OUTPUT); pinMode(4, OUTPUT); i2s_driver_install((i2s_port_t)0, &i2s_in_config, 0, NULL); i2s_set_pin((i2s_port_t)0, &i2s_in_pin_config); } void setup() { Serial.begin(115200); delay(4000); i2s_config(); } void loop() { //get sample from mic for (int i = 0; i < SAMPLES; i++) { int sample = 0; uint32_t sample_val[2] = {0, 0}; bytes_read += i2s_pop_sample((i2s_port_t)0, (char *)sample_val, portMAX_DELAY); samples_for_buffer[i] = (sample_val[0] << 5); } merge_buffer(samples_for_buffer); } void merge_buffer(uint32_t samples_for_buffer[]) { //if the buffer reach the max size send it to the cloud .... //else merge buffer for (int j = 0; j < SAMPLES; j++) { speech_buffer[last_position_written] = samples_for_buffer[j]; last_position_written++; } }
Thank you!