How to add a second soundfile

edited March 2017 in Arduino

I am using an accelerometer to control the rate of a sound but I want to add a second sound that is only triggered when currentX value is greater than 350 for a delay of 3 seconds, I have got the first sound working as I want it, but when I tried adding the second sound as part of an array it all stopped working.

  import processing.sound.*;

import processing.serial.*;
import processing.opengl.*;
Serial myPort;
int baudRate = 9600;
//int lf = 10;


int[] xAxis;
int[] yAxis;
int[] zAxis;

SoundFile file;

int currentX = 0;
int currentY = 0;
int currentZ = 0;

int rateX =1;
int ampY =1;
int addZ =1;

int totalReadings = 400;
int readingPos = 0; // the reading position in the array

void setup() {
  smooth();
  size(600, 300); 
  //file = new SoundFile(this, "magic-chime-02.mp3");
  // file = new SoundFile(this, "magic-chime-04.mp3");
  file = new SoundFile(this, "Blastwave_FX_MagicWindChimesHarp_SFXB.36.mp3");

  file.loop();



  xAxis = new int[totalReadings];
  yAxis = new int[totalReadings];
  zAxis = new int[totalReadings];



  myPort = new Serial(this, "com5", baudRate);
  myPort.bufferUntil(10);

  noLoop();
}

void serialEvent(Serial p) {
  String inString;

  try {
    inString = (myPort.readString());
    currentX = xValue(inString);
    currentY = yValue(inString);
    currentZ = zValue(inString);

  }
  catch(Exception e) {
    println(e);
  }
  redraw();
}

void draw()
//alter the pitch rate and amp depending on xyz values
{
  file.play();
  file.rate(rateX);
  file.amp(ampY);
  file.add(addZ);
  //println( "current"+ currentX, currentY, currentZ);
  rateX = currentZ/100;
  ampY = currentX/100;
  addZ = currentY/100;
  println(rateX);
  println(ampY);
  println(addZ);

//stop the sound if x is less than 350 
  if (currentX < 350) {
    file.play();
    println("Play");
  } else {
    file.stop();
    println("Stop");
    // delay(10);
  }
  delay(500);
}



int xValue(String inString) {
  int pipeIndex = inString.indexOf('|');
  return int(inString.substring(0, pipeIndex));
}

int yValue(String inString) {
  int pipeIndex = inString.indexOf('|');
  int colonIndex = inString.indexOf(':');
  return int(inString.substring(pipeIndex+1, colonIndex));
}

int zValue(String inString) {
  int colonIndex = inString.indexOf(':');
  return int(inString.substring(colonIndex + 1, inString.length() - 2));
}
Sign In or Register to comment.