Quantcast
Channel: Library Questions - Processing 2.x and 3.x Forum
Viewing all articles
Browse latest Browse all 2896

Make particles synchronize with sound

$
0
0

Hello there. I'm trying make the rain particles fall synchronized with the sound using the minim library, but I know nothing about this and I have no idea how to make this happen. If someone could explain me how to do it, it would be great. This is what I have right now. Thank you and sorry for the slightly dumb question!

import ddf.minim.*;

Minim minim;
AudioPlayer player;
int numero = 200; // how many rain drops on your screen??
Rain[] rains = new Rain[numero];

void setup()
{
  size(512, 200, P3D);
  frameRate(40);
  noStroke();
  for(int i=0; i<numero ;i=i+1){
     rains[i]=new Rain();
  }
  // we pass this to Minim so that it can load files from the data directory
  minim = new Minim(this);

  // loadFile will look in all the same places as loadImage does.
  // this means you can find files that are in the data folder and the
  // sketch folder. you can also pass an absolute path, or a URL.
  player = minim.loadFile("sound.mp3");

  // play the file from start to finish.
  // if you want to play the file again,
  // you need to call rewind() first.
  player.play();
}

void draw()
{
  background(0);
  stroke(255);
  for(int i=0 ; i<numero ; i=i+1){
    rains[i].update();
    }
  // draw the waveforms
  // the values returned by left.get() and right.get() will be between -1 and 1,
  // so we need to scale them up to see the waveform
  // note that if the file is MONO, left.get() and right.get() will return the same value
  for(int i = 0; i < player.bufferSize() - 1; i++)
  {
    float x1 = map( i, 0, player.bufferSize(), 0, width );
    float x2 = map( i+1, 0, player.bufferSize(), 0, width );
   line( x1, 50 + player.left.get(i)*50, x2, 50 + player.left.get(i+1)*50 );
    line( x1, 150 + player.right.get(i)*50, x2, 150 + player.right.get(i+1)*50 );
  }
}
class Rain {  //this class setups the shape and movement of raindrop.
 float x = random(0,600);
 float y = random(-1000,0);
 float size = random(3,7); // size of raindrop
 float speed = random(20,80); // speed range
  void update()
  {
    y += speed;
    fill(185,197,209);
    ellipse(x, y-20, size, size*6); // tail of raindrop
    fill(255-(100-speed));
    ellipse(x, y, size, size*6); //head of raindrop

    if (y> height) //initialize raindrop which arrives bottom.
     {
       x = random(0,600);
       y = random(-1200,0);
     }

  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles