Hello everyone~
I am trying the audio shield to record 10s of stereo audio. A while back the initial code was written (as can be read about here: https://forum.pjrc.com/threads/46150-Recording-Stereo-Audio-to-SD-Card)
The recordings initially seemed fine as I was testing it by snapping my fingers. In other words short impulses of sound. However, I noticed that when recording a bit of music bits of the recording were missing.
When I pop the recordings into audacity I can also see that the recordings tend to be around 8.4 - 9.2 seconds long. This means that basically between 8-16% of the recording is lossed.
When checking the serial monitor I observe in an increase time it takes to buffer sound. Whereas it takes roughly 2600us sometimes it pop ups to 8000us for a bit.
Based on this I make the assumption that:
- The buffer is not emptied fast enough. New data gets thrown away when the buffer is full. One way to solve this is to increase the buffer size. I tried this as well as increasing the audioMemory, sadly bearing no positive results.
- Find a way to speed up writing from the buffer to SD card. For example getting a faster SD card. I am currently using an: Class 10 8GB card, what should be fast enough.
This problem appears to be present when recording mono as well (tested it with the example library).
My code:
Does anyone have an idea how to prevent this ' skipping' within the recording?
I am trying the audio shield to record 10s of stereo audio. A while back the initial code was written (as can be read about here: https://forum.pjrc.com/threads/46150-Recording-Stereo-Audio-to-SD-Card)
The recordings initially seemed fine as I was testing it by snapping my fingers. In other words short impulses of sound. However, I noticed that when recording a bit of music bits of the recording were missing.
When I pop the recordings into audacity I can also see that the recordings tend to be around 8.4 - 9.2 seconds long. This means that basically between 8-16% of the recording is lossed.
When checking the serial monitor I observe in an increase time it takes to buffer sound. Whereas it takes roughly 2600us sometimes it pop ups to 8000us for a bit.
Based on this I make the assumption that:
- The buffer is not emptied fast enough. New data gets thrown away when the buffer is full. One way to solve this is to increase the buffer size. I tried this as well as increasing the audioMemory, sadly bearing no positive results.
- Find a way to speed up writing from the buffer to SD card. For example getting a faster SD card. I am currently using an: Class 10 8GB card, what should be fast enough.
This problem appears to be present when recording mono as well (tested it with the example library).
My code:
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S i2s1; //xy=168,145
AudioRecordQueue queue1; //xy=360,62
AudioRecordQueue queue2; //xy=389,145
AudioConnection patchCord1(i2s1, 0, queue1, 0);
AudioConnection patchCord2(i2s1, 1, queue2, 0);
// GUItool: end automatically generated code
AudioControlSGTL5000 sgtl5000_1; //xy=265,212
// 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
#define recPin 2
long recDelay;
long runTime;
String bestandsNaam = "RECORD11.RAW";
// The file where data is recorded
File frec;
void setup() {
pinMode(recPin, INPUT_PULLUP);
// record queue uses this memory to buffer incoming audio.
AudioMemory(120); // 60
// Enable the audio shield, select input, and enable output
sgtl5000_1.enable();
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.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);
}
}
}
void loop() {
runTime = millis();
if (mode == 1) {
continueRecording();
if (runTime > recDelay) {
stopRecording();
}
} else {
if (digitalRead(recPin) == 0 && mode == 0) {
startRecording();
recDelay = runTime + 10000;
}
}
}
void startRecording() {
Serial.println("StartRecording");
if (SD.exists(bestandsNaam.c_str())) {
SD.remove(bestandsNaam.c_str());
}
frec = SD.open(bestandsNaam.c_str(), 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);
//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;
}
Does anyone have an idea how to prevent this ' skipping' within the recording?