Interactive Music

For my interactive music program I incorporated the use of blob detection to detect light. So, the camera will pickup the brightest point on the screen and based upon the location of the light the stroke width and display of the fft will change. I also incorporated the use of key presses. The overall concept for my music piece is the feeling of music itself. This idea that music can be an expression of anything and the song “Goosebumps” by Travis Scott has these very deep base sounds and high pitched auto tune sounds that express a sense of lucidness. This idea of lucidness is the image that I wanted to create with my music piece.

Processing Code: 

import gab.opencv.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import processing.video.*;
import blobDetection.*;
OpenCV opencv;
Minim minim;
AudioPlayer song;
FFT fftLin;
int xPos = 0;
int hueLimit = 160;
Capture cam;
BlobDetection theBlobDetection;
PImage img;
PImage src;
boolean newFrame=false;
BeatDetect beat;
float eRadius;
float brightBall;
color RanColor;
int counter;
int interval=30;
float r;
float g;
float b;
float a;
float diam;
float x;
float y;

void setup()
{
size(940, 280);
String[] cameras = Capture.list();
cam = new Capture(this, 40*4, 30*4, cameras[0]);
cam.start();
src = cam;
opencv = new OpenCV(this, src);
img = new PImage(80,60);
theBlobDetection = new BlobDetection(img.width, img.height);
theBlobDetection.setPosDiscrimination(true);
theBlobDetection.setThreshold(0.5f); // will detect bright areas whose luminosity > 0.2f;
minim = new Minim(this);
song = minim.loadFile(“Goosebumps.mp3”, 512);
beat = new BeatDetect();
eRadius = 40;
song.loop();
fftLin = new FFT(song.bufferSize(), song.sampleRate());
fftLin.linAverages(40);
RanColor = color(random(255), random(255), random(255));
fill (RanColor);
ellipseMode(RADIUS);
eRadius = 20;
}

void captureEvent(Capture cam)
{
cam.read();
newFrame = false;

}

void draw()
{

if (newFrame)
{
newFrame=false;
image(cam,0,0,width,height);
img.copy(cam, 0, 0, cam.width, cam.height,
0, 0, img.width, img.height);
fastblur(img, 2);
theBlobDetection.computeBlobs(img.pixels);
drawBlobsAndEdges(true,true);

}
fftLin.forward(song.mix);
stroke(255,mouseY,0,137);
for(int i = 0; i < fftLin.specSize(); i++)
{
float hueValue = hueLimit – map((fftLin.getBand(i)*32), 0, 256, 0, hueLimit);
colorMode(HSB, 256, 100, 100);

stroke(int(hueValue), 100, 80);
point(xPos, i);
}

xPos = xPos + 1;
if (xPos > width) {
xPos = 0;
}
opencv.loadImage(src);
image(opencv.getOutput(), 0, 0);
PVector loc = opencv.max();
stroke(120, 0, 0);
strokeWeight(55);
noFill();
ellipse(loc.x, loc.y, 2, 2);
if (loc.x <= 85) {
strokeWeight(20);
colorMode(HSB, 220, 80,60);
}
if (loc.y <= 70) {
strokeWeight(100);
colorMode(HSB, 140, 20, 70);
}
}

void drawBlobsAndEdges(boolean drawBlobs, boolean drawEdges)
{
noFill();
Blob b;
EdgeVertex eA,eB;
for (int n=0 ; n<theBlobDetection.getBlobNb() ; n++)
{
b=theBlobDetection.getBlob(n);
if (b!=null)
{
// Edges
if (drawEdges)
{
strokeWeight(70);
stroke(0,255,145);
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
);
}
}

// Blobs
if (drawBlobs)
{
strokeWeight(10);
stroke(10,0,10);
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;
}
}
image(cam,0,0,50,50);
}

void keyPressed() {
if (key == ‘a’) {
fill (random(255));
rect(width,height, 20,20);
}

if (key == ‘s’) {
r = random(255);
g = random(255);
b = random(255);
a = random(255);
diam = random(80);
x = random(width);
y = random(height);
// Use values to draw an ellipse
noStroke();
fill(r,g,b,a);
ellipse(x/4,y*4,diam+15,diam+15);
noStroke();
fill(r,g,b,a);
rect(x,y,diam,diam);
noStroke();
fill(r,g,b,a);
ellipse(x*100,y/100,diam,diam);
noStroke();
fill(r,g,b,a);
rect(x,y,diam,diam);
noStroke();
fill(r,g,b,a);
ellipse(x,y,diam,diam);
}

if (key == ‘d’) {
background(RanColor);
beat.detect(song.mix);
float a = map(eRadius, 20, 80, 60, 255);
fill(60, 255, 0, a);
if ( beat.isOnset() ) eRadius = 80;
ellipse(width/2, height/2, eRadius, eRadius);
eRadius *= 0.95;
if ( eRadius < 20 ) eRadius = 20;

}

}

Advertisements
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