Visual Music Project

https://vimeo.com/189923538

Code:

import blobDetection.*;
import processing.video.*;
import gab.opencv.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
import ddf.minim.signals.*;
import ddf.minim.spi.*;
import ddf.minim.ugens.*;
import java.awt.*;

Minim minim;
AudioPlayer music;
AudioInput input;
Capture video;
OpenCV opencv;
BeatDetect beat;
BlobDetection BlobD;
PImage img;
boolean newFrame=false;
float thick;
float eRadius;

void setup(){
size(800,600);
video = new Capture(this,800/2,600/2);
opencv= new OpenCV(this,800/2,600/2);
opencv.findCannyEdges(20,75);
opencv.startBackgroundSubtraction(5,3,0.5);
opencv.loadCascade(OpenCV.CASCADE_FRONTALFACE);

video.start();

minim = new Minim(this);
music = minim.loadFile(“Up All Night.mp3”,512);
music.play();
beat = new BeatDetect();
img=new PImage(80,60);
BlobD=new BlobDetection(img.width,img.height);
BlobD.setPosDiscrimination(true);
BlobD.setThreshold(0.2f);
}

void draw(){
scale(2);
opencv.loadImage(video);
opencv.updateBackground();
opencv.dilate();
opencv.erode();
image(video,0,0);
fill(155,0,155);
rect(0,0,width,height);
beat.detect(music.mix);
float a = map(thick,140,115,130,255);
float b = map(eRadius,40,115,30,255);
if(beat.isOnset()) thick=10;
strokeWeight(2);
stroke(0,125,0);
fill(0,255,0,b);
if(beat.isOnset()) eRadius=250;
ellipse(width/4,height/4,eRadius,eRadius);
eRadius*=0.91;
if(eRadius<20)eRadius=20;
strokeWeight(1);
stroke(255,0,0);
for(int i=0; i<music.bufferSize()-1;i++){
line(i,270+music.right.get(i)*20,i+1,270+music.right.get(i+1)*20);
}
strokeWeight(2);
fill(100,100,255,a);
stroke(0,0,255);
thick *= 0.5;
if(thick<5) thick=5;
for(Contour contour: opencv.findContours()){
contour.draw();
}
if(newFrame){
newFrame=false;
image(video,0,0,width,height);
img.copy(video,0,0,video.width,video.height,0,0,img.width,img.height);
fastblur(img, 2);
BlobD.computeBlobs(img.pixels);
drawBlobsAndEdges(true,true);
}
}

void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges){
noFill();
Blob b;
EdgeVertex eA, eB;
for (int n=0;n<BlobD.getBlobNb();n++){
b=BlobD.getBlob(n);
if(b!=null){
if(drawEdges){
strokeWeight(3);
stroke(255,0,255);
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(3);
stroke(10,10,255);
rect(b.xMin*width,b.yMin*height,b.w*width,b.h*height);
}
}
}
}
}

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;
}
}

}

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

Advertisements

About degrafgo

I am a fun, green loving guy who owns 3 YouTube channels under the name DeGraphics.
This entry was posted in Time Based and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s