WaveShield - How to Randomize Files on SD Card

Hi!

Any help would be most appreciated. We successfully have a waveshield running all the files on our SD card using the code below (from ladyada.com).

It runs the files in the same order every time. We need the files to run at random.

Does anyone know how to adjust our code so it plays the files at random?

Best.
D-Man

#include <FatReader.h>
#include <SdReader.h>
#include <avr/pgmspace.h>
#include "WaveUtil.h"
#include "WaveHC.h"

SdReader card; // This object holds the information for the card
FatVolume vol; // This holds the information for the partition on the card
FatReader root; // This holds the information for the filesystem on the card

uint8_t dirLevel; // indent level for file/dir names (for prettyprinting)
dir_t dirBuf; // buffer for directory reads

WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time

// Function definitions (we define them here, but the code is below)
void lsR(FatReader &d);
void play(FatReader &dir);

void setup() {
Serial.begin(9600); // set up Serial library at 9600 bps for debugging

putstring_nl("\nWave test!"); // say we woke up!

putstring("Free RAM: "); // This can help with debugging, running out of RAM is bad
Serial.println(freeRam());

// Set the output pins for the DAC control. This pins are defined in the library
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);

// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
if (!card.init()) { //play with 8 MHz spi (default faster!)
putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
sdErrorCheck();
while(1); // then 'halt' - do nothing!
}

// enable optimize read - some cards may timeout. Disable if you're having problems
card.partialBlockRead(true);

// Now we will look for a FAT partition!
uint8_t part;
for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
if (vol.init(card, part))
break; // we found one, lets bail
}
if (part == 5) { // if we ended up not finding one :frowning:
putstring_nl("No valid FAT partition!");
sdErrorCheck(); // Something went wrong, lets print out why
while(1); // then 'halt' - do nothing!
}

// Lets tell the user about what we found
putstring("Using partition ");
Serial.print(part, DEC);
putstring(", type is FAT");
Serial.println(vol.fatType(),DEC); // FAT16 or FAT32?

// Try to open the root directory
if (!root.openRoot(vol)) {
putstring_nl("Can't open root dir!"); // Something went wrong,
while(1); // then 'halt' - do nothing!
}

// Whew! We got past the tough parts.
putstring_nl("Files found:");
dirLevel = 0;
// Print out all of the files in all the directories.
lsR(root);
}

//////////////////////////////////// LOOP
void loop() {
root.rewind();
play(root);
}

/////////////////////////////////// HELPERS

// this handy function will return the number of bytes currently free in RAM, great for debugging!
int freeRam(void)
{
extern int __bss_end;
extern int *__brkval;
int free_memory;
if((int)__brkval == 0) {
free_memory = ((int)&free_memory) - ((int)&__bss_end);
}
else {
free_memory = ((int)&free_memory) - ((int)__brkval);
}
return free_memory;
}

