From a712217fa19c802578191770d8f445f390c2263e Mon Sep 17 00:00:00 2001 From: Neko Ayaka Date: Sat, 20 Dec 2025 23:15:23 +0800 Subject: [PATCH] fix(stage-ui): incorrect logarithm --- .../gadgets/AudioSpectrumVisualizer.vue | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/packages/stage-ui/src/components/gadgets/AudioSpectrumVisualizer.vue b/packages/stage-ui/src/components/gadgets/AudioSpectrumVisualizer.vue index de0bb5ed8..db71a420e 100644 --- a/packages/stage-ui/src/components/gadgets/AudioSpectrumVisualizer.vue +++ b/packages/stage-ui/src/components/gadgets/AudioSpectrumVisualizer.vue @@ -8,6 +8,7 @@ const props = withDefaults(defineProps<{ }) const AMPLIFICATION = 5 +const BYTE_RANGE = 255 function getReductionFactor(index: number, totalBars: number) { const minFactor = 0.1 // More reduction for bass frequencies @@ -24,19 +25,21 @@ function getHeightBounds(totalBars: number) { return { minHeight, maxHeight } } -function toLogScale(value: number) { - const amplified = Math.max(0, value * AMPLIFICATION) - // Map the amplified value (0..AMP) into 0..1 using a logarithmic curve +function toLogScale(normalized: number) { const logMax = Math.log1p(AMPLIFICATION) - return logMax === 0 ? 0 : Math.log1p(amplified) / logMax + return logMax === 0 + ? 0 + : Math.log1p(Math.max(0, normalized) * AMPLIFICATION) / logMax } function getBarHeight(frequency: number, index: number) { const reductionFactor = getReductionFactor(index, props.frequencies.length) const { minHeight, maxHeight } = getHeightBounds(props.frequencies.length) + const normalizedFrequency = Math.min(1, Math.max(0, frequency / BYTE_RANGE)) * reductionFactor const normalized = props.scale === 'linear' - ? Math.min(1, Math.max(0, frequency * AMPLIFICATION * reductionFactor)) - : toLogScale(frequency * reductionFactor) + ? Math.min(1, normalizedFrequency * AMPLIFICATION) + : toLogScale(normalizedFrequency) + const scaled = normalized * 100 return Math.min(maxHeight, Math.max(minHeight, scaled)) }