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

Adafruit Ethernet, Motor, Proto, Wave, Datalogger, GPS Shields - etc!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
Vincent14
 
Posts: 17
Joined: Sun Jan 20, 2013 2:49 pm

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

Post by Vincent14 »

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

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

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?

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

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

Post by adafruit_support_bill »

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.

Vincent14
 
Posts: 17
Joined: Sun Jan 20, 2013 2:49 pm

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

Post by Vincent14 »

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?

User avatar
adafruit_support_bill
 
Posts: 88091
Joined: Sat Feb 07, 2009 10:11 am

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

Post by adafruit_support_bill »

Is there a better way to do that,
Not really. Your solution is the usual way to do it. :D

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino Shields from Adafruit”