这个示例是用MAX9814进行录音,并生成一个wav文件保存到SD上。如果用ESP8266可以不使用SD卡模块直接将文件存到云。
BOM
- MAX9814 模块
- MicroSDCard 模块
- Arduino Uno
线路图
代码
这里我使用了一个叫TMRpcm的库,这个库在Arduino上非常好用,它本来是做软DAC用的,可以用来扩展Arduino进行直接的声音解码播放。另外TMRpcm里面还有一个用于录音的方法,源码中是被注释掉的,在安装该库之后需要打开源码库中的pcmConfig.h
文件将以下的行取消注释,否则会编译不通过:
#define buffSize 128. May need to increase.
#define ENABLE_RECORDING
#define BLOCK_COUNT 10000UL
以下是 Arduino 代码:
#include <SD.h>
#include <SPI.h>
#include <TMRpcm.h>
#define SD_ChipSelectPin 10 //using digital pin 4 on arduino nano 328, can use other pins
TMRpcm audio; // create an object for use in this sketch
void setup() {
audio.speakerPin = 4;
Serial.begin(115200);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD Fail");
return;
}else{
Serial.println("SD OK");
}
// The audio library needs to know which CS pin to use for recording
audio.CSPin = SD_ChipSelectPin;
}
void loop() {
if(Serial.available()){ //Send commands over serial to play
char c = Serial.read();
Serial.println(c);
switch(c){
case 'r': audio.startRecording("test.wav",16000,A0); break; //Record at 16khz sample rate on pin A0
case 'R': audio.startRecording("test.wav",16000,A0,1); break; //Record, but with passthrough to speaker.
case 't': audio.startRecording("test.wav",16000,A0,2); break; //Do not record. Output direct to speaker
//Note: If samples are dropped before writing, it
// will not be heard in passthrough mode
case 's': audio.stopRecording("test.wav"); break; //Stop recording
case 'p': audio.play("test.wav"); break; //Play the recording
case '=': audio.volume(1); break; //Increase volume by 1. Does not affect recording
case '-': audio.volume(0); break; //Decrease volume by 1. Does not affect recording
case 'S': audio.stopPlayback(); break; //Stop all playback
}
}
}