playSDWav.isPlaying() and playSDWav.play(file) are blocking loop

Status
Not open for further replies.

grinch

Well-known member
Hi, I'm working on a project which requires a lot of channels of audio playback (16 mono channels to be precise). I'm trying to do this on a Teensy 3.6 using the SD wav file player object playSDWav. I tested my audio in isolation and it works great (sketch where I read a couple pots and do my thing with the audio channels). However when I try to insert this code into the rest of my project the audio starts behaving really strangely. Specifically my function for looping the audio files starts taking a long time to execute, and either blocking the loop for upwards of a minute, or causing audio dropouts when I add AudioNoInterrupt().

The relevant parts of the code look like this:

Code:
#define SDCARD_CS_PIN    BUILTIN_SDCARD //254
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used

AudioPlaySdWav              playWav[16];
AudioOutputAnalog           audioOutput;
AudioMixer4                 mixer[5];

AudioConnection             patchCord1(playWav[0], 0, mixer[0], 0);
AudioConnection             patchCord2(playWav[1], 0, mixer[0], 1);
AudioConnection             patchCord3(playWav[2], 0, mixer[0], 2);
AudioConnection             patchCord4(playWav[3], 0, mixer[0], 3);
AudioConnection             patchCord5(playWav[4], 0, mixer[1], 0);
AudioConnection             patchCord6(playWav[5], 0, mixer[1], 1);
AudioConnection             patchCord7(playWav[6], 0, mixer[1], 2);
AudioConnection             patchCord8(playWav[7], 0, mixer[1], 3);
AudioConnection             patchCord9(playWav[8], 0, mixer[2], 0);
AudioConnection             patchCord10(playWav[9], 0, mixer[2], 1);
AudioConnection             patchCord11(playWav[10], 0, mixer[2], 2);
AudioConnection             patchCord12(playWav[11], 0, mixer[2], 3);
AudioConnection             patchCord13(playWav[12], 0, mixer[3], 0);
AudioConnection             patchCord14(playWav[13], 0, mixer[3], 1);
AudioConnection             patchCord15(playWav[14], 0, mixer[3], 2);
AudioConnection             patchCord16(playWav[15], 0, mixer[3], 3);

AudioConnection             patchCord17(mixer[0], 0, mixer[4], 0);
AudioConnection             patchCord18(mixer[1], 0, mixer[4], 1);
AudioConnection             patchCord19(mixer[2], 0, mixer[4], 2);
AudioConnection             patchCord20(mixer[3], 0, mixer[4], 3);

AudioConnection             patchCord21(mixer[4], 0, audioOutput, 0);

const char *files[] = {"FIRE1.wav", "FIRE2.wav", "FIRE3.wav", "FIRE4.wav", "RAIN1.wav", "RAIN2.wav", "RAIN3.wav", "RAIN4.wav", 
                      "WIND1.wav", "WIND2.wav", "WIND3.wav", "WIND4.wav", "ANIMAL1.wav", "ANIMAL2.wav", "ANIMAL3.wav", "ANIMAL4.wav"};


void setupAudio(){
  AudioMemory(800);
  audioOutput.analogReference(INTERNAL);

  for(int i = 0; i < 5; ++i){
    for(int j = 0; j < 4; ++j){
      mixer[i].gain(j, 0);
    }
  }
  
  mixer[4].gain(0, 1); //fire level
  mixer[4].gain(1, 1); //rain level
  mixer[4].gain(2, 0.5); //wind level
  mixer[4].gain(3, 1); //wildlife level
  
  SPI.begin();

  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    // stop here, but print a message repetitively
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }
}

void playAudioFiles()
{
  int one = millis();
//  AudioNoInterrupts(); //for testing
  for(int i = 0; i < 16; ++i){
    if(!(playWav[i].isPlaying())){
      playFileFromPlayer(files[i], i);
    }
  }
//  AudioInterrupts(); //for testing
//  delay(1); //for testing
  int two = millis() - one;
  Serial.println(two); 

//prints amount of time function took to execute, prints 0 in test program, 8 in application with AudioNoInterrupts, 60000+ in application without AudioNoInterrupts()
}

void playFileFromPlayer(const char *filename, int player)
{
  Serial.print("Playing file: ");

  // Start playing the file.  This sketch continues to
  // run while the file plays.
  playWav[player].play(filename);

  Serial.println(filename);

  // A brief delay for the library read WAV info
  delay(5);
}

The other things handled by the teensy are controlling an 16x2 OLED character display, a neopixel strip, and reading a couple buttons. I have checked for pin conflicts and there are none.

Is there any reason why the playSDWav.play() or playSDWav.isPlaying() functions would take longer to execute when the microcontroller is doing additional stuff elsewhere?
 
Last edited by a moderator:
To be more specific, the problem is that this function (containing playSDWav.play() and playSDWav.isPlaying()) takes much longer to execute when I add the OLED, neopixel and button controls to the program:

Code:
void playAudioFiles()
{
  int one = millis();
  for(int i = 0; i < 16; ++i){
    if(!(playWav[i].isPlaying())){
      playFileFromPlayer(files[i], i);
    }
  }
  int two = millis() - one;
  Serial.println(two); 

//prints amount of time function took to execute, prints 0 in test program, up to 60000ms+ in application with OLED and pixel strip
}
 
Status
Not open for further replies.
Back
Top