trial voice rec — Xiao Esp32C6 simulation

An interactive Xiao Esp32C6 circuit simulation you can run free in your browser on Velxio, by danielolasunkami9.

Components

Sketch code

// reSpeaker Lite + XIAO ESP32-C6 — far-field microphone level meter.
//
// The reSpeaker Lite is an XMOS XU316 audio front-end: two mics, AEC and
// noise suppression, streaming 16 kHz audio over I2S to the XIAO seated on
// its socket. Wiring here follows the socket (silk -> XIAO ESP32-C6 GPIO):
//
//   BCLK  D9  -> GPIO20     DOUT (mic data)  D7 -> GPIO17
//   WS    D8  -> GPIO19     DIN  (playback)  D6 -> GPIO16
//   SDA   D4  -> GPIO22     SCL              D5 -> GPIO23
//
// The XU316 also answers on I2C address 0x42 (Command Transport Protocol):
// this sketch polls the mic-mute status (0xF1/0x81) and the VNR voice
// activity value (0xF1/0x80), so the MUTE button on the board shows up both
// as silence on the meter and as [MUTED] from the control bus.
#include <ESP_I2S.h>
#include <Wire.h>
#include <math.h>
#include <WiFi.h>
const int SAMPLE_COUNT = 16000;
const size_t AUDIO_BUFFER_SIZE = 64000;
uint8_t audioBuffer[AUDIO_BUFFER_SIZE];

I2SClass I2S;

uint8_t xmosRead1(uint8_t resid, uint8_t cmd) {
  Wire.beginTransmission(0x42);
  Wire.write(resid);
  Wire.write(cmd);
  Wire.write(1);
  Wire.endTransmission();
  Wire.requestFrom(0x42, 2);  // status byte + payload
  Wire.read();                // status, 0 = ok
  return Wire.available() ? Wire.read() : 0;
}
void connectWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin("Velxio-GUEST");

  Serial.print("Connecting to WiFi");

  unsigned long startTime = millis();

  while (WiFi.status() != WL_CONNECTED &&
         millis() - startTime < 15000) {
    delay(250);
    Serial.print(".");
  }

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nWiFi connected!");
    Serial.print("IP address: ");
    Serial.println(WiFi.localIP());
  } else {
    Serial.println("\nWiFi connection failed.");
  }
}
void setup() {
  Serial.begin(115200);
  connectWiFi();
  Wire.begin();                 // XIAO D4/D5 — the reSpeaker control bus
  I2S.setPins(20, 19, 16, 17);  // BCLK, WS, DOUT (to board), DIN (mic data)
  if (!I2S.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) {
    Serial.println("I2S begin failed");
    while (1) delay(1000);
  }
  Serial.println("ReSpeaker Lite listening at 16 kHz...");
}

void loop() {
  size_t totalRead = 0;

  while (totalRead < AUDIO_BUFFER_SIZE) {
    size_t remaining = AUDIO_BUFFER_SIZE - totalRead;

    size_t availableBytes = I2S.available();

    if (availableBytes < 4) {
      delay(1);
      continue;
    }

    size_t bytesToRead = availableBytes;

    if (bytesToRead > remaining) {
      bytesToRead = remaining;
    }

    // Keep reads aligned to complete stereo frames.
    bytesToRead -= bytesToRead % 4;

    if (bytesToRead == 0) {
      continue;
    }

    size_t bytesRead = I2S.readBytes(
      (char*)audioBuffer + totalRead,
      bytesToRead
    );

    if (bytesRead > 0) {
  totalRead += bytesRead;
}
  }
  int16_t* samples = (int16_t*)audioBuffer;
int16_t minSample = 32767;
int16_t maxSample = -32768;

for (int i = 0; i < AUD

More projects by danielolasunkami9