xxxxxxxxxx
42
/**
* @name Frequency Spectrum
* @description <p>Visualize the frequency spectrum of live audio input.</p>
* <p><em><span class="small"> To run this example locally, you will need the
* <a href="http://p5js.org/reference/#/libraries/p5.sound">p5.sound library</a>
* and a running <a href="https://github.com/processing/p5.js/wiki/Local-server">local server</a>.</span></em></p>
*/
let mic, fft, banana;
function setup() {
createCanvas(710, 400);
noFill();
banana = new p5.Filter();
banana.setType('bandpass');
banana.freq(300);
banana.res(0.1);
banana.disconnect();
mic = new p5.AudioIn();
mic.connect(banana);
mic.start();
fft = new p5.FFT();
fft.setInput(banana);
}
function draw() {
background(200);
let spectrum = fft.analyze(128);
// beginShape();
let offset = 8;
for (i = offset; i < (spectrum.length / 8) + offset; i++) {
// circle(40*(i-offset), 200, spectrum[i]);
let origin = 40 * (i-offset);
triangle(origin, 200, origin + 38, 200, origin, 200 - spectrum[i]);
// vertex(i, map(spectrum[i], 0, 255, height, 0));
}
// endShape();
}