Teensy 4 breaking .wav file? Continuous playback, AudioBoard - crackling sounds, crash

jmthmm

New member
Dear Community,
i have a problem with a Teensy 4.0 + AudioBoard-configuration and had the hope, that you maybe have an idea what could be the problem and how to solve it.

Last year, i set up three Teensy 4s with AudioBoards.
They are supposed to be able to loop a .wav file continuously and for a long time.
Weirdly, they behave differently.

Two of them were running simultaneously over a period of 10 days, while being plugged in and unplugged once daily.
When i checked them afterwards both of them apparently broke the .wav file.
They were booting, playing the file with a few distortions and crackling sounds until a certain point - then crashing and rebooting.
I was not able to open the file from the MicroSD or to copy it to the computer.
After formatting the card and replacing the file, everything was working fine again.

The third one meanwhile was running in a different location for a much longer time and still was fine. Same setup though.

About two weeks ago, i placed a new version of the file on the two Teensys that had the problem had left them running continuously, without being unplugged.
This weekend, one of them was showing the same symptoms, the file was broken - the other one is still fine though.

I'm using SanDisk Max Endurance 32GB MicroSDs and the file is a 44100Hz 16Bit file in PCM S16 LE (s16l) codec about 7 minutes long.

/*
Using Teensy 4.0 + Teensy Audio Adapter Board rev. D2
https://www.pjrc.com/store/teensy40.html
https://www.pjrc.com/store/teensy3_audio.html
Tested with Arduino version 2.1.0
with Teensy board package version 1.58.1

Using the Teensy Audio Library and the Design Tool
to generate the patch
https://www.pjrc.com/teensy/td_libs_Audio.html

Pins used by Teensy Audio board:
7,8,20,21,23 Audio Data
18,19 Audio Control (I2C - so could also be used for other I2C devices)
15 (A1) Volume Pot
10,11,12,13 SD card
6,11,12,13 Memory chip (if used)
====================================
Total:
6,7,8, 10,11,12,13, 15, 18,19,20,21, 23
=> available:
0,1,2,3,4,5,9 Digital
14,16,17,22 Analog
A0,A2,A3,A8
+ all bottom pins if we really need more
*/

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioPlaySdWav playSdWav1; //xy=292,600
AudioOutputI2S i2s1; //xy=641,605
AudioConnection patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection patchCord2(playSdWav1, 1, i2s1, 1);
AudioControlSGTL5000 sgtl5000_1; //xy=289,896
// GUItool: end automatically generated code
File root;





#define SD_SELECT_PIN 10
int iFile;
typedef struct {
String name;
int size;
} t_sound_file;
#define MAX_SOUND_FILES 1000
t_sound_file playlist [MAX_SOUND_FILES];
unsigned int playlist_size = 0;



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
// for host communication
Serial.begin (115200);

// audio basics
AudioMemory (20); // might work with less, but no need to optimize
sgtl5000_1.enable ();
sgtl5000_1.volume (1.0);

// init SD card
Serial.print ("Initializing SD card...");
if (!SD.begin (SD_SELECT_PIN)) {
Serial.println ("initialization failed!");
while (1) {
}
}
else {
Serial.println ("initialization done.");
}
// read directory
root = SD.open ("/");
parse_directory (root, 0);
if (playlist_size == 0) {
Serial.println ("zero soundfiles found");
while (1) {
}
}
Serial.printf ("found %d soundfiles\n", playlist_size);

iFile = 0;
Serial.printf ("playing file %d\n", iFile);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void loop()
{
play_file (iFile, 0);
}


///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void play_file (unsigned int i, unsigned int duration)
{
char cname [13];

if (i >= playlist_size) {
Serial.println ("file index out of range");
return;
}

t_sound_file sf = playlist ;
String name = sf.name;
name.toCharArray (cname, 12);
// Serial.printf ("Playing file: %s\n", cname);

playSdWav1.play (cname);
delay (5);

if (duration == 0) {
// play until end of File
while (playSdWav1.isPlaying()) {
// check other stuff while polling
check ();
}
}
else {
unsigned long start = millis ();
while (millis () - start < duration) {
check ();
}

playSdWav1.stop ();
}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void check () {
// here we can check other things while the file is playing
// like buttons and knobs and serial communication etc.
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void parse_directory (File dir, int numTabs) {
unsigned int playlist_index = 0;

while (true) {
File entry = dir.openNextFile();
if (!entry) {
// no more files
// Serial.println("**nomorefiles**");
break;
}
if (entry.isDirectory()) {
// currently not handling sub directories
// Serial.println("/");
// parse_directory(entry, numTabs+1); not interested in subdirectories
}
else {
String name = entry.name ();
int size = entry.size ();

if ((name.endsWith(".WAV")) ||
(name.endsWith(".wav")))
{
// Serial.println ("found a WAV file");
Serial.print (name);
Serial.print ("\t\t");
Serial.println (size, DEC);

playlist [playlist_index].name = name;
playlist [playlist_index].size = size;
playlist_index++;
}
}
entry.close();
}

playlist_size = playlist_index;
}

Serial Monitor output is:
"Initializing SD card...initialization done.
BIT.WAV 73187292
found 1 soundfiles
playing file 0"

Thanks in advance for any ideas or thoughts on this! :)

Edit:

Thought maybe it makes sense to check processor and memory usage.
Serial.print("all=");
Serial.print(AudioProcessorUsage());
Serial.print(",");
Serial.print(AudioProcessorUsageMax());
Serial.print(" ");
Serial.print("Memory: ");
Serial.print(AudioMemoryUsage());
Serial.print(",");
Serial.print(AudioMemoryUsageMax());
Serial.print(" ");
Serial.println();
// very simple timing :)
delay(500);
Numbers are between 14 and 17, though rise up to 18,19 and 20.2 at the beginning of the sound file. Memory is constant at 2.2
Maybe this helps with identifying the problem :)
 
Last edited:
I'm using SanDisk Max Endurance 32GB MicroSDs and the file is a 44100Hz 16Bit file in PCM S16 LE (s16l) codec about 7 minutes long.
To rule out a possible WAV file issue; did you try one of these files here?
Edit:
Thought maybe it makes sense to check processor and memory usage.
Serial.print("all=");
Serial.print(AudioProcessorUsage());
Serial.print(",");
Serial.print(AudioProcessorUsageMax());
Serial.print(" ");
Serial.print("Memory: ");
Serial.print(AudioMemoryUsage());
Serial.print(",");
Serial.print(AudioMemoryUsageMax());
Serial.print(" ");
Serial.println();
// very simple timing :)
delay(500);
Numbers are between 14 and 17, though rise up to 18,19 and 20.2 at the beginning of the sound file. Memory is constant at 2.2
Maybe this helps with identifying the problem :)
Which numbers are fluctuating? AudioProcessorUsage, AudioProcessorUsageMax, AudioMemoryUsage or AudioMemoryUsageMax?
Did you try to increase AudioMemory (20);?

Paul
 
Back
Top