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

Thread: Strange audio playback behavior

  1. #1
    Senior Member
    Join Date
    Oct 2019
    Posts
    174

    Strange audio playback behavior

    I'm making a soundboard that plays a file via the onboard DACs, but WAV playback from the SD card is behaving very strangely.

    I have full code below, edited down to a simple test of playing one file. When I run the code, a portion of the file is played followed by a section static on channel 0. A second attempt to play the same file does nothing. A third attempt is not reached.

    I've tried two different Teensy 3.6 units with the same results.

    The only thing I can think of is a corrupted WAV file with garbage appended and the firmware tries to read it endlessly. How could I verify?








    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>

    AudioPlaySdWav playWav1;
    // Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
    //AudioOutputI2S audioOutput;
    //AudioOutputSPDIF audioOutput;
    AudioOutputAnalog audioOutput;
    //On Teensy LC, use this for the Teensy Audio Shield:
    //AudioOutputI2Sslave audioOutput;

    AudioConnection patchCord1(playWav1, 0, audioOutput, 0);
    AudioConnection patchCord2(playWav1, 1, audioOutput, 1);
    //AudioControlSGTL5000 sgtl5000_1;

    // Use these with the Teensy Audio Shield
    //#define SDCARD_CS_PIN 10
    //#define SDCARD_MOSI_PIN 7
    //#define SDCARD_SCK_PIN 14

    // Use these with the Teensy 3.5 & 3.6 SD card
    #define SDCARD_CS_PIN BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN 11 // not actually used
    #define SDCARD_SCK_PIN 13 // not actually used

    // Use these for the SD+Wiz820 or other adaptors
    //#define SDCARD_CS_PIN 4
    //#define SDCARD_MOSI_PIN 11
    //#define SDCARD_SCK_PIN 13

    //Button pins
    int button_Pins[]={24,25,26,27,28,29,30,31,34};


    void setup() {
    Serial.begin(9600);

    // Audio connections require memory to work. For more
    // detailed information, see the MemoryAndCpuUsage example
    AudioMemory(8);

    // Comment these out if not using the audio adaptor board.
    // This may wait forever if the SDA & SCL pins lack
    // pullup resistors
    //sgtl5000_1.enable();
    //sgtl5000_1.volume(0.5);

    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");
    delay(500);
    }
    }

    for(int i;i<9;i++){
    pinMode(i,INPUT_PULLUP);

    }
    playFile("BUTTON0.WAV");
    delay(2000);
    playFile("BUTTON0.WAV");
    delay(2000);
    playFile("BUTTON0.WAV");



    /*
    attachInterrupt(digitalPinToInterrupt(button_Pins[0]),BUTTON0_ISR,FALLING);

    attachInterrupt(digitalPinToInterrupt(button_Pins[1]),BUTTON1_ISR,FALLING);
    attachInterrupt(digitalPinToInterrupt(button_Pins[2]),BUTTON2_ISR,FALLING);
    attachInterrupt(digitalPinToInterrupt(button_Pins[3]),BUTTON3_ISR,FALLING);
    attachInterrupt(digitalPinToInterrupt(button_Pins[4]),BUTTON4_ISR,FALLING);
    attachInterrupt(digitalPinToInterrupt(button_Pins[5]),BUTTON5_ISR,FALLING);
    attachInterrupt(digitalPinToInterrupt(button_Pins[6]),BUTTON6_ISR,FALLING);
    attachInterrupt(digitalPinToInterrupt(button_Pins[7]),BUTTON7_ISR,FALLING);
    attachInterrupt(digitalPinToInterrupt(button_Pins[8]),BUTTON8_ISR,FALLING);
    */

    Serial.println("Setup Complete");


    }

    void playFile(const char *filename)
    {
    Serial.print("Playing file: ");
    Serial.println(filename);

    // Start playing the file. This sketch continues to
    // run while the file plays.
    Serial.println(playWav1.play(filename));


    // A brief delay for the library read WAV info


    // Simply wait for the file to finish playing.
    while (playWav1.isPlaying()) {
    // uncomment these lines if you audio shield
    // has the optional volume pot soldered
    //float vol = analogRead(15);
    //vol = vol / 1024;
    // sgtl5000_1.volume(vol);
    }

    Serial.println("Done playing...");

    }


    void BUTTON0_ISR(){
    noInterrupts();

    playFile("BUTTON0.WAV");
    Serial.println("PLAY");
    interrupts()
    delay(2500);
    }

    void BUTTON1_ISR(){
    noInterrupts()
    playFile("BUTTON1.WAV");
    Serial.println("Button 1");
    interrupts()
    }

    void BUTTON2_ISR(){
    noInterrupts()
    playFile("BUTTON2.WAV");
    Serial.println("Button 2");
    interrupts()
    }

    void BUTTON3_ISR(){
    noInterrupts()
    playFile("BUTTON3.WAV");
    Serial.println("Button 3");
    interrupts()
    }

    void BUTTON4_ISR(){
    noInterrupts()
    playFile("BUTTON4.WAV");
    Serial.println("Button 4");
    interrupts()
    }

    void BUTTON5_ISR(){
    noInterrupts()
    playFile("BUTTON5.WAV");
    Serial.println("Button 5");
    interrupts()
    }

    void BUTTON6_ISR(){
    noInterrupts()
    playFile("BUTTON6.WAV");
    Serial.println("Button 6");
    interrupts()
    }

    void BUTTON7_ISR(){
    noInterrupts()
    playFile("BUTTON7.WAV");
    Serial.println("Button 7");
    interrupts()
    }

    void BUTTON8_ISR(){
    noInterrupts()
    Serial.println("Button 8");
    playFile("BUTTON8.WAV");
    interrupts()
    }




    void loop() {

    }

  2. #2
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    576
    The only thing I can think of is a corrupted WAV file with garbage appended and the firmware tries to read it endlessly. How could I verify?
    PJRC supplies several example audio files, you can find them here.

    Regards,
    Paul

    PS: you know about the CODE tags to make your post more pleasantly readable?
    Click image for larger version. 

