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

Thread: random WAV player

  1. #1
    Junior Member
    Join Date
    Feb 2016
    Posts
    9

    random WAV player

    Hi all,

    I try to build a very basic system: I have several WAV files on the SD card named SDTEST1.WAV, SDTEST2.WAV, SDTEST3.WAV etc
    I want to have the Teensy (3.2 + Audio Shield) to randomly load a file, play it and randomly load another file etc, ad libitum.
    So, I modified this code which doesn't seems to work with any of my two Teensys.

    Any idea ?
    Thanks for all !

    PHP Code:
    #include <Audio.h>
    #include <SD.h>

    long randNumber;

    AudioPlaySdWav           playSdWav1;
    AudioOutputI2S           i2s1;
    AudioConnection          patchCord1(playSdWav10i2s10);
    AudioConnection          patchCord2(playSdWav11i2s11);
    AudioControlSGTL5000     sgtl5000_1;


    void setup() {
     
      
    Serial.begin(9600);
      
    AudioMemory(8);
      
    sgtl5000_1.enable();
      
    sgtl5000_1.volume(0.5);
      
    SPI.setMOSI(7);
      
    SPI.setSCK(14);
      if (!(
    SD.begin(10))) {
        while (
    1) {
          
    Serial.println("Unable to access the SD card");
          
    delay(500);
        }
      }
      while (!
    Serial && millis() < 2500) ; // wait 2.5 sec for Arduino Serial Monitor
      
    Serial.println("begin random player");
    }


    void loop() {

        
    randNumber random(15);
        
    String myString1  "SDTEST";
        
    String myString2  ".WAV";
        
    String myFile myString1 randNumber myString2;
        
    Serial.println(myFile);
        
    playSdWav1.play(myFile.c_str());
      } 

  2. #2
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,788
    First, I‘d check if a single one of your wav files plays correctly. Perhaps, they are in a wrong format (44.1kHz, 16bit Stereo is required).

    Second, the play() method of the SdWav object is not blocking. That means that your loop() continues to run and tries to launch a new file every few nanoseconds. You’ll have to check with the isPlaying() method (don’t remember the exact name) if the current file is still playing - then do nothing, else launch a new random file.

  3. #3
    Junior Member
    Join Date
    Feb 2016
    Posts
    9
    Hi Theremingenieur,

    The WAV files are ok (I use them in another program, no problem).
    I tried to add a short delay(50) at the very end of the file but it still doesn't work...
    Any idea ??

  4. #4
    Senior Member+ Theremingenieur's Avatar
    Join Date
    Feb 2014
    Location
    Colmar, France
    Posts
    1,788
    With delay() only, you still risk to start a new file while the previous is still playing. You really should put everything which you have inside loop() into an additional if(!playSdWav1.isPlaying()){... here your loop code...} as I told you before.

  5. #5
    Junior Member
    Join Date
    Feb 2016
    Posts
    9
    So, I tried this and it seems to work great !
    Thanks Thereminingenieur !

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    long randNumber; 
    
    AudioPlaySdWav           playSdWav1; 
    AudioOutputI2S           i2s1; 
    AudioConnection          patchCord1(playSdWav1, 0, i2s1, 0); 
    AudioConnection          patchCord2(playSdWav1, 1, i2s1, 1); 
    AudioControlSGTL5000     sgtl5000_1; 
    
    
    void setup() { 
      
      Serial.begin(9600); 
      AudioMemory(8); 
      sgtl5000_1.enable(); 
      sgtl5000_1.volume(0.6); 
      SPI.setMOSI(7); 
      SPI.setSCK(14); 
      if (!(SD.begin(10))) { 
        while (1) { 
          Serial.println("Unable to access the SD card"); 
          delay(500); 
        } 
      } 
      while (!Serial && millis() < 2500) ; // wait 2.5 sec for Arduino Serial Monitor 
      Serial.println("begin random player"); 
    } 
    
    
    void loop() { 
      
      if(!playSdWav1.isPlaying()) {
        randNumber = random(1, 11); 
        String myString1  = "SDTEST"; 
        String myString2  = ".WAV"; 
        String myFile = myString1 + randNumber + myString2; 
        if(!playSdWav1.isPlaying())
        Serial.println(myFile); 
        playSdWav1.play(myFile.c_str()); 
      }
      }

Posting Permissions

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