ihatemornings
Active member
I’ve wired up a Teensy 4.0 with two Audio boards for the quad output setup with a 32GB SanDisk Ultra SD card and it’s working splendidly. This forum (especially https://forum.pjrc.com/threads/61123-Teensy-4-1-and-4-channel-audio-(AudioInputI2SQuad) ) was a goldmine of useful info. But now I’ve run into a problem that I’m having trouble diagnosing.
I’ve put together a simplified version of my code, which reproduces the problem every time for me (View attachment 07_bug_report.ino):
There are two AudioPlaySdRaw objects playing different RAW files from the SD card in parallel, and every second I restart one of them (alternating). It works for a few seconds, then one of the files fails to start playing. The failures form a pretty regular pattern, where one file will fail a few times then both will succeed, then the other file will fail a few times...
This goes on for anywhere between 5 seconds and 5 minutes, then the Teensy locks up – I get a high tone from all the audio outputs and the board is unresponsive until I press the reset button.
Playing a single audio file over and over works fine, as does a single AudioPlaySdRaw object playing alternating files. It’s when I have two objects (or more) playing at once that the failures start.
I added some memory reporting code and saw that the max memory used by the Audio library was 6 and the max processor % was around 50. The board is powered via a USB 2 to USB C adapter connected to a MacBook Pro.
My hunch is that the SD read speed might be the bottleneck. I’m using the SD reader on one of the audio boards. But I might be missing something obvious. I’m new to the Teensy and could do with some pointers.
I’ve put together a simplified version of my code, which reproduces the problem every time for me (View attachment 07_bug_report.ino):
Code:
#include <elapsedMillis.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// Define SD card pins (Teensy 4.0)
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 11
#define SDCARD_SCK_PIN 13
// GUItool: begin automatically generated code
AudioPlaySdRaw playSdRaw1;
AudioPlaySdRaw playSdRaw2;
AudioOutputI2SQuad i2s_quad1;
AudioConnection patchCord1(playSdRaw1, 0, i2s_quad1, 0);
AudioConnection patchCord2(playSdRaw2, 0, i2s_quad1, 1);
AudioControlSGTL5000 sgtl5000_1;
AudioControlSGTL5000 sgtl5000_2;
// GUItool: end automatically generated code
unsigned int tickLength; // ms
elapsedMillis sinceTick;
int alternator = 0;
bool success = false;
void setup() {
Serial.begin(9600);
tickLength = 1000; //ms
AudioMemory(12);
// Set up the two audio shields with different addresses.
sgtl5000_1.setAddress(LOW);
sgtl5000_1.enable();
sgtl5000_1.volume(0.5);
sgtl5000_2.setAddress(HIGH);
sgtl5000_2.enable();
sgtl5000_2.volume(0.5);
// Initialise SD card access.
SPI.setMOSI(SDCARD_MOSI_PIN);
SPI.setSCK(SDCARD_SCK_PIN);
if (!(SD.begin(SDCARD_CS_PIN))) {
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
delay(1000);
}
void loop() {
if (sinceTick >= tickLength) {
sinceTick = sinceTick - tickLength;
if (alternator) {
success = playSdRaw1.play("LOOP1.RAW");
} else {
success = playSdRaw2.play("LOOP2.RAW");
}
alternator = (alternator + 1) % 2;
if (success) {
Serial.print("playing ");
} else {
Serial.print("failed to load ");
}
if (alternator) {
Serial.println("LOOP1.RAW");
} else {
Serial.println("LOOP2.RAW");
}
}
}
There are two AudioPlaySdRaw objects playing different RAW files from the SD card in parallel, and every second I restart one of them (alternating). It works for a few seconds, then one of the files fails to start playing. The failures form a pretty regular pattern, where one file will fail a few times then both will succeed, then the other file will fail a few times...
Code:
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
failed to load LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
failed to load LOOP1.RAW
playing LOOP2.RAW
failed to load LOOP1.RAW
playing LOOP2.RAW
failed to load LOOP1.RAW
playing LOOP2.RAW
failed to load LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
playing LOOP2.RAW
playing LOOP1.RAW
failed to load LOOP2.RAW
playing LOOP1.RAW
failed to load LOOP2.RAW
playing LOOP1.RAW
failed to load LOOP2.RAW
playing LOOP1.RAW
[crash]
This goes on for anywhere between 5 seconds and 5 minutes, then the Teensy locks up – I get a high tone from all the audio outputs and the board is unresponsive until I press the reset button.
Playing a single audio file over and over works fine, as does a single AudioPlaySdRaw object playing alternating files. It’s when I have two objects (or more) playing at once that the failures start.
I added some memory reporting code and saw that the max memory used by the Audio library was 6 and the max processor % was around 50. The board is powered via a USB 2 to USB C adapter connected to a MacBook Pro.
My hunch is that the SD read speed might be the bottleneck. I’m using the SD reader on one of the audio boards. But I might be missing something obvious. I’m new to the Teensy and could do with some pointers.