#include <Audio.h>
// Define audio components
AudioInputI2S microphone; // Audio input object for the microphone
AudioPlayWAVstereo playWav; // Audio object for playing WAV files
AudioOutputI2S audioOutput; // Audio output object
AudioMixer4 mixer; // Audio mixer
AudioRecordWAVmono recordWav; // Audio recorder for WAV files
// AudioAnalyzeRMS audioRMS;
// Audio connections
AudioConnection patchCord1(microphone, 0, mixer, 0); // Microphone to mixer channel 0
// AudioConnection patchCord1(microphone, 0, audioRMS, 0); // Microphone to rms channel 0
AudioConnection patchCord2(playWav, 0, mixer, 1); // WAV player to mixer channel 1
AudioConnection patchCord3(mixer, 0, audioOutput, 0); // Mixer to output (left channel)
AudioConnection patchCord4(mixer, 0, audioOutput, 1); // Mixer to output (right channel)
AudioConnection patchCord5(microphone, 0, recordWav, 0); // Mixer to output (right channel)
// Audio control
AudioControlSGTL5000 audioShield; // Control object for the audio shield
const char* recFile = "rec133.wav";
void setup() {
// Initialize audio memory and audio shield
AudioMemory(60);
audioShield.enable();
audioShield.inputSelect(AUDIO_INPUT_MIC);
audioShield.volume(0.5);
// Initialize the built-in SD card
if (!SD.begin(BUILTIN_SDCARD)) {
// Handle error, such as by signaling with an LED or serial output
}
while (!Serial)
; // Wait for Serial to be ready
// Set mixer levels (adjust as needed)
mixer.gain(0, 0.5); // Set gain for microphone input
mixer.gain(1, 0.5); // Set gain for WAV player
const size_t sz = 65536;
const AudioBuffer::bufType bufMem = AudioBuffer::inHeap;
playWav.createBuffer(sz, bufMem);
recordWav.createBuffer(sz, bufMem);
// playWav.play("rec133.wav", SD); // Play a WAV file from the SD card
// playWav.play("test.wav", SD); // Play a WAV file from the SD card
Serial.println("Playback started");
// delay(2000); // A couple seconds to confirm audio playback is working
// Begin recording to a WAV file
if (!recordWav.record(recFile)) {
Serial.println("Failed to start recording!");
} else {
Serial.println("recording");
Serial.println("currentheader 1");
delay(3000);
recordWav.writeCurrentHeader();
delay(3000);
Serial.println("currentheader 2");
delay(3000);
recordWav.writeCurrentHeader();
Serial.println("unplug now");
// recordWav.stop(); // in case of powerloss not necessarily needed
}
}
void loop() {
}