Skip to content

Instantly share code, notes, and snippets.

@dramalho
Created March 6, 2016 15:27
Show Gist options
  • Save dramalho/ff4151c8a3ab649be915 to your computer and use it in GitHub Desktop.
Save dramalho/ff4151c8a3ab649be915 to your computer and use it in GitHub Desktop.
Processing Sound experiment #1
import processing.sound.*;
FFT fft;
AudioIn in;
Amplitude amp;
int bands = 512;
float[] spectrum = new float[bands];
void stop() {
in.stop();
super.stop();
}
void setup() {
size(512, 360);
//fullScreen();
background(255);
// Create an Input stream which is routed into the Amplitude analyzer
fft = new FFT(this, bands);
in = new AudioIn(this, 0);
// start the Audio Input
in.start();
// patch the AudioIn
fft.input(in);
strokeWeight(2);
}
void draw() {
fft.analyze(spectrum);
float max = 0;
int max_idx = 0;
int[] circles = new int[bands];
for(int i = 0; i < bands; i++){
if (spectrum[i] > 0.005) {
circles[i] = Math.round(spectrum[i] * 1000);
}
}
background(0);
stroke(255 - max_idx/3);
for(int i = 0; i < bands; i++){
if (circles[i] > 0) {
fill(circles[i] / 2, circles[i] / 3, i/2);
ellipse(i, height/3, circles[i], circles[i] );
}
}
for(int i = 0; i < bands; i++){
// The result of the FFT is normalized
// draw the line for frequency band i scaling it up by 5 to get more amplitude.
line( i, (height/2) - (spectrum[i]*height*5)/2, i, (height/2) + (spectrum[i]*height*5)/2 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment