Audio Function

I recently received a digiX, and have been trying to get audio to work through it. I used the SimpleAudioPlayer script and got it to work. Now i'm using a keypad to play sounds.The SimpleAudioPlayer works for one sound that you can loop or have play once. However, there seems to be no documentation on playing multiple sounds.

My code:

      File audioSample = SD.open("button2.wav");
      if (!audioSample){
        //while (true);
        Serial.println("WTF");
      }
      else{
      const int S = 1024;
      short buffer[S];
      while (audioSample.available()) {
        audioSample.read(buffer, sizeof(buffer));
        int volume = 1024;
        Audio.prepare(buffer, S, volume);
        Audio.write(buffer, S);
     }
      audioSample.close();

There is more in the script that includes librarys and such, this is not included. This code appears to be verified. But my problem is that when I open up the SD file. the file becomes nil. This code is fairly bulky. What I was wondering, is if anyone has come up with a function. something like:

playSound(test.wav, 1024)

this would call the function and play test.wav, and play it with a volume of 1024

Ive tried this before with no luck.

Any help would be great. Thanks!

PLease post your whole code.

is a digiX an Arduino clone?

This digiX is not a clone.
http://digistump.com/wiki/digix/tutorials/board-version-1-0-aug2013

My full code:

#include <Keypad.h>
#include <SD.h>
#include <SPI.h>
#include <Audio.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'1','2','3','F1'},
  {'4','5','6','F2'},
  {'7','8','9','F3'},
  {'*','0','#','F4'}
};
byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9, 8, 7, 6}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

int Volume = 1024;

void setup(){
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  
  Serial.print("Initializing SD card...");
  if (!SD.begin(4)) {
    return;
  }
  SPI.setClockDivider(4);
  Audio.begin(88200, 100);
 
}

  
void loop(){
  digitalWrite(13, LOW);
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
    digitalWrite(13, HIGH);
    
    
    Serial.println("Keypress");
    if (customKey == '1'){
      File audioSample = SD.open("button2.wav");
      if (!audioSample){
        //while (true);
        Serial.println("WTF");
      }
      else{
      const int S = 1024;
      short buffer[S];
      while (audioSample.available()) {
        audioSample.read(buffer, sizeof(buffer));
        int volume = 1024;
        Audio.prepare(buffer, S, volume);
        Audio.write(buffer, S);
     }
      audioSample.close();
     }
    }
    
    
  }
  delay(50);
 
  
}

I also have the newer version of the board that has different positioning. The photos are here Front and Back: https://digistump.com/wiki/digix/tutorials/board-version-1-0-aug2013

This code appears to be verified. But my problem is that when I open up the SD file. the file becomes nil.

Check the board specs on what SD cards they qualified. SD on uCs can be fickle and many cards are not supported. If you cannot find the qualified listing, leave Digistump a forum message.

Ray

My problem is not the card, what im trying to do is get a line of code that plays audio. A function.

amen

Not to say the obvious, but maybe posting in the Digix section of http://digistump.com/board/ would be helpful for you. Most of the Arduino mainstream crowd will probably be unfamiliar with this product.

Ray

Maybe try something like this?

#define BUF_SZ = 1024;

void main () {
    ...
}

void loop () {
  char filename[13];   // Max length of 8.3 filename + NULL
  byte buffer[BUF_SZ]; // Sample buffer with BUF_SZ elements
  int  volume = 1024;  // This could also be #define'd above
    
  ...
  customKey = customKeypad.getKey();

  if (customKey) {

    Serial.println("Keypress");
    
    switch (customKey) {
    
    case '1':
      //File audioSample = SD.open("button2.wav");
      strcpy(filename, "button2.wav");
      break;
    
    case '2':
      strcpy(filename, "button3.wav");
      break;
    
    ...
    
    default:
      // This serves as a "don't play anything" flag
      // Feel free to do it some other, more elegant, way
      filename[0] = 0;
      break;
    }
    
    if (filename[0] != 0) {
      audioSample = SD.open(filename);
      if (!audioSample) {
        //while (true);
        Serial.println("WTF");
      }
      else {
        // Don't need these here, already allocated above
        //const int S = 1024;
        //short buffer[S];
        
        while (audioSample.available()) {
          audioSample.read(buffer, sizeof(buffer));
          //int volume = 1024;
          Audio.prepare(buffer, BUF_SZ, volume);
          Audio.write(buffer, BUF_SZ);
       }
       audioSample.close();
     }
  }
  
  ...
}