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.
IndexDiscussionExhibition › Sound and Sensor Help
Page Index Toggle Pages: 1
Sound and Sensor Help (Read 485 times)
Sound and Sensor Help
Nov 26th, 2008, 4:54am
 
Check out this piece I've been working on! I want audio to play when someone enters one side of the screen. But I'm not the greatest with audio, let alone with sensors. Could someone PLEASE help me with this code. Thanks!

My Setup:

//Catch Me by Kurtis Smejkal

//With my piece I wanted to explore the polar dynamics of power. With power I find it can either be a corrupting force or a force for change, so this idea of power being pure and corrupt is what I wanted to work off of.
//For me I chose to have the background divided into a Black and White side to evoke what people think in terms of morality with the person(s) being represented by a simple grey to show the simple neutrality of humanity.
//Changing text will float away from the side the person resides in (or if with others it simply will be in this constant state of erratic). While in each side I want to play speeches from a multitude of different moments in history.
//So when someone steps into the morally white side it might play Martin Luther Kings “I have a dream” speech or Obama’s Presidential Acceptance speech. For the morally black side perhaps a speech from Adolf Hitler, George W. Bush, and so on.
//I also wanted to show the idea of what time will do to someone in this distinct area of morality. So over time I want the person to lose that moral grey and replace it with the white or black that they spend time in.
//Showing how too much power can cause us to be an agent of chaos or an agent for good.
//What I wanted to do with this piece is show how fragile we really are in terms of our morality in relation to power. It influences and changes us into an extension of itself.
//I want people to see that in terms of power we all have to deal with it in some form or another whether it’s at work or at home. I want to show we do have choice; such as, running aimlessly hoping we don’t grasp on to what is or we accept it and use it, like a simple tool.


import fullscreen.*;
import blobDetection.*;
import processing.video.*;




PFont font;
float x = 250;
float y = 250;
int maxBlobIndex = 0;

FullScreen fs;

boolean cheatScreen;

void setup()
{
 size(640, 480);
 frameRate(240);
 fs = new FullScreen(this);
 fs.enter();

 font = loadFont("Braggadocio-48.vlw");
 textFont(font);
 noStroke();

 cameraSetup();

}

void draw()
{
 fill(255);
 rect(0, 0, width, height);
 fill(201, 0, 7);

 blobDetection();

 Blob b;

 b=theBlobDetection.getBlob(maxBlobIndex);  



 if (b!=null) {
   if ((b.x*width >= 0) && (b.x*width <= x*width) &&
     (b.y*height >= 0) && (b.y*height <= y*height)) {
     x += random(-200, 200);
     y += random(-200, 200);
   }
 }
 text("absolute power", constrain (x, 0, 480), constrain (y, 0, 600));

}
{
 loop();
}


And my Camera Sensing is in the Post below:
Re: Sound and Sensor Help
Reply #1 - Nov 26th, 2008, 4:54am
 
int numPixels;
int[] backgroundPixels;
Capture video;
BlobDetection theBlobDetection;
PImage img, binaryImg;

int threshold = 40;
int minBlobSize = 2000;

void cameraSetup() {

 video = new Capture(this, width, height, 24);
 numPixels = video.width * video.height;

 backgroundPixels = new int[numPixels];

 loadPixels();

 binaryImg = new PImage(width, height);

 img = new PImage(80,60);
 theBlobDetection = new BlobDetection(img.width, img.height);
 theBlobDetection.setPosDiscrimination(true);
 theBlobDetection.setThreshold(0.5f);
 theBlobDetection.activeCustomFilter(this);
}

void blobDetection() {
 if (video.available()) {
   video.read();
   video.loadPixels();
   
   int presenceSum = 0;

   for (int i = 0; i < numPixels; i++) {
     
     color currColor = video.pixels[i];
     color bkgdColor = backgroundPixels[i];
   
     int currR = (currColor >> 16) & 0xFF;
     int currG = (currColor >> 8) & 0xFF;
     int currB = currColor & 0xFF;
   
     int bkgdR = (bkgdColor >> 16) & 0xFF;
     int bkgdG = (bkgdColor >> 8) & 0xFF;
     int bkgdB = bkgdColor & 0xFF;
     
     int diffR = abs(currR - bkgdR);
     int diffG = abs(currG - bkgdG);
     int diffB = abs(currB - bkgdB);

     threshold = int(map(mouseY, 0, height, 0, 255));

     
     if (diffR > threshold || diffG > threshold || diffB > threshold )
     {
       diffR = 255;
       diffG = 255;
       diffB = 255;
     }
     else
     {
       diffR = 0;
       diffG = 0;
       diffB = 0;
     }

   
     presenceSum += diffR + diffG + diffB;

     
     pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB;

     binaryImg.pixels[i] = pixels[i];  

     if (i%width>width/2) {

       if  (diffR == 255) {

         diffR=0;
         diffG=0;
         diffB=0;
       }
       else {

         diffR=255;
         diffG=255;
         diffB=255;

       }
     }

     pixels[i] = 0xFF000000 | (diffR << 16) | (diffG << 8) | diffB;

   }
   updatePixels();

   
   filter(ERODE);
   filter(ERODE);
   filter(DILATE);
   println(presenceSum);

   img.copy(binaryImg, 0, 0, width, height,
   0, 0, img.width, img.height);
 
   theBlobDetection.computeBlobs(img.pixels);
   drawBlobsAndEdges(true,false);
 }
}


