Teensy + Audio board noise on polyphonic WAV playback

Status
Not open for further replies.

Claptrap

Member
I have a Teensy 3.2 & an audio shield rev B. I soldered headers for the connections between the boards, that seems to all be correct. The SDTest and WAVFilePlayer examples work OK.
Arduino 1.6.5 with Teensyduino 1.25 installed on OS X 10.9.5 is used for programming.

The aim is to have a background sound that loops (actually two 300MB, 30 minute WAVs).
6 buttons trigger the shield to play short sounds that also set an output to activate LEDs during playback (mosfets will handle that). I'm testing with a single LED at the moment (pin 16).

The buttons are setup with hardware debouncing (via inverting Schmitt trigger). As covered at http://www.jeremyblum.com/2011/03/07/arduino-tutorial-10-interrupts-and-hardware-debouncing/

I'm using interrupts for handling the inputs.

At the moment the audio keeps failing to work correctly - the sound randomly plays a 'tone' straight after a button press & the teensy needs to be powered off to stop it. The background sound ceases playback too, it will reboot via USB. There is a brief burst of 'white noise' before the failure.
It seems to be less of an issue if I turn down the volume, however I can't see anything wrong with my source WAV's they appear to be encoded fine without signs of clipping.

Tried so far...
  • I tried to use different switches with software debouncing the the same issue appeared.
  • Converting WAVs & trying different naming on the SD card, the issue has appeared with the SDTEST files that are linked on PJRC.com
  • Formatting SD as FAT 32 (on OS X) and FAT 16 (on Linux).
  • Tried 3 SD cards, a class 4 & two class 10 cards (SD card test results in ZIP file).
  • Powered via USB from Mac
  • Powered via LM317 supply, run from a 12V DC mains supply.
  • Disabling serial output has no effect.
  • Line out & headphones from the audio board have the issue - I'm monitoring sound via iPod headphones or a 4 ohm unpowered 0.5W speaker (is this too much for the output?).
  • Tried using individual AudioPlaySdWav objects for each WAV.


I saw a mention of the audio library version as a possible issue in another thread here, I have the 'built in' 1.2.0 installed and 1.25 of the Teensyduino library, could that be an issue? Please point me to sources if that needs to be updated. It's a little unclear to me which pieces are from Arduino & which are from Teensyduino libraries.


I have a Sandisk Ultra SD Card on order & will test again when it arrives.


What can I do to debug this? Does it seem like a possible hardware fault? It's my first Teensy & audio board. Has anyone ever heard this noise from these components.


Example of failure - NOTE this is mp3 is clipped as the Teensy is powered on & off so keep your sound levels low or ask me to edit that out, it happens twice in this example. One button was pressed until it failed (the 'Fox call' is the foreground sound). This was using a second version of the Schmitt trigger on a breadboard.
https://dl.dropboxusercontent.com/u/59660/Audioboard sound issue/Failing-sound.mp3

Shortened WAV files (49MB)
https://dl.dropboxusercontent.com/u/59660/Audioboard sound issue/SD Card.zip

Debounce layout (breadboard & schematic).https://dl.dropboxusercontent.com/u/59660/Audioboard sound issue/hardware debounce_bb.jpg
https://dl.dropboxusercontent.com/u/59660/Audioboard sound issue/hardware debounce_schem.jpg

SD Card read test results.
https://dl.dropboxusercontent.com/u... issue/SD Card tests/SD2-8GBTranscend-C10.png
https://dl.dropboxusercontent.com/u...d issue/SD Card tests/SD1-8GB-Kingston-C4.png
https://dl.dropboxusercontent.com/u/59660/Audioboard sound issue/SD Card tests/Kingston-8GB-C10.png


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

AudioPlaySdWav           playSdWav1;   // background sound
AudioPlaySdWav           playSdWav2;  // foreground sound
AudioMixer4              mixer1;
AudioOutputI2S           audioOutput;
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord2(playSdWav1, 1, mixer1, 1);
AudioConnection          patchCord3(playSdWav2, 0, mixer1, 2);
AudioConnection          patchCord4(playSdWav2, 1, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, audioOutput, 0);
AudioConnection          patchCord6(mixer1, 0, audioOutput, 1);
AudioControlSGTL5000     sgtl5000_1;

// audio adaptor board pins
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14

// INPUTS - hardware debounced
#define BUTTON1 0
#define BUTTON2 1
#define BUTTON3 2
#define BLINKY  13 // onboard LED for status


#define LED1 16
#define LED2 17
#define LED3 20

void setup() {
  // OUTPUT for LEDs
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT);
  pinMode(BLINKY, OUTPUT);

  // INPUT Buttons
  pinMode(BUTTON1, INPUT);
  pinMode(BUTTON2, INPUT);
  pinMode(BUTTON3, INPUT);

  // Interrupts
  attachInterrupt(BUTTON1, play1, RISING);
  attachInterrupt(BUTTON2, play2, RISING);
  attachInterrupt(BUTTON3, play3, RISING);

  //Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();        // turn on the output
  sgtl5000_1.volume(0.3);

  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    // stop here, but print a message repetitively
    while (1) {
      //Serial.println("Unable to access the SD card");
      // LED feedback for SD failure without serial
      digitalWrite(BLINKY, LOW);
      delay(100);
      digitalWrite(BLINKY, HIGH);
      delay(1000);
    }
  }
}

