donperryjm
Well-known member
Hello, I'm recording audio using the example code below. T4.1
The output has however been garbled.
In passthrough mode the audio comes over cleanly to the USB. No issues there.
Issue arises when trying to record to the stereo raw file to SD card.
I've also attached the source audio file and the recorded garbled file. (I'm playing the source file to the input and using the teensy to record it to compare quality/clarity)
The output has however been garbled.
In passthrough mode the audio comes over cleanly to the USB. No issues there.
Issue arises when trying to record to the stereo raw file to SD card.
I've also attached the source audio file and the recorded garbled file. (I'm playing the source file to the input and using the teensy to record it to compare quality/clarity)
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S is2stereoIn; //xy=127.19999694824219,332
AudioAmplifier amp1L; //xy=372.1999969482422,177
AudioAmplifier amp1R; //xy=425.1999969482422,490
AudioFilterBiquad biquad1L; //xy=638.1999969482422,186
AudioFilterBiquad biquad1R; //xy=661.1999969482422,539
AudioAnalyzePeak peakL; //xy=810.1999969482422,136
AudioOutputUSB usb2; //xy=826.1999969482422,331
AudioAnalyzePeak peakR; //xy=839.1999969482422,626
AudioRecordQueue queue1; //xy=851.1999969482422,208.1999969482422
AudioRecordQueue queue2; //xy=888.1999969482422,523.1999969482422
AudioConnection patchCord1(is2stereoIn, 0, amp1L, 0);
AudioConnection patchCord2(is2stereoIn, 1, amp1R, 0);
AudioConnection patchCord3(amp1L, biquad1L);
AudioConnection patchCord4(amp1R, biquad1R);
AudioConnection patchCord5(biquad1L, peakL);
AudioConnection patchCord6(biquad1L, 0, usb2, 0);
AudioConnection patchCord7(biquad1L, queue1);
AudioConnection patchCord8(biquad1R, peakR);
AudioConnection patchCord9(biquad1R, 0, usb2, 1);
AudioConnection patchCord10(biquad1R, queue2);
// 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 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
// The file where data is recorded
File frec;
void setup() {
// record queue uses this memory to buffer incoming audio.
AudioMemory(120); // 60
// 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);
}
}
amp1L.gain(1);
amp1R.gain(1);
biquad1L.setHighpass(0, 200, 1.0); //1.5 was 2.0
biquad1L.setLowpass(1, 10000, 1.0); //1.5 was 2.0
biquad1R.setHighpass(0, 200, 1.0); //1.5 was 2.0
biquad1R.setLowpass(1, 10000, 1.0); //1.5 was 2.0
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();
mode = 1;
}
}
// write all 512 bytes to the SD card
void continueRecording() {
if (queue1.available() >= 2 && queue2.available() >= 2) {
byte buffer[512];
byte bufferL[256];
byte bufferR[256];
memcpy(bufferL, queue1.readBuffer(), 256);
memcpy(bufferR, queue2.readBuffer(), 256);
queue1.freeBuffer();
queue2.freeBuffer();
int b = 0;
for (int i = 0; i < 512; i += 4) {
buffer[i] = bufferL[b];
buffer[i + 1] = bufferL[b + 1];
buffer[i + 2] = bufferR[b];
buffer[i + 3] = bufferR[b + 1];
b = b+2;
}
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();
// flush buffer
while (queue1.available() > 0 && queue2.available() > 0) {
queue1.readBuffer();
queue1.freeBuffer();
queue2.readBuffer();
queue2.freeBuffer();
}
frec.close(); // close file
mode = 4;
}}
Attachments
Last edited: