From 45bf10105b360a2bd728e48f56f473f7411301d7 Mon Sep 17 00:00:00 2001 From: Armin Date: Wed, 8 Jul 2026 19:05:06 +0200 Subject: [PATCH] Refactor VU meter peak hold to use direct variable access --- index.html | 33 +++++++++++++++++++-------------- 1 file changed, 19 insertions(+), 14 deletions(-) diff --git a/index.html b/index.html index ee4c17e..4495187 100644 --- a/index.html +++ b/index.html @@ -583,15 +583,11 @@ function drawSpectrum() { if (levelR > vuSmoothR) vuSmoothR = levelR; else vuSmoothR = vuSmoothR * vuSmooth + levelR * (1 - vuSmooth); levelL = vuSmoothL; levelR = vuSmoothR; - function drawVuRow(y, level, peak, hold) { + function drawVuRow(y, level) { const lit = Math.round(level * segCount); - if (level > peak) { peak = level; hold = 0; } - else if (hold < vuPeakGrace) { hold++; } - else { peak *= 0.990; } for (let i = 0; i < segCount; i++) { const sx = segLeft + i * (segW + segG); const on = i < lit; - const pi = Math.round(peak * segCount); vuctx.fillStyle = on ? '#84a0c6' : '#1e2132'; vuctx.fillRect(sx, y, segW, rowH); if (on) { @@ -601,17 +597,26 @@ function drawSpectrum() { vuctx.fillStyle = grd; vuctx.fillRect(sx, y, segW, 2); } - if (i === pi && peak > 0.05) { - vuctx.fillStyle = '#e98989'; - vuctx.fillRect(sx, y, segW, rowH); - } } - return [peak, hold]; } - const rL = drawVuRow(0, levelL, vuPeakL, vuPeakHoldL); - vuPeakL = rL[0]; vuPeakHoldL = rL[1]; - const rR = drawVuRow(gap + rowH, levelR, vuPeakR, vuPeakHoldR); - vuPeakR = rR[0]; vuPeakHoldR = rR[1]; + if (levelL > vuPeakL) { vuPeakL = levelL; vuPeakHoldL = 0; } + else if (vuPeakHoldL < vuPeakGrace) { vuPeakHoldL++; } + else { vuPeakL *= 0.990; } + if (levelR > vuPeakR) { vuPeakR = levelR; vuPeakHoldR = 0; } + else if (vuPeakHoldR < vuPeakGrace) { vuPeakHoldR++; } + else { vuPeakR *= 0.990; } + drawVuRow(0, levelL); + drawVuRow(gap + rowH, levelR); + const piL = Math.round(vuPeakL * segCount); + const piR = Math.round(vuPeakR * segCount); + if (vuPeakL > 0.05) { + vuctx.fillStyle = '#e98989'; + vuctx.fillRect(segLeft + piL * (segW + segG), 0, segW, rowH); + } + if (vuPeakR > 0.05) { + vuctx.fillStyle = '#e98989'; + vuctx.fillRect(segLeft + piR * (segW + segG), gap + rowH, segW, rowH); + } } animId = requestAnimationFrame(drawSpectrum); }