Arduino入門

2. デバイス&サンプル

5. 拡張モジュール&サンプル

アクセサリー

6. アプリケーション

CoreP4X Mic マイク

CoreP4X のマイクに関する API とサンプルプログラムです。

サンプルプログラム

コンパイル要件

  • M5Stack ボードマネージャーバージョン >= 3.3.10-a
  • ボード選択 = M5CoreP4X
  • M5Unified ライブラリバージョン >= 0.3.1
  • M5GFX ライブラリバージョン >= 0.3.1

CoreP4X は ES7210 オーディオ ADC と 2 系統の MEMS マイクを使用し、AEC エコーキャンセル入力に対応しています。マイクの I2S データ線は G5 で、スピーカーと MCLK G2 / BCLK G6 / LRCK G4 を共有します。

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
#include <M5Unified.h>
#include <atomic>

static constexpr size_t SAMPLE_COUNT = 512;
static constexpr uint32_t SAMPLE_RATE = 16000;

static int16_t samples[SAMPLE_COUNT];
static std::atomic<bool> ready {false};
static bool micReady = false;

static void onRecordRelease(void*, void*, size_t)
{
  ready.store(true, std::memory_order_release);
}

void setup()
{
  auto cfg = M5.config();
  cfg.serial_baudrate = 115200;
  M5.begin(cfg);

  M5.Display.setRotation(0);
  M5.Display.setBrightness(128);
  M5.Display.fillScreen(TFT_BLACK);
  M5.Display.setTextDatum(top_center);
  M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
  M5.Display.setFont(&fonts::FreeSansBold24pt7b);
  M5.Display.drawString("CoreP4X Mic", 240, 32);

  M5.Speaker.end();
  M5.Mic.setBufferReleaseCallback(nullptr, onRecordRelease);

  micReady = M5.Mic.begin();
  if (!micReady) {
    M5.Display.setTextColor(TFT_RED, TFT_BLACK);
    M5.Display.setFont(&fonts::FreeSans18pt7b);
    M5.Display.drawString("Mic init failed", 240, 190);
    Serial.println("Mic init failed");
  }
}

void loop()
{
  if (!micReady || !M5.Mic.isEnabled()) {
    delay(500);
    return;
  }

  if (!ready.load(std::memory_order_acquire)) {
    if (M5.Mic.record(samples, SAMPLE_COUNT, SAMPLE_RATE)) {
      while (!ready.load(std::memory_order_acquire)) {
        delay(1);
      }
      ready.store(false, std::memory_order_release);

      int peak = 0;
      for (const int16_t sample : samples) {
        const int value = abs(sample);
        if (value > peak) { peak = value; }
      }

      const int barWidth = map(peak, 0, 32767, 0, 360);
      M5.Display.setTextDatum(middle_center);
      M5.Display.setFont(&fonts::FreeSans18pt7b);
      M5.Display.setTextColor(TFT_WHITE, TFT_BLACK);
      M5.Display.drawString("Input level", 240, 130);
      M5.Display.fillRect(60, 174, 360, 40, TFT_DARKGREY);
      M5.Display.fillRect(60, 174, barWidth, 40, TFT_GREEN);
      M5.Display.fillRect(0, 268, 480, 52, TFT_BLACK);
      char peakText[24];
      snprintf(peakText, sizeof(peakText), "Peak: %d", peak);
      M5.Display.drawString(peakText, 240, 294);

      Serial.printf("mic peak: %d\n", peak);
    }
  }

  delay(5);
}

書き込み後、画面にマイクのピークバーが表示されます。マイクに向かって話すか音を再生すると、緑色のバーが音量に応じて変化し、シリアルモニターにもピーク値が出力されます。

API

CoreP4X のマイクは M5Unified ライブラリの Mic_Class を使用します。その他の API については以下を参照してください:

Page Tools
PDF
On This Page