Pitch bars colors

This commit is contained in:
Benjamin Bädorf 2019-06-08 16:29:22 +02:00
parent bc3f1e46ca
commit 550712d2e0

View file

@ -43,6 +43,8 @@ async function main() {
let fresh = false; let fresh = false;
let spectrum = []; let spectrum = [];
let pitch = 0; let pitch = 0;
let minPitch = 90;
let maxPitch = 90;
let volume = 0; let volume = 0;
let volumeThreshold = 0; let volumeThreshold = 0;
let bars = []; let bars = [];
@ -62,9 +64,16 @@ async function main() {
const currentVolume = getRMS(spectrum); const currentVolume = getRMS(spectrum);
// get peak - a hack when our volumes are low // get peak - a hack when our volumes are low
if (currentVolume > volumeThreshold) volumeThreshold = currentVolume; if (currentVolume > volumeThreshold) {
volumeThreshold = currentVolume;
}
volume = currentVolume; volume = currentVolume;
pitch = getPitch(spectrum, volume, volumeThreshold); pitch = getPitch(spectrum, volume, volumeThreshold);
if (pitch > maxPitch) {
maxPitch = pitch
} else if (pitch < minPitch) {
minPitch = pitch;
}
} }
const input = audioCtx.createMediaStreamSource(microphone); const input = audioCtx.createMediaStreamSource(microphone);
@ -97,7 +106,8 @@ async function main() {
const newBars = []; const newBars = [];
for (const bar of bars) { for (const bar of bars) {
sketch.fill(sketch.color(0, 0, (bar.volume / volumeThreshold) * 255)); const normalizedPitchColor = ((bar.pitch - minPitch) / (maxPitch - minPitch)) * 220;
sketch.fill(sketch.color(normalizedPitchColor, bar.volume, (bar.volume / volumeThreshold) * 255));
sketch.rect(bar.x, 0, bar.age * bar.volume * 2, window.innerHeight); sketch.rect(bar.x, 0, bar.age * bar.volume * 2, window.innerHeight);
bar.age /= 2; bar.age /= 2;
if (bar.age > 0.1) { if (bar.age > 0.1) {
@ -108,6 +118,10 @@ async function main() {
bars = newBars; bars = newBars;
}; };
}); });
setTimeout(() => {
// analyser.disconnect();
}, 5000);
} }
main(); main();