Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 4 of 4

Thread: Erratic static in playback

  1. #1
    Junior Member
    Join Date
    Nov 2017

    Erratic static in playback

    I'm aware the code is awful. This is derived, mostly, from the PlaySdWav example. I have four samples.

    If I throttle playback more (increase the delay in the "loop()" function), I get flawless playback of all the samples. You'll note that I have four AudioPlaySdWav devices defined. If I use all four of them (use %4 instead of %2 in the playFile function), I get static much more often, unless I throttle playback.

    But I can also make it through several loops through playing all the sound files with no static at all. AudioMemoryUsage was reporting that 0-5 blocks were in use, it seems like 20 should be plenty. The SD card is a SanDisk "ultra".

    As an experiment, I also tried with the SDTEST?.WAV files from the audio library documentation. With those, I get static pretty much constantly and right away if I'm trying to use more than one WAV playback. On further study, this looks like it's probably SD card performance. The SD card test says:

    Overall speed = 0.26 Mbyte/sec
    Worst block time = 15.16 ms
    522.55% of audio frame time

    ... But I'm going to post this anyway in case I've done something else obviously stupid.

    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    AudioPlaySdWav           playWav1, playWav2, playWav3, playWav4;
    AudioMixer4              mixerL;
    AudioMixer4              mixerR;
    AudioOutputI2S           audioOutput;
    AudioConnection          patchCord1(playWav1, 0, mixerL, 0);
    AudioConnection          patchCord2(playWav1, 1, mixerR, 0);
    AudioConnection          patchCord3(playWav2, 0, mixerL, 1);
    AudioConnection          patchCord4(playWav2, 1, mixerR, 1);
    AudioConnection          patchCord5(playWav3, 0, mixerL, 2);
    AudioConnection          patchCord6(playWav3, 1, mixerR, 2);
    AudioConnection          patchCord7(playWav4, 0, mixerL, 3);
    AudioConnection          patchCord8(playWav4, 1, mixerR, 3);
    AudioConnection          patchCordA(mixerL, 0, audioOutput, 0);
    AudioConnection          patchCordB(mixerR, 0, audioOutput, 1);
    AudioControlSGTL5000     sgtl5000_1;
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    void setup() {
      if (!(SD.begin(SDCARD_CS_PIN))) {
        // stop here, but print a message repetitively
        while (1) {
          Serial.println("Unable to access the SD card");
    int counter = 0;
    AudioPlaySdWav *outs[4] = { 
      &playWav1, &playWav2, &playWav3, &playWav4 
    void playFile(const char *filename)
      while (outs[counter]->isPlaying()) {
      counter = (counter + 1) % 2;
    int samp = 0;
    void loop() {
      char name[] = "KEY0.WAV";
      name[3] = '1' + samp;
      samp = (samp + 1) % 4;
      uint8_t count = (rand() % 20) + 5;
      delay (count * 5);
    EDIT: After a bit of reading, it looks like I might want flash chips for this. Digikey says the W25Q128FV is "discontinued at digikey". But also, chips like that appear to come in many, many, packages. I am not sure exactly which package I would be looking for. 8-SOIC, 3.9 or 5.3 mm width? The traces are wide enough that those both seem likely to work. It looks like DigiKey no longer has the FV (~3V), only the FW (~1.8V).
    Last edited by seebs; 12-12-2017 at 01:35 AM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Try editing SD_t3.h to enable the Teensy optimizations. Does that help?

  3. #3
    Junior Member
    Join Date
    Nov 2017
    Hmm. I do have a file called "SD_t3.h", but it doesn't look like it is getting used. In particular, if I just add a #error to it, nothing happens, so I'm guessing I'm using a different SD library... ah-hah! I found another SD library in my arduino sketch directory (??). I am also sort of confused because "SD_t3.h" is in hardware/teensy/avr/libraries/SD, and why is it in an AVR path if it's ARM?

    So, if I just move the other one out of the way, so I'm using your copy of the library, I get a significant performance improvement:

    Overall speed = 0.63 Mbyte/sec
    Worst block time = 6.88 ms
    237.13% of audio frame time

    If I uncomment the line for the Teensy3 optimizations:

    SdCardTest: In function 'void setup()':
    SdCardTest:34: error: 'Sd2Card' was not declared in this scope
    Sd2Card card;
    SdCardTest:35: error: 'SdVolume' was not declared in this scope
    SdVolume volume;
    [... and many more]

    On study: SD.h includes SD_t3.h where applicable, and does so outside the include guards for SD.h. SD.h includes things which would define, and use, Sd2Card. Ah-hah!

    Overall speed = 0.73 Mbyte/sec
    Worst block time = 4.43 ms
    152.86% of audio frame time

     #include "utility/ioreg.h"
    +#include <utility/SdFat.h>
    +#include <utility/SdFatUtil.h>
    this change allowed me to compile the thing, and yeah, another 100% speed increase. Nice!

  4. #4
    Junior Member
    Join Date
    Nov 2017
    So I was having really weird problems. And it turns out: If I have USE_TEENSY3_OPTIMIZED_CODE, I get severe distortion and crackles in the output. If I don't use that, but do use the teensy version of the library, things sound fine. (Test case: Playing back a single WAV file, no other code running, I just wanted to play a WAV file from something that wasn't an iphone or something for testing some audio stuff.)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts