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

Changing sound-value with camera tracking?

$
0
0

Hey guys, so I have this code. You can track two different colors by choosing them with right-click. As you can see I also got two sliders. One of them controls frequencies, the other controls the volume. Now I want to change the values by the color tracked objects. Lets say if I move my yellow object vertically, it changes the frequencies and if move my pink object horizontally, I want to change the volume with it. Does someone have any ideas on how to realise this? (I hope I explained everythin well).

best regards

//Importing librarys
import controlP5.*;
import ddf.minim.*;
import ddf.minim.ugens.*;
import processing.video.*;

//Declaration
ControlP5      cp5;
Minim          minim;
AudioOutput    out;
Oscil          wave;
Frequency      currentFreq;
Slider         sliderHertz;
Slider         sliderVolume;
Capture        video;

final int TOLERANCE = 20;

float XRc = 0;// XY coordinate of the center of the first target
float YRc = 0;
float XRh = 0;// XY coordinate of the center of the second target
float YRh = 0;

int ii=0; //Mouse click counter
int Hertz = 150; //Hertz-Slider
int Volume = 50; //Volume-Slider

color trackColor; //The first color is the center of the robot
color trackColor2; //The second color is the head of the robot

void setup() {

size(640,680);
video = new Capture(this,640,480);
video.start();

trackColor = color(255,0,0);
trackColor2 = color(255,0,0);
smooth();

//Hertz-Slider
cp5 = new ControlP5(this);
sliderHertz = cp5.addSlider("Hertz").setPosition(265,495).setRange(50,400);

//Volume-Slider
cp5 = new ControlP5(this);
sliderVolume = cp5.addSlider("Volume").setPosition(445,495).setRange(0,30);

// Initializating minim and out
minim = new Minim(this);
out   = minim.getLineOut();

currentFreq = Frequency.ofHertz( 50 );
wave = new Oscil( currentFreq, 0.6f, Waves.SINE );

wave.patch( out );
}

void draw() {
background( 0, 0, 0 );
stroke( 255, 255, 255 );

if (video.available()) {
    video.read();
}

//Slidervalue and Text
text( "Frequenz in Hertz: " + sliderHertz.getValue(), 35, 505 );

  wave.setFrequency( Frequency.ofHertz( sliderHertz.getValue() ) );

  wave.setAmplitude(sliderVolume.getValue());


 //Sinusfunction
  for( int i = 0; i < out.bufferSize() - 1; i++ )
  {
    float x1  =  map( i, 0, out.bufferSize(), 0, width );
    line( x1, 595 + out.left.get(i)*50, x1, 595 + out.left.get(i+1)*50);
  }


video.loadPixels();
image(video,0,0);

  float r2 = red(trackColor);
  float g2 = green(trackColor);
  float b2 = blue(trackColor);

  float r3 = red(trackColor2);
  float g3 = green(trackColor2);
  float b3 = blue(trackColor2);


  int somme_x = 0, somme_y = 0; // Calculating center
  int compteur = 0;

  int somme_x2 = 0, somme_y2 = 0; // Calculating center
  int compteur2 = 0;

  int somme_x3 = 0, somme_y3 = 0; // Calculating center
  int compteur3 = 0;

  int somme_x4 = 0, somme_y4 = 0; // Calculating center
  int compteur4 = 0;


  for(int x = 0; x < video.width; x++) {
    for(int y = 0; y < video.height; y++) {

      int currentLoc = x + y*video.width;
      color currentColor = video.pixels[currentLoc];

      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);


      if(dist(r1,g1,b1,r2,g2,b2) < TOLERANCE) {
         somme_x += x;
         somme_y += y;
        compteur++;
      }

      else if(compteur > 0) {
        XRc = somme_x / compteur;
        YRc = somme_y / compteur;
      }


      if(dist(r1,g1,b1,r3,g3,b3) < TOLERANCE) {
         somme_x2 += x;
         somme_y2 += y;
        compteur2++;
      }

      else if(compteur2 > 0) {
        XRh = somme_x2 / compteur2;
        YRh = somme_y2 / compteur2;
      }

  }
  }




  if(XRc != 0 || YRc != 0) { // Draw a circle at the first target
    fill(trackColor);
    strokeWeight(0.05);
    stroke(0);
    ellipse(XRc,YRc,20,20);
  }


  if(XRh != 0 || YRh != 0) {// Draw a circle at the second target
    fill(trackColor2);
    strokeWeight(0.05);
    stroke(0);
    ellipse(XRh,YRh,20,20);
  }


}
void mousePressed() {
  if (mousePressed && (mouseButton == RIGHT)) { // Save color where the mouse is clicked in trackColor variable
  if(ii==0){

    if (mouseY>480){mouseY=0;mouseX=0;}
  int loc = mouseX + mouseY*video.width;

  trackColor = video.pixels[loc];
ii=1;
}
  else if(ii==1){
    if (mouseY>480){mouseY=0;mouseX=0;}
  int loc2 = mouseX + mouseY*video.width;
  trackColor2 = video.pixels[loc2];
ii=2;
}
  }
}

Viewing all articles
Browse latest Browse all 2896

Trending Articles