/*

  • print error message and halt if SD I/O error, great for debugging!
    /
    void sdErrorCheck(void)
    {
    if (!card.errorCode()) return;
    putstring("\n\rSD I/O error: ");
    Serial.print(card.errorCode(), HEX);
    putstring(", ");
    Serial.println(card.errorData(), HEX);
    while(1);
    }
    /
  • print dir_t name field. The output is 8.3 format, so like SOUND.WAV or FILENAME.DAT
    */
    void printName(dir_t &dir)
    {
    for (uint8_t i = 0; i < 11; i++) { // 8.3 format has 8+3 = 11 letters in it
    if (dir.name == ' ')
  • continue; // dont print any spaces in the name*
  • if (i == 8)*
  • Serial.print('.'); // after the 8th letter, place a dot*
    _ Serial.print(dir.name*); // print the n'th digit*_
    * }*
    * if (DIR_IS_SUBDIR(dir))
    _
    Serial.print('/'); // directories get a / at the end*_
    }
    /*
    * list recursively - possible stack overflow if subdirectories too nested
    */
    void lsR(FatReader &d)
    {
    * int8_t r; // indicates the level of recursion*

* while ((r = d.readDir(dirBuf)) > 0) { // read the next file in the directory*
* // skip subdirs . and ..*
* if (dirBuf.name[0] == '.')*
* continue;*

* for (uint8_t i = 0; i < dirLevel; i++)
_
Serial.print(' '); // this is for prettyprinting, put spaces in front*_
* printName(dirBuf); // print the name of the file we just found*
* Serial.println(); // and a new line*

* if (DIR_IS_SUBDIR(dirBuf)) { // we will recurse on any direcory*
* FatReader s; // make a new directory object to hold information*
* dirLevel += 2; // indent 2 spaces for future prints*
* if (s.open(vol, dirBuf))*
* lsR(s); // list all the files in this directory now!*
* dirLevel -=2; // remove the extra indentation*
* }*
* }*
* sdErrorCheck(); // are we doign OK?*
}
/*
* play recursively - possible stack overflow if subdirectories too nested
*/
void play(FatReader &dir)
{
* FatReader file;*
* while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time*
* // skip . and .. directories*
* if (dirBuf.name[0] == '.')*
* continue;*

* Serial.println(); // clear out a new line*

* for (uint8_t i = 0; i < dirLevel; i++)
_
Serial.print(' '); // this is for prettyprinting, put spaces in front*_
* if (!file.open(vol, dirBuf)) { // open the file in the directory*
* Serial.println("file.open failed"); // something went wrong :(*
* while(1); // halt*
* }*

* if (file.isDir()) { // check if we opened a new directory*
* putstring("Subdir: ");*
* printName(dirBuf);*
* dirLevel += 2; // add more spaces*
* // play files in subdirectory*
* play(file); // recursive!*
* dirLevel -= 2; *
* }*
* else {*
* // Aha! we found a file that isnt a directory*
* putstring("Playing "); printName(dirBuf); // print it out*
* if (!wave.create(file)) { // Figure out, is it a WAV proper?*
* putstring(" Not a valid WAV"); // ok skip it*
* } else {*
* Serial.println(); // Hooray it IS a WAV proper!*
* wave.play(); // make some noise!*

* while (wave.isplaying) { // playing occurs in interrupts, so we print dots in realtime*
* putstring(".");*
* delay(100);*
* }*
* sdErrorCheck(); // everything OK?*
// if (wave.errors)Serial.println(wave.errors); // wave decoding errors
* }*
* }*
* }*
}

Step #1: In the lsR function, count the number of files. You'll need a global variable to store the count.

Thanks so much for the help! I'm fairly new to arduino and processing, so I'm not familiar with the specific strings of code that follow from your suggestion. .

One patch I've been playing with is the following:

int i[7]={0,1,2,3,4,5,6};
for (int i = 0; i < 30; i = i + 1)

There are seven files on the SD card, and we labeled them 0 through 6. I've been trying to work this patch into the code from my previous post, but to no avail.

