We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
Combing sound and video (Read 2319 times)
Combing sound and video
Dec 19th, 2006, 3:42pm
 
I still learning the in's and outs of processing and at the same time i'm creating a tester project using brightness tracking and ESS sound. I have worked out the brightness tracking with this code:

import processing.video.*;
import krister.Ess.*;
import java.io.File;
import java.io.IOException;

Capture myCapture;


import krister.Ess.*;

AudioChannel myChannel, myChannel2;
PinkNoise myNoise;


public void stop() {
 Ess.stop();
 super.stop();
}


// Processing code for tracking the brightest pixel in a live video signal

int video_width = 320;
int video_height = 240;


//-------------------------------------------
void setup(){
size(320, 240);
 Ess.start(this);

// This line prints out all available cameras so you'll be able to
// spot if you's is there or not
println(Capture.list());
String s = "";
myCapture = new Capture(this, s, width, height, 30);
}
void captureEvent(Capture myCapture) {
myCapture.read();
 // create a new AudioChannel
 myChannel=new AudioChannel();
 myChannel2 = new AudioChannel();

 // set the channel size to 5 seconds
 myChannel.initChannel(myChannel.frames(5000));

 // generate 3 seconds of soft pink noise
 myNoise=new PinkNoise(.1);
 myNoise.generate(myChannel,0,myChannel.frames(9000));

 // play
 myChannel.play();
 myChannel.fadeTo(.5,500);
 
}

//-------------------------------------------
void draw(){
image(myCapture, 0, 0); // Draw the webcam video onto the screen.


// Declare some numbers to be computed later:
int brightest_x = 0; // the x-coordinate of the brightest video pixel
int brightest_y = 0; // the y-coordinate of the brightest video pixel
float brightest_val = 0; // the brightness of the brightest video pixel


// Now search for the brightest pixel:
for (int y=0; y<video_height; y++){ // For each row of pixels in the video image,
for (int x=0; x<video_width; x++){ // and for each pixel in the y'th row,
int index = y*video_width + x; // compute each pixel's index in the video,
int pix_val = myCapture.pixels[index];// fetch the color stored in that pixel,
float pix_bri = brightness(pix_val);


// and determine the brightness of that pixel.
if (pix_bri > brightest_val){ // If that value is brighter than any previous,
brightest_val = pix_bri; // then store the brightness of that pixel,
brightest_y = y; // as well as its (x,y) location.
brightest_x = x;
}else if (brightest_val < 200){
brightest_y = -10;
brightest_x = -10;
}


}
}




fill(255,0,0); // Set the fill color to red, and then
ellipse( brightest_x, brightest_y, 10,10); // draw a circle at the brightest pixel.
}


I would like to combine it with this code which is currently on a mouseclick event:


import krister.Ess .*;

AudioChannel myChannel1,myChannel2;
SineWave myWave;
PinkNoise myNoise;

void setup() {
 size(256,200);
 
 // start up Ess
 
 Ess.start(this);

 myChannel1=new AudioChannel();
 myChannel2=new AudioChannel();

 myChannel1.initChannel(myChannel1.frames(2000));
 myChannel2.initChannel(myChannel2.frames(2000));

 myWave=new SineWave(480,.5);
 myNoise=new PinkNoise(.5);

 myWave.generate(myChannel1);
 myNoise.generate(myChannel2);

 myChannel1.volume(0);
 myChannel2.volume(0);

 // pan only at zero crossings

 myChannel1.smoothPan=true;

 myChannel1.play(Ess.FOREVER);
 myChannel2.play(Ess.FOREVER);

 // small fade in

 myChannel1.fadeTo(.5,500);
 myChannel2.fadeTo(.5,500);

 framerate(30);

 noFill();
 smooth();
 ellipseMode(CENTER);
}

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

 strokeWeight(1);
 stroke(255);

 int tx=mouseX;
 int ty=mouseY;

 float newPan=1-(tx/float(width))*2;
 float newVolume=1-ty/float(height);

 if (!myChannel1.panning) myChannel1.panTo(newPan,500);
 if (!myChannel2.panning) myChannel2.panTo(-newPan,500);
 
 line(tx,0,tx,height);
 line(width-tx,0,width-tx,height);

 if (!myChannel1.fading) myChannel1.fadeTo(newVolume,500);
 if (!myChannel2.fading) myChannel2.fadeTo(1-newVolume,500);

 line(0,ty,width,ty);
 line(0,height-ty,width,height-ty);

 int d=((millis()/75) % 20)+4;

 strokeWeight(3);
 stroke(255,255-d*(255/20));

 ellipse(tx,ty,d,d);
 ellipse(width-tx,height-ty,d,d);
}

