Skip to content

Instantly share code, notes, and snippets.

@benfarahmand
Created April 27, 2014 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benfarahmand/11335079 to your computer and use it in GitHub Desktop.
Save benfarahmand/11335079 to your computer and use it in GitHub Desktop.
Giving Human Form to Audio Visualizers
import ddf.minim.analysis.*;
import ddf.minim.*;
import gab.opencv.*;
import processing.video.*;
OpenCV opencv;
Capture video;
Minim minim;
AudioPlayer jingle;
FFT fft;
AudioInput in;
float[] angle;
float[] y, x;
ArrayList<PVector> coordinates;
void setup()
{
coordinates = new ArrayList<PVector>();
size(displayWidth, displayHeight, P3D);
video = new Capture(this, 640/2, 480/2);
opencv = new OpenCV(this, 640/2, 480/2);
opencv.startBackgroundSubtraction(5, 3, 0.5);
video.start();
// size(1280,768,P3D);
minim = new Minim(this);
in = minim.getLineIn(Minim.STEREO, 2048, 192000.0);
fft = new FFT(in.bufferSize(), in.sampleRate());
y = new float[fft.specSize()];
x = new float[fft.specSize()];
angle = new float[fft.specSize()];
frameRate(240);
}
void draw()
{
opencv.loadImage(video);
opencv.updateBackground();
opencv.dilate();
opencv.erode();
background(0);
fft.forward(in.mix);
doubleAtomicSprocket();
}
void doubleAtomicSprocket() {
for (Contour contour : opencv.findContours()) {
for (PVector point : contour.getPolygonApproximation().getPoints()) {
coordinates.add(new PVector(point.x, point.y, 0));
}
}
noStroke();
pushMatrix();
for (int i = 0; i < fft.specSize() ; i++) {
y[i] = y[i] + fft.getBand(i)/100;
x[i] = x[i] + fft.getFreq(i)/100;
angle[i] = angle[i] + fft.getFreq(i)/2000;
fill(0, 255-fft.getFreq(i)*2, 255-fft.getBand(i)*2);
pushMatrix();
if ((int)map(i, 0, fft.specSize(), 0, coordinates.size()) < coordinates.size()) {
translate((map((coordinates.get((int)map(i, 0, fft.specSize(), 0, coordinates.size())).x), 0, 640/2, displayWidth, 0))%displayWidth,
(map((coordinates.get((int)map(i, 0, fft.specSize(), 0, coordinates.size())).y), 0, 480/2, 0, displayHeight))%displayHeight);
rotateX(sin(angle[i]/2));
rotateY(cos(angle[i]/2));
translate(0, (x[i]+(map((coordinates.get((int)map(i, 0, fft.specSize(), 0, coordinates.size())).x), 0, 640/2, displayWidth/10, 0)))%width/5,
(y[i]+(map((coordinates.get((int)map(i, 0, fft.specSize(), 0, coordinates.size())).y), 0, 480/2, 0, displayHeight/10)))%height/5);
box(fft.getBand(i)/20+fft.getFreq(i)/15);
}
else {
rotateX(sin(angle[i]/2));
rotateY(cos(angle[i]/2));
translate(0,(x[i])%width, (y[i])%height);
box(fft.getBand(i)/20+fft.getFreq(i)/15);
}
popMatrix();
}
popMatrix();
coordinates.clear();
}
void captureEvent(Capture c) {
c.read();
}
void stop()
{
// always close Minim audio classes when you finish with them
jingle.close();
minim.stop();
super.stop();
}
@benfarahmand
Copy link
Author

Download OpenCV for Processing from here: https://github.com/atduskgreg/opencv-processing/releases
And Here: http://opencv.org/

Probably need v2.4.5 of opencv from opencv.org, but I was able to get it to work with v2.4.9

@joshangehr
Copy link

Dude thank you so much for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment