Playing Sounds kind of simultaneously

Status
Not open for further replies.
Hallo everyone,

as i'm new to programming, i got some (hopefully) little trouble to smooth the performance of my Teensy.
Hopefully somebody could help me solving this Problem:

The Code below plays four short sounds after pushing four buttons and it's working (yeah!). But i'd like them to be played kinda simultaneously. So when i push two buttons exactly in time, one sound will be not played. Same Story, another image: when the first sound (which has got a little reverb) is playing, it will be cut by pushing another button.

i hope, you got, what i'm after.
So any suggestion is appreciated!

Thanx a lot for help!!!



Code:
// Teensy 3.2
// Arduino 1.8.5
// Teensyduino 1.4.2
// Mac Book Pro
// OSX 10.11.6
// Code built with help from the brilliant System Design Tool


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

AudioPlaySdWav           playSdWav1;     //xy=136,245
AudioPlaySdWav           playSdWav2;     //xy=165,292
AudioPlaySdWav           playSdWav3;     //xy=193,344
AudioPlaySdWav           playSdWav4;     //xy=212,390
AudioMixer4              mixer1;         //xy=477,277
AudioOutputI2S           i2s1;
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord2(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord3(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord4(playSdWav4, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;

Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);
Bounce button2 = Bounce(2, 15);
Bounce button3 = Bounce(3, 15);  // 15 = 15 ms debounce time


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


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

void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  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);
    }
  }
  pinMode(13, OUTPUT); // LED on pin 13
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  delay(5); // 10
}


const char *track_A = "SOUND01.WAV"; // some reverb within …
const char *track_B = "SOUND02.WAV"; 
const char *track_C = "SOUND03.WAV"; 
const char *track_D = "SOUND04.WAV"; 


elapsedMillis blinkTime;

void loop() {

  // read pushbuttons
  button0.update();
  if (button0.fallingEdge()) {
   Serial.println("Button_0 pushed");
   playSdWav1.play(track_A);
  }
  button1.update();
  if (button1.fallingEdge()) {
    Serial.println("Button_1 pushed");
    playSdWav1.play(track_B);
  }
  button2.update();
  if (button2.fallingEdge()) {
    Serial.println("Button_2 pushed");
    playSdWav1.play(track_C);
  }
  button3.update();
  if (button3.fallingEdge()) {
    Serial.println("Button_3 pushed");
    playSdWav1.play(track_D);
  }

  // blink the LED without delays – not necessary, just to see if Teensy is alive ;-)
  if (blinkTime < 250) {
    digitalWrite(13, LOW);
  } else if (blinkTime < 500) {
    digitalWrite(13, HIGH);
  } else {
    blinkTime = 0; // start blink cycle over again
  }
  
}
 
