×
#include <driver/i2s.h>
#include <math.h>

// =====================================================
// INMP441 I2S receive test
// ESP32 DEVKIT V1 / Fs = 16kHz / 1024 samples
// =====================================================

// ---------- I2S pin assignment ----------
static const int I2S_BCLK_PIN = 14;   // INMP441 SCK / BCLK
static const int I2S_WS_PIN   = 15;   // INMP441 WS / LRCLK
static const int I2S_SD_PIN   = 32;   // INMP441 SD

// ---------- audio settings ----------
static const int SAMPLE_RATE = 16000;
static const int BLOCK_SIZE  = 1024;

// I2S port
static const i2s_port_t I2S_PORT = I2S_NUM_0;

// raw receive buffer
int32_t samples[BLOCK_SIZE];

// =====================================================
// I2S init
// =====================================================
void initI2SMic()
{
  // I2S configuration
  const i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
    .sample_rate = SAMPLE_RATE,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_32BIT,
    .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,   // L/R pin = GND を想定
    .communication_format = I2S_COMM_FORMAT_STAND_I2S,
    .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
    .dma_buf_count = 8,
    .dma_buf_len = 256,
    .use_apll = false,
    .tx_desc_auto_clear = false,
    .fixed_mclk = 0
  };

  // I2S pin configuration
  const i2s_pin_config_t pin_config = {
    .bck_io_num = I2S_BCLK_PIN,
    .ws_io_num = I2S_WS_PIN,
    .data_out_num = I2S_PIN_NO_CHANGE,
    .data_in_num = I2S_SD_PIN
  };

  // uninstall once just in case
  i2s_driver_uninstall(I2S_PORT);

  // install
  esp_err_t err;
  err = i2s_driver_install(I2S_PORT, &i2s_config, 0, NULL);
  if (err != ESP_OK) {
    Serial.printf("ERROR: i2s_driver_install failed (%d)\n", err);
    while (1) delay(1000);
  }

  err = i2s_set_pin(I2S_PORT, &pin_config);
  if (err != ESP_OK) {
    Serial.printf("ERROR: i2s_set_pin failed (%d)\n", err);
    while (1) delay(1000);
  }

  // clear DMA
  i2s_zero_dma_buffer(I2S_PORT);

  Serial.println("I2S microphone initialized.");
}

// =====================================================
// Read one block from INMP441
// return true if BLOCK_SIZE samples were read
// =====================================================
bool readMicBlock(int32_t* dst, size_t count)
{
  size_t bytesRead = 0;
  esp_err_t err = i2s_read(
    I2S_PORT,
    (void*)dst,
    count * sizeof(int32_t),
    &bytesRead,
    portMAX_DELAY
  );

  if (err != ESP_OK) {
    Serial.printf("ERROR: i2s_read failed (%d)\n", err);
    return false;
  }

  size_t samplesRead = bytesRead / sizeof(int32_t);
  return (samplesRead == count);
}

// =====================================================
// Analyze block
//
// INMP441 data is often effectively 24-bit left-justified
// in a 32-bit frame. To make the displayed numbers more
// human-readable, we shift down by 8 bits.
// =====================================================
void analyzeBlock(const int32_t* src, size_t count)
{
  int32_t minVal =  2147483647;
  int32_t maxVal = -2147483647;

  double sum = 0.0;
  double sumSq = 0.0;
  double sumAbs = 0.0;

  for (size_t i = 0; i < count; i++) {
    // normalize a little for readability
    int32_t s = src[i] >> 8;

    if (s < minVal) minVal = s;
    if (s > maxVal) maxVal = s;

    sum += (double)s;
    sumSq += (double)s * (double)s;
    sumAbs += fabs((double)s);
  }

  double mean = sum / (double)count;
  double rms  = sqrt(sumSq / (double)count);
  double avgAbs = sumAbs / (double)count;
  int32_t p2p = maxVal - minVal;

  Serial.println("--------------------------------------------------");
  Serial.printf("Samples   : %d @ %d Hz\n", (int)count, SAMPLE_RATE);
  Serial.printf("Min       : %ld\n", (long)minVal);
  Serial.printf("Max       : %ld\n", (long)maxVal);
  Serial.printf("Peak-Peak : %ld\n", (long)p2p);
  Serial.printf("Mean      : %.2f\n", mean);
  Serial.printf("AvgAbs    : %.2f\n", avgAbs);
  Serial.printf("RMS       : %.2f\n", rms);
}

// =====================================================
// setup
// =====================================================
void setup()
{
  Serial.begin(115200);
  delay(1500);

  Serial.println();
  Serial.println("==============================================");
  Serial.println("INMP441 I2S Receive Test");
  Serial.println("Fs = 16kHz, Block = 1024 samples");
  Serial.println("Channel = LEFT only");
  Serial.println("L/R pin on INMP441 should be tied to GND");
  Serial.println("==============================================");

  initI2SMic();
}

// =====================================================
// loop
// =====================================================
void loop()
{
  bool ok = readMicBlock(samples, BLOCK_SIZE);

  if (!ok) {
    Serial.println("Block read incomplete.");
    delay(500);
    return;
  }

  analyzeBlock(samples, BLOCK_SIZE);

  // 見やすさのため少し間を空ける
  delay(300);
}

投稿者

KeroYon