Problems triggering WAV audio clips from SD reader with pushbuttons

Hello:

I'm building a replica of the PKE meter prop from Ghostbusters; I'm using a Teensy 3.1 or 3.2 to drive the LED chasers and servos, and I have the code for the lights and wings (controlled by touch switches) worked out. Now I'm getting more ambitious and want to add two sound effects, triggered by the touch switches.

My two sound effect clips have been encoded as WAV files and stored on a MicroSD card in a Teensy audio adapter board.

Starting with a WavFilePlayer sketch from PJRC Teensy I'm attempting to cobble together the code to play two different audio clips that play when one of two (physical) momentary buttons are pressed and stop playing when released. At this point I have it sort of working. When I push (and release) one button the "Slow" sound effect will play. When I push (and release) the other button the "Fast" sound effect will play.

PKE sound effects test

The main problem I'm having is the way the buttons are behaving. The appropriate sound should be playing only when one of the buttons is pressed and stop playing when the button is released. Right now I have to press and release one of the buttons before the sound starts playing, and then it plays the entire sound clip.

I've added an LED to the sketch to confirm the state of the buttons, lighting up when one or the other is pushed, off when released.

I thought that maybe playing a WAV clip of just silence would work for the default (off) state, but if I uncomment those lines of the sketch nothing works. I think there is a way to stop playback of the audio file, but I don't know how or where to integrate that.

The other thing that's happening is if the setup sits for a few minutes, and then I push the button to play the SLOW sound nothing happens. I can push the button for the FAST sound, and it will play, and then the SLOW button will start responding again if pushed.

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

// GUItool: begin automatically generated code
AudioPlaySdWav           playWav1;       //xy=154,78
AudioOutputI2S           i2s1;           //xy=334,89
AudioConnection          patchCord1(playWav1, 0, i2s1, 0);
AudioConnection          patchCord2(playWav1, 1, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=240,153
// GUItool: end automatically generated code

void setup()
{
  

  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(5);

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

  SPI.setMOSI(7);
  SPI.setSCK(14);
  SD.begin(10);
  
  pinMode(4, INPUT_PULLUP); // Pushbutton connected to pin 4 input and ground for SLOW sound
  pinMode(5, INPUT_PULLUP); // Pushbutton connected to pin 5 input and ground for FAST sound
  pinMode(16, OUTPUT);  //LED connected between pin 16 and ground via 220 ohm resistor
}

void playFile(const char *filename)
{
  

  // Start playing the file.  This sketch continues to
  // run while the file plays.
  playWav1.play(filename);

  
}

void loop()
{
  if (digitalRead(4)) // 4 pin is high due to pullup resistor - Returns 1, so this block happens.  If it returns 0 (button pressed, pin goes low), else happens  
  {
    if (digitalRead(5)) // 5 pin is high due to pullup resistor - Returns 1, so this block happens.  If it returns 0 (button pressed, pin goes low), else happens  
  {
  digitalWrite(16, LOW);
  //playFile("SILENCE.WAV");
  }
  else
  {
    // Pin 5 is Low - returns 0 due to pushbutton pressed  SLOW SOUND EFFECT, status LED
    digitalWrite(16, HIGH);
    playFile("SLOWREV.WAV");
  }
  } 
  
  else
  {
    // Pin 4 is Low - returns 0 due to pushbutton pressed  FAST SOUND EFFECT, status LED
    digitalWrite(16, HIGH);
    playFile("FASTREV.WAV");
  }
}

Sorry, I'm fairly new to this and not what you'd call a coding whiz. I can sort of cut and paste chunks of code and insert some of the proper variables and pin connections, but that's about it. In this case I'm not clear on how the SD card .wav player stuff at the top of the sketch relates to the looping portion where I want to trigger (and stop) the specific sound effects. The WavFilePlayer sketch I started with had some code to send the status of the player via serial; I believe I've removed all of that since I don't need it here.

Any thoughts would be appreciated.

Thanks.

Shawn