Skip to content

Commit 0629480

Browse files
committed
Fix bar levels when shown less than 512
Fix for FFTControl2 showing just 16 bars
1 parent 9ff0083 commit 0629480

File tree

11 files changed

+93
-15
lines changed

11 files changed

+93
-15
lines changed

WASApiBassNet/Components/FFT/FFTAnalyzer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public int BarsCount
2121
}
2222
}
2323

24+
public int ShowingBarsCount { get; set; }
25+
2426
/// <inheritdoc/>
2527
public IFFTProvider FFTProvider { get; set; }
2628

WASApiBassNet/Components/FFT/FFTPeakAnalyzer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ public int BarsCount
2121
}
2222
}
2323

24+
int _showingBarsCount;
25+
public int ShowingBarsCount
26+
{
27+
get => _showingBarsCount;
28+
set
29+
{
30+
_showingBarsCount = value;
31+
}
32+
}
33+
2434
/// <inheritdoc/>
2535
public IFFTAnalyzer FFTAnalyzer { get; set; }
2636

WASApiBassNet/Components/FFT/IFFTAnalyzer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,6 @@ public interface IFFTAnalyzer : IAudioPlugin
2121
/// bars count
2222
/// </summary>
2323
int BarsCount { get; set; }
24+
int ShowingBarsCount { get; set; }
2425
}
2526
}

WASApiBassNet/Components/FFT/IFFTPeakAnalyzer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ public interface IFFTPeakAnalyzer : IAudioPlugin
2626
/// bars count
2727
/// </summary>
2828
int BarsCount { get; set; }
29+
int ShowingBarsCount { get; set; }
2930
}
3031
}

WindowsAudioSession/AppComponents.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
22
using System.Windows.Media;
33

44
using WASApiBassNet.Components.AudioCapture;
@@ -142,15 +142,18 @@ public void BuildComponents(IWASMainViewModel viewModel)
142142
// FFT component #2
143143

144144
App.WASMainWindow.fftControl2.ViewModel = FFTViewModel2;
145+
FFTViewModel2.ShowingBarCount = App.WASMainWindow.fftControl2.ShowingBarCount;
145146
FFTAnalyser2.FFTProvider = FFTProvider;
146147
FFTAnalyser2.BarsCount = FFTViewModel2.BarCount;
147148
FFTDrawer2.BarBrush = HatchRawBrush.Create(Brushes.LightGreen, 4, 3);
149+
FFTAnalyser2.ShowingBarsCount = FFTViewModel2.ShowingBarCount;
148150
FFTDrawer2.BarWidthPercent = FFTViewModel2.BarWidthPercent;
149151
FFTDrawer2.Drawable = App.WASMainWindow.fftControl2;
150152
FFTDrawer2.FFTAnalyser = FFTAnalyser2;
151-
153+
152154
FFTPeakAnalyser2.FFTAnalyzer = FFTAnalyser2;
153155
FFTPeakAnalyser2.BarsCount = FFTViewModel2.BarCount;
156+
FFTPeakAnalyser2.ShowingBarsCount = FFTViewModel2.ShowingBarCount;
154157
FFTPeakDrawer.WidthPercent = 80d;
155158
FFTPeakDrawer.Drawable = App.WASMainWindow.fftControl2;
156159
FFTPeakDrawer.FFTPeakAnalyser = FFTPeakAnalyser2;

WindowsAudioSession/UI/FFT/FFTControl.xaml.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ public int BarCount
8686
public static readonly DependencyProperty BarCountProperty =
8787
DependencyProperty.Register("BarCount", typeof(int), typeof(FFTControl), new PropertyMetadata(512));
8888

89+
public int ShowingBarCount
90+
{
91+
get => (int)GetValue(ShowingBarCountProperty);
92+
set
93+
{
94+
SetValue(ShowingBarCountProperty, value);
95+
ViewModel.ShowingBarCount = value;
96+
}
97+
}
98+
99+
public static readonly DependencyProperty ShowingBarCountProperty =
100+
DependencyProperty.Register("ShowingBarCount", typeof(int), typeof(FFTControl), new PropertyMetadata(512));
101+
89102
/// <inheritdoc/>
90103
public int BarWidthPercent
91104
{

WindowsAudioSession/UI/FFT/FFTDrawer.cs

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Linq;
23
using System.Windows.Controls;
34
using System.Windows.Media;
45
using System.Windows.Shapes;
@@ -46,13 +47,16 @@ double[] barSizes
4647
{
4748
var canvas = Drawable.GetDrawingSurface();
4849
var barCount = barSizes.Length;
49-
var barMaxWidth = (width - (2d * Margin)) / barCount;
50+
var showingBarCount = FFTAnalyser.ShowingBarsCount;
51+
showingBarCount = (showingBarCount > 0 && (showingBarCount % 2) == 0) ? showingBarCount : barCount;
52+
var showingBarRatio = (showingBarCount > 0) ? barCount / showingBarCount : 1;
53+
var barMaxWidth = (width - (2d * Margin)) / showingBarCount;
5054
var barWidth = barMaxWidth * BarWidthPercent / 100d;
5155

5256
if (_bars == null)
5357
{
54-
_bars = new Rectangle[barCount];
55-
for (var i = 0; i < barCount; i++)
58+
_bars = new Rectangle[showingBarCount];
59+
for (var i = 0; i < showingBarCount; i++)
5660
{
5761
var bar = new Rectangle()
5862
{
@@ -65,9 +69,23 @@ double[] barSizes
6569

6670
var x = x0;
6771

68-
for (var i = 0; i < barCount; i++)
72+
for (var i = 0; i < showingBarCount; i++)
6973
{
70-
var barHeight = Math.Max(0, barSizes[i] * (height - 2 * Margin) / 255d);
74+
double maxValue;
75+
76+
if (showingBarRatio > 1)
77+
{
78+
int startIndex = i * showingBarRatio;
79+
int endIndex = (i + 1) * showingBarRatio;
80+
81+
maxValue = barSizes.Skip(startIndex).Take(endIndex - startIndex).Max();
82+
}
83+
else
84+
{
85+
maxValue = barSizes[i];
86+
}
87+
88+
var barHeight = Math.Max(0, maxValue * (height - 2 * Margin) / 255d);
7189
var y_top = y0 + height - 2 * Margin - barHeight;
7290

7391
var bar = _bars[i];

WindowsAudioSession/UI/FFT/FFTPeakDrawer.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Linq;
23
using System.Windows.Controls;
34
using System.Windows.Media;
45
using System.Windows.Shapes;
@@ -47,13 +48,16 @@ double[] barSizes
4748
{
4849
var canvas = Drawable.GetDrawingSurface();
4950
var barCount = barSizes.Length;
50-
var barMaxWidth = (width - (2d * Margin)) / barCount;
51+
var showingBarCount = FFTPeakAnalyser.ShowingBarsCount;
52+
showingBarCount = (showingBarCount > 0 && (showingBarCount % 2) == 0) ? showingBarCount : barCount;
53+
var showingBarRatio = (showingBarCount > 0) ? barCount / showingBarCount : 1;
54+
var barMaxWidth = (width - (2d * Margin)) / showingBarCount;
5155
var barWidth = barMaxWidth * WidthPercent / 100d;
5256

5357
if (_bars == null)
5458
{
55-
_bars = new Rectangle[barCount];
56-
for (var i = 0; i < barCount; i++)
59+
_bars = new Rectangle[showingBarCount];
60+
for (var i = 0; i < showingBarCount; i++)
5761
{
5862
var bar = new Rectangle();
5963
_bars[i] = bar;
@@ -64,10 +68,24 @@ double[] barSizes
6468

6569
var x = x0;
6670

67-
for (var i = 0; i < barCount; i++)
71+
for (var i = 0; i < showingBarCount; i++)
6872
{
69-
var barHeight = Math.Max(0, barSizes[i] * (height - 2 * Margin) / 255d);
70-
var y_top = y0 + height - 2 * Margin - barHeight;
73+
double maxValue;
74+
75+
if (showingBarRatio > 1)
76+
{
77+
int startIndex = i * showingBarRatio;
78+
int endIndex = (i + 1) * showingBarRatio;
79+
80+
maxValue = barSizes.Skip(startIndex).Take(endIndex - startIndex).Max();
81+
}
82+
else
83+
{
84+
maxValue = barSizes[i];
85+
}
86+
87+
var barHeight = Math.Max(0, maxValue * (height - 2 * Margin) / 255d);
88+
var y_top = (y0 + height - 2 * Margin - barHeight);
7189

7290
var bar = _bars[i];
7391

WindowsAudioSession/UI/FFT/FFTViewModel.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ public int BarCount
2525
}
2626
}
2727

28+
int _showingBarCount = 512;
29+
public int ShowingBarCount
30+
{
31+
get => _showingBarCount;
32+
set
33+
{
34+
_showingBarCount = value;
35+
}
36+
}
37+
2838
int _barWidthPercent = 100;
2939

3040
/// <summary>

WindowsAudioSession/UI/FFT/IFFTControl.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public interface IFFTControl : IDrawable
3232
/// bar count
3333
/// </summary>
3434
int BarCount { get; set; }
35+
int ShowingBarCount { get; set; }
3536

3637
/// <summary>
3738
/// bar width percent

0 commit comments

Comments
 (0)