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();