From 550712d2e03104c6fb12f8a402d8305270dd7da8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20B=C3=A4dorf?= Date: Sat, 8 Jun 2019 16:29:22 +0200 Subject: [PATCH] Pitch bars colors --- src/index.js | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index 9223541..635212f 100644 --- a/src/index.js +++ b/src/index.js @@ -43,6 +43,8 @@ async function main() { let fresh = false; let spectrum = []; let pitch = 0; + let minPitch = 90; + let maxPitch = 90; let volume = 0; let volumeThreshold = 0; let bars = []; @@ -62,9 +64,16 @@ async function main() { const currentVolume = getRMS(spectrum); // get peak - a hack when our volumes are low - if (currentVolume > volumeThreshold) volumeThreshold = currentVolume; + if (currentVolume > volumeThreshold) { + volumeThreshold = currentVolume; + } volume = currentVolume; pitch = getPitch(spectrum, volume, volumeThreshold); + if (pitch > maxPitch) { + maxPitch = pitch + } else if (pitch < minPitch) { + minPitch = pitch; + } } const input = audioCtx.createMediaStreamSource(microphone); @@ -97,7 +106,8 @@ async function main() { const newBars = []; 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); bar.age /= 2; if (bar.age > 0.1) { @@ -108,6 +118,10 @@ async function main() { bars = newBars; }; }); + + setTimeout(() => { + // analyser.disconnect(); + }, 5000); } main();