particles react on audio

edited April 2015 in How To...

I don't know if this is the right place to post this, but I have a code that creates particles ( eclipes) when there is sound. So it react with the microphone. I found this code on the internet and edited it a little bit. Now I would like the particles to react on music. ( no microphone) Some words in this code are Dutch, I hope that's not a problem. Sorry for my bad English.

import ddf.minim.*;

AudioPlayer player;
AudioInput input;
Minim minim;
AudioInput in;
float volume = 0;
float volumeF = 0;

int maxParticles=300; // max number of particles. real particles count depends on ppf and its lifetime
int addPPF=2;         // number of particles per frame added
ArrayList particles;

void setup(){
  size(1000,1000);
  frameRate(30);
  smooth();
  background(255);
  noStroke();
      minim = new Minim(this);
  player = minim.loadFile("aventry-fire.mp3", 2048);
  player.play();

  // get a line in from Minim, default bit depth is 16
  in = minim.getLineIn(Minim.MONO, 512);


  particles = new ArrayList();
}

void draw(){
  background(0); // overpaint circles the frame before
  //  for(int i = 0; i < 1; i++)
  //{
     volumeF = in.right.level()*1000;
  volume = 0.7*volume + 0.1*volumeF;
  if(particles.size()<maxParticles){
    /** add particle(s) */
    for(int k=1; k<= volume; k++){
      particles.add(new Particles());
    }
  }
  //}

  /** run trought all Balls and make some action */
  for(int j=0; j<particles.size(); j++){
    Particles particle = (Particles)particles.get(j);
    /** if particle still alive */
    if( particle.alive() ){
      /** update x/y positions */
      particle.position();
      /** now draw as cirle */
      //fill(255,0,0, particle.lt);

     if(key == '1'){
        fill(0,255,0, particle.lt);
        text("verander kleur",10,50);

     }
     if(key == '2'){
        fill(0,255,255, particle.lt);
         text("verander kleur",10,50);
     }
     if(key == '3'){
        fill(255,255,0, particle.lt);
         text("verander kleur",10,50);
     }
     if(key == '4'){
        fill(0,0,255, particle.lt);
         text("verander kleur",10,50);
     }
     if(key == '5'){
        fill(random(255),random(255),random(255), particle.lt);
        text("verander kleur",10,50);
     }
      if(key == '6'){
        fill(random(255),random(255),random(255));
        text("verander kleur",10,50);
     }

     if(key== 'q'){
       text("Reageer op geluid",10,50);
      if(volume>0){
         fill(0,0,255, particle.lt);

       } 
      if(volume>3){
         fill(255,255,255, particle.lt);
       } 
       if(volume>6){
         fill(0,255,255, particle.lt);
       }
       if(volume>9){
         fill(255,0,255, particle.lt);
       }
       if(volume>12){
         fill(255,255,0, particle.lt);
       }
       if(volume>15){
         fill(255,100,100, particle.lt);
       }

      }


     if(key == 'a'){
       text("Vierkantjes",10,50);
       rect(particle.x, particle.y, particle.r, particle.r);}
    else{
     ellipse(particle.x, particle.y, particle.r, particle.r);  
   }


  }else{
      /** if lifetime is running out, delete */
      particles.remove(j);
    }
  }
}


  void stop()
{
  // always close Minim audio classes when you are done with them
  in.close();
  minim.stop();

  super.stop();
}

public class Particles {
  float r, x, y, _x, _y, dx=0, dy=0, lt;

  /** init start-values */
  public Particles() {
    /** start position center */
    x=width/2;
    y=height/2;
    /** now random values to make it non-linear */
    lt=random(50,100);       // lifetime in frames
    r=random(20, 50);        // circle Radius
    _x=random(-0.5, 0.5);    // stepwidth in x-pos (left, right)
    _y=random(-0.5, 0.5);    // stepwidth in y-pos (up, down)

    if(key == 'w'){
    text("Volg muis",10,50);
    x=mouseX;
    y=mouseY;
    }

  if(key == 's'){
    _x=random(-4,-0);    // stepwidth in x-pos (left, right)
    _y=random(-2, 2); 

  }

   if(key == 'd'){
    _x=random(0,4);    // stepwidth in x-pos (left, right)
    _y=random(-4, 4); 

  }
  if(key == 'z'){
    text("verander Snelheid",10,50);
     _x=random(-2.5,4);    // stepwidth in x-pos (left, right)
    _y=random(-2.5,4 ); 
  }
 if(key == 'x'){
   text("verander grootte",10,50);
   r=random(50, 120); 

 }
    }

    void stop(){
  player.close();
  minim.stop();

}

  public void position() {
    /** stepwidth increases, circlespeed is exponential */
    dx+=_x;
    dy+=_y;
    x+=dx;
    y+=dy;
  }

  public boolean alive(){
     if(lt<=0){ return false; }else{ lt--; return true; }
  }


}
Sign In or Register to comment.