Find the file SD_t3.h. Edit it, to uncomment the read-only optimized SD card code (just one #define to enable it). Does that help?
 
Hallo Paul,
thank your for your suggestion. I found the file but i'm a bit anxious to edit without having asked before. I suppose you refer to this particular line which i should uncomment:
//#define USE_TEENSY3_OPTIMIZED_CODE

Right?
 
I found the file but i'm a bit anxious to edit without having asked before. I suppose you refer to this particular line which i should uncomment:
//#define USE_TEENSY3_OPTIMIZED_CODE

Right?

Yup, that's the right one. Don't be shy, just uncomment it and see what happens. Worst case you can just comment it out again, or reinstall Teensyduino, which will overwrite that file and all others Teensyduino installs.

Does turning on the optimization many any difference for this 4 file player?
 
Hello Paul,

meanwhile i have uncommented the line as you suggested. Having restarted the Software and uploaded the script i can't determine significant difference. …

The interesting part is the behavior seemingly depending on some attribute of the sound files. I've got four drumset Sounds: dry basedrum, snaredrum, closed hihat and crash cymbal. The only combination partially working is to play (hit the button) »Crash« followed by the middle-frequency »snare«. Although cut shorter, a little reverb flag is hearable. All other combination are cutting the former one respectively prevent them to be played.

…?
 
meanwhile i have uncommented the line as you suggested. Having restarted the Software and uploaded the script i can't determine significant difference. …

Did you see any difference in the memory usage numbers Arduino shows?

The optimized code uses about 3K more RAM. Maybe try commenting it out again and check whether the memory usage goes back down, as a sanity check it was really using the optimization.
 
I think its because you're running each sample off the one sample object. (playSdWav1)

Change it so each button uses a different playSdWav object, then they'll play all together. Also make the mixer max volume on all channels.

here I did it for you, this may or may not work, I don't have the same hardware setup to test on:

Code:
// Teensy 3.2
// Arduino 1.8.5
// Teensyduino 1.4.2
// Mac Book Pro
// OSX 10.11.6
// Code built with help from the brilliant System Design Tool


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

AudioPlaySdWav           playSdWav1;     //xy=136,245
AudioPlaySdWav           playSdWav2;     //xy=165,292
AudioPlaySdWav           playSdWav3;     //xy=193,344
AudioPlaySdWav           playSdWav4;     //xy=212,390
AudioMixer4              mixer1;         //xy=477,277
AudioOutputI2S           i2s1;
AudioConnection          patchCord1(playSdWav1, 0, mixer1, 0);
AudioConnection          patchCord2(playSdWav2, 0, mixer1, 1);
AudioConnection          patchCord3(playSdWav3, 0, mixer1, 2);
AudioConnection          patchCord4(playSdWav4, 0, mixer1, 3);
AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;

Bounce button0 = Bounce(0, 15);
Bounce button1 = Bounce(1, 15);
Bounce button2 = Bounce(2, 15);
Bounce button3 = Bounce(3, 15);  // 15 = 15 ms debounce time


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


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

void setup() {
  Serial.begin(9600);
  AudioMemory(8);
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.6);
  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);
    }
  }
  pinMode(13, OUTPUT); // LED on pin 13
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  delay(5); // 10

  mixer1.gain(0, 1); //this makes all channels of the mixer max volume.
  mixer1.gain(1, 1); //change the second number to adjust volume for each sample player
  mixer1.gain(2, 1);
  mixer1.gain(3, 1);
}


const char *track_A = "SOUND01.WAV"; // some reverb within …
const char *track_B = "SOUND02.WAV"; 
const char *track_C = "SOUND03.WAV"; 
const char *track_D = "SOUND04.WAV"; 


elapsedMillis blinkTime;

void loop() {

  // read pushbuttons
  //I changed it so each button uses its own individual sampler.
  //you already had them set up but you only used the 1st sampler.
  //this means if it is playing a sample, then you trigger it again with a new sample,
  //the first sample will then be cut off.
  button0.update();
  if (button0.fallingEdge()) {
   Serial.println("Button_0 pushed");
   playSdWav1.play(track_A);
  }
  button1.update();
  if (button1.fallingEdge()) {
    Serial.println("Button_1 pushed");
    playSdWav2.play(track_B);
  }
  button2.update();
  if (button2.fallingEdge()) {
    Serial.println("Button_2 pushed");
    playSdWav3.play(track_C);
  }
  button3.update();
  if (button3.fallingEdge()) {
    Serial.println("Button_3 pushed");
    playSdWav4.play(track_D);
  }

  // blink the LED without delays – not necessary, just to see if Teensy is alive ;-)
  if (blinkTime < 250) {
    digitalWrite(13, LOW);
  } else if (blinkTime < 500) {
    digitalWrite(13, HIGH);
  } else {
    blinkTime = 0; // start blink cycle over again
  }
  
}
 
Hallo Waterme11on,

thanx a million – your my personal hero for the month!
I forgot to address the other players.

Again thank you for correcting the code. Have a wonderful year!!!
 
Hallo Paul,

it was just a simple error as you can see below. Nevertheless thank you for your engagement!
Fun to work with!!!

Best wishes
 
Status
Not open for further replies.
Back
Top