// clean up Ess before exiting

public void stop() {
 Ess.stop();
 super.stop();
}



Would anyone have any ideas on how to combine the two peices of code so that when a person moves their hand around the screen it changes the pitch and volume of the sounds. Any help would be really appeciated.
Re: Combing sound and video
Reply #1 - Dec 24th, 2006, 7:02pm
 
In answer to my own question - this code below takes code from Krister to change the volume and Pan of two audio channels with brightness tracking.


import processing.video.*;


// Variable for capture device
Capture video;
color trackColor;

//Audio setup

AudioChannel myChannel, myChannel2;
SineWave myWave;
PinkNoise myNoise;

void setup()
{
 Ess.start(this);
 size(200, 200);
 frameRate(30);
 colorMode(RGB,255,255,255,100);
 // Using the default capture device
 video = new Capture(this, 200, 200, 12);
 trackColor = color(255); // Start off tracking for white
 myChannel=new AudioChannel();
 myChannel2=new AudioChannel();
 
 myChannel.initChannel(myChannel.frames(2000));
 myChannel2.initChannel(myChannel2.frames(2000));
 
 myWave=new SineWave(480,.5);
 myNoise=new PinkNoise(.5);
 
 myWave.generate(myChannel);
 myNoise.generate(myChannel2);

 myChannel.volume(0);
 myChannel2.volume(0);
 
 // pan only at zero crossings

 myChannel.smoothPan=true;

 myChannel.play(Ess.FOREVER);
 myChannel2.play(Ess.FOREVER);

 // small fade in

 myChannel.fadeTo(.5,500);
 myChannel2.fadeTo(.5,500);

 noFill();
 smooth();
 strokeWeight(2.0);
 stroke(0);
 

}

void captureEvent(Capture camera)
{
 camera.read();
}

void draw()
{

 loadPixels();
 
 // Draw the video image on the background
 image(video,0,0);
 // Local variables to track the color
 float closestDiff = 500.0f;
 int closestX = 0;
 int closestY = 0;
 // Begin loop to walk through every pixel
 for ( int x = 0; x < video.width; x++) {
   for ( int y = 0; y < video.height; y++) {
     int loc = x + y*video.width;
     // What is current color
     color currentColor = video.pixels[loc];
     float r1 = red(currentColor); float g1 = green(currentColor); float b1 = blue(currentColor);
     float r2 = red(trackColor);   float g2 = green(trackColor);   float b2 = blue(trackColor);
     // Using euclidean distance to compare colors
     float d = dist(r1,g1,b1,r2,g2,b2);
     // If current color is more similar to tracked color than
     // closest color, save current location and current difference
     if (d < closestDiff) {
       closestDiff = d;
       closestX = x;
       closestY = y;
     }
   }
 }
 float newPan=1-(closestX/float(width))*2;
 float newVolume=1-closestY/float(height);
 
 if (!myChannel.panning) myChannel.panTo(newPan,500);
 if (!myChannel2.panning) myChannel2.panTo(-newPan,500);
 
 line(closestX,0,closestX,height);
 line(width-closestX,0,width-closestX,height);
 
 if (!myChannel.fading) myChannel.fadeTo(newVolume,500);
 if (!myChannel2.fading) myChannel2.fadeTo(1-newVolume,500);

 line(0,closestY,width,closestY);
 line(0,height-closestY,width,height-closestY);
 
   int d=((millis()/75) % 20)+4;

 strokeWeight(3);
 stroke(255,255-d*(255/20));

 ellipse(closestX,closestY,d,d);
 ellipse(width-closestX,height-closestY,d,d);
}

public void stop() {
 Ess.stop();
 super.stop();
}

Re: Combing sound and video
Reply #2 - Mar 1st, 2009, 10:57pm
 
hello,
i am really into this program you made, but i was wondering if you explain something for me. how did you make the brightness tracking replace mouseinput?

thanks for the help,
gil
Page Index Toggle Pages: 1