Skip to content

Instantly share code, notes, and snippets.

@mharju
Created September 29, 2012 20:17
Show Gist options
  • Save mharju/3805082 to your computer and use it in GitHub Desktop.
Save mharju/3805082 to your computer and use it in GitHub Desktop.
Unknown pleasures album cover visualization for Processing. This is the version in http://www.youtube.com/watch?v=nYOh_laT4_Q
import ddf.minim.*;
import ddf.minim.signals.*;
import ddf.minim.analysis.*;
import ddf.minim.effects.*;
Minim minim;
AudioPlayer player;
AudioInput input;
FFT fft;
int NBUF = 0, MAX_HEIGHT = 96,
MIN_VALUE = 0, MAX_VALUE = 127, NUM_SAMPLES = 32,
BLANK = 128, PADDING_LEFT = 96, PADDING_TOP = 170;
color bg, fg;
ArrayList buffers;
void setup()
{
fg = color(255, 255, 255);
bg = color(0, 0, 0);
size(700, 600, P3D);
background( bg );
stroke(fg);
fill(color(0, 0, 0));
minim = new Minim(this);
player = minim.loadFile("disorder.mp3");
player.loop();
input = minim.getLineIn();
fft = new FFT(player.bufferSize(), player.sampleRate());
frameRate( 25 );
smooth();
NBUF = fft.specSize();
buffers = new ArrayList();
for(int i=0;i<NUM_SAMPLES;i++)
{
byte[] data = new byte[NBUF];
next_buffer(data);
buffers.add( data );
}
}
void next_buffer(byte[] data)
{
fft.forward(player.mix);
for(int i=BLANK;i<data.length-BLANK;i++)
{
data[i] = (byte) map(fft.getBand(i), 0, 1, MIN_VALUE, MAX_VALUE);
if(data[i] < 0) data[i] = 0;
}
}
void plot_buffer(byte[] data)
{
beginShape();
for(int i=0;i<data.length;i+=16)
{
vertex(i, 0, map( data[i], MIN_VALUE, MAX_VALUE, 0, MAX_HEIGHT ));
}
endShape(CLOSE);
}
void draw()
{
background(bg);
translate(width/2, height/2);
//rotateX(radians((float)mouseY/(float)width * 360.0));
rotateX( PI / 6.0 );
translate(-width/2, -height/2);
for(int f=0;f<buffers.size();f++)
{
pushMatrix();
translate(PADDING_LEFT, PADDING_TOP + f * 10);
plot_buffer( (byte[]) buffers.get(f) );
popMatrix();
}
buffers.remove(buffers.size() - 1);
byte[] data = new byte[NBUF];
next_buffer(data);
buffers.add( 0, data);
}
void stop()
{
player.close();
input.close();
minim.stop();
super.stop();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment