chiro-canto/public/larynx/scripts/p5.spectro.js
2021-04-15 12:29:53 +02:00

56 lines
1.1 KiB
JavaScript
Executable File

let mic, sound;
let audio = document.getElementById('audio');
let fft;
let currentLine = 0;
var file;
function preload() {
file = loadSound(audio.src);
}
function setup() {
createCanvas(windowWidth, windowHeight);
var smoothing = 0.6;
var bins = 2048;
fft = new p5.FFT(smoothing, bins);
fft.setInput(file);
file.loop();
background(0);
}
function draw() {
var spectrum = fft.analyze();
// iterate thru current freq spectrum
for (var i = 0; i < spectrum.length; i++) {
var value = spectrum[i];
var c = value;
fill(c);
noStroke();
var hauteurDuRectangle = height / (spectrum.length);
var y = map(i, 0, spectrum.length, height - hauteurDuRectangle, 0);
rect(currentLine, y, 4, hauteurDuRectangle);
}
currentLine = currentLine + 4;
if (currentLine > width) currentLine = 0;
}
function logScale(index, total, opt_base) {
var base = opt_base || 2;
var logmax = logBase(total + 1, base);
var exp = logmax * index / total;
return Math.round(Math.pow(base, exp) - 1);
}
function logBase(val, base) {
return Math.log(val) / Math.log(base);
}