//Use following Audio Library (SD Buffered)
//https://github.com/h4yn0nnym0u5e/Audio/tree/feature/buffered-SD
//A backup was saved in SoundRecorder archive folder.
//Replace content on /usr/share/arduino/hardware/teensy/avr/libraries/Audio
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Bounce.h>
#include <SerialFlash.h>
#include <EEPROM.h>
AudioInputI2S i2s1; //xy=220,698
AudioInputUSB usb2; //xy=223,853
AudioPlayWAVstereo playWAVstereo1; //xy=244,609
AudioRecordWAVstereo recordWAVstereo1; //xy=321,98
AudioMixer4 mixer1; //xy=468,691
AudioMixer4 mixer2; //xy=467,769
AudioMixer4 mixer3; //xy=470,854
AudioMixer4 mixer4; //xy=482,934
AudioAnalyzePeak peak1; //xy=603,576
AudioOutputI2S2 i2s2_1; //xy=643,732
AudioOutputUSB usb1; //xy=649,914
AudioConnection patchCord1(i2s1, 0, mixer1, 0);
AudioConnection patchCord2(i2s1, 1, mixer2, 0);
AudioConnection patchCord3(i2s1, 0, mixer3, 0);
AudioConnection patchCord4(i2s1, 1, mixer4, 0);
AudioConnection patchCord5(playWAVstereo1, 0, mixer1, 2);
AudioConnection patchCord6(playWAVstereo1, 1, mixer2, 2);
AudioConnection patchCord7(usb2, 0, mixer1, 1);
AudioConnection patchCord8(usb2, 1, mixer2, 1);
AudioConnection patchCord9(mixer1, 0, i2s2_1, 0);
AudioConnection patchCord10(i2s1, 0, recordWAVstereo1, 0);
AudioConnection patchCord11(i2s1, 0, peak1, 0);
AudioConnection patchCord12(mixer2, 0, i2s2_1, 1);
AudioConnection patchCord13(i2s1, 1, recordWAVstereo1, 1);
AudioConnection patchCord14(mixer3, 0, usb1, 0);
AudioConnection patchCord15(mixer4, 0, usb1, 1);
//Set clock speed
//#if defined(__IMXRT1062__)
//extern "C" uint32_t set_arm_clock(uint32_t frequency);
//#endif
//uint32_t count, prior_count;
//uint32_t prior_msec;
//uint32_t count_per_second;
//bool flip = true;
const int pdnADC = 22;
const int VOLUME_POT = 19;
Bounce buttonRecord = Bounce(10, 8);
Bounce buttonStop = Bounce(9, 8); // 8 = 8 ms debounce time
Bounce buttonPlay = Bounce(7, 8);
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 11 // not actually used
#define SDCARD_SCK_PIN 13 // not actually used
// Remember which mode we're doing
int mode = 0; // 0=stopped, 1=recording, 2=playing
int File_Number;
int address = 10;
// The file where data is recorded
File frec;
void setup() {
//Set clock speed 96MHz
//#if defined(__IMXRT1062__)
// set_arm_clock(96000000);
//#endif
// Serial.begin(1000000); // edit for highest baud your board can use
// while (!Serial) ;
//#if defined(__IMXRT1062__)
// //set_arm_clock(24000000);
// Serial.print("F_CPU_ACTUAL=");
// Serial.println(F_CPU_ACTUAL);
//#endif
AudioMemory(12);
pinMode(pdnADC, OUTPUT);
delay(500); // wait for 10 seconds
digitalWrite(pdnADC, HIGH); // set the PDN on
//mdr20190828 IOMUXC stuff below was required to get SDcard to work and to attach to other boards.
//from: https://forum.pjrc.com/threads/57167-Teensy-4-0-I2S-Support?p=213128&viewfull=1#post213128
// defaults were all 0x10B0 which is keeper, Medium speed (100 Mhz), drive strength = R0/6 = 150/6 = 25 ohms (the second strongest drive strength available) // My changes were:
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_09 = 0x8; // MCLK, low speed, drive strength at R0 (150 ohm).
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_10 = 0x8; // LRCLK
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_11 = 0x8; // BCKL
IOMUXC_SW_PAD_CTL_PAD_GPIO_B1_01 = 0x8; // OUT1A
IOMUXC_SW_PAD_CTL_PAD_GPIO_B1_00 = 0x8; // IN1
IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_04 = 0x8; // OUT2
IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_05 = 0x8; // LRCLK2
IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_06 = 0x8; // BCLK2
IOMUXC_SW_PAD_CTL_PAD_GPIO_EMC_07 = 0x8; // MCLK2
IOMUXC_SW_PAD_CTL_PAD_GPIO_B1_00 = 0x8; // IN2
//IOMUXC_SW_MUX_CTL_PAD_GPIO_SD_B0_00 = 0x8;
//IOMUXC_SW_MUX_CTL_PAD_GPIO_SD_B0_01 = 0x8;
//IOMUXC_SW_MUX_CTL_PAD_GPIO_SD_B0_02 = 0x8;
//IOMUXC_SW_MUX_CTL_PAD_GPIO_SD_B0_03 = 0x8;
//IOMUXC_SW_MUX_CTL_PAD_GPIO_SD_B0_04 = 0x8;
//IOMUXC_SW_MUX_CTL_PAD_GPIO_SD_B0_05 = 0x8;
// Configure the pushbutton pins
pinMode(7, INPUT_PULLUP);
pinMode(9, INPUT_PULLUP);
pinMode(10, INPUT_PULLUP);
// Initialize the SD card
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here if no SD card, but print a message
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
// SD audio objects need buffers configuring:
const size_t sz = 65536;
const AudioBuffer::bufType bufMem = AudioBuffer::inHeap;
playWAVstereo1.createBuffer(sz,bufMem);
recordWAVstereo1.createBuffer(sz,bufMem);
//Filename numbers
File_Number = EEPROM16_Read(address);
Serial.print("Actual File Number: ");
Serial.println(File_Number);
}
void loop() {
float vol = analogRead(VOLUME_POT);
vol = vol / 1024;
mixer1.gain(0, 2*vol);
mixer2.gain(0, 2*vol);
Serial.print("Digital volume is: ");
Serial.println(vol);
delay(250);
// First, read the buttons
buttonRecord.update();
buttonStop.update();
buttonPlay.update();
// Respond to button presses
if (buttonRecord.fallingEdge()) {
Serial.println("Record Button Press");
if (mode == 2) stopPlaying();
if (mode == 0) startRecording();
}
if (buttonStop.fallingEdge()) {
Serial.println("Stop Button Press");
if (mode == 1) stopRecording();
if (mode == 2) stopPlaying();
}
if (buttonPlay.fallingEdge()) {
Serial.println("Play Button Press");
if (mode == 1) stopRecording();
if (mode == 0) startPlaying();
// if (mode == 2) playPrevious();
}
// If we're playing or recording, carry on...
if (mode == 1) {
continueRecording();
}
if (mode == 2) {
continuePlaying();
}
}
void startRecording() {
Serial.print("startRecording RECORD_");
Serial.println(File_Number);
char buffer[40];
sprintf(buffer, "%d.wav", File_Number);
if (SD.exists(buffer)) {
// 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(buffer);
}
frec = SD.open(buffer, FILE_WRITE);
if (frec) {
patchCord9.disconnect();
patchCord12.disconnect();
delay(200);
patchCord9.connect();
patchCord12.connect();
recordWAVstereo1.recordSD(buffer);
mode = 1;
}
}
void continueRecording() {
// nothing to do here!
}
void stopRecording() {
Serial.println("stopRecording");
recordWAVstereo1.stop();
patchCord9.disconnect();
patchCord12.disconnect();
delay(200);
patchCord9.connect();
patchCord12.connect();
File_Number = File_Number + 1;
EEPROM16_Write(address, File_Number);
mode = 0;
}
void startPlaying() {
Serial.print("startPlaying RECORD_");
Serial.println(File_Number - 1);
char buffer[40];
sprintf(buffer, "%d.wav", File_Number - 1);
playWAVstereo1.playSD(buffer);
patchCord1.disconnect();
patchCord2.disconnect();
mode = 2;
}
void continuePlaying() {
if (!playWAVstereo1.isPlaying())
{
Serial.println("endOfPlayback");
patchCord1.connect();
patchCord2.connect();
mode = 0;
}
}
void stopPlaying() {
Serial.println("stopPlaying");
playWAVstereo1.stop();
patchCord1.connect();
patchCord2.connect();
mode = 0;
}
void EEPROM16_Write(uint8_t a, uint16_t b) {
EEPROM.write(a, lowByte(b));
EEPROM.write(a + 1, highByte(b));
}
uint16_t EEPROM16_Read(uint8_t a) {
return word(EEPROM.read(a + 1), EEPROM.read(a));
}