From 17adece6ad2c3b07a5fc8180245b507e9285c8fe Mon Sep 17 00:00:00 2001
From: Martin Dahlgren <mdahlgrengadd@users.noreply.github.com>
Date: Mon, 3 Jun 2019 12:58:33 +0200
Subject: [PATCH] Add windowing before FFT to avoid flickering spectrogram

---
 servers/audio/effects/audio_effect_spectrum_analyzer.cpp | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/servers/audio/effects/audio_effect_spectrum_analyzer.cpp b/servers/audio/effects/audio_effect_spectrum_analyzer.cpp
index 05cba416be1..5a201b7dfac 100644
--- a/servers/audio/effects/audio_effect_spectrum_analyzer.cpp
+++ b/servers/audio/effects/audio_effect_spectrum_analyzer.cpp
@@ -81,9 +81,10 @@ void AudioEffectSpectrumAnalyzerInstance::process(const AudioFrame *p_src_frames
 
 		float *fftw = temporal_fft.ptrw();
 		for (int i = 0; i < to_fill; i++) { //left and right buffers
-			fftw[(i + temporal_fft_pos) * 2] = p_src_frames[i].l;
+			float window = -0.5 * Math::cos(2.0 * Math_PI * (double)i / (double)to_fill) + 0.5;
+			fftw[(i + temporal_fft_pos) * 2] = window * p_src_frames[i].l;
 			fftw[(i + temporal_fft_pos) * 2 + 1] = 0;
-			fftw[(i + temporal_fft_pos + fft_size * 2) * 2] = p_src_frames[i].r;
+			fftw[(i + temporal_fft_pos + fft_size * 2) * 2] = window * p_src_frames[i].r;
 			fftw[(i + temporal_fft_pos + fft_size * 2) * 2 + 1] = 0;
 		}