Skip to content

Instantly share code, notes, and snippets.

@kasthor
Last active March 20, 2021 02:28
Show Gist options
  • Save kasthor/e6b4e72db38eab2b76eef3b81de674df to your computer and use it in GitHub Desktop.
Save kasthor/e6b4e72db38eab2b76eef3b81de674df to your computer and use it in GitHub Desktop.
p5 Audio analysis with FFT
let fft, mic, waveform, spectrum;
function setupAudio(){
userStartAudio();
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT(0.9, 1024);
fft.setInput(mic);
}
function updateAudio(){
spectrum = fft.analyze();
waveform = fft.waveform();
}
function setup() {
createCanvas(windowWidth, windowHeight);
setupAudio();
background(30);
}
function draw(){
updateAudio();
background(30, 90);
stroke(255);
noFill();
// Draw Circle
push();
translate(width / 2, height / 2);
beginShape();
for(let i = 0; i < waveform.length; i++ ){
let angle = map(i, 0, waveform.length, 0, PI * 2)
let x = sin(angle) * map(waveform[i], -1, 1, 0,height / 2);
let y = cos(angle) * map(waveform[i], -1, 1, 0,height / 2);
curveVertex(x,y);
}
endShape(CLOSE);
pop();
// Draw Line
beginShape();
for(let i = 0; i < spectrum.length; i++ ){
let x = map(i, 0, spectrum.length, width/2 - height/4, width/2 + width /4);
let y = map(spectrum[i], 0, 255, height/2, 0);
vertex(x,y);
}
endShape();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment