From 06294804444a44714ff8ae9d2aa79c323dc6b543 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Kotas?= Date: Tue, 26 Sep 2023 03:49:44 +0200 Subject: [PATCH] Fix bar levels when shown less than 512 Fix for FFTControl2 showing just 16 bars --- WASApiBassNet/Components/FFT/FFTAnalyzer.cs | 2 ++ .../Components/FFT/FFTPeakAnalyzer.cs | 10 ++++++ WASApiBassNet/Components/FFT/IFFTAnalyzer.cs | 1 + .../Components/FFT/IFFTPeakAnalyzer.cs | 1 + WindowsAudioSession/AppComponents.cs | 7 ++-- WindowsAudioSession/UI/FFT/FFTControl.xaml.cs | 13 ++++++++ WindowsAudioSession/UI/FFT/FFTDrawer.cs | 30 +++++++++++++---- WindowsAudioSession/UI/FFT/FFTPeakDrawer.cs | 32 +++++++++++++++---- WindowsAudioSession/UI/FFT/FFTViewModel.cs | 10 ++++++ WindowsAudioSession/UI/FFT/IFFTControl.cs | 1 + WindowsAudioSession/UI/FFT/IFFTViewModel.cs | 1 + 11 files changed, 93 insertions(+), 15 deletions(-) diff --git a/WASApiBassNet/Components/FFT/FFTAnalyzer.cs b/WASApiBassNet/Components/FFT/FFTAnalyzer.cs index b43352c..87e7d36 100644 --- a/WASApiBassNet/Components/FFT/FFTAnalyzer.cs +++ b/WASApiBassNet/Components/FFT/FFTAnalyzer.cs @@ -21,6 +21,8 @@ public int BarsCount } } + public int ShowingBarsCount { get; set; } + /// public IFFTProvider FFTProvider { get; set; } diff --git a/WASApiBassNet/Components/FFT/FFTPeakAnalyzer.cs b/WASApiBassNet/Components/FFT/FFTPeakAnalyzer.cs index 8a9d632..fb81f37 100644 --- a/WASApiBassNet/Components/FFT/FFTPeakAnalyzer.cs +++ b/WASApiBassNet/Components/FFT/FFTPeakAnalyzer.cs @@ -21,6 +21,16 @@ public int BarsCount } } + int _showingBarsCount; + public int ShowingBarsCount + { + get => _showingBarsCount; + set + { + _showingBarsCount = value; + } + } + /// public IFFTAnalyzer FFTAnalyzer { get; set; } diff --git a/WASApiBassNet/Components/FFT/IFFTAnalyzer.cs b/WASApiBassNet/Components/FFT/IFFTAnalyzer.cs index e04299b..eb9aa92 100644 --- a/WASApiBassNet/Components/FFT/IFFTAnalyzer.cs +++ b/WASApiBassNet/Components/FFT/IFFTAnalyzer.cs @@ -21,5 +21,6 @@ public interface IFFTAnalyzer : IAudioPlugin /// bars count /// int BarsCount { get; set; } + int ShowingBarsCount { get; set; } } } diff --git a/WASApiBassNet/Components/FFT/IFFTPeakAnalyzer.cs b/WASApiBassNet/Components/FFT/IFFTPeakAnalyzer.cs index bf8a522..1444861 100644 --- a/WASApiBassNet/Components/FFT/IFFTPeakAnalyzer.cs +++ b/WASApiBassNet/Components/FFT/IFFTPeakAnalyzer.cs @@ -26,5 +26,6 @@ public interface IFFTPeakAnalyzer : IAudioPlugin /// bars count /// int BarsCount { get; set; } + int ShowingBarsCount { get; set; } } } diff --git a/WindowsAudioSession/AppComponents.cs b/WindowsAudioSession/AppComponents.cs index 08797dd..5d09b0d 100644 --- a/WindowsAudioSession/AppComponents.cs +++ b/WindowsAudioSession/AppComponents.cs @@ -1,4 +1,4 @@ - + using System.Windows.Media; using WASApiBassNet.Components.AudioCapture; @@ -142,15 +142,18 @@ public void BuildComponents(IWASMainViewModel viewModel) // FFT component #2 App.WASMainWindow.fftControl2.ViewModel = FFTViewModel2; + FFTViewModel2.ShowingBarCount = App.WASMainWindow.fftControl2.ShowingBarCount; FFTAnalyser2.FFTProvider = FFTProvider; FFTAnalyser2.BarsCount = FFTViewModel2.BarCount; FFTDrawer2.BarBrush = HatchRawBrush.Create(Brushes.LightGreen, 4, 3); + FFTAnalyser2.ShowingBarsCount = FFTViewModel2.ShowingBarCount; FFTDrawer2.BarWidthPercent = FFTViewModel2.BarWidthPercent; FFTDrawer2.Drawable = App.WASMainWindow.fftControl2; FFTDrawer2.FFTAnalyser = FFTAnalyser2; - + FFTPeakAnalyser2.FFTAnalyzer = FFTAnalyser2; FFTPeakAnalyser2.BarsCount = FFTViewModel2.BarCount; + FFTPeakAnalyser2.ShowingBarsCount = FFTViewModel2.ShowingBarCount; FFTPeakDrawer.WidthPercent = 80d; FFTPeakDrawer.Drawable = App.WASMainWindow.fftControl2; FFTPeakDrawer.FFTPeakAnalyser = FFTPeakAnalyser2; diff --git a/WindowsAudioSession/UI/FFT/FFTControl.xaml.cs b/WindowsAudioSession/UI/FFT/FFTControl.xaml.cs index befa747..e57f0f1 100644 --- a/WindowsAudioSession/UI/FFT/FFTControl.xaml.cs +++ b/WindowsAudioSession/UI/FFT/FFTControl.xaml.cs @@ -86,6 +86,19 @@ public int BarCount public static readonly DependencyProperty BarCountProperty = DependencyProperty.Register("BarCount", typeof(int), typeof(FFTControl), new PropertyMetadata(512)); + public int ShowingBarCount + { + get => (int)GetValue(ShowingBarCountProperty); + set + { + SetValue(ShowingBarCountProperty, value); + ViewModel.ShowingBarCount = value; + } + } + + public static readonly DependencyProperty ShowingBarCountProperty = + DependencyProperty.Register("ShowingBarCount", typeof(int), typeof(FFTControl), new PropertyMetadata(512)); + /// public int BarWidthPercent { diff --git a/WindowsAudioSession/UI/FFT/FFTDrawer.cs b/WindowsAudioSession/UI/FFT/FFTDrawer.cs index 1305e8e..220acd2 100644 --- a/WindowsAudioSession/UI/FFT/FFTDrawer.cs +++ b/WindowsAudioSession/UI/FFT/FFTDrawer.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Linq; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; @@ -46,13 +47,16 @@ double[] barSizes { var canvas = Drawable.GetDrawingSurface(); var barCount = barSizes.Length; - var barMaxWidth = (width - (2d * Margin)) / barCount; + var showingBarCount = FFTAnalyser.ShowingBarsCount; + showingBarCount = (showingBarCount > 0 && (showingBarCount % 2) == 0) ? showingBarCount : barCount; + var showingBarRatio = (showingBarCount > 0) ? barCount / showingBarCount : 1; + var barMaxWidth = (width - (2d * Margin)) / showingBarCount; var barWidth = barMaxWidth * BarWidthPercent / 100d; if (_bars == null) { - _bars = new Rectangle[barCount]; - for (var i = 0; i < barCount; i++) + _bars = new Rectangle[showingBarCount]; + for (var i = 0; i < showingBarCount; i++) { var bar = new Rectangle() { @@ -65,9 +69,23 @@ double[] barSizes var x = x0; - for (var i = 0; i < barCount; i++) + for (var i = 0; i < showingBarCount; i++) { - var barHeight = Math.Max(0, barSizes[i] * (height - 2 * Margin) / 255d); + double maxValue; + + if (showingBarRatio > 1) + { + int startIndex = i * showingBarRatio; + int endIndex = (i + 1) * showingBarRatio; + + maxValue = barSizes.Skip(startIndex).Take(endIndex - startIndex).Max(); + } + else + { + maxValue = barSizes[i]; + } + + var barHeight = Math.Max(0, maxValue * (height - 2 * Margin) / 255d); var y_top = y0 + height - 2 * Margin - barHeight; var bar = _bars[i]; diff --git a/WindowsAudioSession/UI/FFT/FFTPeakDrawer.cs b/WindowsAudioSession/UI/FFT/FFTPeakDrawer.cs index 11d2aa3..f878367 100644 --- a/WindowsAudioSession/UI/FFT/FFTPeakDrawer.cs +++ b/WindowsAudioSession/UI/FFT/FFTPeakDrawer.cs @@ -1,4 +1,5 @@ -using System; +using System; +using System.Linq; using System.Windows.Controls; using System.Windows.Media; using System.Windows.Shapes; @@ -47,13 +48,16 @@ double[] barSizes { var canvas = Drawable.GetDrawingSurface(); var barCount = barSizes.Length; - var barMaxWidth = (width - (2d * Margin)) / barCount; + var showingBarCount = FFTPeakAnalyser.ShowingBarsCount; + showingBarCount = (showingBarCount > 0 && (showingBarCount % 2) == 0) ? showingBarCount : barCount; + var showingBarRatio = (showingBarCount > 0) ? barCount / showingBarCount : 1; + var barMaxWidth = (width - (2d * Margin)) / showingBarCount; var barWidth = barMaxWidth * WidthPercent / 100d; if (_bars == null) { - _bars = new Rectangle[barCount]; - for (var i = 0; i < barCount; i++) + _bars = new Rectangle[showingBarCount]; + for (var i = 0; i < showingBarCount; i++) { var bar = new Rectangle(); _bars[i] = bar; @@ -64,10 +68,24 @@ double[] barSizes var x = x0; - for (var i = 0; i < barCount; i++) + for (var i = 0; i < showingBarCount; i++) { - var barHeight = Math.Max(0, barSizes[i] * (height - 2 * Margin) / 255d); - var y_top = y0 + height - 2 * Margin - barHeight; + double maxValue; + + if (showingBarRatio > 1) + { + int startIndex = i * showingBarRatio; + int endIndex = (i + 1) * showingBarRatio; + + maxValue = barSizes.Skip(startIndex).Take(endIndex - startIndex).Max(); + } + else + { + maxValue = barSizes[i]; + } + + var barHeight = Math.Max(0, maxValue * (height - 2 * Margin) / 255d); + var y_top = (y0 + height - 2 * Margin - barHeight); var bar = _bars[i]; diff --git a/WindowsAudioSession/UI/FFT/FFTViewModel.cs b/WindowsAudioSession/UI/FFT/FFTViewModel.cs index 0969369..65649b1 100644 --- a/WindowsAudioSession/UI/FFT/FFTViewModel.cs +++ b/WindowsAudioSession/UI/FFT/FFTViewModel.cs @@ -25,6 +25,16 @@ public int BarCount } } + int _showingBarCount = 512; + public int ShowingBarCount + { + get => _showingBarCount; + set + { + _showingBarCount = value; + } + } + int _barWidthPercent = 100; /// diff --git a/WindowsAudioSession/UI/FFT/IFFTControl.cs b/WindowsAudioSession/UI/FFT/IFFTControl.cs index 7931878..2fdf812 100644 --- a/WindowsAudioSession/UI/FFT/IFFTControl.cs +++ b/WindowsAudioSession/UI/FFT/IFFTControl.cs @@ -32,6 +32,7 @@ public interface IFFTControl : IDrawable /// bar count /// int BarCount { get; set; } + int ShowingBarCount { get; set; } /// /// bar width percent diff --git a/WindowsAudioSession/UI/FFT/IFFTViewModel.cs b/WindowsAudioSession/UI/FFT/IFFTViewModel.cs index 4921057..ee8d2df 100644 --- a/WindowsAudioSession/UI/FFT/IFFTViewModel.cs +++ b/WindowsAudioSession/UI/FFT/IFFTViewModel.cs @@ -14,6 +14,7 @@ public interface IFFTViewModel : IModelBase, IValidableModel, IAudioPlugin /// bar count /// int BarCount { get; set; } + int ShowingBarCount { get; set; } /// /// bar width percent