Last active
March 20, 2021 02:28
-
-
Save kasthor/e6b4e72db38eab2b76eef3b81de674df to your computer and use it in GitHub Desktop.
p5 Audio analysis with FFT
This file contains hidden or 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
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