| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

SongUtility

Page history last edited by William 11 years, 8 months ago

/*

 * example sketch to play audio file(s) in a directory, using the mp3 library

 * for playback and the arduino sd library to read files from a microsd card.

 * pins are setup to work well for teensy 2.0. double-check if using arduino.

 * 

 * originally based on frank zhao's player: http://frank.circleofcurrent.com/

 * utilities adapted from previous versions of the functions by matthew seal.

 *

 * (c) 2011 david sirkin sirkin@cdr.stanford.edu

 *          akil srinivasan akils@stanford.edu

 */

 

// check that the microsd card is present, can be initialized and has a valid

// root volume. a pointer to the card's root object is returned as sd_root.

 

void sd_card_setup() {

  if (!card.init(SPI_HALF_SPEED, sd_cs)) {

    Serial.println("Card found, but initialization failed.");

    return;

  }

  if (!volume.init(card)) {

    Serial.println("Initialized, but couldn't find partition.");

    return;

  }

  if (!sd_root.openRoot(&volume)) {

    Serial.println("Partition found, but couldn't open root");

    return;

  }

}

 

// for each song file in the current directory, store its file name in eeprom

// for later retrieval. this saves on using program memory for the same task,

// which is helpful as you add more functionality to the program. This sets up

// the bpm array so that it matches the way the songs are read in

 

void sd_dir_setup() {

  dir_t p;

  num_songs = 0;

 

  sd_root.rewind();

 

  while (sd_root.readDir(&p) > 0 && num_songs < max_num_songs) {

    // break out of while loop when we wrote all files (past the last entry).

 

    if (p.name[0] == DIR_NAME_FREE) {

      break;

    }

 

    // only store current (not deleted) file entries, and ignore the . and ..

    // sub-directory entries. also ignore any sub-directories.

 

    if (p.name[0] == DIR_NAME_DELETED || p.name[0] == '.' || !DIR_IS_FILE(&p)) {

      continue;

    }

 

    // only store mp3 or wav files in eeprom (for now). if you add other file

    // types, you should add their extension here.

 

    // it's okay to hard-code the 8, 9 and 10 as indices here, since SdFatLib

    // pads shorter file names with a ' ' to fill 8 characters. the result is

    // that file extensions are always stored in the last 3 positions.

 

    if ((p.name[8] == 'M' && p.name[9] == 'P' && p.name[10] == '3') ||

      (p.name[8] == 'W' && p.name[9] == 'A' && p.name[10] == 'V')) {

 

      // store each character of the file name into an individual byte in the

      // eeprom. sd_file->name doesn't return the '.' part of the name, so we

      // add that back later when we read the file from eeprom.

 

      unsigned char pos = 0;

 

      int bpm = 0;

      for (unsigned char i = 0; i < 11; i++) {

        char letter = p.name[i];

        if (letter != ' ') {

          EEPROM.write(num_songs * max_name_len + pos, letter);

          if(i == 0)

            bpm += (int)letter;

          else if(i == 1)

            bpm += (int)letter * 10;

          else if(i == 2)

            bpm += (int)letter * 100; 

          pos++;

        }

      }

      bpmArray[num_songs] = bpm;

 

 

      // add an 'end of string' character to signal the end of the file name.

 

      EEPROM.write(num_songs * max_name_len + pos, '\0');

      num_songs++;

    }

  }

}

 

// given the numerical index of a particular song to play, go to its location

// in eeprom, retrieve its file name and set the global variable 'fn' to it.

 

void map_current_song_to_fn() {

  int null_index = max_name_len - 1;

 

  // based on the current_song index, get song's name and null index position from eeprom.

 

  for (int i = 0; i < max_name_len; i++) {

    fn[i] = EEPROM.read(current_song * max_name_len + i);

 

    // break if we reach the end of the file name.

    // keep track of the null index position, so we can put the '.' back.

 

    if (fn[i] == '\0') {

      null_index = i;

      break;

    }

  }

 

  // now restore the '.' that dir_t->name didn't store in its array for us.

 

  if (null_index > 3) {

    fn[null_index + 1] = '\0';

    fn[null_index]     = fn[null_index - 1];

    fn[null_index - 1] = fn[null_index - 2];

    fn[null_index - 2] = fn[null_index - 3];

    fn[null_index - 3] = '.';

  }

Comments (0)

You don't have permission to comment on this page.