void keyPressed() {
 video.loadPixels();
 arraycopy(video.pixels, backgroundPixels);
}


void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges)
{
 strokeWeight(1);
 stroke(255,0,0);
 noFill();
 Blob b;
 EdgeVertex eA,eB;
 for (int n=0 ; n<theBlobDetection.getBlobNb(); n++)
 {
   b=theBlobDetection.getBlob(n);    
           if (b.w*width * b.h*height > minBlobSize) {
   if (b!=null)
   {
     
     if (drawEdges)
     {
       strokeWeight(3);
       stroke(0,255,0);
       for (int m=0;m<b.getEdgeNb();m++)
       {
         eA = b.getEdgeVertexA(m);
         eB = b.getEdgeVertexB(m);
         if (eA !=null && eB !=null)
           line(
           eA.x*width, eA.y*height,
           eB.x*width, eB.y*height
             );
       }

     }

     
     if (drawBlobs)
     {
       strokeWeight(1);
       stroke(255,0,0);
       rect(
        b.xMin*width,b.yMin*height,
       b.w*width,b.h*height
         );
       
       println(n);
       println("center " + b.x*width + " " + b.y*height);
       println("mass " + b.w*width * b.h*height);

     }

   }
              }

 }
}


boolean newBlobDetectedEvent (Blob b) {
 if (b.w*width * b.h*height > minBlobSize)
   return true;
 else
   return false;
}

void fastblur(PImage img,int radius)
{
 if (radius<1){
   return;
 }
 int w=img.width;
 int h=img.height;
 int wm=w-1;
 int hm=h-1;
 int wh=w*h;
 int div=radius+radius+1;
 int r[]=new int[wh];
 int g[]=new int[wh];
 int b[]=new int[wh];
 int rsum,gsum,bsum,x,y,i,p,p1,p2,yp,yi,yw;
 int vmin[] = new int[max(w,h)];
 int vmax[] = new int[max(w,h)];
 int[] pix=img.pixels;
 int dv[]=new int[256*div];
 for (i=0;i<256*div;i++){
   dv[i]=(i/div);
 }

 yw=yi=0;

 for (y=0;y<h;y++){
   rsum=gsum=bsum=0;
   for(i=-radius;i<=radius;i++){
     p=pix[yi+min(wm,max(i,0))];
     rsum+=(p & 0xff0000)>>16;
     gsum+=(p & 0x00ff00)>>8;
     bsum+= p & 0x0000ff;
   }
   for (x=0;x<w;x++){

     r[yi]=dv[rsum];
     g[yi]=dv[gsum];
     b[yi]=dv[bsum];

     if(y==0){
       vmin[x]=min(x+radius+1,wm);
       vmax[x]=max(x-radius,0);
     }
     p1=pix[yw+vmin[x]];
     p2=pix[yw+vmax[x]];

     rsum+=((p1 & 0xff0000)-(p2 & 0xff0000))>>16;
     gsum+=((p1 & 0x00ff00)-(p2 & 0x00ff00))>>8;
     bsum+= (p1 & 0x0000ff)-(p2 & 0x0000ff);
     yi++;
   }
   yw+=w;
 }

 for (x=0;x<w;x++){
   rsum=gsum=bsum=0;
   yp=-radius*w;
   for(i=-radius;i<=radius;i++){
     yi=max(0,yp)+x;
     rsum+=r[yi];
     gsum+=g[yi];
     bsum+=b[yi];
     yp+=w;
   }
   yi=x;
   for (y=0;y<h;y++){
     pix[yi]=0xff000000 | (dv[rsum]<<16) | (dv[gsum]<<8) | dv[bsum];
     if(x==0){
       vmin[y]=min(y+radius+1,hm)*w;
       vmax[y]=max(y-radius,0)*w;
     }
     p1=x+vmin[y];
     p2=x+vmax[y];

     rsum+=r[p1]-r[p2];
     gsum+=g[p1]-g[p2];
     bsum+=b[p1]-b[p2];

     yi+=w;
   }
 }

}

Page Index Toggle Pages: 1