xxxxxxxxxx
61
/**
this is a copy from https://editor.p5js.org/p5/sketches/Sound:_FFT_Spectrum
* @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;
let prev;
let t = 0;
let dt = 0.005;
let thestyle = 'bars'
function setup() {
createCanvas(710, 400);
background(0)
colorMode(HSB);
noFill();
mic = new p5.AudioIn();
mic.start();
fft = new p5.FFT();
fft.setInput(mic);
prev = [0, height];
selector = createSelect('hi');
selector.option('bars');
selector.option('lined')
selector.input((e) => {background(0);thestyle=selector.value()})
}
function draw() {
//
let spectrum = fft.analyze();
for (i = 0; i < spectrum.length; i++) {
stroke(noise(t)*255, 255,map(spectrum[i], 0, 255, height, 0));
switch(thestyle) {
case 'bars':
line(i,height, i, map(spectrum[i], 0, 255, height, 0));
break;
case 'lined':
line(prev[0], prev[1], i, map(spectrum[i], 0, 255, height, 0));
default:
break
}
prev = [i, map(spectrum[i], 0, 255, height, 0)]
}
prev = [0, height];
t+=dt;
}