Multi-track audio crashes Teensy 4.1

cjames53

Member
The code below crashes the Teensy 4.1 + Audio Shield. It's a sketch that attempts to play four tracks simultaneously. It works for 1 or 2 simultaneous tracks, but the third track causes NULL-pointer exception.

Using Arduino 1.8.16, Teensyduino 1.55. (Note: I tried a newer Teensyduino, I think it was 1.58, but it wouldn't compile anything that used the SdFat library, reporting that it was finding the wrong version of the SD library.)

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

AudioPlaySdWav           playSdWav1;     //xy=124,100
AudioPlaySdWav           playSdWav2;     //xy=124,160
AudioPlaySdWav           playSdWav3;     //xy=124,220
AudioPlaySdWav           playSdWav4;     //xy=124,280
AudioMixer4              mixer1;         //xy=470,160
AudioMixer4              mixer2;         //xy=470,280
AudioOutputI2S           i2s1;           //xy=650,220
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord2(playSdWav1, 1, mixer2, 0);
AudioConnection          patchCord3(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord4(playSdWav2, 1, mixer2, 1);
AudioConnection          patchCord5(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord6(playSdWav3, 1, mixer2, 2);
AudioConnection          patchCord7(playSdWav4, 0, mixer1, 3);
AudioConnection          patchCord8(playSdWav4, 1, mixer2, 3);
AudioConnection          patchCord9(mixer1, 0, i2s1, 0);
AudioConnection          patchCord10(mixer2, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000;     //xy=127,379.111083984375

void setup() {

  // Start the SD card. These definitions are specific to the
  // Teensy Audio Shield when mounted on the Teensy 4.1 computer.
#define SDCARD_MOSI_PIN 11
#define SDCARD_SCK_PIN  13
#define SDCARD_CS_PIN   10
  Serial.println("Initializing SD card...");
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!SD.begin(SDCARD_CS_PIN)) {
    Serial.println("SD card initialization failed!");  // always print, even of logging turned off.
    while (1);
  }

  // Initialization for the Teensy Audio Shield
  AudioMemory(8);
  sgtl5000.enable();
  sgtl5000.volume(1.0);
  delay(1000);  // wait for SGTL5000 to initialize

  for (int i = 0; i < 4; i++) {
    mixer1.gain(i, 1.0);
    mixer2.gain(i, 1.0);
  }
  Serial.println("setup() done.\n");
}

void loop() {

  Serial.println("play 1");
  playSdWav1.play("SDTEST1.WAV");
  delay(2000);

  Serial.println("play 2");
  playSdWav2.play("SDTEST2.WAV");
  delay(2000);

  Serial.println("play 3");
  playSdWav3.play("SDTEST4.WAV");
  delay(2000);

  Serial.println("play 4");
  playSdWav4.play("SDTEST4.WAV");
  delay(2000);

  Serial.println("stop 1");
  playSdWav1.stop();
  delay(2000);

  Serial.println("stop 2");
  playSdWav2.stop();
  delay(2000);

  Serial.println("stop 3");
  playSdWav3.stop();
  delay(2000);

  Serial.println("stop 4");
  playSdWav4.stop();
  delay(2000);
}

The output on the Serial Monitor looks like this:
Code:
Initializing SD card...
setup() done.

play 1
play 2
play 3

It never gets to the 4th track. The first three tracks are sort of playing, but badly distorted. I get a crash report:
Code:
CrashReport:
  A problem occurred at (system time) 0:0:23
  Code was executing from address 0x120
  CFSR: 82
	(DACCVIOL) Data Access Violation
	(MMARVALID) Accessed Address: 0x0 (nullptr)
	  Check code at 0x120 - very likely a bug!
	  Run "addr2line -e mysketch.ino.elf 0x120" for filename & line number.
  Temperature inside the chip was 38.04 °C
  Startup CPU clock speed is 600MHz

Which, when run through addr2line reports this:
Code:
$ addr2line -e /var/folders/9t/cb_zcrt138n3z64mj13z74k80000gn/T/arduino_build_939888/test_multi_track_raw.ino.elf 0x120
/Applications/Teensyduino.app/Contents/Java/hardware/teensy/avr/libraries/Audio/play_sd_wav.h:37

Thanks for any help.
Craig
 
Craig:

How did you determine the value passed to the audio memory allocator (specifically: AudioMemory(8);) ?? Your audio processing may be running short on memory . . . try bumping that up. Take a look at the Audio=>MemoryAndCpuUsage example for info on how to measure what's needed and/or actively being used (on-the-fly value & maximum value).

Hope that helps . . .

Mark J Culross
KD5RXT
 
Your audio processing may be running short on memory . . . try bumping that up.

Hi Mark, thanks for that. The AudioMemory was indeed to low. However ... it made no difference. The program still crashes at the exact same location with the same message. I increased from 8 to 16 to 32, then 128 just to be sure. No change, still crashed.

Thanks,
Craig
 
Note: I tried a newer Teensyduino, I think it was 1.58, but it wouldn't compile anything that used the SdFat library, reporting that it was finding the wrong version of the SD library.

Before we spend a lot more time on this, install Teensyduino 1.57 and fix this SD & SdFat problem first! Let's dig into this null pointer problem only when we're all using the same set of code.

This SdFat library override has been a real problem with all versions in 1.54, but only just recently we have code which detects the issue and gives you that error. By reverting to an older version, you're not fixing the problem, you're only going back to older code where it's not (easily) visible.

The real problem is you have a copy of SdFat that's overriding the copy Teensyduino installed. Arduino should give you messages about detecting duplicate libraries, with the full pathname. The library with a path like {Arduino}/hardware/teensy/avr/libraries/SdFat is the correct library to use. Delete or move the other(s) so Arduino doesn't use them.

This may or may not actually fix the problem. But please get to version 1.57 and check any messages about duplicate libraries carefully to make sure Arduino is really using all the "{Arduino}/hardware/teensy/avr/libraries" pathnames. Or if you turn on verbose output in File > Preferences, Arduino IDE will show you the full pathnames even without errors or duplicates. That way we can all be sure we're really using the same software with the libraries Teensyduino installed, before diving deeper into this problem....
 
Before we spend a lot more time on this, install Teensyduino 1.57 and fix this SD & SdFat problem first! ...
The real problem is you have a copy of SdFat that's overriding the copy Teensyduino installed. Arduino should give you messages about detecting duplicate libraries, with the full pathname. The library with a path like {Arduino}/hardware/teensy/avr/libraries/SdFat is the correct library to use. Delete or move the other(s) so Arduino doesn't use them.

Thanks ... in fact I already spent almost a full day trying to sort this out, but I'll try again. The problem is that there's no documentation on stuff that (to experienced users) may seem obvious. The output lists all of the places it finds the SD library, but they're in the code-relocation (anti-hacking sandbox) subdirectory on the Macintosh, places like:

Code:
 /var/folders/9t/cb_zcrt138n3z64mj13z74k80000gn/T/arduino_build_734191/

Tracking that back to the actual directories proved difficult. It took me hours of googling before I stumbled on the $HOME/Libraries/Arduino15 directory ... and do I delete the extra SD files from that? Or is there another set of libraries in /Library, or maybe in the Teensyduino.app folder? (As an old Linux programmer, I miss the days of search paths and such!)

Now that I've been through that (and failed), maybe I can try again and get at it faster ...

Thanks!
Craig
 
In Arduino click Tools > Manage Libraries and search for SdFat. If it says a copy is installed, use the Library Manager to uninstall it. That may or may not be enough, but it's the easy thing to try first.
 
Problem solved ... thanks to all for your help. Happily, Arduino 2.0.0 was released this week, and I was able to install it. The problem of selecting the wrong SD library went away, and the crash disappeared too (I also increased the AudioMemory() setting, which was too low.)

I have to add that I very much appreciate the fast responses to my question. It really makes a difference.

Craig
 
Back
Top