Can't play more than one audio file simultaneously

Status
Not open for further replies.

alexandros

Well-known member
I'm using a Sandisk Ultra 16 GB SD card (which is class 10, but all Sandisk Ultra are class 10, so I guess these are the ones tested and reporter to work here https://www.pjrc.com/store/teensy3_audio.html) with the four test files provided by Teensy's webpage https://www.pjrc.com/teensy/td_libs_AudioDataFiles.html
This is my code (partly copied from WavFilePlayer.ino):
Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

const int numPlayers = 4;

AudioPlaySdWav           wavPlayers[numPlayers];
AudioMixer4              mixer2;
AudioMixer4              mixer1;
AudioOutputI2S           audioOutput;
AudioConnection          patchCord1(wavPlayers[0], 0, mixer1, 0);
AudioConnection          patchCord2(wavPlayers[0], 1, mixer2, 0);
AudioConnection          patchCord3(wavPlayers[1], 0, mixer1, 1);
AudioConnection          patchCord4(wavPlayers[1], 1, mixer2, 1);
AudioConnection          patchCord5(wavPlayers[2], 0, mixer1, 2);
AudioConnection          patchCord6(wavPlayers[2], 1, mixer2, 2);
AudioConnection          patchCord7(wavPlayers[3], 0, mixer1, 3);
AudioConnection          patchCord8(wavPlayers[3], 1, mixer2, 3);
AudioConnection          patchCord9(mixer2, 0, audioOutput, 1);
AudioConnection          patchCord10(mixer1, 0, audioOutput, 0);
AudioControlSGTL5000     sgtl5000_1;

// Use these with the Teensy Audio Shield
#define SDCARD_CS_PIN    10
#define SDCARD_MOSI_PIN  7
#define SDCARD_SCK_PIN   14

// filenames are always uppercase 8.3 format
const char * filelist[numPlayers] = {
  "SDTEST1.WAV", "SDTEST2.WAV", "SDTEST3.WAV", "SDTEST4.WAV"
};

// analog pin 0, 1, 4, 5 are taken by the audio board
// so we must set the ones used, here
int analogIns[numPlayers] = { 2, 3, 6, 7 };

void setup() {
  Serial.begin(9600);

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

  // Comment these out if not using the audio adaptor board.
  // This may wait forever if the SDA & SCL pins lack
  // pullup resistors
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);

  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 loop() {
  for (int i = 0; i < numPlayers; i++) {
    int potVal = analogRead(analogIns[i]);
    if (potVal > 50) {
      if (!wavPlayers[i].isPlaying()) {
        const char *filename = filelist[i];
        wavPlayers[i].play(filename);
      }
    }
    else {
      if (wavPlayers[i].isPlaying()) {
        wavPlayers[i].stop();
      }
    }
    float amp = (float)potVal / 1023.0;
    mixer1.gain(i, amp);
    mixer2.gain(i, amp);
    delay(50);
  }
  Serial.println(AudioMemoryUsageMax());
  delay(250);
}
I have four potentiometers to determine the amplitude of each track. When a value drops below a threshold (which is 50), the audio file player is supposed to stop. Playing one file at a time plays fine, but when I turn two knobs up, their speed and pitch drops, and their sound gets distorted.
Also, AudioMemoryUsageMax() keeps on reporting the number 4 once any track starts playing, even when all sound files are supposed to have stopped playing.

I guess there is a solution since the audio board's webpage (https://www.pjrc.com/store/teensy3_audio.html) reports that simultaneous playback of 2, 3, or 4 tracks is possible.
 
Status
Not open for further replies.
Back
Top