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

Thread: Simple Wav Player

  1. #1
    Junior Member
    Join Date
    Feb 2017
    Posts
    14

    WavFilePlayer - Problems with DAC on the 3.6

    Hello All

    I'm using Teensy 3.6 without the an audio adapter (just using DAC0) trying to run WavFilePlayer example but am encountering problems:

    Code:
    // Simple WAV file player example
    //
    // Three types of output may be used, by configuring the code below.
    //
    //   1: Digital I2S - Normally used with the audio shield:
    //         http://www.pjrc.com/store/teensy3_audio.html
    //
    //   2: Digital S/PDIF - Connect pin 22 to a S/PDIF transmitter
    //         https://www.oshpark.com/shared_projects/KcDBKHta
    //
    //   3: Analog DAC - Connect the DAC pin to an amplified speaker
    //         http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
    //
    // To configure the output type, first uncomment one of the three
    // output objects.  If not using the audio shield, comment out
    // the sgtl5000_1 lines in setup(), so it does not wait forever
    // trying to configure the SGTL5000 codec chip.
    //
    // The SD card may connect to different pins, depending on the
    // hardware you are using.  Uncomment or configure the SD card
    // pins to match your hardware.
    //
    // Data files to put on your SD card can be downloaded here:
    //   http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
    //
    // This example code is in the public domain.
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioPlaySdWav           playWav1;
    // Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
    //AudioOutputI2S           audioOutput;
    //AudioOutputSPDIF       audioOutput;
    AudioOutputAnalog      audioOutput;
    
    AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
    AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
    
    //AudioControlSGTL5000     sgtl5000_1;
    
    // Use these with the audio adaptor board
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    //#define SDCARD_MOSI_PIN  7
    //#define SDCARD_SCK_PIN   14
    
    
    
    void setup() {
      Serial.begin(9600);
    
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(8);
    
    
    //  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);
    //    }
    //  }
    }
    
    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.
      playWav1.play(filename);
    
      // A brief delay for the library read WAV info
      delay(5);
    
      // Simply wait for the file to finish playing.
      while (playWav1.isPlaying()) {
       
      }
    }
    
    
    void loop() {
      playFile("SDTEST1.WAV");  // filenames are always uppercase 8.3 format
      delay(500);
    //  playFile("SDTEST2.WAV");
    //  delay(500);
    //  playFile("SDTEST3.WAV");
    //  delay(500);
    //  playFile("SDTEST4.WAV");
    //  delay(1500);
    }
    So as it is above all that happens is I get to monitor about a million serial messages a second saying "Playing SDTEST1.WAV" while I sit in silence.


    If I enable the if (!(SD.begin(SDCARD_CS_PIN))) handler, the program does nothing i.e. no serial activity to monitor, not even the error message"Unable to access SD card".

    When I run cardInfo, all is well...

    Any advice?

    Please excuse my blatant lack of wisdom on the subject.
    Last edited by FerengiOrFoe; 02-17-2017 at 01:41 PM.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    13,791
    It's never going to work with SD.begin(BUILTIN_SDCARD) commented out.

    Are you using the latest Teensyduino 1.35? Use Help > About (or Arduino > About on Mac) to check which version you have.

  3. #3
    Junior Member
    Join Date
    Feb 2017
    Posts
    14
    Quote Originally Posted by PaulStoffregen View Post
    It's never going to work with SD.begin(BUILTIN_SDCARD) commented out.

    Are you using the latest Teensyduino 1.35? Use Help > About (or Arduino > About on Mac) to check which version you have.
    Yes I'm using the latest Teensyduino 1.35. There wasn't a SD.begin(BUILTIN_SDCARD) commented out, just the if statement condition. I added the SD.begin(BUILTIN_SDCARD) to the setup and now no more serial prints, nothing happens at all. Also I'm not getting anything out of the 5v pin, do I need to code something in for that to work?

  4. #4
    Senior Member
    Join Date
    Nov 2012
    Posts
    778
    Try this code. It works when I use the I2S output instead of Analog.

    Pete

    Code:
    // Simple WAV file player example
    //
    // Three types of output may be used, by configuring the code below.
    //
    //   1: Digital I2S - Normally used with the audio shield:
    //         http://www.pjrc.com/store/teensy3_audio.html
    //
    //   2: Digital S/PDIF - Connect pin 22 to a S/PDIF transmitter
    //         https://www.oshpark.com/shared_projects/KcDBKHta
    //
    //   3: Analog DAC - Connect the DAC pin to an amplified speaker
    //         http://www.pjrc.com/teensy/gui/?info=AudioOutputAnalog
    //
    // To configure the output type, first uncomment one of the three
    // output objects.  If not using the audio shield, comment out
    // the sgtl5000_1 lines in setup(), so it does not wait forever
    // trying to configure the SGTL5000 codec chip.
    //
    // The SD card may connect to different pins, depending on the
    // hardware you are using.  Uncomment or configure the SD card
    // pins to match your hardware.
    //
    // Data files to put on your SD card can be downloaded here:
    //   http://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
    //
    // This example code is in the public domain.
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    AudioPlaySdWav           playWav1;
    // Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
    //AudioOutputI2S           audioOutput;
    //AudioOutputSPDIF       audioOutput;
    AudioOutputAnalog      audioOutput;
    
    AudioConnection          patchCord1(playWav1, 0, audioOutput, 0);
    AudioConnection          patchCord2(playWav1, 1, audioOutput, 1);
    
    AudioControlSGTL5000     sgtl5000_1;
    
    // Use these with the audio adaptor board
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    
    
    
    void setup() {
      Serial.begin(9600);
      while(!Serial);
      // Audio connections require memory to work.  For more
      // detailed information, see the MemoryAndCpuUsage example
      AudioMemory(8);
      sgtl5000_1.enable();
      sgtl5000_1.volume(.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);
        }
      }
    }
    
    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.
      playWav1.play(filename);
    
      // A brief delay for the library read WAV info
      delay(5);
    
      // Simply wait for the file to finish playing.
      while (playWav1.isPlaying()) {
       
      }
    }
    
    
    void loop() {
      playFile("SDTEST1.WAV");  // filenames are always uppercase 8.3 format
      delay(500);
    //  playFile("SDTEST2.WAV");
    //  delay(500);
    //  playFile("SDTEST3.WAV");
    //  delay(500);
    //  playFile("SDTEST4.WAV");
    //  delay(1500);
    }

  5. #5
    Senior Member
    Join Date
    Nov 2012
    Posts
    778
    I've just messed with it a bit more and found that these two statements can be removed/commented.
    Code:
      SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
    Pete

  6. #6
    Junior Member
    Join Date
    Feb 2017
    Posts
    14
    This works as in audio comes out of the DAC? I don't know anything about 12S, and was only trying to use one of the two DACs on board. Do I need to use another output pin when using this I2S method? I'd test it myself but I'm still in need of an answer about the 5v pin not working... Thanks.

  7. #7
    Senior Member
    Join Date
    Nov 2012
    Posts
    778
    This works as in audio comes out of the DAC?
    Yes, it should. Try it.

    In your code you have:
    Code:
    // Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
    //AudioOutputI2S           audioOutput;
    //AudioOutputSPDIF       audioOutput;
    AudioOutputAnalog      audioOutput;
    The uncommented statement directs the audio output to the Analog output (dac).
    All I did was change this to:
    Code:
    // Use one of these 3 output types: Digital I2S, Digital S/PDIF, or Analog DAC
    AudioOutputI2S           audioOutput;
    //AudioOutputSPDIF       audioOutput;
    //AudioOutputAnalog      audioOutput;
    so that the audio is directed to the I2S which is the audio output on the Audio board (headphone by default).
    The code works with I2S, so changing it back to use Analog should work as well, as long as you have connected something to the DAC output correctly.

    Also I'm not getting anything out of the 5v pin
    Which pin are you referring to? Vin or the 5V on the USB host port on the bottom of the board?

    Pete

  8. #8
    Junior Member
    Join Date
    Feb 2017
    Posts
    14
    Thank you Pete. I'm referring to the 5V pin on the USB host port. I thought this might supply voltage but I'm not sure what it does.

    If this code directs it to the I2S on the audio board why would audio come out of the DAC? I'm not using any audio board just the Teensy.

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    778
    Never mind about the 5V. I see that you split that question into a separate thread which has been answered.
    Wasted my time.

    Pete

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    13,791
    I'm running this code right now and listening to it play through the DAC0 pin.

    I can't know why it's not working for you, but I can absolutely confirm this code works:

    Code:
    #include <Audio.h>
    #include <Wire.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
    
    void setup() {
      Serial.begin(9600);
      AudioMemory(8);
      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);
        }
      }
    }
    
    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.
      playWav1.play(filename);
    
      // A brief delay for the library read WAV info
      delay(5);
    
      // Simply wait for the file to finish playing.
      while (playWav1.isPlaying()) {
       
      }
    }
    
    
    void loop() {
      playFile("SDTEST1.WAV");  // filenames are always uppercase 8.3 format
      delay(500);
    //  playFile("SDTEST2.WAV");
    //  delay(500);
    //  playFile("SDTEST3.WAV");
    //  delay(500);
    //  playFile("SDTEST4.WAV");
    //  delay(1500);
    }

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    13,791
    If you're running that code and it's still not playing, the problem must be hardware related. Perhaps the files aren't really on your SD card? Or maybe they've been renamed slightly?

    Or maybe something's connected incorrectly. Here's how I tested. The red cable goes to an amplified speaker. The nearby clutter is from the USB host work I've been doing lately. It's still playing as I write this, and I must say of the 4 test files, SDTEST1.WAV gets old quickly when looping over and over.....

    Click image for larger version. 

Name:	dactest.jpg 
Views:	16 
Size:	145.7 KB 
ID:	9735
    (click for full size)

  12. #12
    Junior Member
    Join Date
    Feb 2017
    Posts
    14
    Ok, got it working- code was fine. It was all just because I tried using that 5v USB pin on the back as power for the amp while board was plugged into computer. Based on serial print outs I thought there was an issue with the code. Thank you Paul for the amazing series of development boards and for the support, I have carbon proof of your busyness sitting here on my desk, playing this test WAV so I really appreciate the personal touch and the time it takes to offer this support. You the man!

Posting Permissions

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