Hello,
I am very new to the Teensy topic. With four MAX4466s, two Audioshields and one Teensy 3.2, I'm trying to build a quad-recording device based on the Sparkfun tutorial. I tried to extend the stereo recording sketch from "inmidi" to 4 channels. But it does not work.
This is a recording of a 1 kHz Sine-wave.
i hope someone can help me.
p.s.: Sorry, I have not spoken much English since school and have some problems expressing myself.
I am very new to the Teensy topic. With four MAX4466s, two Audioshields and one Teensy 3.2, I'm trying to build a quad-recording device based on the Sparkfun tutorial. I tried to extend the stereo recording sketch from "inmidi" to 4 channels. But it does not work.
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2SQuad i2s_quad1; //xy=375,276
AudioRecordQueue queue3; //xy=580,292
AudioRecordQueue queue4; //xy=580,329
AudioRecordQueue queue1; //xy=581,222
AudioRecordQueue queue2; //xy=582,258
AudioConnection patchCord1(i2s_quad1, 0, queue1, 0);
AudioConnection patchCord2(i2s_quad1, 1, queue2, 0);
AudioConnection patchCord3(i2s_quad1, 2, queue3, 0);
AudioConnection patchCord4(i2s_quad1, 3, queue4, 0);
AudioControlSGTL5000 sgtl5000_2; //xy=131,135
AudioControlSGTL5000 sgtl5000_1; //xy=135,40
// GUItool: end automatically generated code
// which input on the audio shield will be used?
const int myInput = AUDIO_INPUT_LINEIN;
//const int myInput = AUDIO_INPUT_MIC;
// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14
// Use these with the Teensy 3.5 & 3.6 SD card
//#define SDCARD_CS_PIN BUILTIN_SDCARD // 254?
//#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
// The file where data is recorded
File frec;
void setup() {
// record queue uses this memory to buffer incoming audio.
AudioMemory(120); // 60
// Enable the audio shield, select input, and enable output
sgtl5000_1.setAddress(LOW);
sgtl5000_1.enable();
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.volume(0.5);
sgtl5000_2.setAddress(HIGH);
sgtl5000_2.enable();
sgtl5000_2.inputSelect(myInput);
sgtl5000_2.volume(0.5);
// 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);
}
}
startRecording();
}
void loop() {
if (millis() > 10000 && mode == 1) {
stopRecording();
}
else {
if (mode == 1) continueRecording();
}
}
void startRecording() {
Serial.println("StartRecording");
if (SD.exists("RECORD.RAW")) {
SD.remove("RECORD.RAW");
}
frec = SD.open("RECORD.RAW", FILE_WRITE);
if (frec) {
Serial.println("File Open");
queue1.begin();
queue2.begin();
queue3.begin();
queue4.begin();
mode = 1;
}
}
// write all 512 bytes to the SD card
void continueRecording() {
if (queue1.available() >= 2 && queue2.available() >= 2 && queue3.available() >= 2 && queue4.available() >= 2) {
byte buffer[512];
byte buffer1[128];
byte buffer2[128];
byte buffer3[128];
byte buffer4[128];
memcpy(buffer1, queue1.readBuffer(), 128);
memcpy(buffer2, queue2.readBuffer(), 128);
memcpy(buffer3, queue3.readBuffer(), 128);
memcpy(buffer4, queue4.readBuffer(), 128);
queue1.freeBuffer();
queue2.freeBuffer();
queue3.freeBuffer();
queue4.freeBuffer();
int b = 0;
for (int i = 0; i < 512; 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+4;
}
elapsedMicros usec = 0;
frec.write(buffer, 512); //256 or 512 (dudes code)
Serial.print("SD write, us=");
Serial.println(usec);
}
}
void stopRecording() {
Serial.println("StopRecording");
queue1.end();
queue2.end();
queue3.end();
queue4.end();
// flush buffer
while (queue1.available() > 0 && queue2.available() > 0 && queue3.available() > 0 && queue4.available() > 0) {
queue1.readBuffer();
queue1.freeBuffer();
queue2.readBuffer();
queue2.freeBuffer();
queue3.readBuffer();
queue3.freeBuffer();
queue4.readBuffer();
queue4.freeBuffer();
}
frec.close(); // close file
mode = 4;
}
This is a recording of a 1 kHz Sine-wave.
i hope someone can help me.
p.s.: Sorry, I have not spoken much English since school and have some problems expressing myself.