Play a Folder of Music Randomly

This is a holiday example. The sketch plays all the songs in a folder randomly.

This sketch randomly plays all the songs in a folder. You can control the volume, play/pause/stop/next, and ff/rew using serial input (from the IDE).

Throw a bunch of your favorite holiday songs in a folder named “XMAS” on your SD card, stick it in the rMP3, and run the sketch.

rMP3_Xmas.pde
#include <RogueSD.h>
#include <RogueMP3.h>
#include <NewSoftSerial.h>
 
NewSoftSerial rmp3_serial(6, 7);
 
RogueMP3 rmp3(rmp3_serial);
RogueSD filecommands(rmp3_serial);
 
int numberOfSongs;
int lastSong;
char path[96];
 
const char *directory = "/XMAS";
 
 
void setup()
{
  Serial.begin(9600);
 
  Serial.println("Merry Xmas!");
 
  rmp3_serial.begin(9600);
 
  rmp3.sync();
  rmp3.stop();
 
  filecommands.sync();
 
  // mix up our random number generator
  randomSeed(analogRead(0));
 
  // get the number of songs available
  strcpy(path, directory);
  strcat(path, "/");
  strcat(path, "*.mp3");  // we have to do this because the IDE thinks that "/*" needs to be terminated everywhere
 
  numberOfSongs = filecommands.filecount(path);
 
  lastSong = -1;
}
 
 
 
void playNextSong()
{
  uint8_t s, i;
  char filename[80];
  char path[96];
  int nextSong = 0;
 
  if (numberOfSongs > 0)
  {
    // Select our next song randomly
    if (numberOfSongs > 2)
    {
      do
        nextSong = random(numberOfSongs);
      while (nextSong == lastSong);
    }
    else if (numberOfSongs == 2)
    {
      // we only have two songs
      if (lastSong == 0)
        nextSong = 1;
      else
        nextSong = 0;
    }
 
    // now, get our file name from file list
 
    filecommands.opendir(directory);
 
    for (i = 0; i <= nextSong; i++)
    {
      filecommands.readdir(filename, "*.mp3");
    }
 
    strcpy(path, directory);
    strcat(path, "/");
    strcat(path, filename);
 
    rmp3.playfile(path);
 
    Serial.print("Playing: ");
    Serial.println(path);
 
    lastSong = nextSong;
  }
  else
  {
    Serial.println("No files to play.");
  }
}
 
 
void loop()
{
  char c;
  uint8_t i;
  uint8_t lastvolume = 20;
  int16_t newtime;
 
  playbackinfo playinfo;
 
  char status = 'S';
  uint8_t playing = 1;
  uint8_t volume = 20;
  uint8_t boostOn = false;
 
  volume = rmp3.getvolume();  // this only gets the left volume
 
  playNextSong();
 
  while(1)
  {
    while(!Serial.available())
    {
      // we should do fancy stuff like flash lights on our Xmas tree here!
      // got lots of time!
      delay(200);
 
      status = rmp3.getplaybackstatus();
      playinfo = rmp3.getplaybackinfo();
 
      if (status == 'S' && playing)
        playNextSong();
    }
 
    // OOH!! got a character!
    c = Serial.read();
 
    switch(c)
    {
      case 'p':
        // pause
        if(status == 'D')
        {
          // fade in
          rmp3.playpause();
          rmp3.fade(volume, 400);
        }
        else if(status == 'P')
        {
          // fade out
          rmp3.fade(100, 400);
          rmp3.playpause();
        }
        else
        {
          // start playing
          playNextSong();
          playing = 1;
        }
        break;
      case 's':
        rmp3.stop();
        playing = 0;
        break;
      case 'n':
        playNextSong();
        playing = 1;
        break;
 
      case 'e':
        if(boostOn)
        {
          rmp3.setboost(0);
          boostOn = false;
        }
        else
        {
          rmp3.setboost(8, 6, 7, 3);
          boostOn = true;
        }
        break;
 
      case 'a':
        // jump back 5 seconds
        newtime = playinfo.position - 5;
        if (newtime < 0) newtime = 0;
        rmp3.jump(newtime);
        break;
 
      case 'd':
        // jump forward 5 seconds
        rmp3.jump(playinfo.position + 5);
        break;
 
      case 'k':
        if(volume < 254) volume++;
        if(status != 'D') rmp3.setvolume(volume);
        break;
 
      case 'i':
        if(volume > 0) volume--;
        if(status != 'D') rmp3.setvolume(volume);
        break;
 
    }
  }
}

