int Jukebox2::fileCount(FatReader &dir){ //Serial.print("WTF!"); int count = 0; FatReader tempFile; dir_t dirBuf; // buffer for directory reads dir.rewind(); while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time //Serial.print("QQQ!"); // Skip it if not a subdirectory and not a .WAV file if (!DIR_IS_SUBDIR(dirBuf) && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) { continue; } if (!tempFile.open(vol, dirBuf)) { // open the file in the directory Serial.print("file.open failed"); // something went wrong } if (tempFile.isDir()) { // check if we opened a new directory } else{ count++; } } dir.rewind(); Serial.print("Count was:"); Serial.println(count); return count; }
/* * play recursively - possible stack overflow if subdirectories too nested */ void play(FatReader &dir) { FatReader file; while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time // Skip it if not a subdirectory and not a .WAV file if (!DIR_IS_SUBDIR(dirBuf) && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) { continue; } Serial.println(); // clear out a new line for (uint8_t i = 0; i < dirLevel; i++) { Serial.write(' '); // this is for prettyprinting, put spaces in front } if (!file.open(vol, dirBuf)) { // open the file in the directory error("file.open failed"); // something went wrong } if (file.isDir()) { // check if we opened a new directory putstring("Subdir: "); printEntryName(dirBuf); Serial.println(); dirLevel += 2; // add more spaces // play files in subdirectory play(file); // recursive! dirLevel -= 2; } else { // Aha! we found a file that isnt a directory putstring("Playing "); printEntryName(dirBuf); // print it out if (!wave.create(file)) { // Figure out, is it a WAV proper? putstring(" Not a valid WAV"); // ok skip it } else { Serial.println(); // Hooray it IS a WAV proper! wave.play(); // make some noise! uint8_t n = 0; while (wave.isplaying) {// playing occurs in interrupts, so we print dots in realtime putstring("."); if (!(++n % 32))Serial.println(); delay(100); } sdErrorCheck(); // everything OK? // if (wave.errors)Serial.println(wave.errors); // wave decoding errors } } } }
/* * Open and start playing a WAV file */ void playfile(char *name) { if (wave.isplaying) {// already playing something, so stop it! wave.stop(); // stop it } if (!file.open(root, name)) { PgmPrint("Couldn't open file "); Serial.print(name); return; } if (!wave.create(file)) { PgmPrintln("Not a valid WAV"); return; } // ok time to play! wave.play(); }
void Jukebox2::PlayRandomSound(FatReader &dir){ //Serial.print("OMG!"); int count = 0; FatReader tempFile; dir_t dirBuf; // buffer for directory reads int r = random(1,fileCount(dir)); dir.rewind(); while (dir.readDir(dirBuf) > 0) { // Read every file in the directory one at a time //Serial.print("LOL!"); //Serial.println(r); // Skip it if not a subdirectory and not a .WAV file if (!DIR_IS_SUBDIR(dirBuf) && strncmp_P((char *)&dirBuf.name[8], PSTR("WAV"), 3)) { continue; } if (!tempFile.open(vol, dirBuf)) { // open the file in the directory Serial.print("file.open failed"); // something went wrong } if (tempFile.isDir()) { // check if we opened a new directory } else { count++; if(count == r){ Serial.println("found a sound!"); PlaySound(dirBuf); dir.rewind(); return; } } } Serial.println("Never found a sound!"); Serial.print("Rand was:"); Serial.println(r); }