User:Pleun/openaanbod/processing 3

From XPUB & Lens-Based wiki

// === CODE ===

// Animating shape, sound and colour


PVector v1 = new PVector(50,250);
PVector v2 = new PVector(150,250);
PVector v3 = new PVector(450,550);
PVector v4 = new PVector(600,50);
boolean v1Selected = false;
boolean v2Selected = false;
boolean v3Selected = false;
boolean v4Selected = false;
int steps = 200;
int step = 1;
int i = 0;


import ddf.minim.*;
import ddf.minim.ugens.*;
Minim       minim;
AudioOutput out;
Oscil       wave;


void setup() {
 size (1000,800);
 
  minim = new Minim(this);
 
 // use the getLineOut method of the Minim object to get an AudioOutput object
 out = minim.getLineOut();
 
 // create a sine wave Oscil, set to 440 Hz, at 0.5 amplitude
 wave = new Oscil( 440, 0.5f, Waves.SINE );
 // patch the Oscil to the output
 wave.patch( out );
 

}

void draw() {
 background(0);
 strokeWeight(3);
 stroke(255);
 noFill();
 
 bezier(v1.x, v1.y, v2.x, v2.y, v3.x, v3.y, v4.x, v4.y);
 
 fill(0);
 ellipse(v1.x, v1.y, 15, 15);
  ellipse(v2.x, v2.y, 15, 15);
   ellipse(v3.x, v3.y, 15, 15);
    ellipse(v4.x, v4.y, 15, 15);
    
    
    float t = i/float(steps);
    float x = bezierPoint(v1.x, v2.x, v3.x, v4.x, t);
    float y = bezierPoint(v1.y, v2.y, v3.y, v4.y, t);
    
    fill(0,255,0);
    noStroke();
    ellipse(x, y, 40,40);
    ellipse(x, 700, 30 ,30);
    
    int c = (int)map(x, 0, 660, 0, 255);
    fill(c,0,255);
    rect(width - 240, y, c, c);
    
   float amp = map( y, 0, height, 1, 0 );
     wave.setAmplitude( amp );
 
   float freq = map( x, 0, width, 110, 880 );
     wave.setFrequency( freq );
 
    i = i + step;
    if (i >= steps || i < 0){
    step = - step;
    }
}
void mousePressed() {
 if(dist(mouseX, mouseY, v1.x, v1.y) < 7.5) {
  v1Selected = true;
 }
  if(dist(mouseX, mouseY, v2.x, v2.y) < 7.5) {
  v2Selected = true;
 }
  if(dist(mouseX, mouseY, v3.x, v3.y) < 7.5) {
  v3Selected = true;
 }
  if(dist(mouseX, mouseY, v4.x, v4.y) < 7.5) {
  v4Selected = true;
  }
}
void mouseDragged() {
 if(v1Selected) {
  v1.x = mouseX;
   v1.y = mouseY; 
 }
   if(v2Selected) {
  v2.x = mouseX;
   v2.y = mouseY; 
 }
   if(v3Selected) {
  v3.x = mouseX;
   v3.y = mouseY; 
 }
   if(v4Selected) {
  v4.x = mouseX;
   v4.y = mouseY; 
  }
}
void mouseReleased() {
v1Selected = false;
v2Selected = false;
v3Selected = false;
v4Selected = false;
}