random WAV player

Status
Not open for further replies.

nakatano

Member
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:
#include <Audio.h>
#include <SD.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.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(1, 5);
    String myString1  = "SDTEST";
    String myString2  = ".WAV";
    String myFile = myString1 + randNumber + myString2;
    Serial.println(myFile);
    playSdWav1.play(myFile.c_str());
  }
 
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.
 
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 ??
 
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.
 
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()); 
  }
  }
 
Status
Not open for further replies.
Back
Top