triggering audio files with button from SD card

Status
Not open for further replies.
Hi,
I want to build an interface that is going to trigger 1 of 10 sound sample. I tried to do it with the playMem option but memory is too low to keep that many samples. I am trying to do it via SD card but I cant make it work. I am using Teensy 3.2 with audio board. Everything is soldered correctly (did the hardware check) and the SD card is good as well (SanDisk ultra 16gb class 1). I was trying to do it with the codes from tutorial as I am only a beginner programmer. Any ideas? Please I need help because its for a project of friend of mine and I thought I could manage it. Thanks in advance!

Code:
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav3;     //xy=101.00000762939453,217.0000057220459
AudioPlaySdWav           playSdWav2;     //xy=147.00000762939453,138.00000381469727
AudioPlaySdWav           playSdWav1;     //xy=162.00000762939453,91.00000381469727
AudioMixer4              mixer1;         //xy=335,143
AudioOutputI2S           i2s1;           //xy=555.0000381469727,138.00001525878906
AudioConnection          patchCord1(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord2(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord3(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord4(mixer1, 0, i2s1, 0);
AudioConnection          patchCord5(mixer1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=394.00000762939453,228.00003051757812
// GUItool: end automatically generated code

// Bounce objects to read pushbuttons 
Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);  // 15 ms debounce time
Bounce button2 = Bounce(2, 15);


#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14

void setup() {
  Serial.begin(9600);
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);

  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);

  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
}

void playFile(const char *filename)
{
  Serial.print("Playing file: ");
  Serial.println(filename);
  playSdWav1.play(filename); 
  delay(100);
  while (playSdWav1.isPlaying()) {
  }
}


void loop() {
  
    button0.update();
 button1.update();
  button2.update(); 

   if (button0.fallingEdge()) {
  playFile("JOANNA1"); // filenames are always uppercase 8.3 format
   }
  delay(5000);
   if (button1.fallingEdge()) {
  playFile("JOANNA2");
   }
  delay(5000);
   if (button2.fallingEdge()) {
  playFile("JOANNA3");
   }
  delay(5000);
   /*if (button3.fallingEdge()) {
  playFile("JOANNA4");
  delay(1500);
   }*/
}
 
I think your missing the SD card initialisation.

Add Sd2Card like so:

Code:
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14
Sd2Card card;


Then add the following line into your setup routine.

Code:
card.init(SPI_FULL_SPEED, SDCARD_CS_PIN);
 
When you press the buttons, does it do anything? Do you get the message: Playing file: <some file name>

If so this would show the button code is working.

I would probably add something like: while (!Serial && (millis() < 2000)) ;
At the start of the program to make sure we have a debug terminal ready... Then see if there were any debug messages that came out.

Do your file name names you specified in the code exactly match the name of the files in the root directory of your SD Card? With no extension?

Maybe try changing the file names from: JOANNA2 to JOANNA2.WAV

And change the code to match... Not sure if that would matter or not

I notice you have 3 of the: AudioPlaySdWav playSdWav1;
They are all connected by Mixer but only playSdWav1 is used anywhere. Maybe start again from wavFilePlay example for audio library.
 
okey so I organized my sketch a bit, now I am using only 1 AudioPlaySdWav playSdWav1; and no mixer, all the names are correct and af added while (!Serial && (millis() < 2000)) it started responding to buttons (I get a message of file being played when I press matching button)! Although I still get no sound out, I have noticed that LED on teensy isnt on if thats something.
 
Are you using an audio file from the exampes or one you created?


The library requires the files to be recorded at 11025Hz or 22050Hz 16Bit.


Audacity is useful for creating the WAV files.


Try one of the sample files first to see if it works.
 
I am using the audio I created but I tried it with SDTEST files from tutorial and I get the message that song is playing but I still have no sound. Output is for sure good, I checked it before
 
I would probably add debug code to see if you can figure out things like did the play statement return true or false.

Also check to see if demo program works...
 
your sketch worked for me if i commented out all the delay(5000); in loop(), and i tested with Paul's WAV files, e.g. SDTEST1.WAV SDTEST2....
button presses won't be recognized until playFile() finishes, unless you use attachInterrupt() or change your isPlaying() logic
 
Last edited:
your sketch worked for me if i commented out all the delay(5000); in loop(), and i tested with Paul's WAV files, e.g. SDTEST1.WAV SDTEST2....
button presses won't be recognized until playFile() finishes, unless you use attachInterrupt() or change your isPlaying() logic

This.
Your current code executes several delays of 5000ms each loop, regardless of the result of the if statement.
 
your sketch worked for me if i commented out all the delay(5000); in loop(), and i tested with Paul's WAV files, e.g. SDTEST1.WAV SDTEST2....
button presses won't be recognized until playFile() finishes, unless you use attachInterrupt() or change your isPlaying() logic

yes, thats it! I had no clue this could be causing problems but thank you very much, everything works as it should be :)
 
Status
Not open for further replies.
Back
Top