Teensy 3.2 + Audio Adapter: Memory Spikes and Playback Stutters

Status
Not open for further replies.

lerxstrulz

Well-known member
I've been trying to diagnose an issue whereby the Max Processor Usage (see code) suddenly spikes and a loop I have playing from SD will stutter for a second. This cycle continues at a seemingly random interval (spike, stutter, drop to normal, spike, stutter....)

I am playing a 1 minute loop .WAV file and restarting it after it ends. I have tried different SD cards, different WAV files, different AudioMemory values...nothing seems to affect it. I noticed this in a different sketch, so I put together the basic one below to start from a base to diagnose, and it's happening even with this very simple sketch.

The output below is written every 1 second. This spike does not happen often...it could be anywhere from 3 to around 10 minutes between occurrences.

Screen Shot 2018-07-10 at 11.45.50 PM.jpg

Screen Shot 2018-07-10 at 11.53.40 PM.jpg

Here is the code I am testing with:

Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           loopPlayer;     //xy=383,257
AudioOutputI2S           i2s2;           //xy=559,260
AudioConnection          patchCord1(loopPlayer, 0, i2s2, 0);
AudioControlSGTL5000     audioShield;    //xy=113,275
// GUItool: end automatically generated code

elapsedMillis loopMillis;
unsigned long len = 0;

void setup() {
  Serial.begin(57600);
  
  delay(250);

  // Always allocate memory for the audio shield!
  AudioMemory(4);
  
  // turn on audio shield
  audioShield.enable();

  // turn on post processing
  audioShield.audioPostProcessorEnable();

  // Check SD card
  SPI.setMOSI(7);  // Set to 7 for Teensy
  SPI.setSCK(14);  // Set to 14 for Teensy
  // CS Pin is 10..do not change for Teensy!
  if (!(SD.begin(10))) {
     Serial.println("Unable to access the SD card");
  }

  delay(250);

  loopPlayer.play("/loops/breath.wav");
  delay(10);
  len = loopPlayer.lengthMillis();
  loopMillis = 0;

}

elapsedMillis timer;

void loop() {
  // put your main code here, to run repeatedly:
  if (timer >= 1000) {
    timer = 0;
    showMemory();
  }

  if (loopMillis >= len) {
    loopMillis = 0;
    loopPlayer.play("/loops/breath.wav");
    delay(10);
    len = loopPlayer.lengthMillis();
  }

}

void showMemory() {
    Serial.println("");
    Serial.print("Proc = ");
    Serial.print(AudioProcessorUsage());
    Serial.print(" (");    
    Serial.print(AudioProcessorUsageMax());
    Serial.print("),  Mem = ");
    Serial.print(AudioMemoryUsage());
    Serial.print(" (");    
    Serial.print(AudioMemoryUsageMax());
    Serial.print(") ");
    Serial.print("Free Mem: ");
    Serial.println(freeMemory());
    Serial.println("");
}

#ifdef __arm__
// should use uinstd.h to define sbrk but Due causes a conflict
extern "C" char* sbrk(int incr);
#else  // __ARM__
extern char *__brkval;
#endif  // __arm__
 
int freeMemory() {
  char top;
#ifdef __arm__
  return &top - reinterpret_cast<char*>(sbrk(0));
#elif defined(CORE_TEENSY) || (ARDUINO > 103 && ARDUINO != 151)
  return &top - __brkval;
#else  // __arm__
  return __brkval ? &top - __brkval : &top - __malloc_heap_start;
#endif  // __arm__
}
// END

Also I am using Teensyduino 1.42 with Arduino 1.8.5, Teensy 3.2 and Mac OSX Sierra 10.12.6.

Any ideas?

Thank you!
 
Since you hit AudioMemoryUsageMax = 4 on the second screenshot, I'd up the allocation, since there's ample memory:

// Always allocate memory for the audio shield!
AudioMemory(8);

Since you are also working with a whole other embedded system in the SD card, different SD cards might have more favorable garbage collection timing or whatever. I'd try that as well.

If that doesn't fix it, you can try different SD card libraries or including a modified read-only library from Paul. These have been discussed in the forum but I don't have links to hand. Good luck and report back!
 
Did this problem ever get resolved?

Please let me know if I should still try to look into it...

I was actually testing this again today. I have tested it on several different devices (all Teensy 3.2 with an Audio adaptor.) One of them is from parts I received about 6 (?) weeks ago. The other two are probably around a year old (I was wondering if maybe it "could" have been related to hardware...)

I've tested with ADATA and PNY and Samsung Class 10 cards. I was going to grab a SanDisk and test with that. So far same results :/ I also tested with several different loop files, all 44,100Mhz 16-bit mono in lengths varying from a few seconds to 30 minutes.
 
Did this problem ever get resolved?

Please let me know if I should still try to look into it...

Hi Paul,

I’m having the exact same issue. I increased the memory allocation but no luck. The max usage never peaks above 8.
I checked the CPU usage and after the stutter it shows the max at around 372.
Not sure what I can change to help with this? The stutter happens randomly during playback.
 
I added a 10uF Electrolytic and 0.1uF Ceramic cap to the Teensy setup and the stutter seems to be gone. I still need to verify this with a few more files, bit for now it seems a current spike during high processor usage is causing the stutter. I'll hook it up to the scope tomorrow to see what's cooking.
 
Don't buy cheap SD cards!

I was using an Apacer Class 10 card, but when I replaced it with a Sandisk Ultra Class 10 the stutter vanished. Memory and processor usage also halved!

So... Don't buy cheap SD cards! (Although the Sandisk cards are cheaper - but not at my usual supplier)
 
Don't buy cheap SD cards!

I was using an Apacer Class 10 card, but when I replaced it with a Sandisk Ultra Class 10 the stutter vanished. Memory and processor usage also halved!

So... Don't buy cheap SD cards! (Although the Sandisk cards are cheaper - but not at my usual supplier)

I started off using the SanDisk, but was having to reformat the card every couple of weeks or so as all of a sudden I couldn't read it. I switched to ADATA and have never had to reformat, but get the stutter. I am also getting it with Samsung and PNY. Going to try it with the SanDisk again, and hopefully both issues get resolved 😉
 
Don't buy cheap SD cards!

I was using an Apacer Class 10 card, but when I replaced it with a Sandisk Ultra Class 10 the stutter vanished. Memory and processor usage also halved!

So... Don't buy cheap SD cards! (Although the Sandisk cards are cheaper - but not at my usual supplier)

I got my SanDisk Ultra SDHC 16G in the mail today and so far so good. Have been playing the loop continuously for over an hour and no stutters, the memory is consistent, and memory usage is SO much lower as you stated!

Next test is the reformatting issue I mentioned. I'll write up a sketch to read to and write from the SD on a continual basis and see if that is still an issue. Hopefully not. If not, I'll be switching back to SanDisk ;)
 
Status
Not open for further replies.
Back
Top