Make particles synchronize with sound

edited April 2017 in Library Questions

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);
     } 

  } 
}
Tagged:

Answers

  • Please provide more details. What is the meaning of your statement:

    I'm trying make the rain particles fall synchronized with the sound

    Kf

  • I mean, I want the particles to fall faster when the sound gets higher, and the particles to be less when the sound is lower.. if that's even possible

  • edited April 2017 Answer ✓

    Yes, that is possible and your description is now clear.

    You see, in line 43 you get a buffer with your waveform. You could EITHER [1] find the maximum in the buffer or [2] find the average value of the whole bugger and use it as your metric for amplitude. You will need to characterize the max (or average) of a whole song to be able to determine the maximum range of these values.

    Then you can do speed_factor=map(your_metric,metric_low,metric_high,0.05,0.95); In other words, you map the min and max value of your amplitude to a factor between 5 to 95% and you can use this factor to control the speed of the particles. You will pass this factor to your Rain object in draw() and your Rain class will have this modification at line 56: y += speed*speed_factor;

    I hope this helps,

    Kf

  • thank you!!

Sign In or Register to comment.