Last active
January 1, 2025 08:42
-
-
Save 3110/35d00b7bddd22fdd8d943ea45cb2099c to your computer and use it in GitHub Desktop.
EchoBaseのサンプルRecordPlay.inoをPlatformIO IDE 向け M5Stack 定型コード環境で動かす
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// PlatformIO IDE 向け M5Stack 定型コード環境 | |
// https://github.com/3110/m5stack-platformio-boilerplate-code | |
// clang-format off | |
#include "main.hpp" | |
// clang-format on | |
#include <M5EchoBase.h> | |
#define RECORD_DURATION_SEC 3 | |
#define SAMPLE_RATE 16000 | |
#if defined(ARUDINO_M5STACK_ATOMS3R) || defined(ARDUINO_M5STACK_ATOMS3) || \ | |
defined(ARDUINO_M5STACK_ATOMS3_LITE) | |
#define I2C_SDA 38 | |
#define I2C_SCL 39 | |
#define I2S_DIN 7 | |
#define I2S_WS 6 | |
#define I2S_DOUT 5 | |
#define I2S_BCK 8 | |
#elif defined(ARDUINO_M5STACK_ATOM_MATRIX) || defined(ARDUINO_M5STACK_ATOM_LITE) | |
#define I2C_SDA 25 | |
#define I2C_SCL 21 | |
#define I2S_DIN 23 | |
#define I2S_WS 19 | |
#define I2S_DOUT 22 | |
#define I2S_BCK 33 | |
#endif | |
M5EchoBase echobase(I2S_NUM_0); | |
static uint8_t *audioBuf = nullptr; | |
static size_t audioBufSize = 0; | |
void setup(void) { | |
M5_BEGIN(); | |
echobase.init(SAMPLE_RATE, I2C_SDA, I2C_SCL, I2S_DIN, I2S_WS, I2S_DOUT, | |
I2S_BCK, Wire); | |
echobase.setSpeakerVolume(70); | |
echobase.setMicGain(ES8311_MIC_GAIN_6DB); | |
audioBufSize = echobase.getBufferSize(RECORD_DURATION_SEC); | |
audioBuf = (uint8_t *)malloc(audioBufSize); | |
if (!audioBuf) { | |
while (true) { | |
M5DEV_LOGE("Failed to allocate memory :("); | |
delay(1000); | |
} | |
} | |
M5DEV_LOGI("EchoBase ready, start recording and playing!"); | |
} | |
void loop(void) { | |
M5_UPDATE(); | |
if (M5DEV.BtnA.wasPressed()) { | |
delay(1000); | |
M5DEV_LOGI("Recording..."); | |
echobase.setMute(true); | |
delay(10); | |
echobase.record(audioBuf, audioBufSize); | |
delay(100); | |
M5DEV_LOGI("Playing..."); | |
echobase.setMute(false); | |
delay(10); | |
echobase.play(audioBuf, audioBufSize); | |
delay(100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment