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

Working with minim library - Fade in - Fade out ellipse ?

$
0
0

I just did this with the minim library by using amplitude of the sound .... my interest is to make a circle disappear , in the moment that you 'feed' with the sound it gets some colours re-appears again .... so my question is , is it possible to do that without background ... cause I have another sketch on top of this , so if I draw a background it doesn't involve the other sketch ...

I'll share the code with you ... but this is done with background

 import ddf.minim.*;
 import ddf.minim.analysis.*;
 import ddf.minim.effects.*;
 import ddf.minim.signals.*;
 import ddf.minim.spi.*;
 import ddf.minim.ugens.*;

 Minim minim;
 AudioInput in;

 // The data for the circles
 ArrayList<Float> cx = new ArrayList<Float>();
 ArrayList<Float> cy = new ArrayList<Float>();
 ArrayList<Float> cr = new ArrayList<Float>();

 int currCircle;
 float currAngle, prevAngle;
 int dir; // -1 or +1
 float speedFactor = 200;
 float speed;

 float prevTime, currTime;

 int ccCol, moverCol;

 // Data needed to manage connections
 ArrayList<Integer> from = new ArrayList<Integer>();
 ArrayList<Integer> to = new ArrayList<Integer>();
 ArrayList<Float> fromTouchAngle = new ArrayList<Float>();
 ArrayList<Float> toTouchAngle = new ArrayList<Float>();
 ArrayList<Boolean> reverse = new ArrayList<Boolean>();

 void setup() {
 size(600, 440);
 minim= new Minim(this);

 in = minim.getLineIn();

  // Initialise some colors for drawing
  ccCol = color(100, 230, 100);
 moverCol = color(255, 100, 100);
 // Create our circles
 addCircle(100, 100, 90);
 addCircle(300, 250, 160);
 addCircle(500, 250, 40);
 addCircle(300, 350, 60);
 addCircle(300, 320, 30);
 // Add connections
 addConnection(0, 1);
 addConnection(1, 2);
 addConnection(1, 3);
 addConnection(3, 4);
 // Starting position for mover
 currCircle = 1;
 speed = speedFactor / cr.get(currCircle);
 dir = 1;
 prevAngle = currAngle = random(TWO_PI);
 prevTime = currTime = millis();
  }

 void draw() {
 float currTime = millis();
 float elapsedTime = (currTime - prevTime) / 2500;
 background(0);
 drawCircles();
 updateMoverPosition(elapsedTime);
 drawMover();

 // Current tima and position become previous time
  // and position in the next frame
   prevTime = currTime;
   prevAngle = currAngle;
  }

   void updateMoverPosition(float et) {
    // Calculate current angle
    currAngle = prevAngle + dir * speed * et;
    if (currAngle < 0)
    currAngle += TWO_PI;
    else if (currAngle >= TWO_PI)
    currAngle -= TWO_PI;
   // Now see if the mover has reached a connection point
   float low = min(prevAngle, currAngle);
   float high = max(prevAngle, currAngle);
   // See if the previous and curent angles straddle the zero
   // degree position (that is east) we need to do this because
   // of the discontinuity in the angle value
   boolean straddleZero = (high - low > PI); // Straddle
  // Assume that we are not going to have to switch circles
   for (int i = 0; i < from.size(); i++) {
   // See if the connection is for the current circle
   if (from.get(i) == currCircle) {
   boolean switchCircle = false;
   float ta = fromTouchAngle.get(i);
   // See if the touch angle is between the previous and current
   // angle depending on whether the
   if (straddleZero) {
     switchCircle = ta > high || ta < low;
   } else {
    switchCircle = ta < high && ta > low;
    }
   if (switchCircle) {
    currCircle = to.get(i);
    prevAngle = currAngle = toTouchAngle.get(i);
    speed = speedFactor / cr.get(currCircle);
    if (reverse.get(i)) dir *= -1;
    break; // essential since we have changed circles
    }
    }
   }
   }

     void drawCircles() {
     stroke(0);
     float w = 0;
     for(int i=0;i<150; i++ ){
     w += in.mix.get(i);
    }
    strokeWeight(abs(w));
    noFill();
    for (int i = 0; i < cx.size(); i++) {
    float d = cr.get(i) * 2;
    ellipse(cx.get(i), cy.get(i), d, d);
    }
    }

   void drawMover() {
   noStroke();
  //fill(255);
    float w = 0;
   for(int i=0;i<10; i++ ){
   w += in.mix.get(i);
    }
    fill(255*abs(w));

   float x = cx.get(currCircle);
   float y = cy.get(currCircle);
   float r = cr.get(currCircle);
  x += r * cos(currAngle);
  y += r * sin(currAngle);
  ellipse(x, y, 14, 14);
  }

   void addCircle(float x, float y, float r) {
   cx.add(x);
   cy.add(y);
   cr.add(r);
    }

   void addConnection(int fromCirc, int toCirc) {
   float fX = cx.get(fromCirc);
   float fY = cy.get(fromCirc);
   float fR = cr.get(fromCirc);
   float tX = cx.get(toCirc);
   float tY = cy.get(toCirc);
   // Angle from-circle-centre >> to-circle-centre
   float ang = atan2(tY - fY, tX - fX);
   // Calculate where the 2 circles touch
   float touchX = fX + fR * cos(ang);
   float touchY = fY + fR * sin(ang);
   // Calculate touch angle for both circles and
   // make sure they fall in the range 0 - TWO_PI
   float fAngle = atan2(touchY - fY, touchX - fX);
   if (fAngle < 0) fAngle += TWO_PI;
   float tAngle = atan2(touchY - tY, touchX - tX);
   if (tAngle < 0) tAngle += TWO_PI;
   // Determine whther we need to reverse direction when
   // we change circle
   boolean reverseDir = abs(fAngle - tAngle) > 0.9;
   // Add forward connection
   from.add(fromCirc);
   to.add(toCirc);
   fromTouchAngle.add(fAngle);
   toTouchAngle.add(tAngle);
   reverse.add(reverseDir);
   // Add reverse connection
   from.add(toCirc);
   to.add(fromCirc);
   fromTouchAngle.add(tAngle);
    toTouchAngle.add(fAngle);
   reverse.add(reverseDir);
     }

Viewing all articles
Browse latest Browse all 2896

Trending Articles