Skip to content

Fix bar levels when shown less than 512 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions WASApiBassNet/Components/FFT/FFTAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public int BarsCount
}
}

public int ShowingBarsCount { get; set; }

/// <inheritdoc/>
public IFFTProvider FFTProvider { get; set; }

Expand Down
10 changes: 10 additions & 0 deletions WASApiBassNet/Components/FFT/FFTPeakAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ public int BarsCount
}
}

int _showingBarsCount;
public int ShowingBarsCount
{
get => _showingBarsCount;
set
{
_showingBarsCount = value;
}
}

/// <inheritdoc/>
public IFFTAnalyzer FFTAnalyzer { get; set; }

Expand Down
1 change: 1 addition & 0 deletions WASApiBassNet/Components/FFT/IFFTAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ public interface IFFTAnalyzer : IAudioPlugin
/// bars count
/// </summary>
int BarsCount { get; set; }
int ShowingBarsCount { get; set; }
}
}
1 change: 1 addition & 0 deletions WASApiBassNet/Components/FFT/IFFTPeakAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@ public interface IFFTPeakAnalyzer : IAudioPlugin
/// bars count
/// </summary>
int BarsCount { get; set; }
int ShowingBarsCount { get; set; }
}
}
7 changes: 5 additions & 2 deletions WindowsAudioSession/AppComponents.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@


using System.Windows.Media;

using WASApiBassNet.Components.AudioCapture;
Expand Down Expand Up @@ -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;
Expand Down
13 changes: 13 additions & 0 deletions WindowsAudioSession/UI/FFT/FFTControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

/// <inheritdoc/>
public int BarWidthPercent
{
Expand Down
30 changes: 24 additions & 6 deletions WindowsAudioSession/UI/FFT/FFTDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
Expand Down Expand Up @@ -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()
{
Expand All @@ -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];
Expand Down
32 changes: 25 additions & 7 deletions WindowsAudioSession/UI/FFT/FFTPeakDrawer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
Expand Down Expand Up @@ -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;
Expand All @@ -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];

Expand Down
10 changes: 10 additions & 0 deletions WindowsAudioSession/UI/FFT/FFTViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ public int BarCount
}
}

int _showingBarCount = 512;
public int ShowingBarCount
{
get => _showingBarCount;
set
{
_showingBarCount = value;
}
}

int _barWidthPercent = 100;

/// <summary>
Expand Down
1 change: 1 addition & 0 deletions WindowsAudioSession/UI/FFT/IFFTControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public interface IFFTControl : IDrawable
/// bar count
/// </summary>
int BarCount { get; set; }
int ShowingBarCount { get; set; }

/// <summary>
/// bar width percent
Expand Down
1 change: 1 addition & 0 deletions WindowsAudioSession/UI/FFT/IFFTViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public interface IFFTViewModel : IModelBase, IValidableModel, IAudioPlugin
/// bar count
/// </summary>
int BarCount { get; set; }
int ShowingBarCount { get; set; }

/// <summary>
/// bar width percent
Expand Down