Attempting to create an array for different sounds, which then produce the sound when

Hi all I'm having some problems creating an array with different sounds, these are then attached to the balls so when they touch the bottom or top of the screen each produce a unique sound. Please can anyone help me to see where I'm going wrong. Cheers

Boolean IsMouseInCircle; 
Boolean IsMousePressed; 
PVector MyCoor[]; 
PVector CircleSize; 
PVector MousePos; 
int CircleNumber;

float diameter[];

boolean isMoving[];

float CircleSpeed_Y[];

int counter;

AudioPlayer [] songs;
import ddf.minim.*; 
Minim minim; 
AudioPlayer Drum01;
AudioPlayer Drum02;
AudioPlayer Guitar;
AudioPlayer Bass;
AudioPlayer Beats;
AudioPlayer Music;



void setup() { 
  size(1024, 700); 
  frameRate(60);

  minim = new Minim(this); 




  CircleNumber = 6;     // sets the number of balls being created //

  AudioPlayer [] songs = new AudioPlayer[CircleNumber];
  MyCoor = new PVector[CircleNumber]; // creates all the variable for the balls//
  diameter = new float[CircleNumber]; // These include the balls coordinates, their size, their speed  
  isMoving = new boolean[CircleNumber]; // and whether they have been clicked allowing them to move //

  CircleSpeed_Y = new float[CircleNumber]; 
  int counter = 0;

  IsMouseInCircle = false; 
  IsMousePressed = false;

  while (counter < CircleNumber) {
    diameter[counter] = 80;                        // While statement sets up all the variable on the stage//
    float xp = diameter[counter] + counter * 170;  // spreads the balls out evenly across the stage on the X axis //
    float yp = diameter[counter] ;

    CircleSpeed_Y[counter] = 20;
    MyCoor[counter] = new PVector(xp, yp);

    songs[counter] = songs[counter]  ;

    counter = counter + 1;
  }



  songs[0] = minim.loadFile("Drum.wav"); // Loads first drum sound effect //
  songs[1] = minim.loadFile("Drum.wav");
  songs[2] = minim.loadFile("Guitar.wav"); // Loads first drum sound effect //
  songs[3] = minim.loadFile("Bass.wav");
  songs[4] = minim.loadFile("Beats.wav"); // Loads first drum sound effect //
  songs[5] = minim.loadFile("Music.wav");
}

void draw() {

  background(255, 255, 255);                                 // sets the background to white //
  counter = 0;

  while (counter < CircleNumber) {

    if (checkMouseInZone(MyCoor[counter], diameter[counter]/2)) {      // this if statement tests the mouse is in the ball //
      if ( IsMousePressed ) {                                          // tests if the mouse clicked within the each ball //


        isMoving[counter] = true;                                      // if the click is within each ball make the boolean true and proceeds with the if statement below //
      }
    }  
    else {
      fill(100, 100, 0);
    }  

    // ------------------------------------------------------

    if (isMoving[counter]) {                                          // if the isMoving[counter] is true make the balls move //
      //
      MyCoor[counter].y = MyCoor[counter].y + CircleSpeed_Y[counter];//

      fill(255, 0, 0);                                               // Once the ball is moving change the balls colour to Red //

      if (MyCoor[counter].y > height) {                              // Once the ball touches the bottom window reverse the balls speed, which makes it go back up//
        MyCoor[counter].y = height;
        CircleSpeed_Y[counter] = - CircleSpeed_Y[counter]  ;
        println("too far bottom");
        songs[CircleNumber].pause();
        songs[CircleNumber].rewind();
        songs[CircleNumber].play();
        //        Drum01.pause();
        //        Drum01.rewind();
        //        Drum01.play();
      }

      if (MyCoor[counter].y < 0) {                                    // Once the ball touches the top of the window reverse the balls speed, which makes it go back down//
        MyCoor[counter].y = 50;
        CircleSpeed_Y[counter] = -CircleSpeed_Y[counter] ;
        println("too far top");
        //        Drum01.pause();
        //        Drum01.rewind();
        //        Drum01.play();
      }
    }

    ellipse(MyCoor[counter].x, MyCoor[counter].y, diameter[counter], diameter[counter]);   // this creates the balls //
    fill(0, 255, 0);                                                                      // When the cursor hangs over the stationary balls turn the colour green//

    counter = counter + 1;
  }
}

boolean checkMouseInZone(PVector p, float radius) {              //this is my function to check if the mouse is in range of the button




  PVector m = new PVector(mouseX, mouseY);                        // a temporary PVector to store the current mouse position 

  return (m.dist(p) <= radius);                                   // returns true or false
}

void mousePressed() {

  IsMousePressed = true;
}

void mouseReleased() {

  IsMousePressed = false;
}

Answers

  • I deleted the duplicate thread and moved the topic to the right category.

    If you described more precisely what is your issue, perhaps it would be easier to help you...

    General advices:

    • Avoid using Boolean instead of boolean (unless you know what you are doing!).
    • By convention, variable names always start with a lowercase letter... Doing otherwise, in a non-consistent way, make the code harder to read.
    • You should move your imports at the top of the sketch.
    • You should remove dead code / unused variables, like the various AudioPlayers used nowhere.
  • Hi PhiLho.

    Thanks for the above advice.

    I'll try and explain a bit better with what I'm trying to do.

    This code is one one of my tasks for my Uni module. What it had originally done was when you played the code it would produce 6 circles near the top in a stationary position, when you clicked on one of them they would move until they touched the bottom of the window and then a noise would be produced. Each circle would move independent from the others, however the noises when they touched the bottom or top of the windows would be the same.

    What I'm now trying to do with the above code, is have 5 different sounds, have them put into an array linked with each ball individually, so when they touch the bottom or top they would produce a different sound. This would then hopefully make a simple tune.

    Cheers for reading all this PhiLho, look forward to hearing from you and any help you can give on this/

  • Answer ✓

    I fear I don't know much about Minim, but when I look at the examples shipped with Processing, I think the TriggerASample example is close of what you want to do. Instead of using AudioPlayers, you should try and use AudioSamples instead.

Sign In or Register to comment.