17.9: Sound Visualization: Graphing Amplitude - p5.js Sound Tutorial
17.11: Sound Visualization: Frequency Analysis with FFT - p5.js Sound Tutorial
//Note: this code requires not only the p5.js library but the p5.js sound library, too.
let fft;
function setup() {
createCanvas(windowWidth, windowHeight);
//boilerplate audio analysis setup
let mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
}
function draw(){
background(0, 10);
fft.analyze(); // Compute the energy levels for each frequency band.
const energy = fft.getEnergy("treble");
circle(300, 300, energy * 2);
}
Uncaught TypeError: p5.AudioIn is not a constructor
- import the p5.sound library//setup:
let mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
//per frame, e.g. in p5.js draw() function:
fft.analyze();
// for each frequency of interest
const level = fft.getEnergy(freq);
//do something with level (colour / size)