Name:	Untitled.png 
Views:	7 
Size:	16.9 KB 
ID:	24939

  3. #3
    Senior Member
    Join Date
    Oct 2019
    Posts
    174
    I formatted the SD card and tried one of the files posted and it did almost the same thing. This time, the file quits playing after a fraction of a second, then garbage for a second or two. (The file is over a minute long.)

    I cleaned up the code to make it minimal.
    What else can I check on ?

    Code:
    #include <Audio.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    
    AudioPlaySdWav           playWav1;
    AudioOutputAnalog      audioOutput;
    AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
    AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
    
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN  11  // not actually used
    #define SDCARD_SCK_PIN   13  // not actually used
    
    void setup() {
      Serial.begin(9600);
      AudioMemory(8);
     
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
    
      playFile("BUTTON0.WAV");
      delay(2000);
      playFile("BUTTON0.WAV");
      delay(2000);
      playFile("BUTTON0.WAV");  
    
     Serial.println("Setup Complete");
    
    }
    
    void playFile(const char *filename)
    {
      Serial.print("Playing file: ");
      Serial.println(filename);
      Serial.println(playWav1.play(filename));  
      while (playWav1.isPlaying()) {
        
      }
    
      Serial.println("Done playing...");
      
    }
    
    void loop() {
      
    }

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,691
    I modified your code to play three of the test WAV files and output audio to the headphones. It plays the first file completely. It plays the first second or so of the next file and goes to the third which it plays completely. Odd.
    Code:
    Playing file: SDTEST1.WAV
    1
    Done playing...
    Playing file: SDTEST2.WAV      <<< Only plays this for a second or so
    1
    Done playing...
    Playing file: SDTEST3.WAV
    1
    Done playing...
    Setup Complete
    Code:
    #include <Audio.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    
    AudioPlaySdWav           playWav1;
    //AudioOutputAnalog      audioOutput;
    AudioOutputI2S      audioOutput;
    AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
    AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
    
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN  11  // not actually used
    #define SDCARD_SCK_PIN   13  // not actually used
    
    void setup() {
      Serial.begin(9600);
      AudioMemory(8);
     
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
    
      playFile("SDTEST1.WAV");
      delay(2000);
      playFile("SDTEST2.WAV");
      delay(2000);
      playFile("SDTEST3.WAV");  
    
     Serial.println("Setup Complete");
    
    }
    
    void playFile(const char *filename)
    {
      Serial.print("Playing file: ");
      Serial.println(filename);
      Serial.println(playWav1.play(filename));  
      while (playWav1.isPlaying()) {
        
      }
    
      Serial.println("Done playing...");
      
    }
    
    void loop() {
      
    }

    Pete

  5. #5
    Senior Member
    Join Date
    Apr 2020
    Location
    DFW area in Texas
    Posts
    266
    Quote Originally Posted by kdharbert View Post
    What else can I check on ?
    If you have not already loaded TD 1.54beta9, you might try that . . . it contains a fix for at least one core bug which caused a delay/hang of approximately 1.7 seconds (in my case, it caused MIDI to misbehave & put out 1.7 second "screech tones" unrelated to the MIDI commands that were being processed).

    Mark J Culross
    KD5RXT

  6. #6
    Senior Member
    Join Date
    Aug 2013
    Location
    Gothenburg, Sweden
    Posts
    419
    The part

    Code:
    for(int i;i<9;i++){
    pinMode(i,INPUT_PULLUP);
    
    }
    should it be:

    Code:
    for(int i=0;i<9;i++){
    pinMode(button_Pins[i],INPUT_PULLUP);
    
    }
    the original code sets pin 4 used as CS to input after the SD card is initialised, this could create isues.

    Also the test if the file is playing takes a few cycles to become true, I seems to remember having issues with that so needed to delay that test a few millis.

  7. #7
    Senior Member
    Join Date
    Oct 2019
    Posts
    174
    I added a 10 millisecond delay and it it helped. I now see the full file play the first time and no static. The second attempt still does nothing. The third is not reached. I get nothing on channel 1.

  8. #8
    Senior Member
    Join Date
    Oct 2019
    Posts
    174
    Wow. Between this and the below it works now. Thanks for the help.

Posting Permissions

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