I have, on the other hand, managed to insert this patch into another code, and have seem to be getting random play successfully.

    #include <FatReader.h>
    #include <SdReader.h>
    #include <avr/pgmspace.h>
    #include "WaveUtil.h"
    #include "WaveHC.h"


    SdReader card;    // This object holds the information for the card
    FatVolume vol;    // This holds the information for the partition on the card
    FatReader root;   // This holds the information for the filesystem on the card
    FatReader f;      // This holds the information for the file we're play

    WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

    #define DEBOUNCE 100  // button debouncer

    // this handy function will return the number of bytes currently free in RAM, great for debugging!   
    int freeRam(void)
    {
      extern int  __bss_end;
      extern int  *__brkval;
      int free_memory;
      if((int)__brkval == 0) {
        free_memory = ((int)&free_memory) - ((int)&__bss_end);
      }
      else {
        free_memory = ((int)&free_memory) - ((int)__brkval);
      }
      return free_memory;
    }

    void sdErrorCheck(void)
    {
      if (!card.errorCode()) return;
      putstring("\n\rSD I/O error: ");
      Serial.print(card.errorCode(), HEX);
      putstring(", ");
      Serial.println(card.errorData(), HEX);
      while(1);
    }

    void setup() {
      // set up serial port
      Serial.begin(9600);
      putstring_nl("WaveHC with 6 buttons");
     
       putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
      Serial.println(freeRam());      // if this is under 150 bytes it may spell trouble!
     
      // Set the output pins for the DAC control. This pins are defined in the library
      pinMode(2, OUTPUT);
      pinMode(3, OUTPUT);
      pinMode(4, OUTPUT);
      pinMode(5, OUTPUT);

      // pin13 LED
      pinMode(13, OUTPUT);

      // enable pull-up resistors on switch pins (analog inputs)
      digitalWrite(14, HIGH);
      digitalWrite(15, HIGH);
      digitalWrite(16, HIGH);
      digitalWrite(17, HIGH);
      digitalWrite(18, HIGH);
      digitalWrite(19, HIGH);

      //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
      if (!card.init()) {         //play with 8 MHz spi (default faster!) 
        putstring_nl("Card init. failed!");  // Something went wrong, lets print out why
        sdErrorCheck();
        while(1);                            // then 'halt' - do nothing!
      }
     
      // enable optimize read - some cards may timeout. Disable if you're having problems
      card.partialBlockRead(true);

    // Now we will look for a FAT partition!
      uint8_t part;
      for (part = 0; part < 5; part++) {     // we have up to 5 slots to look in
        if (vol.init(card, part))
          break;                             // we found one, lets bail
      }
      if (part == 5) {                       // if we ended up not finding one  :(
        putstring_nl("No valid FAT partition!");
        sdErrorCheck();      // Something went wrong, lets print out why
        while(1);                            // then 'halt' - do nothing!
      }
     
      // Lets tell the user about what we found
      putstring("Using partition ");
      Serial.print(part, DEC);
      putstring(", type is FAT");
      Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
     
      // Try to open the root directory
      if (!root.openRoot(vol)) {
        putstring_nl("Can't open root dir!"); // Something went wrong,
        while(1);                             // then 'halt' - do nothing!
      }
     
      // Whew! We got past the tough parts.
      putstring_nl("Ready!");
    }
        
    void loop() {
       int i[7]={0,1,2,3,4,5,6};
       for (int i = 0; i < 30; i = i + 1)
      //putstring(".");            // uncomment this to see if the loop isnt running
      switch (check_switches()) {
        case 1:
        {
       switch (i)
       {
          case 0: playcomplete("0.WAV"); 
          case 1: playcomplete("2.WAV"); 
          case 2: playcomplete("3.WAV"); 
          case 3: playcomplete("4.WAV"); 
          case 4: playcomplete("5.WAV"); 
          case 5: playcomplete("6.WAV");
          case 6: playcomplete("1.WAV"); 
    
       }
    }
       
      }
    }

    byte check_switches()
    {
      static byte previous[6];
      static long time[6];
      byte reading;
      byte pressed;
      byte index;
      pressed = 0;

      for (byte index = 0; index < 6; ++index) {
        reading = digitalRead(14 + index);
        if (reading == LOW && previous[index] == HIGH && millis() - time[index] > DEBOUNCE)
        {
          // switch pressed
          time[index] = millis();
          pressed = index + 1;
          break;
        }
        previous[index] = reading;
      }
      // return switch number (1 - 6)
      return (pressed);
    }


    // Plays a full file from beginning to end with no pause.
    void playcomplete(char *name) {
      // call our helper to find and play this name
      playfile(name);
      while (wave.isplaying) {
      // do nothing while its playing
      }
      // now its done playing
    }

    void playfile(char *name) {
      // see if the wave object is currently doing something
      if (wave.isplaying) {// already playing something, so stop it!
        wave.stop(); // stop it
      }
      // look in the root directory and open the file
      if (!f.open(root, name)) {
        putstring("Couldn't open file "); Serial.print(name); return;
      }
      // OK read the file and turn it into a wave object
      if (!wave.create(f)) {
        putstring_nl("Not a valid WAV"); return;
      }
     
      // ok time to play! start playback
      wave.play();
    }

The only problem here is our project doesn't use a switch. We are trying to remove the switch from this new code above, so the files play as soon as it is powered. Haven't gotten it yet.

Any help would be greatly appreciated. Many thanks,
D-Man