Sound Library Issue with array

edited February 2018 in Library Questions

I am trying to create multiple instances of a sound file to play each time my shapes hit the left || right edges of screen. This event triggers correctly but the resultant sound is extremely loud, distorted and mangled (I had to reduce the amplitude drastically).

Also I get the following error:

Screen Shot 2018-02-17 at 00.54.06

My code is as follows. Beware not to raise the amplitude if you test this out with your own sound file.

Rect_Class[] many_Rects;
int noRects = 1;

import processing.sound.*;
String turnsound = "pop.mp3";

void setup()
{
  many_Rects = new Rect_Class[noRects];  
  for (int i=0; i<many_Rects.length; i++) 
  {
    many_Rects[i] = new Rect_Class(this, int(random(50, 255)), random(10, 20)); 
    many_Rects[i].loadSound(turnsound);
  }
  size(displayWidth, displayHeight);
  noStroke();

}



void draw()
{
  fill(0, 100);
  rectMode(CORNER);  
  rect(0, 0, displayWidth, displayHeight);
  for (int i=0; i<many_Rects.length; i++) 
  {
    //many_Rects[i].loadSound(turnsound);
    many_Rects[i].moveRect();
    many_Rects[i].drawRect();
  }
}



class Rect_Class 
{
  // declare variable accesible within object instance
  int fill;
  float xoff;   // Speed
  float rectWidth = 64;
  float xpos = 900; 

  SoundFile turnsound;
  PApplet applet;

  //initialise
  Rect_Class(PApplet temp_applet, int tempFill, float tempxOff)
  {
    applet = temp_applet;
    fill = tempFill;
    xoff = tempxOff;
  }

  void loadSound(String filename1)
  {
    turnsound = new SoundFile(applet, filename1);
  }

  void moveRect()
  {
    if (xpos <= 0 || xpos >= displayWidth)
    {
      turnsound.stop();
      turnsound.play(1.0, 0.2);
      turnsound.amp(0.001);
      xoff = xoff * -1;
    }
    xpos = xpos + xoff;  
  }



  void drawRect()
  {
    rectMode(CENTER);
    fill(fill);
    rect(xpos, displayHeight/2, rectWidth, displayHeight);
  }

}

Any tips much appreciated!

Sign In or Register to comment.