mirror of
https://github.com/DarthAffe/KeyboardAudioVisualizer.git
synced 2025-12-12 23:28:30 +00:00
Tweaked LevelVisualization
This commit is contained in:
parent
3171223b36
commit
111cc52e71
@ -2,7 +2,6 @@
|
||||
using System.Linq;
|
||||
using KeyboardAudioVisualizer.AudioCapture;
|
||||
using KeyboardAudioVisualizer.Configuration;
|
||||
using KeyboardAudioVisualizer.Helper;
|
||||
|
||||
namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
|
||||
{
|
||||
@ -15,25 +14,32 @@ namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
|
||||
|
||||
public class LevelVisualizationProviderConfiguration : AbstractConfiguration
|
||||
{
|
||||
private double _smoothing = 8;
|
||||
private ConversionMode _conversionMode = ConversionMode.Logarithmic;
|
||||
public ConversionMode ConversionMode
|
||||
{
|
||||
get => _conversionMode;
|
||||
set => SetProperty(ref _conversionMode, value);
|
||||
}
|
||||
|
||||
private double _smoothing = 3;
|
||||
public double Smoothing
|
||||
{
|
||||
get => _smoothing;
|
||||
set => SetProperty(ref _smoothing, value);
|
||||
}
|
||||
|
||||
private double _scale = 4;
|
||||
private double _scale = 5;
|
||||
public double Scale
|
||||
{
|
||||
get => _scale;
|
||||
set => SetProperty(ref _scale, value);
|
||||
}
|
||||
|
||||
private ConversionMode _conversionMode = ConversionMode.Exponential;
|
||||
public ConversionMode ConversionMode
|
||||
private double _referenceLevel = 90;
|
||||
public double ReferenceLevel
|
||||
{
|
||||
get => _conversionMode;
|
||||
set => SetProperty(ref _conversionMode, value);
|
||||
get => _referenceLevel;
|
||||
set => SetProperty(ref _referenceLevel, value);
|
||||
}
|
||||
}
|
||||
|
||||
@ -41,12 +47,6 @@ namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
|
||||
|
||||
public class LevelVisualizationProvider : IVisualizationProvider
|
||||
{
|
||||
#region Constants
|
||||
|
||||
private const int DB_THRESHOLD = 90;
|
||||
|
||||
#endregion
|
||||
|
||||
#region Properties & Fields
|
||||
|
||||
private readonly LevelVisualizationProviderConfiguration _configuration;
|
||||
@ -56,6 +56,7 @@ namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
|
||||
private float[] _sampleDataRight;
|
||||
private float[] _sampleDataMix;
|
||||
private double _smoothingFactor;
|
||||
private double _scalingFactor;
|
||||
|
||||
public IConfiguration Configuration => _configuration;
|
||||
public float[] VisualizationData { get; } = new float[3];
|
||||
@ -88,7 +89,26 @@ namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
|
||||
private void RecalculateConfigValues(string changedPropertyName)
|
||||
{
|
||||
if ((changedPropertyName == null) || (changedPropertyName == nameof(LevelVisualizationProviderConfiguration.Smoothing)))
|
||||
_smoothingFactor = MathHelper.Clamp(0.000015 * Math.Pow(2, _configuration.Smoothing), 0, 0.99);
|
||||
_smoothingFactor = Math.Log10(_configuration.Smoothing);
|
||||
|
||||
if ((changedPropertyName == null) || (changedPropertyName == nameof(LevelVisualizationProviderConfiguration.Scale))
|
||||
|| (changedPropertyName == nameof(LevelVisualizationProviderConfiguration.ConversionMode)))
|
||||
{
|
||||
switch (_configuration.ConversionMode)
|
||||
{
|
||||
case ConversionMode.Linear:
|
||||
_scalingFactor = _configuration.Scale / 2.5f;
|
||||
break;
|
||||
case ConversionMode.Logarithmic:
|
||||
_scalingFactor = _configuration.Scale * 2.5f;
|
||||
break;
|
||||
case ConversionMode.Exponential:
|
||||
_scalingFactor = _configuration.Scale;
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentOutOfRangeException();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Update()
|
||||
@ -110,13 +130,14 @@ namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
|
||||
|
||||
private float Convert(float level)
|
||||
{
|
||||
// DarthAffe 12.08.2017: The naming here is a bit off, but as long as it loos good :p
|
||||
switch (_configuration.ConversionMode)
|
||||
{
|
||||
case ConversionMode.Exponential:
|
||||
return level * level;
|
||||
|
||||
case ConversionMode.Logarithmic:
|
||||
return (float)Math.Max(0, (DB_THRESHOLD + (Math.Log10(level) * 20)) / DB_THRESHOLD);
|
||||
return (float)Math.Max(0, (Math.Pow(_configuration.ReferenceLevel, level) - 1) / _configuration.ReferenceLevel);
|
||||
|
||||
default: return level;
|
||||
}
|
||||
@ -124,7 +145,7 @@ namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
|
||||
|
||||
private void UpdateData(int index, float level)
|
||||
{
|
||||
VisualizationData[index] = (float)((VisualizationData[index] * _smoothingFactor) + (level * _configuration.Scale * (1.0 - _smoothingFactor)));
|
||||
VisualizationData[index] = (float)((VisualizationData[index] * _smoothingFactor) + (level * _scalingFactor * (1.0 - _smoothingFactor)));
|
||||
if (double.IsNaN(VisualizationData[index])) VisualizationData[index] = 0;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user