Forked from brendandawes/Ben Folds Five Poster - Cityscape Generator.pde
Last active
December 14, 2015 18:59
-
-
Save hungrymedia/5133550 to your computer and use it in GitHub Desktop.
Adapted from Brendan Dawes' cityscape generator shown at OFFF Cincinnati. I instantly saw the buildings as frequency meters, so set about making them behave as such. My first foray into audio analysis with Processing. Video demo: https://vimeo.com/61520834
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Ben Folds Five Poster - Cityscape Generator by Brendan Dawes | |
// http://brendandawes.com/projects/benfoldsfive/ | |
// Adapted an animated by Warren Harrison (hungry-media.com) | |
// Any music.mp3 file in the same folder as the .pde will work, but I used | |
// a Ben Folds song in the spirit of the original: http://goo.gl/BEBn5 | |
// Requires library from http://toxiclibs.org | |
import toxi.color.*; | |
import toxi.util.datatypes.*; | |
import ddf.minim.analysis.*; | |
import ddf.minim.*; | |
final color BACKGROUND_COLOR = color(240); | |
final int NUMBER_OF_COLORS = 2; | |
final int NUMBER_OF_CITIES = 10; | |
final int BUILDING_HEIGHT = 120; | |
int xStep = 175; | |
int yStep = 200; | |
int cols = 3; | |
int totalWidth = xStep*cols; | |
float noiseCounter; | |
ColorList palette = new ColorList(); | |
int m = millis(); | |
float xCenterOffset = (width/2)-(totalWidth/2); | |
ArrayList cities = new ArrayList(); | |
Minim minim; | |
AudioPlayer music; | |
FFT fftLog; | |
void setup() { | |
size(648,864); | |
background(BACKGROUND_COLOR); | |
noStroke(); | |
noiseDetail(8,0.5); | |
palette = getColorPalette(); | |
for (int i=0; i < NUMBER_OF_CITIES; i++) { | |
cities.add( makeCity() ); | |
} | |
minim = new Minim(this); | |
music = minim.loadFile("music.mp3", 2048); | |
music.loop(); | |
fftLog = new FFT(music.bufferSize(), music.sampleRate()); | |
fftLog.logAverages(8, 2); | |
} | |
void draw() { | |
background(BACKGROUND_COLOR); | |
fftLog.forward(music.mix); | |
// loop over cities and draw them | |
for(int i=0; i < NUMBER_OF_CITIES; i++){ | |
float x = floor((i*xStep)%totalWidth); | |
float y = floor((i*xStep)/totalWidth)*yStep; | |
pushMatrix(); | |
translate(x+100,y+150); | |
noStroke(); | |
// each city | |
ArrayList city = (ArrayList)cities.get(i); | |
int buildingCtr = 0; | |
for(int j=0; j < city.size(); j++){ | |
// each rect | |
ColorRect rectangle = (ColorRect) city.get(j); | |
if(!rectangle.isWindow){ | |
buildingCtr++; | |
} | |
rectangle.display(); | |
fill(rectangle.c); | |
rect(rectangle.xpos,rectangle.ypos,rectangle.w,rectangle.h - fftLog.getAvg(buildingCtr)/10); | |
} | |
popMatrix(); | |
} | |
} | |
ColorList getColorPalette() { | |
ColorTheme theme = new ColorTheme("theme"); | |
theme.addRange("soft teal",0.5); | |
theme.addRange("soft ivory",0.1); | |
ColorList l = theme.getColors(NUMBER_OF_COLORS); | |
return l; | |
} | |
boolean isMinorKey(int i){ | |
if (i == 1 || i == 3 || i == 6 || i == 8 || i == 10) { | |
return true; | |
} else { | |
return false; | |
} | |
} | |
ColorRect drawMinorKey(float x, float y) { | |
fill(0); | |
float w = max(9,random(20)); | |
float h = max(3,random(30)); | |
ColorRect key = new ColorRect(color(0), x, y, w, -h, false); | |
return key; | |
} | |
ArrayList drawMajorKey(float x, float y){ | |
color c = palette.get(int(random(palette.size()))).toARGB(); | |
fill(c); | |
float w = max(8,random(20)); | |
float n = noise(noiseCounter); | |
float h = n*BUILDING_HEIGHT; | |
ArrayList majorkeyData = new ArrayList(); | |
ColorRect key = new ColorRect(c, x, y, w, -h, false); | |
majorkeyData.add(key); | |
for (int j=0; j < random(5); j++) { | |
float winY = (h/2)+random(h/2); | |
float winX = 2+random(w-4); | |
fill(BACKGROUND_COLOR); | |
ColorRect window = new ColorRect(BACKGROUND_COLOR, x+winX, y-winY, random(3), random(3), true); | |
majorkeyData.add(window); | |
noiseCounter += 0.5; | |
} | |
return majorkeyData; | |
} | |
ArrayList makeCity() { | |
float x; | |
float y; | |
noiseCounter = random(1000); | |
ArrayList city = new ArrayList(); | |
for (int i=0; i < 12; i++) { | |
x = (9*i); | |
y = 0; | |
if (isMinorKey(i)) { | |
city.add( drawMinorKey(x,y) ); | |
} else { | |
// for each array in drawMajorKey, push onto city | |
ArrayList majorKeyData = drawMajorKey(x,y); | |
for(int k = 0; k < majorKeyData.size(); k++){ | |
city.add( majorKeyData.get(k) ); | |
} | |
} | |
} | |
return city; | |
} | |
class ColorRect{ | |
color c; | |
float xpos; | |
float ypos; | |
float w; | |
float h; | |
boolean isWindow; | |
ColorRect(color _c, float _xpos, float _ypos, float _w, float _h, boolean _isWindow){ | |
c = _c; | |
xpos = _xpos; | |
ypos = _ypos; | |
w = _w; | |
h = _h; | |
isWindow = _isWindow; | |
} | |
void display(){ | |
fill(c); | |
rect(xpos, ypos, w, h); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment