I am trying to simultaneously record and play to alternate files when input 2 is intermittently switched low.
One file records and plays back but not the second.
Any suggestions welcome.
One file records and plays back but not the second.
Any suggestions welcome.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Bounce.h>
// GUItool: begin automatically generated code
AudioInputI2S i2s2; //xy=246.66665649414062,247
AudioPlaySdRaw playRaw1; //xy=428.6666564941406,588
AudioAnalyzePeak peak1; //xy=429.6666564941406,528
AudioMixer4 mixer1; //xy=682.6666564941406,448
AudioMixer4 mixer3; //xy=690.6666564941406,658.6666641235352
AudioMixer4 mixer2; //xy=692.6666564941406,542
AudioRecordQueue queue1; //xy=963.6666259765625,657.0000228881836
AudioOutputI2S i2s1; //xy=980.6666564941406,478
AudioConnection patchCord1(i2s2, 0, mixer3, 0);
AudioConnection patchCord2(i2s2, 1, peak1, 0);
AudioConnection patchCord3(playRaw1, 0, mixer1, 1);
AudioConnection patchCord4(playRaw1, 0, mixer2, 1);
AudioConnection patchCord5(playRaw1, 0, mixer3, 1);
AudioConnection patchCord6(mixer1, 0, i2s1, 0);
AudioConnection patchCord7(mixer3, queue1);
AudioConnection patchCord8(mixer2, 0, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=337.6666564941406,133
// GUItool: end automatically generated code
// Use these with the audio adaptor board
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
const int myInput = AUDIO_INPUT_MIC;
Bounce buttonStop = Bounce(0, 8);
Bounce buttonPlay = Bounce(1, 8);
Bounce buttonRecord = Bounce(2, 8);
File frec;
int mode = 0;
int playing = 0;
int firstRec = 1;
const char* file1 = "a1.RAW";
const char* file2 = "a2.RAW";
const char* getRecFileName(){
if (mode == 0){
return file1;
}else{
return file2;
}
}
const char* getPlayFileName(){
if (mode == 1){
return file1;
}else{
return file2;
}
}
void startRecording(){
Serial.print("Recording to: ");
Serial.println(getRecFileName());
while (SD.exists(getRecFileName())) {
// The SD library writes new data to the end of the
// file, so to start a new recording, the old file
// must be deleted before new data is written.
SD.remove(getRecFileName());
}
frec = SD.open(getRecFileName(), FILE_WRITE);
frec.seek(0);
queue1.begin();
}
void continueRecording() {
if (queue1.available() >= 2) {
byte buffer[512];
memcpy(buffer, queue1.readBuffer(), 256);
queue1.freeBuffer();
memcpy(buffer+256, queue1.readBuffer(), 256);
queue1.freeBuffer();
elapsedMicros usec = 0;
frec.write(buffer, 512);
}
}
void stopRecording() {
queue1.end();
while (queue1.available() > 0) {
frec.write((byte*)queue1.readBuffer(), 256);
queue1.freeBuffer();
}
frec.close();
}
void playFile(){
Serial.print("Playing from: ");
Serial.println(getPlayFileName());
playRaw1.play(getPlayFileName());
}
void setup() {
pinMode(0, INPUT_PULLUP);
pinMode(1, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
Serial.begin(9600);
// Audio connections require memory to work. For more
// detailed information, see the MemoryAndCpuUsage example
AudioMemory(60);
sgtl5000_1.enable();
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.volume(0.5);
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here, but print a message repetitively
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
}
void loop() {
buttonRecord.update();
if(buttonRecord.fallingEdge()){
if(mode){
mode = 0;
}else{
mode = 1;
}
if(!firstRec){
stopRecording();
}else{
firstRec = 0;
}
playRaw1.stop();
playFile();
delay(5);
startRecording();
}
if(!playRaw1.isPlaying()){
playFile();
}
continueRecording();
}