// Avoid playing when a file is already running
void playfile(const char *filename) {
  if (! playSdWav2.isPlaying()) {
    playSdWav2.play(filename);
    delay(5);
  }
}

void play1() {
  playfile("FOXB.WAV");
}

void play2() {
  playfile("HHOGB.WAV");
}

void play3() {
  playfile("OWLB.WAV");
}

void playbackground() {
  if (!playSdWav1.isPlaying()) {
    playSdWav1.play("/short/BG01.WAV");
    delay(10);
  }
}

void loop() {
  playbackground();
  while (playSdWav2.isPlaying()) {
    digitalWrite(LED1, HIGH);
    //Serial.println("Playing...");
  }
  digitalWrite(LED1, LOW);
}


The source is also at…
https://dl.dropboxusercontent.com/u...-forum-version/Buttonbox-v2-forum-version.ino

Apologies for the long post & thanks for getting this far :)
 
Don't use attachInterrupt. Calling the audio library play function from an interrupt is going to cause problems.

You may also need to enable the Teensy optimized SD library code. Edit SD_t3.h to enable it. The optimized code is usually needed for accessing more than one file at a time.
 
Thank you, I'll give that a go :)

I had the same sound issue with bounced pins instead of interrupts so I suspect it is the SD card reading.
 
I had the same sound issue with bounced pins instead of interrupts so I suspect it is the SD card reading.


fwiw, using interrupts in conjunction with the audio library is fine in my experience but (as Paul said) you probably don't want to actually open the file in the ISR, ie:

Code:
void play1() {
  playfile("FOXB.WAV");
}

that'll take many ms to return. i doubt "bounced pins" and this will result in the same issue. i'd try something like:

Code:
void play1() {
  _play1 = true;
}

where _play1 is a variable (typically, i suppose, declared volatile).


then you can put this into your main loop:

Code:
if (_play1) {

       playfile("FOXB.WAV");
       _play1 = false;

}

that said, why not work from the known-working example? ie SamplePlayer.ino. it does almost what you want, you'd just have to change to play_sd_wav .
 
Last edited:
Thanks mxxx, I appreciate the help.
My example is the mess I got into trying to work out why the sound kept dying. I did try the SamplePlayer.ino example & thought that the formatting of the files was the issue (SD card vs. WAV2Sketch files) I will take another look now playback is reliable.

Is there any functional reason for the underscore on the volatile variable or is it just for ease of use? My C knowledge is lacking.


Paul, I noticed that the Audio design tool is not including all requirements (http://www.pjrc.com/teensy/gui). When adding playSDWAV objects the #include <SerialFlash.h> is not included. I got the same error as another user on here…
https://forum.pjrc.com/threads/2961...error-SerialFlash-h-No-such-file-or-directory
I suspect it may help to add that if appropriate.

Thanks to both of you :)
 
AudioMemory(8) seems a little low with all the objects defined, perhaps look at AudioMemoryUsageMax() or just bump it up to 12 or 16.
 
Thanks mxxx, I appreciate the help.
My example is the mess I got into trying to work out why the sound kept dying. I did try the SamplePlayer.ino example & thought that the formatting of the files was the issue (SD card vs. WAV2Sketch files) I will take another look now playback is reliable.

Is there any functional reason for the underscore on the volatile variable or is it just for ease of use? My C knowledge is lacking.

so it's working now?

and no, no reason whatsoever, the underscore was just to differentiate the variable name from the name of the callback.
 
so it's working now?

and no, no reason whatsoever, the underscore was just to differentiate the variable name from the name of the callback.

Yes it's working fine now I have switched to using the USE_TEENSY3_OPTIMIZED_CODE in SD_t3.h. I have also switched to using the bounce library but I will get around to trying interrupts with flags as you suggested, it seems a bit more logical to me.

When I get the new SD card I'll post back with the new code & results.


cartere, I have increased the memory to 16, it seems to remain around 7 as far as I can see. I'm calling this in the main loop to view it…

Code:
void printMemory() {
  ms++ ;
  if (ms / 5000 == 1 ) {
    Serial.print("AudioMemoryUsageMax :");
    Serial.println(AudioMemoryUsageMax());
    ms = 1;
  }
}

Thanks to the input.
 
I fixed this yesterday. Just a quick followup, in case anyone sees this later or missed the thread about it yesterday.

Thanks for posting this, I had missed it, the OS X installer doesn't seem to have this fix yet,
is the source available?
Ignore that I just found your github page.
 
Last edited:
Status
Not open for further replies.
Back
Top