Memorial day is 05/25/2015 – Orders will ship out on May 26th. There will not be any deliveries or shipping on Monday. Any order placed after 11am ET on Friday May 22nd will not start to ship out until Tuesday May 26th.

Play a random file from a directory (WaveShield, WaveHC)
Moderators: adafruit_support_bill, adafruit

Play a random file from a directory (WaveShield, WaveHC)

by Vincent14 on Fri Feb 15, 2013 7:32 am

Hello, I'm french

I've put some files in a folder on my SD card. I've wrote a function using the folder name, and now my goal is to be able to select a random file then play it on the Wave Shield.

Unfortunatly, I meet a problem with my algorythm:
Code: Select all | TOGGLE FULL SIZE
dir_t dirBuf;
int folderSize = 0;
while (folder.readDir(dirBuf) > 0)
{
   folderSize ++;
}

folder.rewind();

int randomFile = random(1, folderSize+1);   //random() exclude the max value

int selected = 1;      //There is at least 1 file to play

while (folder.readDir(dirBuf) > 0)
{
   if (selected == randomFile)
   {
      /*
       * Now, translate the dirBuf.name (8.3 format) into
       * a char array comaptible with FatReader open() function
       *
       * Count real characters contained in dirBuf.name:
       * 8.3 mean 8 characters for name and 3 for extension
       * if the file name is under 8 characters, some spaces are added
       */

      int fileNameLength = 1;   //dirBuf.name does not contain the dot
      for (int i = 0; i < 11; i++) //11 because a 8.3 file name has 8+3 characters
      {
         if (dirBuf.name[i] == ' ')
            continue;
         else
            fileNameLength++;
      }
      
      char fileName[fileNameLength];
      int letters = 0;
      for (int i = 0; i < 11; i++)
      {
         if (dirBuf.name[i] == ' ')
            continue;
         if (i == 8)
         {
            fileName[letters] = '.';
            letters++;
         }
         fileName[letters] = dirBuf.name[i];
         letters++;
      }
      
      Serial.println(sizeof(fileName)/sizeof(char));
      Serial.println(fileName);
      
      if (!file.open(folder, fileName))
      {
         error("open the file has failed");
      }
      
      break;
   }
   
   selected++;
}


In the terminal, I get an error very incredible, then the size of the table is smaller than the number of characters that the variable prints! Look at this:

Code: Select all | TOGGLE FULL SIZE
Searching for Serial monitor (picocom) ... /usr/bin/picocom
Guessing serial port ... /dev/ttyACM0
picocom v1.7

port is        : /dev/ttyACM0
flowcontrol    : none
baudrate is    : 9600
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : yes
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        :
omap is        :
emap is        : crcrlf,delbs,

Terminal ready
8
R2_D.WAVR2_D    WAV
Error: open the file has failed


The file name is however "R2_D.WAV" (8 characters) but I can't understand why "R2_D WAV " is added at the end of my array?

I've tried many things, like putting a println just before and after (to see if a loop could be the reason), or making my own "for()" instead of using println on the array.

I can't understand what happening, could you help me?
Vincent14
 
Posts: 17
Joined: Sun Jan 20, 2013 2:49 pm

Re: Play a random file from a directory (WaveShield, WaveHC)

by adafruit_support_bill on Fri Feb 15, 2013 7:50 am

Strings in C require a null character at the end for termination. The extra characters at the end are probably whatever is in memory following your "filename" array.

adafruit_support_bill
 
Posts: 37526
Joined: Sat Feb 07, 2009 10:11 am

Re: Play a random file from a directory (WaveShield, WaveHC)

by Vincent14 on Fri Feb 15, 2013 9:34 am

I just changed the initialization of "int fileNameLength" to 2 and added "fileName[fileNameLength-1] = NULL;" after the second "for", and it has worked! Thanks very much!

Is there a better way to do that, according to you?
Vincent14
 
Posts: 17
Joined: Sun Jan 20, 2013 2:49 pm

Re: Play a random file from a directory (WaveShield, WaveHC)

by adafruit_support_bill on Fri Feb 15, 2013 9:44 am

Is there a better way to do that,

Not really. Your solution is the usual way to do it. :D

adafruit_support_bill
 
Posts: 37526
Joined: Sat Feb 07, 2009 10:11 am