Discussion

Jerry Wishum, 2010/09/14 18:22

Last Christmas my Arduino ran our Christmas lights using AC relay (stage light dimmers). I guess this Christmas there will be music too.

test50.102.203.141, 2012/04/16 23:11

Okay! So has anyone been able to get the libraries to work with Arduino 1.0?

I've been searching the forums on arduino.cc and reading all the docs I can get my hands on but nothing so far.

Thanks Thomas

You could leave a comment if you were logged in.
  • Bookmark at
  • Bookmark "Play a Folder of Music Randomly" at del.icio.us
  • Bookmark "Play a Folder of Music Randomly" at Digg
  • Bookmark "Play a Folder of Music Randomly" at Google
  • Bookmark "Play a Folder of Music Randomly" at StumbleUpon
  • Bookmark "Play a Folder of Music Randomly" at Technorati
  • Bookmark "Play a Folder of Music Randomly" at Facebook
  • Bookmark "Play a Folder of Music Randomly" at Twitter
  • Bookmark "Play a Folder of Music Randomly" at Slashdot
  • Bookmark "Play a Folder of Music Randomly" at Yahoo! Bookmarks
  • Bookmark "Play a Folder of Music Randomly" at Furl
  • Bookmark "Play a Folder of Music Randomly" at Reddit
  • Bookmark "Play a Folder of Music Randomly" at Ask
  • Bookmark "Play a Folder of Music Randomly" at BlinkList
  • Bookmark "Play a Folder of Music Randomly" at blogmarks
  • Bookmark "Play a Folder of Music Randomly" at Ma.gnolia
  • Bookmark "Play a Folder of Music Randomly" at Netscape
  • Bookmark "Play a Folder of Music Randomly" at ppnow
  • Bookmark "Play a Folder of Music Randomly" at Rojo
  • Bookmark "Play a Folder of Music Randomly" at Shadows
  • Bookmark "Play a Folder of Music Randomly" at Simpy
  • Bookmark "Play a Folder of Music Randomly" at Socializer
  • Bookmark "Play a Folder of Music Randomly" at Spurl
  • Bookmark "Play a Folder of Music Randomly" at Tailrank
  • Bookmark "Play a Folder of Music Randomly" at Live Bookmarks
  • Bookmark "Play a Folder of Music Randomly" at Wists
  • Bookmark "Play a Folder of Music Randomly" at Yahoo! Myweb
  • Bookmark "Play a Folder of Music Randomly" at BobrDobr
  • Bookmark "Play a Folder of Music Randomly" at Memori
  • Bookmark "Play a Folder of Music Randomly" at Faves
  • Bookmark "Play a Folder of Music Randomly" at Favorites
  • Bookmark "Play a Folder of Music Randomly" at Newsvine
  • Bookmark "Play a Folder of Music Randomly" at myAOL
  • Bookmark "Play a Folder of Music Randomly" at Fark
  • Bookmark "Play a Folder of Music Randomly" at RawSugar
  • Bookmark "Play a Folder of Music Randomly" at LinkaGoGo
  • Bookmark "Play a Folder of Music Randomly" at Mister Wong
  • Bookmark "Play a Folder of Music Randomly" at Wink
  • Bookmark "Play a Folder of Music Randomly" at BackFlip
  • Bookmark "Play a Folder of Music Randomly" at Diigo
  • Bookmark "Play a Folder of Music Randomly" at Segnalo
  • Bookmark "Play a Folder of Music Randomly" at Netvouz
  • Bookmark "Play a Folder of Music Randomly" at DropJack
  • Bookmark "Play a Folder of Music Randomly" at Feed Me Links
  • Bookmark "Play a Folder of Music Randomly" at funP
  • Bookmark "Play a Folder of Music Randomly" at HEMiDEMi
code/play_folder_of_music_randomly.txt · Last modified: 2009/12/18 01:34 by bhagman
Valid CSS Recent changes RSS feed Valid XHTML 1.0