little_sophia_brain/i2s_streaming_test/stream_i2s.ino

52 lines
1.5 KiB
C++

#include <driver/i2s.h>
#define I2S_WS 6 // LRCL (Word Select)
#define I2S_SCK 7 // BCLK (Serial Clock)
#define I2S_SD 8 // DOUT (Serial Data from mic)
#define I2S_PORT I2S_NUM_0
#define BUFFER_SIZE 2048 // Larger buffer = fewer calls
void setup() {
Serial.begin(1000000); // High-speed serial for streaming audio
// I2S configuration
const i2s_config_t i2s_config = {
.mode = i2s_mode_t(I2S_MODE_MASTER | I2S_MODE_RX),
.sample_rate = 8000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
.communication_format = I2S_COMM_FORMAT_I2S,
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 4,
.dma_buf_len = BUFFER_SIZE / 4,
.use_apll = false,
.tx_desc_auto_clear = false,
.fixed_mclk = 0
};
const i2s_pin_config_t pin_config = {
.bck_io_num = I2S_SCK,
.ws_io_num = I2S_WS,
.data_out_num = I2S_PIN_NO_CHANGE,
.data_in_num = I2S_SD
};
i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
i2s_set_pin(I2S_PORT, &pin_config);
}
void loop() {
uint8_t buffer[BUFFER_SIZE];
size_t bytes_read;
if (i2s_read(I2S_PORT, &buffer, BUFFER_SIZE, &bytes_read, portMAX_DELAY) == ESP_OK && bytes_read > 0) {
for (int i = 0; i < bytes_read; i += 4) {
int32_t sample_32 = *(int32_t*)&buffer[i];
int16_t sample_16 = sample_32 >> 12; // Less aggressive shift
Serial.write((uint8_t*)&sample_16, 2);
}
}
}