Go Down

Topic: Play Random sound file from SD card (Read 3768 times) previous topic - next topic

skuznarsky

I am looking for some help creating a function that plays a randomly generated sound from a file on an SD card using the Adafruit MP3 shield.  I am looking for the function to look something like this:

Code: [Select]

void Goal_player{
  randGoal_sound=rand(1,10); //generate a random number 1-10 (not including 10)
 
//missing code to convert the int randGoal_sound into the proper input to the musicPlayer.statPlaingFile()

musicPlayer.startPlayingFile(randGoal_sound);

}



Basically I want to generate a random number, that number corresponds to the track name on the SD card and will then be played when called upon in the main loop.  I think all I need to do is figure out how to convert from an int to the char properly? Not sure so that is why I am asking for help.

Thanks,
Scott

Grumpy_Mike

The music player needs a string containing the file name you want to play.
There are two ways to do this:-
1) Create a string of the random number and append ".mp3" to it. Your files will have to have numbers for names.

2) Have a sting array of file names, and use the random number as an index into that string array to choose the file.

skuznarsky

I would like to do this using option 1), however I am not exactly sure how to assign a string with a file name and add ".WAV" or ".mp3" to the end.  Also, I am not sure what kind of input it is looking for in the musicplayer function: musicPlayer.startPlayingFile(??????); I think it is a char or string.  Or I maybe overthinking it.

Grumpy_Mike

This is probably more sophisticated than you would produce but ....
Code: [Select]

char toPlay[11];     //  string array for file to play 00.WAV to 99999.WAV

// then pass this function a random in the range of the files you have
makeName(randNumber,0);  // generate file name in global array toPlay

void makeName(int number, int depth){  // generates a file name 0.WAV to 9999.WAV suppressing leading zeros
  if(number > 9) {
    makeName(number / 10, ++depth); // recursion
    depth--;
    number = number % 10;   // only have to deal with the next significant digit of the number
     }
  toPlay[indexToWrite] = (number & 0xf) | 0x30;
  indexToWrite++;
  if(depth > 0) return; // return if we have more levels of recursion to go
  else {  // finish off the string with the wave extesion
     toPlay[indexToWrite] = '.';
     toPlay[1+indexToWrite] = 'W';
     toPlay[2+indexToWrite] = 'A';
     toPlay[3+indexToWrite] = 'V';
     toPlay[4+indexToWrite] = '\0'; // terminator
     indexToWrite = 0; // reset pointer for next time we enter
  }
}

// Then play the file name
musicPlayer.startPlayingFile(toPlay);

skuznarsky

Thanks for the code. I am trying to get it to work; however I am not sure what the "indexToWrite" is or means.  Is this an integer that I need to declare at the beginning of my code?  not sure what it does.

Grumpy_Mike

It is a global variable which starts off at zero before you call the makeName function.

skuznarsky

Got it to work  :) , thanks for the help GM. 

Thanks,
Scott

Go Up