// Enregistre le microphone interne et externe separement et joue separement chaque microphone pour deux oreillette
// On branche le cable jaune (micro externe) sur le channel L et le bleu(micro interne) sur le R
#include <Arduino.h>
#include "Adafruit_NeoTrellis.h"
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#include <Keyboard.h>
#define FILE_BASE_NAME "Audio_2Earpiece"
#define BUFFER_SIZE_AUDIO 1024
const int myInput = AUDIO_INPUT_LINEIN;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName1[] = FILE_BASE_NAME "00.raw";
float volume = 0.5;
int lineIninter = 5;
int lineIn = 5;
bool record_flag = false;
bool buffer_flag = false;
// GUItool: begin automatically generated code
AudioInputI2SQuad i2s_quad1; // xy=82,307
AudioRecordQueue queue1; // xy=98,73
AudioRecordQueue queue4; // xy=146,554
AudioRecordQueue queue2; // xy=381,460
AudioRecordQueue queue3; // xy=420,33
AudioOutputI2SQuad i2s_quad2; // xy=793,287
AudioConnection patchCord1(i2s_quad1, 0, queue1, 0);
// AudioConnection patchCord2(i2s_quad1, 0, i2s_quad2, 0);
// AudioConnection patchCord3(i2s_quad1, 0, i2s_quad2, 2);
AudioConnection patchCord4(i2s_quad1, 1, queue2, 0);
AudioConnection patchCord5(i2s_quad1, 2, queue3, 0);
// AudioConnection patchCord6(i2s_quad1, 2, i2s_quad2, 1);
// AudioConnection patchCord7(i2s_quad1, 2, i2s_quad2, 3);
AudioConnection patchCord8(i2s_quad1, 3, queue4, 0);
AudioControlSGTL5000 sgtl5000_1; // xy=285,444
AudioControlSGTL5000 sgtl5000_2; // xy=382,494
// GUItool: end automatically generated code
// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN BUILTIN_SDCARD
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// Remember which mode we're doing
int mode = 0; // 0=stopped, 1=recording, 2=playing
int startTime;
// The file where data is recorded
File frec;
void setup()
{
// Audio connections require memory, and the record queue
// uses this memory to buffer incoming audio.
AudioMemory(120);
// Enable the audio shield, select input, and enable output
sgtl5000_1.setAddress(HIGH);
sgtl5000_1.enable();
sgtl5000_1.inputSelect(AUDIO_INPUT_LINEIN);
sgtl5000_1.micGain(15);
sgtl5000_1.lineInLevel(5, 5);
sgtl5000_1.volume(0.5);
sgtl5000_2.setAddress(LOW);
sgtl5000_2.enable();
sgtl5000_2.inputSelect(AUDIO_INPUT_LINEIN);
sgtl5000_2.micGain(15);
sgtl5000_2.lineInLevel(5, 5);
sgtl5000_2.volume(0.5);
queue1.begin(); // Internal Right
queue2.begin(); // External Right
queue3.begin(); // Internal Left
queue4.begin(); // External Left
Serial.println("Audio Shield initialized");
// Initialize the SD card
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
Keyboard.begin();
if (!(SD.begin(BUILTIN_SDCARD)))
{
// stop here if no SD card, but print a message
while (1)
{
Serial.println("Unable to access the SD card");
delay(500);
}
}
Serial.println("Setup Done");
}
void loop()
{
if (Serial.available() > 0)
{
char inChar = Serial.read();
switch (inChar)
{
case 'r':
Serial.println("Record Button Press");
if (mode == 0)
{
startRecording(); // Création du fichier AUDIO dans la carte SD
mode = 1;
}
break;
case 's':
Serial.println("Stop Button Press");
if (mode == 1)
{
stopRecording(); // Fermeture du fichier AUDIO dans la carte SD
mode = 0;
}
break;
}
}
if (mode == 1)
{ // Si on appui sur le bouton Play : // Sinon :
continueRecording(); // La fonction d'acquisition de l'audio NE PEUT PAS écrire les données sur la carte SD
}
}
// Strat recording for the right earpiece
void startRecording()
{
Serial.println("startRecording");
while (SD.exists(fileName1))
{
if (fileName1[BASE_NAME_SIZE + 1] != '9')
{
fileName1[BASE_NAME_SIZE + 1]++;
}
else if (fileName1[BASE_NAME_SIZE] != '9')
{
fileName1[BASE_NAME_SIZE + 1] = '0';
fileName1[BASE_NAME_SIZE]++;
}
else
{
Serial.println(F("Can't create file name"));
return;
}
}
frec = SD.open(fileName1, FILE_WRITE);
if (frec) {
Serial.println("File Open");
queue1.begin(); // queue 1 = shield non modifé channel L
queue2.begin(); // queue 2 = shield non modifé channel R
queue3.begin(); // queue 3 = shield modifé channel L
queue4.begin(); // queue 4 = shield modifé channel R
}
}
// write all 512 bytes to the SD card
void continueRecording() {
if (queue1.available() >= 2 && queue2.available() >= 2) {
byte buffer[BUFFER_SIZE_AUDIO];
byte buffer1[BUFFER_SIZE_AUDIO / 4]; // buffer du channel L shield normal
byte buffer2[BUFFER_SIZE_AUDIO / 4]; // buffer du channel R shield normal
byte buffer3[BUFFER_SIZE_AUDIO / 4]; // buffer du channel L shield modifié
byte buffer4[BUFFER_SIZE_AUDIO / 4]; // buffer du channel R shield modifié
memcpy(buffer1, queue1.readBuffer(), BUFFER_SIZE_AUDIO / 4); // Oreillette externe droite
memcpy(buffer2, queue2.readBuffer(), BUFFER_SIZE_AUDIO / 4); // Oreillette interne droite
memcpy(buffer3, queue3.readBuffer(), BUFFER_SIZE_AUDIO / 4); // Oreillette externe gauche
memcpy(buffer4, queue4.readBuffer(), BUFFER_SIZE_AUDIO / 4); // Oreillette interne gauche
queue1.freeBuffer();
queue2.freeBuffer();
queue3.freeBuffer();
queue4.freeBuffer();
int b =0;
for (int i = 0; i < BUFFER_SIZE_AUDIO; i += 8)
{
buffer[i] = buffer1[b];
buffer[i + 1] = buffer1[b + 1];
buffer[i + 2] = buffer2[b];
buffer[i + 3] = buffer2[b + 1];
buffer[i + 4] = buffer3[b];
buffer[i + 5] = buffer3[b + 1];
buffer[i + 6] = buffer4[b];
buffer[i + 7] = buffer4[b + 1];
b = b + 2;
}
frec.write(buffer, BUFFER_SIZE_AUDIO); //256 or 512 (dudes code)
}
}
void stopRecording() {
Serial.println("StopRecording");
queue1.end();
queue2.end();
queue3.end();
queue4.end();
// flush buffer
while (queue1.available() > 0 && queue2.available() > 0) {
queue1.readBuffer();
queue1.freeBuffer();
queue2.readBuffer();
queue2.freeBuffer();
queue3.readBuffer();
queue3.freeBuffer();
queue4.readBuffer();
queue4.freeBuffer();
}
frec.close(); // close file
}
}