Exemple #1
0
void setup() {
  
    // set up Serial library at 9600 bps
  Serial.begin(9600);           
  
  if (!card.init()) {
    error("Card init. failed!");
  }
  if (!vol.init(card)) {
    error("No partition!");
  }
  if (!root.openRoot(vol)) {
    error("Couldn't open dir");
  }

  PgmPrintln("Files found:");
  root.ls();
  
  // declare the Pins
  pinMode(readyLEDPin, OUTPUT); 
 // pinMode(blowLEDPin, OUTPUT); 
 // pinMode(waitLEDPin, OUTPUT); 
  pinMode(level0LEDPin, OUTPUT); 
  pinMode(level1LEDPin, OUTPUT); 
  pinMode(level2LEDPin, OUTPUT); 
  pinMode(level3LEDPin, OUTPUT); 
  pinMode(buttonPin, INPUT); 
  
  initialButtonState = digitalRead(buttonPin);
  Serial.println("initial switch value= " + initialButtonState);
  
  ///***** set the char mode based on the pot position
  initialPot = analogRead(PotPin);  
  Serial.println("initial pot value= " + initialPot);
   
  range = map(initialPot, 0, 1024, 0, 3);
  Serial.println("initial pot range is: " + range);
  switch (range) {
  case 0:    // your hand is on the sensor
    mode = 0;
    Serial.println("initial mode set to 0: princess");
    break;
  case 1:    // your hand is close to the sensor
    mode = 1;
    Serial.println("initial mode set to 1: pirate");
    break;
  case 2:    // your hand is a few inches from the sensor
    mode = 2;
    Serial.println("initial mode set to 2: halloween");
    break;
  case 3:    // your hand is nowhere near the sensor
    mode = 3;
    Serial.println("initial mode set to 3: insult");
    break;
  }   
}
Exemple #2
0
/*
 * 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
      }
    }
  }
}
Exemple #3
0
//////////////////////////////////// SETUP
void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  
  putstring_nl("\nWave test!");  // say we woke up!
  
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(FreeRam());

  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  if (!card.init()) {         //play with 8 MHz spi (default faster!)  
    error("Card init. failed!");  // Something went wrong, lets print out why
  }
  
  // enable optimize read - some cards may timeout. Disable if you're having problems
  card.partialBlockRead(true);
  
  // Now we will look for a FAT partition!
  uint8_t part;
  for (part = 0; part < 5; part++) {   // we have up to 5 slots to look in
    if (vol.init(card, part)) 
      break;                           // we found one, lets bail
  }
  if (part == 5) {                     // if we ended up not finding one  :(
    error("No valid FAT partition!");  // Something went wrong, lets print out why
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(), DEC);     // FAT16 or FAT32?
  
  // Try to open the root directory
  if (!root.openRoot(vol)) {
    error("Can't open root dir!");      // Something went wrong,
  }
  
  // Whew! We got past the tough parts.
  putstring_nl("Files found (* = fragmented):");

  // Print out all of the files in all the directories.
  root.ls(LS_R | LS_FLAG_FRAGMENTED);
}
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;
}
/*
 * 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);
}
/**
 * Read a wave file's metadata and initialize member variables.
 *
 * \param[in] f A open FatReader instance for the wave file.
 *
 * \return The value one, true, is returned for success and
 * the value zero, false, is returned for failure.  Reasons
 * for failure include I/O error, an invalid wave file or a wave
 *  file with features that WaveHC does not support.
 */
uint8_t WaveHC::create(FatReader &f)
{
  // 18 byte buffer
  // can use this since Arduino and RIFF are Little Endian
  union {
    struct {
      char     id[4];
      uint32_t size;
      char     data[4];
    } riff;  // riff chunk
    struct {
      uint16_t compress;
      uint16_t channels;
      uint32_t sampleRate;
      uint32_t bytesPerSecond;
      uint16_t blockAlign;
      uint16_t bitsPerSample;
      uint16_t extraBytes;
    } fmt; // fmt data
  } buf;
  
#if OPTIMIZE_CONTIGUOUS
  // set optimized read for contiguous files
  f.optimizeContiguous();
#endif // OPTIMIZE_CONTIGUOUS

  // must start with WAVE header
  if (f.read(&buf, 12) != 12
      || strncmp(buf.riff.id, "RIFF", 4)
      || strncmp(buf.riff.data, "WAVE", 4)) {
        return false;
  }

  // next chunk must be fmt
  if (f.read(&buf, 8) != 8
      || strncmp(buf.riff.id, "fmt ", 4)) {
        return false;
  }
  
  // fmt chunk size must be 16 or 18
  uint16_t size = buf.riff.size;
  if (size == 16 || size == 18) {
    if (f.read(&buf, size) != (int16_t)size) {
      return false;
    }
  }
  else {
    // compressed data - force error
    buf.fmt.compress = 0;
  }
  
  if (buf.fmt.compress != 1 || (size == 18 && buf.fmt.extraBytes != 0)) {
    putstring_nl("Compression not supported");
    return false;
  }
  
  Channels = buf.fmt.channels;
  if (Channels > 2) {
    putstring_nl("Not mono/stereo!");
    return false;
  }
  else if (Channels > 1) {
    putstring_nl(" Warning stereo file!");
  }
  
  BitsPerSample = buf.fmt.bitsPerSample;
  if (BitsPerSample > 16) {
    putstring_nl("More than 16 bits per sample!");
    return false;
  }
  
  dwSamplesPerSec = buf.fmt.sampleRate;
  uint32_t clockRate = dwSamplesPerSec*Channels;
  uint32_t byteRate = clockRate*BitsPerSample/8;
  
#if RATE_ERROR_LEVEL > 0
  if (clockRate > MAX_CLOCK_RATE
      || byteRate > MAX_BYTE_RATE) {
    putstring_nl("Sample rate too high!");
    if (RATE_ERROR_LEVEL > 1) {
      return false;
    }
  }
  else if (byteRate > 44100 && !f.isContiguous()) {
    putstring_nl("High rate fragmented file!");
    if (RATE_ERROR_LEVEL > 1) {
      return false;
    }
  }
#endif // RATE_ERROR_LEVEL > 0

  fd = &f;

  errors = 0;
  isplaying = 0;
  remainingBytesInChunk = 0;
  
#if DVOLUME
  volume = 0;
#endif //DVOLUME
  // position to data
  return readWaveData(0, 0) < 0 ? false: true;
}
Exemple #8
0
//////////////////////////////////// LOOP
void loop() {
  root.rewind();
  play(root);
}