Repository: DarthAffe/KeyboardAudioVisualizer
Branch: master
Commit: 008fd65c08da
Files: 109
Total size: 373.6 KB
Directory structure:
gitextract_124yql35/
├── .gitattributes
├── .gitignore
├── KeyboardAudioVisualizer/
│ ├── App.config
│ ├── App.xaml
│ ├── App.xaml.cs
│ ├── ApplicationManager.cs
│ ├── Attached/
│ │ ├── SliderValue.cs
│ │ └── SliderValueAdorner.cs
│ ├── Attributes/
│ │ ├── DisplayNameAttribute.cs
│ │ └── VisualizerForAttribute.cs
│ ├── AudioCapture/
│ │ ├── AudioBuffer.cs
│ │ ├── CSCoreAudioInput.cs
│ │ └── IAudioInput.cs
│ ├── AudioProcessing/
│ │ ├── AbstractAudioProcessor.cs
│ │ ├── AudioVisualizationFactory.cs
│ │ ├── Equalizer/
│ │ │ ├── EqualizerBand.cs
│ │ │ ├── IEqualizer.cs
│ │ │ └── MultiBandEqualizer.cs
│ │ ├── IAudioProcessor.cs
│ │ ├── Spectrum/
│ │ │ ├── AbstractSpectrum.cs
│ │ │ ├── Band.cs
│ │ │ ├── FourierSpectrumProvider.cs
│ │ │ ├── GammaSpectrum.cs
│ │ │ ├── ISpectrum.cs
│ │ │ ├── ISpectrumProvider.cs
│ │ │ ├── LinearSpectrum.cs
│ │ │ ├── LogarithmicSpectrum.cs
│ │ │ └── RawSpectrumProvider.cs
│ │ ├── VisualizationPRovider/
│ │ │ ├── FrequencyBarsVisualizationProvider.cs
│ │ │ └── IVisualizationProvider.cs
│ │ └── VisualizationProvider/
│ │ ├── BeatVisualizationProvider.cs
│ │ ├── LevelVisualizationProvider.cs
│ │ └── VisualizationType.cs
│ ├── Configuration/
│ │ ├── AbstractConfiguration.cs
│ │ ├── ColorSerializer.cs
│ │ ├── EqualizerConfiguration.cs
│ │ ├── IConfiguration.cs
│ │ └── Settings.cs
│ ├── Controls/
│ │ ├── BlurredDecorationWindow.cs
│ │ ├── ColorSelector.cs
│ │ ├── Form.cs
│ │ ├── GradientEditor.cs
│ │ └── ImageButton.cs
│ ├── Converter/
│ │ ├── BoolToVisibilityConverter.cs
│ │ ├── EqualizerBandsToPointsConverter.cs
│ │ ├── EqualsToBoolConverter.cs
│ │ ├── OffsetToPosXConverter.cs
│ │ ├── ValueToPosYConverter.cs
│ │ ├── VisualizationProviderDisplayNameConverter.cs
│ │ ├── VisualizationToLastChildFillConverter.cs
│ │ └── VisualizationTypeSelectableConverter.cs
│ ├── Decorators/
│ │ ├── BeatDecorator.cs
│ │ ├── FrequencyBarsDecorator.cs
│ │ └── LevelBarDecorator.cs
│ ├── Helper/
│ │ ├── ActionCommand.cs
│ │ ├── EnumExtension.cs
│ │ ├── ExceptionExtension.cs
│ │ ├── FrequencyHelper.cs
│ │ ├── MathHelper.cs
│ │ ├── ObservableDictionary.cs
│ │ ├── RingBuffer.cs
│ │ ├── VisualizationIndex.cs
│ │ └── WPFHelper.cs
│ ├── KeyboardAudioVisualizer.csproj
│ ├── KeyboardAudioVisualizer.csproj.DotSettings
│ ├── Legacy/
│ │ ├── ConfigurationMigrator.cs
│ │ ├── ConfigurationUpdates.cs
│ │ ├── SerializationHelper.cs
│ │ └── Settings.cs
│ ├── Properties/
│ │ ├── AssemblyInfo.cs
│ │ ├── Resources.Designer.cs
│ │ ├── Resources.resx
│ │ ├── Settings.Designer.cs
│ │ └── Settings.settings
│ ├── Resources/
│ │ └── KeyboardAudioVisualizer.xaml
│ ├── Styles/
│ │ ├── BlurredDecorationWindow.xaml
│ │ ├── Button.xaml
│ │ ├── CachedResourceDictionary.cs
│ │ ├── ColorSelector.xaml
│ │ ├── ComboBox.xaml
│ │ ├── Form.xaml
│ │ ├── FrameworkElement.xaml
│ │ ├── GradientEditor.xaml
│ │ ├── GroupBox.xaml
│ │ ├── ImageButton.xaml
│ │ ├── Navigation.xaml
│ │ ├── Slider.xaml
│ │ ├── Theme.xaml
│ │ └── ToolTip.xaml
│ ├── UI/
│ │ ├── Configuration/
│ │ │ ├── BeatConfiguration.xaml
│ │ │ ├── FrequencyBarsConfiguration.xaml
│ │ │ └── LevelConfiguration.xaml
│ │ ├── ConfigurationViewModel.cs
│ │ ├── ConfigurationWindow.xaml
│ │ ├── ConfigurationWindow.xaml.cs
│ │ └── Visualization/
│ │ ├── BeatVisualization.xaml
│ │ ├── BeatVisualizer.cs
│ │ ├── EqualizerVisualization.xaml
│ │ ├── EqualizerVisualizer.cs
│ │ ├── FrequencyBarsVisualization.xaml
│ │ ├── FrequencyBarsVisualizer.cs
│ │ ├── LevelVisualization.xaml
│ │ └── LevelVisualizer.cs
│ └── packages.config
├── KeyboardAudioVisualizer.sln
├── KeyboardAudioVisualizer.sln.DotSettings
├── LICENSE
├── NuGet.Config
└── README.md
================================================
FILE CONTENTS
================================================
================================================
FILE: .gitattributes
================================================
###############################################################################
# Set default behavior to automatically normalize line endings.
###############################################################################
* text=auto
###############################################################################
# Set default behavior for command prompt diff.
#
# This is need for earlier builds of msysgit that does not have it on by
# default for csharp files.
# Note: This is only used by command line
###############################################################################
#*.cs diff=csharp
###############################################################################
# Set the merge driver for project and solution files
#
# Merging from the command prompt will add diff markers to the files if there
# are conflicts (Merging from VS is not affected by the settings below, in VS
# the diff markers are never inserted). Diff markers may cause the following
# file extensions to fail to load in VS. An alternative would be to treat
# these files as binary and thus will always conflict and require user
# intervention with every merge. To do so, just uncomment the entries below
###############################################################################
#*.sln merge=binary
#*.csproj merge=binary
#*.vbproj merge=binary
#*.vcxproj merge=binary
#*.vcproj merge=binary
#*.dbproj merge=binary
#*.fsproj merge=binary
#*.lsproj merge=binary
#*.wixproj merge=binary
#*.modelproj merge=binary
#*.sqlproj merge=binary
#*.wwaproj merge=binary
###############################################################################
# behavior for image files
#
# image files are treated as binary by default.
###############################################################################
#*.jpg binary
#*.png binary
#*.gif binary
###############################################################################
# diff behavior for common document formats
#
# Convert binary document formats to text before diffing them. This feature
# is only available from the command line. Turn it on by uncommenting the
# entries below.
###############################################################################
#*.doc diff=astextplain
#*.DOC diff=astextplain
#*.docx diff=astextplain
#*.DOCX diff=astextplain
#*.dot diff=astextplain
#*.DOT diff=astextplain
#*.pdf diff=astextplain
#*.PDF diff=astextplain
#*.rtf diff=astextplain
#*.RTF diff=astextplain
================================================
FILE: .gitignore
================================================
## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.
##
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates
# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs
# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/
# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*
# NUNIT
*.VisualState.xml
TestResult.xml
# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c
# .NET Core
project.lock.json
project.fragment.lock.json
artifacts/
**/Properties/launchSettings.json
*_i.c
*_p.c
*_i.h
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.svclog
*.scc
# Chutzpah Test files
_Chutzpah*
# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opendb
*.opensdf
*.sdf
*.cachefile
*.VC.db
*.VC.VC.opendb
# Visual Studio profiler
*.psess
*.vsp
*.vspx
*.sap
# TFS 2012 Local Workspace
$tf/
# Guidance Automation Toolkit
*.gpState
# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper
*.DotSettings.user
# JustCode is a .NET coding add-in
.JustCode
# TeamCity is a build add-in
_TeamCity*
# DotCover is a Code Coverage Tool
*.dotCover
# Visual Studio code coverage results
*.coverage
*.coveragexml
# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*
# MightyMoose
*.mm.*
AutoTest.Net/
# Web workbench (sass)
.sass-cache/
# Installshield output folder
[Ee]xpress/
# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html
# Click-Once directory
publish/
# Publish Web Output
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj
# Microsoft Azure Web App publish settings. Comment the next line if you want to
# checkin your Azure Web App publish settings, but sensitive information contained
# in these scripts will be unencrypted
PublishScripts/
# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignorable files
*.nuget.props
*.nuget.targets
# Microsoft Azure Build Output
csx/
*.build.csdef
# Microsoft Azure Emulator
ecf/
rcf/
# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt
# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/
# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.jfm
*.pfx
*.publishsettings
orleans.codegen.cs
# Since there are multiple workflows, uncomment next line to ignore bower_components
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/
# RIA/Silverlight projects
Generated_Code/
# Backup & report files from converting an old project file
# to a newer Visual Studio version. Backup files are not needed,
# because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm
# SQL Server files
*.mdf
*.ldf
*.ndf
# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings
# Microsoft Fakes
FakesAssemblies/
# GhostDoc plugin setting file
*.GhostDoc.xml
# Node.js Tools for Visual Studio
.ntvs_analysis.dat
node_modules/
# Typescript v1 declaration files
typings/
# Visual Studio 6 build log
*.plg
# Visual Studio 6 workspace options file
*.opt
# Visual Studio 6 auto-generated workspace file (contains which files were open etc.)
*.vbw
# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions
# Paket dependency manager
.paket/paket.exe
paket-files/
# FAKE - F# Make
.fake/
# JetBrains Rider
.idea/
*.sln.iml
# CodeRush
.cr/
# Python Tools for Visual Studio (PTVS)
__pycache__/
*.pyc
# Cake - Uncomment if you are using it
# tools/**
# !tools/packages.config
# Telerik's JustMock configuration file
*.jmconfig
# BizTalk build output
*.btp.cs
*.btm.cs
*.odx.cs
*.xsd.cs
================================================
FILE: KeyboardAudioVisualizer/App.config
================================================
================================================
FILE: KeyboardAudioVisualizer/App.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/App.xaml.cs
================================================
using System;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using Hardcodet.Wpf.TaskbarNotification;
using KeyboardAudioVisualizer.AudioProcessing;
using KeyboardAudioVisualizer.Configuration;
using KeyboardAudioVisualizer.Helper;
using KeyboardAudioVisualizer.Legacy;
using Newtonsoft.Json;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
using Settings = KeyboardAudioVisualizer.Configuration.Settings;
namespace KeyboardAudioVisualizer
{
public partial class App : Application
{
#region Constants
private const string PATH_SETTINGS = "Settings.json";
#endregion
#region Properties & Fields
private TaskbarIcon _taskbarIcon;
#endregion
#region Methods
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
try
{
ToolTipService.ShowDurationProperty.OverrideMetadata(typeof(DependencyObject), new FrameworkPropertyMetadata(int.MaxValue));
_taskbarIcon = (TaskbarIcon)FindResource("TaskbarIcon");
_taskbarIcon.DoubleClickCommand = ApplicationManager.Instance.OpenConfigurationCommand;
//Settings settings = SerializationHelper.LoadObjectFromFile(PATH_SETTINGS);
Settings settings = null;
try { settings = JsonConvert.DeserializeObject(File.ReadAllText(PATH_SETTINGS), new ColorSerializer()); }
catch (Exception ex)
{
Console.WriteLine(ex.Message);
/* File doesn't exist or is corrupt - just create a new one. */
}
if (settings == null)
settings = ConfigurationMigrator.MigrateOldConfig();
if (settings == null)
{
settings = new Settings
{
Version = Settings.CURRENT_VERSION,
Background = new LinearGradient(new GradientStop(0.5, new Color(64, 0, 0, 0)))
};
_taskbarIcon.ShowBalloonTip("Keyboard Audio-Visualizer is starting in the tray!", "Click on the icon to open the configuration.", BalloonIcon.Info);
}
else if (settings.Version != Settings.CURRENT_VERSION)
ConfigurationUpdates.PerformOn(settings);
ApplicationManager.Instance.Settings = settings;
AudioVisualizationFactory.Initialize();
ApplicationManager.Instance.InitializeDevices();
}
catch (Exception ex)
{
File.WriteAllText("error.log", $"[{DateTime.Now:G}] Exception!\r\n\r\nMessage:\r\n{ex.GetFullMessage()}\r\n\r\nStackTrace:\r\n{ex.StackTrace}\r\n\r\n");
MessageBox.Show("An error occured while starting the Keyboard Audio-Visualizer.\r\nPlease double check if SDK-support for your devices is enabled.\r\nMore information can be found in the error.log file in the application directory.", "Can't start Keyboard Audio-Visualizer.");
try { ApplicationManager.Instance.ExitCommand.Execute(null); }
catch { Environment.Exit(0); }
}
}
protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
File.WriteAllText(PATH_SETTINGS, JsonConvert.SerializeObject(ApplicationManager.Instance.Settings, new ColorSerializer()));
ConfigurationMigrator.CleanupOldConfigs();
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/ApplicationManager.cs
================================================
using System.Collections.Generic;
using System.Windows;
using KeyboardAudioVisualizer.AudioProcessing;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Configuration;
using KeyboardAudioVisualizer.Decorators;
using KeyboardAudioVisualizer.Helper;
using KeyboardAudioVisualizer.UI;
using RGB.NET.Brushes;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
using RGB.NET.Devices.CoolerMaster;
using RGB.NET.Devices.Corsair;
using RGB.NET.Devices.Logitech;
using RGB.NET.Devices.Novation;
using RGB.NET.Devices.Razer;
using RGB.NET.Devices.SteelSeries;
using RGB.NET.Groups;
using Point = RGB.NET.Core.Point;
using GetDecoratorFunc = System.Func;
namespace KeyboardAudioVisualizer
{
public class ApplicationManager
{
#region Constants
#endregion
#region Properties & Fields
public static ApplicationManager Instance { get; } = new ApplicationManager();
private ConfigurationWindow _configurationWindow;
public Settings Settings { get; set; }
public ObservableDictionary Visualizations { get; } = new ObservableDictionary();
private readonly Dictionary> _groups = new Dictionary>();
public TimerUpdateTrigger UpdateTrigger { get; } = new TimerUpdateTrigger();
#endregion
#region Commands
private ActionCommand _openConfiguration;
public ActionCommand OpenConfigurationCommand => _openConfiguration ?? (_openConfiguration = new ActionCommand(OpenConfiguration));
private ActionCommand _exitCommand;
public ActionCommand ExitCommand => _exitCommand ?? (_exitCommand = new ActionCommand(Exit));
#endregion
#region Constructors
private ApplicationManager() { }
#endregion
#region Methods
public void InitializeDevices()
{
RGBSurface surface = RGBSurface.Instance;
UpdateTrigger.UpdateFrequency = 1.0 / MathHelper.Clamp(Settings.UpdateRate, 1, 60);
surface.RegisterUpdateTrigger(UpdateTrigger);
LoadDevices(surface, CorsairDeviceProvider.Instance);
LoadDevices(surface, CoolerMasterDeviceProvider.Instance);
LoadDevices(surface, NovationDeviceProvider.Instance);
LoadDevices(surface, RazerDeviceProvider.Instance);
LoadDevices(surface, LogitechDeviceProvider.Instance);
LoadDevices(surface, SteelSeriesDeviceProvider.Instance);
surface.AlignDevices();
ILedGroup background = new ListLedGroup(surface.Leds);
background.Brush = new LinearGradientBrush(Settings.Background);
LinearGradient primaryGradient = Settings[VisualizationIndex.Primary].Gradient;
LinearGradient secondaryGradient = Settings[VisualizationIndex.Secondary].Gradient;
LinearGradient tertiaryGradient = Settings[VisualizationIndex.Tertiary].Gradient;
List<(ILedGroup, GetDecoratorFunc)> primaryGroups = new List<(ILedGroup, GetDecoratorFunc)>();
List<(ILedGroup, GetDecoratorFunc)> secondaryGroups = new List<(ILedGroup, GetDecoratorFunc)>();
List<(ILedGroup, GetDecoratorFunc)> tertiaryGroups = new List<(ILedGroup, GetDecoratorFunc)>();
foreach (IRGBDevice device in RGBSurface.Instance.Devices)
switch (device.DeviceInfo.DeviceType)
{
case RGBDeviceType.Keyboard:
case RGBDeviceType.Keypad:
case RGBDeviceType.LedMatrix:
ListLedGroup primary = new ListLedGroup(device);
LightbarSpecialPart lightbar = device.GetSpecialDevicePart();
if (lightbar != null)
{
primary.RemoveLeds(lightbar.Leds);
ILedGroup lightbarLeft = new ListLedGroup(lightbar.Left);
lightbarLeft.Brush = new LinearGradientBrush(new Point(1.0, 0.5), new Point(0.0, 0.5), tertiaryGradient);
tertiaryGroups.Add((lightbarLeft, (visualizationType, visualizer) => CreateDecorator(visualizationType, visualizer, LevelBarDirection.Left, 0)));
ILedGroup lightbarRight = new ListLedGroup(lightbar.Right);
lightbarRight.Brush = new LinearGradientBrush(tertiaryGradient);
tertiaryGroups.Add((lightbarRight, (visualizationType, visualizer) => CreateDecorator(visualizationType, visualizer, LevelBarDirection.Right, 1)));
ILedGroup lightbarCenter = new ListLedGroup(lightbar.Center);
lightbarCenter.Brush = new LinearGradientBrush(secondaryGradient);
secondaryGroups.Add((lightbarCenter, (visualizationType, visualizer) => CreateDecorator(visualizationType, visualizer)));
}
primary.Brush = new LinearGradientBrush(primaryGradient);
primaryGroups.Add((primary, (visualizationType, visualizer) => CreateDecorator(visualizationType, visualizer, LevelBarDirection.Horizontal, 0, primaryGradient)));
break;
case RGBDeviceType.Mousepad:
case RGBDeviceType.LedStripe:
case RGBDeviceType.HeadsetStand:
ILedGroup left = new RectangleLedGroup(new Rectangle(device.Location.X, device.Location.Y, device.Size.Width / 2.0, device.Size.Height));
left.Brush = new LinearGradientBrush(new Point(0.5, 1), new Point(0.5, 0), tertiaryGradient);
tertiaryGroups.Add((left, (visualizationType, visualizer) => CreateDecorator(visualizationType, visualizer, LevelBarDirection.Top, 0)));
ILedGroup right = new RectangleLedGroup(new Rectangle(device.Location.X + (device.Size.Width / 2.0), device.Location.Y, device.Size.Width / 2.0, device.Size.Height));
right.Brush = new LinearGradientBrush(new Point(0.5, 1), new Point(0.5, 0), tertiaryGradient);
tertiaryGroups.Add((right, (visualizationType, visualizer) => CreateDecorator(visualizationType, visualizer, LevelBarDirection.Top, 1)));
break;
case RGBDeviceType.Mouse:
case RGBDeviceType.Headset:
case RGBDeviceType.Speaker:
case RGBDeviceType.Fan:
case RGBDeviceType.GraphicsCard:
case RGBDeviceType.DRAM:
case RGBDeviceType.Mainboard:
ILedGroup deviceGroup = new ListLedGroup(device);
deviceGroup.Brush = new LinearGradientBrush(secondaryGradient);
secondaryGroups.Add((deviceGroup, (visualizationType, visualizer) => CreateDecorator(visualizationType, visualizer)));
break;
}
_groups[VisualizationIndex.Primary] = primaryGroups;
_groups[VisualizationIndex.Secondary] = secondaryGroups;
_groups[VisualizationIndex.Tertiary] = tertiaryGroups;
ApplyVisualization(VisualizationIndex.Primary, Settings[VisualizationIndex.Primary].SelectedVisualization);
ApplyVisualization(VisualizationIndex.Secondary, Settings[VisualizationIndex.Secondary].SelectedVisualization);
ApplyVisualization(VisualizationIndex.Tertiary, Settings[VisualizationIndex.Tertiary].SelectedVisualization);
surface.Updating += args => AudioVisualizationFactory.Instance.Update();
}
private void LoadDevices(RGBSurface surface, IRGBDeviceProvider deviceProvider)
{
surface.LoadDevices(deviceProvider, RGBDeviceType.Keyboard | RGBDeviceType.LedMatrix
| RGBDeviceType.Mousepad | RGBDeviceType.LedStripe
| RGBDeviceType.Mouse | RGBDeviceType.Headset
| RGBDeviceType.HeadsetStand);
}
//TODO DarthAffe 12.09.2017: This is just a big mess - is this worth to rework before arge?
public void ApplyVisualization(VisualizationIndex visualizationIndex, VisualizationType visualizationType)
{
IVisualizationProvider visualizer = AudioVisualizationFactory.Instance.CreateVisualizationProvider(visualizationIndex, visualizationType);
Visualizations[visualizationIndex] = visualizer;
foreach ((ILedGroup group, GetDecoratorFunc getDecoratorFunc) in _groups[visualizationIndex])
{
group.Brush.RemoveAllDecorators();
if (visualizer != null)
{
IBrushDecorator decorator = getDecoratorFunc(visualizationType, visualizer);
if (decorator != null)
group.Brush.AddDecorator(decorator);
}
}
}
private IBrushDecorator CreateDecorator(VisualizationType visualizationType, IVisualizationProvider visualizationProvider, LevelBarDirection direction = LevelBarDirection.Top, int dataIndex = 0, LinearGradient gradient = null)
{
if (visualizationType == VisualizationType.FrequencyBars)
return new FrequencyBarsDecorator(visualizationProvider);
if (visualizationType == VisualizationType.Level)
return new LevelBarDecorator(visualizationProvider, direction, dataIndex, gradient);
if (visualizationType == VisualizationType.Beat)
return new BeatDecorator(visualizationProvider);
return null;
}
private void OpenConfiguration()
{
if (_configurationWindow == null) _configurationWindow = new ConfigurationWindow();
_configurationWindow.Show();
}
private void Exit()
{
try { AudioVisualizationFactory.Instance?.Dispose(); } catch { }
try { RGBSurface.Instance?.Dispose(); } catch { }
Application.Current.Shutdown();
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Attached/SliderValue.cs
================================================
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
namespace KeyboardAudioVisualizer.Attached
{
public static class SliderValue
{
#region Properties & Fields
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty UnitProperty = DependencyProperty.RegisterAttached(
"Unit", typeof(string), typeof(SliderValue), new PropertyMetadata(default(string)));
public static void SetUnit(DependencyObject element, string value) => element.SetValue(UnitProperty, value);
public static string GetUnit(DependencyObject element) => (string)element.GetValue(UnitProperty);
public static readonly DependencyProperty IsShownProperty = DependencyProperty.RegisterAttached(
"IsShown", typeof(bool), typeof(SliderValue), new PropertyMetadata(default(bool), IsShownChanged));
public static void SetIsShown(DependencyObject element, bool value) => element.SetValue(IsShownProperty, value);
public static bool GetIsShown(DependencyObject element) => (bool)element.GetValue(IsShownProperty);
public static readonly DependencyProperty BorderBrushProperty = DependencyProperty.RegisterAttached(
"BorderBrush", typeof(Brush), typeof(SliderValue), new PropertyMetadata(default(Brush)));
public static void SetBorderBrush(DependencyObject element, Brush value) => element.SetValue(BorderBrushProperty, value);
public static Brush GetBorderBrush(DependencyObject element) => (Brush)element.GetValue(BorderBrushProperty);
public static readonly DependencyProperty BackgroundProperty = DependencyProperty.RegisterAttached(
"Background", typeof(Brush), typeof(SliderValue), new PropertyMetadata(default(Brush)));
public static void SetBackground(DependencyObject element, Brush value) => element.SetValue(BackgroundProperty, value);
public static Brush GetBackground(DependencyObject element) => (Brush)element.GetValue(BackgroundProperty);
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.RegisterAttached(
"Foreground", typeof(Brush), typeof(SliderValue), new PropertyMetadata(default(Brush)));
public static void SetForeground(DependencyObject element, Brush value) => element.SetValue(ForegroundProperty, value);
public static Brush GetForeground(DependencyObject element) => (Brush)element.GetValue(ForegroundProperty);
public static readonly DependencyProperty FontProperty = DependencyProperty.RegisterAttached(
"Font", typeof(FontFamily), typeof(SliderValue), new PropertyMetadata(default(FontFamily)));
public static void SetFont(DependencyObject element, FontFamily value) => element.SetValue(FontProperty, value);
public static FontFamily GetFont(DependencyObject element) => (FontFamily)element.GetValue(FontProperty);
public static readonly DependencyProperty FontSizeProperty = DependencyProperty.RegisterAttached(
"FontSize", typeof(double), typeof(SliderValue), new PropertyMetadata(default(double)));
public static void SetFontSize(DependencyObject element, double value) => element.SetValue(FontSizeProperty, value);
public static double GetFontSize(DependencyObject element) => (double)element.GetValue(FontSizeProperty);
// ReSharper enable InconsistentNaming
#endregion
#region Methods
private static void IsShownChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is Slider slider)) return;
if (dependencyPropertyChangedEventArgs.NewValue as bool? == true)
{
slider.MouseEnter += SliderOnMouseEnter;
slider.MouseLeave += SliderOnMouseLeave;
}
else
{
slider.MouseEnter -= SliderOnMouseEnter;
slider.MouseLeave -= SliderOnMouseLeave;
RemoveAdorner(slider);
}
}
private static void SliderOnMouseEnter(object sender, MouseEventArgs mouseEventArgs)
{
if (!(sender is Slider slider)) return;
AdornerLayer.GetAdornerLayer(slider)?.Add(new SliderValueAdorner(slider, GetUnit(slider))
{
BorderBrush = GetBorderBrush(slider),
Background = GetBackground(slider),
Foreground = GetForeground(slider),
Font = GetFont(slider),
FontSize = GetFontSize(slider)
});
}
private static void SliderOnMouseLeave(object sender, MouseEventArgs mouseEventArgs)
{
if (!(sender is Slider slider)) return;
RemoveAdorner(slider);
}
private static void RemoveAdorner(Slider slider)
{
AdornerLayer adornerLayer = AdornerLayer.GetAdornerLayer(slider);
Adorner adorner = adornerLayer?.GetAdorners(slider)?.FirstOrDefault(x => x is SliderValueAdorner);
if (adorner != null)
{
adornerLayer.Remove(adorner);
(adorner as SliderValueAdorner)?.Cleanup();
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Attached/SliderValueAdorner.cs
================================================
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using Point = System.Windows.Point;
namespace KeyboardAudioVisualizer.Attached
{
public class SliderValueAdorner : System.Windows.Documents.Adorner
{
#region Properties & Fields
private readonly string _unit;
private readonly Slider _slider;
private readonly Thumb _thumb;
private readonly RepeatButton _decreaseRepeatButton;
public Brush BorderBrush { get; set; } = Brushes.Black;
public Brush Background { get; set; } = Brushes.Black;
public Brush Foreground { get; set; } = Brushes.White;
public FontFamily Font { get; set; } = new FontFamily("Verdana");
public double FontSize { get; set; } = 14;
#endregion
#region Constructors
public SliderValueAdorner(UIElement adornedElement, string unit)
: base(adornedElement)
{
this._unit = unit;
_slider = (Slider)adornedElement;
Track track = (Track)_slider.Template.FindName("PART_Track", _slider);
_thumb = track.Thumb;
_decreaseRepeatButton = track.DecreaseRepeatButton;
_decreaseRepeatButton.SizeChanged += OnButtonSizeChanged;
}
#endregion
#region Methods
public void Cleanup()
{
_decreaseRepeatButton.SizeChanged -= OnButtonSizeChanged;
}
private void OnButtonSizeChanged(object sender, SizeChangedEventArgs sizeChangedEventArgs) => InvalidateVisual();
protected override void OnRender(DrawingContext drawingContext)
{
double offset = _decreaseRepeatButton.ActualWidth + (_thumb.ActualWidth / 2.0);
FormattedText text = new FormattedText(GetText(), CultureInfo.InvariantCulture, FlowDirection.LeftToRight, new Typeface(Font, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), FontSize, Foreground);
Geometry border = CreateBorder(offset, text.Width, text.Height);
drawingContext.DrawGeometry(Background, new Pen(BorderBrush, 1), border);
drawingContext.DrawText(text, new Point(offset - (text.Width / 2.0), -26));
}
private string GetText()
{
string valueText = _slider.Value.ToString();
if (!string.IsNullOrWhiteSpace(_unit))
valueText += " " + _unit;
return valueText;
}
private Geometry CreateBorder(double offset, double width, double height)
{
double halfWidth = width / 2.0;
PathGeometry borderGeometry = new PathGeometry();
PathFigure border = new PathFigure
{
StartPoint = new Point(offset, 0),
IsClosed = true,
IsFilled = true
};
border.Segments.Add(new LineSegment(new Point(offset + 4, -6), true));
border.Segments.Add(new LineSegment(new Point(offset + 4 + halfWidth, -6), true));
border.Segments.Add(new LineSegment(new Point(offset + 4 + halfWidth, -10 - height), true));
border.Segments.Add(new LineSegment(new Point(offset - 4 - halfWidth, -10 - height), true));
border.Segments.Add(new LineSegment(new Point(offset - 4 - halfWidth, -6), true));
border.Segments.Add(new LineSegment(new Point(offset - 4, -6), true));
borderGeometry.Figures.Add(border);
return borderGeometry;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Attributes/DisplayNameAttribute.cs
================================================
using System;
namespace KeyboardAudioVisualizer.Attributes
{
public class DisplayNameAttribute : Attribute
{
#region Properties & Fields
public string DisplayName { get; set; }
#endregion
#region Constructors
public DisplayNameAttribute(string displayName)
{
this.DisplayName = displayName;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Attributes/VisualizerForAttribute.cs
================================================
using System;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.Attributes
{
public class VisualizerForAttribute : Attribute
{
#region Properties & Fields
public RGBDeviceType VisualizerFor { get; set; }
#endregion
#region Constructors
public VisualizerForAttribute(RGBDeviceType visualizerFor)
{
this.VisualizerFor = visualizerFor;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioCapture/AudioBuffer.cs
================================================
using System;
namespace KeyboardAudioVisualizer.AudioCapture
{
public class AudioBuffer
{
#region Properties & Fields
private readonly int _capacity;
private readonly float[] _bufferLeft;
private readonly float[] _bufferRight;
private int _currentIndex;
public int Size => _capacity;
public float? Prescale { get; set; } = null;
#endregion
#region Constructors
public AudioBuffer(int capacity)
{
this._capacity = capacity;
_bufferLeft = new float[capacity];
_bufferRight = new float[capacity];
}
#endregion
#region Methods
public void Put(float left, float right)
{
_currentIndex++;
if (_currentIndex >= _capacity) _currentIndex = 0;
_bufferLeft[_currentIndex] = left;
_bufferRight[_currentIndex] = right;
}
public void Put(float[] src, int offset, int count)
{
if ((count & 1) != 0) return; // we expect stereo-data to be an even amount of values
if (count > _capacity)
{
offset += count - _capacity;
count = _capacity;
}
for (int i = 0; i < count; i += 2)
{
_currentIndex++;
if (_currentIndex >= _capacity) _currentIndex = 0;
if (Prescale.HasValue)
{
_bufferLeft[_currentIndex] = src[offset + i] / Prescale.Value;
_bufferRight[_currentIndex] = src[offset + i + 1] / Prescale.Value;
}
else
{
_bufferLeft[_currentIndex] = src[offset + i];
_bufferRight[_currentIndex] = src[offset + i + 1];
}
}
}
public void CopyLeftInto(ref float[] data, int offset) => CopyLeftInto(ref data, offset, Math.Min(data.Length, _capacity));
public void CopyLeftInto(ref float[] data, int offset, int count)
{
int bufferOffset = _capacity - count;
for (int i = 0; i < count; i++)
data[offset + i] = _bufferLeft[(_currentIndex + (bufferOffset + i)) % _capacity];
}
public void CopyRightInto(ref float[] data, int offset) => CopyRightInto(ref data, offset, Math.Min(data.Length, _capacity));
public void CopyRightInto(ref float[] data, int offset, int count)
{
int bufferOffset = _capacity - count;
for (int i = 0; i < count; i++)
data[offset + i] = _bufferRight[(_currentIndex + (bufferOffset + i)) % _capacity];
}
public void CopyMixInto(ref float[] data, int offset) => CopyMixInto(ref data, offset, Math.Min(data.Length, _capacity));
public void CopyMixInto(ref float[] data, int offset, int count)
{
int bufferOffset = _capacity - count;
for (int i = 0; i < count; i++)
{
int index = (_currentIndex + (bufferOffset + i)) % _capacity;
data[offset + i] = (_bufferLeft[index] + _bufferRight[index]) / 2f;
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioCapture/CSCoreAudioInput.cs
================================================
using System;
using CSCore;
using CSCore.CoreAudioAPI;
using CSCore.SoundIn;
using CSCore.Streams;
namespace KeyboardAudioVisualizer.AudioCapture
{
public class CSCoreAudioInput : IAudioInput
{
#region Properties & Fields
private WasapiCapture _capture;
private SoundInSource _soundInSource;
private IWaveSource _source;
private SingleBlockNotificationStream _stream;
private AudioEndpointVolume _audioEndpointVolume;
public int SampleRate => _soundInSource?.WaveFormat?.SampleRate ?? -1;
public float MasterVolume => _audioEndpointVolume.MasterVolumeLevelScalar;
#endregion
#region Event
public event AudioData DataAvailable;
#endregion
#region Methods
public void Initialize()
{
MMDevice captureDevice = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console);
WaveFormat deviceFormat = captureDevice.DeviceFormat;
_audioEndpointVolume = AudioEndpointVolume.FromDevice(captureDevice);
//DarthAffe 07.02.2018: This is a really stupid workaround to (hopefully) finally fix the surround driver issues
for (int i = 1; i < 13; i++)
try { _capture = new WasapiLoopbackCapture(100, new WaveFormat(deviceFormat.SampleRate, deviceFormat.BitsPerSample, i)); } catch { /* We're just trying ... */ }
if (_capture == null)
throw new NullReferenceException("Failed to initialize WasapiLoopbackCapture");
_capture.Initialize();
_soundInSource = new SoundInSource(_capture) { FillWithZeros = false };
_source = _soundInSource.WaveFormat.SampleRate == 44100
? _soundInSource.ToStereo()
: _soundInSource.ChangeSampleRate(44100).ToStereo();
_stream = new SingleBlockNotificationStream(_source.ToSampleSource());
_stream.SingleBlockRead += StreamOnSingleBlockRead;
_source = _stream.ToWaveSource();
byte[] buffer = new byte[_source.WaveFormat.BytesPerSecond / 2];
_soundInSource.DataAvailable += (s, aEvent) =>
{
while ((_source.Read(buffer, 0, buffer.Length)) > 0) ;
};
_capture.Start();
}
public void Dispose()
{
_capture?.Stop();
_capture?.Dispose();
}
private void StreamOnSingleBlockRead(object sender, SingleBlockReadEventArgs singleBlockReadEventArgs)
=> DataAvailable?.Invoke(singleBlockReadEventArgs.Left, singleBlockReadEventArgs.Right);
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioCapture/IAudioInput.cs
================================================
using System;
namespace KeyboardAudioVisualizer.AudioCapture
{
public delegate void AudioData(float left, float right);
public interface IAudioInput : IDisposable
{
int SampleRate { get; }
float MasterVolume { get; }
event AudioData DataAvailable;
void Initialize();
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/AbstractAudioProcessor.cs
================================================
namespace KeyboardAudioVisualizer.AudioProcessing
{
public abstract class AbstractAudioProcessor : IAudioProcessor
{
#region Properties & Fields
public bool IsActive { get; set; } = true;
#endregion
#region Methods
public abstract void Initialize();
public abstract void Update();
public virtual void Dispose() { }
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/AudioVisualizationFactory.cs
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using KeyboardAudioVisualizer.AudioCapture;
using KeyboardAudioVisualizer.AudioProcessing.Equalizer;
using KeyboardAudioVisualizer.AudioProcessing.Spectrum;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.AudioProcessing
{
public class AudioVisualizationFactory : IDisposable
{
#region Properties & Fields
public static AudioVisualizationFactory Instance { get; private set; }
private IAudioInput _audioInput;
private AudioBuffer _audioBuffer;
private readonly List _processors = new List();
#endregion
#region Constructors
private AudioVisualizationFactory() { }
#endregion
#region Methods
public void Update()
{
if (ApplicationManager.Instance.Settings.EnableAudioPrescale)
_audioBuffer.Prescale = _audioInput.MasterVolume;
else
_audioBuffer.Prescale = null;
foreach (IAudioProcessor processor in _processors.Where(x => x.IsActive))
processor.Update();
}
public static void Initialize()
{
if (Instance != null) return;
Instance = new AudioVisualizationFactory();
Instance.InitializeInstance();
}
private void InitializeInstance()
{
_audioInput = new CSCoreAudioInput();
_audioInput.Initialize();
_audioBuffer = new AudioBuffer(4096); // Working with ~93ms -
_audioInput.DataAvailable += (left, right) => _audioBuffer.Put(left, right);
_processors.Add(new FourierSpectrumProvider(_audioBuffer));
foreach (IAudioProcessor processor in _processors)
processor.Initialize();
}
private T GetAudioProcessor() => (T)_processors.FirstOrDefault(x => x.GetType() == typeof(T));
public IVisualizationProvider CreateVisualizationProvider(VisualizationIndex visualizationIndex, VisualizationType visualizationType)
{
IVisualizationProvider visualizationProvider = default;
switch (visualizationType)
{
case VisualizationType.FrequencyBars:
MultiBandEqualizer equalizer = new MultiBandEqualizer();
ApplicationManager.Instance.Settings[visualizationIndex].EqualizerConfiguration.LoadInto(equalizer);
equalizer.PropertyChanged += (sender, args) => ApplicationManager.Instance.Settings[visualizationIndex].EqualizerConfiguration.SaveFrom(equalizer);
visualizationProvider = new FrequencyBarsVisualizationProvider(ApplicationManager.Instance.Settings[visualizationIndex].GetConfiguration(visualizationType), GetAudioProcessor()) { Equalizer = equalizer };
break;
case VisualizationType.Level:
visualizationProvider = new LevelVisualizationProvider(ApplicationManager.Instance.Settings[visualizationIndex].GetConfiguration(visualizationType), _audioBuffer);
break;
case VisualizationType.Beat:
visualizationProvider = new BeatVisualizationProvider(ApplicationManager.Instance.Settings[visualizationIndex].GetConfiguration(visualizationType), GetAudioProcessor());
break;
}
visualizationProvider?.Initialize();
return visualizationProvider;
}
public void Dispose() => _audioInput.Dispose();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Equalizer/EqualizerBand.cs
================================================
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.AudioProcessing.Equalizer
{
public class EqualizerBand : AbstractBindable
{
#region Properties & Fields
private float _offset;
public float Offset
{
get => _offset;
set
{
if (!IsFixedOffset)
SetProperty(ref _offset, float.IsNaN(value) ? 0 : MathHelper.Clamp(value, 0, 1));
}
}
private float _value;
public float Value
{
get => _value;
set => SetProperty(ref _value, float.IsNaN(value) ? 0 : MathHelper.Clamp(value, -1, 1));
}
public bool IsFixedOffset { get; set; }
#endregion
#region Constructors
public EqualizerBand() : this(0) { }
public EqualizerBand(float offset, float value = 0, bool fixedOffset = false)
{
this.Offset = offset;
this.Value = value;
this.IsFixedOffset = fixedOffset;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Equalizer/IEqualizer.cs
================================================
using System.Collections.ObjectModel;
namespace KeyboardAudioVisualizer.AudioProcessing.Equalizer
{
public interface IEqualizer
{
bool IsEnabled { get; set; }
ObservableCollection Bands { get; }
float[] CalculateValues(int count);
EqualizerBand AddBand(float offset, float modification);
void RemoveBandBand(EqualizerBand band);
void Reset();
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Equalizer/MultiBandEqualizer.cs
================================================
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.AudioProcessing.Equalizer
{
public class MultiBandEqualizer : AbstractBindable, IEqualizer
{
#region Properties & Fields
public ObservableCollection Bands { get; } = new ObservableCollection();
private readonly Dictionary _values = new Dictionary();
private bool _isEnabled;
public bool IsEnabled
{
get => _isEnabled;
set => SetProperty(ref _isEnabled, value);
}
#endregion
#region Constructors
public MultiBandEqualizer()
{
Reset();
}
#endregion
#region Methods
public EqualizerBand AddBand(float offset, float modification) => AddBand(offset, modification, false);
public EqualizerBand AddBand(float offset, float modification, bool isFixedFrequency)
{
EqualizerBand band = new EqualizerBand(offset, modification, isFixedFrequency);
band.PropertyChanged += (sender, args) => InvalidateCache();
Bands.Add(band);
InvalidateCache();
return band;
}
public void RemoveBandBand(EqualizerBand band)
{
if (!band.IsFixedOffset)
Bands.Remove(band);
InvalidateCache();
}
public void Reset()
{
Bands.Clear();
AddBand(0, 0, true);
AddBand(1, 0, true);
}
public float[] CalculateValues(int count)
{
if (!_values.TryGetValue(count, out float[] values))
{
values = RecalculateValues(count);
_values[count] = values;
}
return values;
}
private float[] RecalculateValues(int count)
{
float[] values = new float[count];
List orderedBands = Bands.OrderBy(x => x.Offset).ToList();
if (orderedBands.Count < 2) return values;
for (int i = 0; i < count; i++)
{
float offset = (i / (float)count);
EqualizerBand bandBefore = orderedBands.Last(n => n.Offset <= offset);
EqualizerBand bandAfter = orderedBands.First(n => n.Offset >= offset);
offset = (bandAfter.Offset <= 0) || (Math.Abs(bandAfter.Offset - bandBefore.Offset) < 0.0001)
? 0 : (offset - bandBefore.Offset) / (bandAfter.Offset - bandBefore.Offset);
float value = (float)((3.0 * (offset * offset)) - (2.0 * (offset * offset * offset)));
values[i] = bandBefore.Value + (value * (bandAfter.Value - bandBefore.Value));
}
return values;
}
private void InvalidateCache()
{
_values.Clear();
// ReSharper disable once ExplicitCallerInfoArgument
OnPropertyChanged(nameof(Bands));
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/IAudioProcessor.cs
================================================
using System;
namespace KeyboardAudioVisualizer.AudioProcessing
{
public interface IAudioProcessor : IDisposable
{
bool IsActive { get; set; }
void Initialize();
void Update();
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/AbstractSpectrum.cs
================================================
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public abstract class AbstractSpectrum : ISpectrum
{
#region Properties & Fields
protected Band[] Bands { get; set; }
public int BandCount => Bands.Length;
public Band this[int index] => Bands[index];
public Band this[float frequency] => Bands.FirstOrDefault(band => (band.LowerFrequency <= frequency) && (band.UpperFrequency >= frequency));
public Band[] this[float minFrequency, float maxFrequency] => Bands.Where(band => (band.LowerFrequency > minFrequency) && (band.UpperFrequency < maxFrequency)).ToArray();
#endregion
#region Methods
public IEnumerator GetEnumerator() => Bands.AsEnumerable().GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/Band.cs
================================================
using System.Linq;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public class Band
{
#region Properties & Fields
private readonly float[] _data;
private readonly float _resolution;
public float LowerFrequency { get; }
public float UpperFrequency { get; }
public float CenterFrequency { get; }
private float? _average = null;
public float Average => _average ?? (_average = _data.Average()).Value;
private float? _min = null;
public float Min => _min ?? (_min = _data.Min()).Value;
private float? _max = null;
public float Max => _max ?? (_max = _data.Max()).Value;
private float? _sum = null;
public float Sum => _sum ?? (_sum = _data.Sum()).Value;
public float this[int index] => _data[index];
public float this[float frequency] => _data[(int)((frequency - LowerFrequency) / _resolution)];
#endregion
#region Constructors
public Band(float lowerFrequency, float upperFrequency, float[] data)
{
this.LowerFrequency = lowerFrequency;
this.UpperFrequency = upperFrequency;
this.CenterFrequency = (LowerFrequency + UpperFrequency) / 2f; //TODO DarthAffe 12.08.2017: Is this valid for logarithmic scaling?
this._data = data;
_resolution = (UpperFrequency - LowerFrequency) / data.Length;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/FourierSpectrumProvider.cs
================================================
using System;
using KeyboardAudioVisualizer.AudioCapture;
using MathNet.Numerics;
using MathNet.Numerics.IntegralTransforms;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public class FourierSpectrumProvider : AbstractAudioProcessor, ISpectrumProvider
{
#region Properties & Fields
private readonly AudioBuffer _audioBuffer;
private float[] _sampleData;
private double[] _hamming;
private Complex32[] _complexBuffer;
private float[] _spectrum;
private int _usableDataLength;
#endregion
#region Constructors
public FourierSpectrumProvider(AudioBuffer audioBuffer)
{
this._audioBuffer = audioBuffer;
}
#endregion
#region Methods
public override void Initialize()
{
_hamming = Window.Hamming(_audioBuffer.Size);
_sampleData = new float[_audioBuffer.Size];
_complexBuffer = new Complex32[_audioBuffer.Size];
_usableDataLength = (_audioBuffer.Size / 2) + 1;
_spectrum = new float[_usableDataLength];
}
public override void Update()
{
_audioBuffer.CopyMixInto(ref _sampleData, 0);
ApplyHamming(ref _sampleData);
CreateSpectrum(ref _sampleData);
}
private void ApplyHamming(ref float[] data)
{
for (int i = 0; i < data.Length; i++)
data[i] = (float)(data[i] * _hamming[i]);
}
private void CreateSpectrum(ref float[] data)
{
for (int i = 0; i < data.Length; i++)
_complexBuffer[i] = new Complex32(data[i], 0);
Fourier.Forward(_complexBuffer, FourierOptions.NoScaling);
for (int i = 0; i < _spectrum.Length; i++)
{
Complex32 fourierData = _complexBuffer[i];
_spectrum[i] = (float)Math.Sqrt(fourierData.Real * fourierData.Real) + (fourierData.Imaginary * fourierData.Imaginary);
}
}
public ISpectrum GetLinearSpectrum(int bands = 64, float minFrequency = -1, float maxFrequency = -1) => new LinearSpectrum(_spectrum, bands, minFrequency, maxFrequency);
public ISpectrum GetLogarithmicSpectrum(int bands = 12, float minFrequency = -1, float maxFrequency = -1) => new LogarithmicSpectrum(_spectrum, bands, minFrequency, maxFrequency);
public ISpectrum GetGammaSpectrum(int bands = 64, float gamma = 2, float minFrequency = -1, float maxFrequency = -1) => new GammaSpectrum(_spectrum, bands, gamma, minFrequency, maxFrequency);
public ISpectrum GetRawSpectrum() => new RawSpectrumProvider(_spectrum);
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/GammaSpectrum.cs
================================================
using System;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public class GammaSpectrum : AbstractSpectrum
{
#region Constructors
public GammaSpectrum(float[] data, int bands, float gamma = 2, float minFrequency = -1, float maxFrequency = -1)
{
int dataReferenceCount = (data.Length - 1) * 2;
int fromIndex = minFrequency < 0 ? 0 : MathHelper.Clamp(FrequencyHelper.GetIndexOfFrequency(minFrequency, dataReferenceCount), 0, data.Length - 1 - bands); // -bands since we need at least enough data to get our bands
int toIndex = maxFrequency < 0 ? data.Length - 1 : MathHelper.Clamp(FrequencyHelper.GetIndexOfFrequency(maxFrequency, dataReferenceCount), fromIndex, data.Length - 1);
int usableSourceData = Math.Max(bands, (toIndex - fromIndex) + 1);
Bands = new Band[bands];
int index = fromIndex;
for (int i = 0; i < Bands.Length; i++)
{
int count = Math.Max(1, (((int)(Math.Pow((i + 1f) / Bands.Length, gamma) * usableSourceData))) - index);
float[] bandData = new float[count];
Array.Copy(data, index, bandData, 0, count);
Bands[i] = new Band(FrequencyHelper.GetFrequencyOfIndex(index, dataReferenceCount),
FrequencyHelper.GetFrequencyOfIndex(index + count, dataReferenceCount),
bandData);
index += count;
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/ISpectrum.cs
================================================
using System.Collections.Generic;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public interface ISpectrum : IEnumerable
{
int BandCount { get; }
Band this[int index] { get; }
Band this[float frequency] { get; }
Band[] this[float minFrequency, float maxFrequency] { get; }
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/ISpectrumProvider.cs
================================================
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public interface ISpectrumProvider : IAudioProcessor
{
ISpectrum GetLinearSpectrum(int bands = 64, float minFrequency = -1, float maxFrequency = -1);
ISpectrum GetLogarithmicSpectrum(int bands = 1, float minFrequency = -1, float maxFrequency = -1);
ISpectrum GetGammaSpectrum(int bands = 1, float gamma = 2, float minFrequency = -1, float maxFrequency = -1);
ISpectrum GetRawSpectrum();
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/LinearSpectrum.cs
================================================
using System;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public class LinearSpectrum : AbstractSpectrum
{
#region Constructors
public LinearSpectrum(float[] data, int bands, float minFrequency = -1, float maxFrequency = -1)
{
int dataReferenceCount = (data.Length - 1) * 2;
int fromIndex = minFrequency < 0 ? 0 : MathHelper.Clamp(FrequencyHelper.GetIndexOfFrequency(minFrequency, dataReferenceCount), 0, data.Length - 1 - bands); // -bands since we need at least enough data to get our bands
int toIndex = maxFrequency < 0 ? data.Length - 1 : MathHelper.Clamp(FrequencyHelper.GetIndexOfFrequency(maxFrequency, dataReferenceCount), fromIndex, data.Length - 1);
int usableSourceData = Math.Max(bands, (toIndex - fromIndex) + 1);
Bands = new Band[bands];
double frequenciesPerBand = (double)usableSourceData / bands;
double frequencyCounter = 0;
int index = fromIndex;
for (int i = 0; i < Bands.Length; i++)
{
frequencyCounter += frequenciesPerBand;
int count = (int)frequencyCounter;
float[] bandData = new float[count];
Array.Copy(data, index, bandData, 0, count);
Bands[i] = new Band(FrequencyHelper.GetFrequencyOfIndex(index, dataReferenceCount),
FrequencyHelper.GetFrequencyOfIndex(index + count, dataReferenceCount),
bandData);
index += count;
frequencyCounter -= count;
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/LogarithmicSpectrum.cs
================================================
using System;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public class LogarithmicSpectrum : AbstractSpectrum
{
#region Constructors
public LogarithmicSpectrum(float[] data, int bands, float minFrequency = -1, float maxFrequency = -1)
{
int dataReferenceCount = (data.Length - 1) * 2;
int fromIndex = minFrequency < 0 ? 0 : MathHelper.Clamp(FrequencyHelper.GetIndexOfFrequency(minFrequency, dataReferenceCount), 0, data.Length - 1 - bands); // -bands since we need at least enough data to get our bands
int toIndex = maxFrequency < 0 ? data.Length - 1 : MathHelper.Clamp(FrequencyHelper.GetIndexOfFrequency(maxFrequency, dataReferenceCount), fromIndex, data.Length - 1);
int usableSourceData = Math.Max(bands, (toIndex - fromIndex) + 1);
Bands = new Band[bands];
double ratio = Math.Pow(usableSourceData, 1.0 / bands);
double calculation = 1;
int index = fromIndex;
for (int i = 0; i < Bands.Length; i++)
{
calculation *= ratio;
int count = Math.Max(1, ((int)calculation) - index);
float[] bandData = new float[count];
Array.Copy(data, index, bandData, 0, count);
Bands[i] = new Band(FrequencyHelper.GetFrequencyOfIndex(index, dataReferenceCount),
FrequencyHelper.GetFrequencyOfIndex(index + count, dataReferenceCount),
bandData);
index += count;
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/Spectrum/RawSpectrumProvider.cs
================================================
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.AudioProcessing.Spectrum
{
public class RawSpectrumProvider : AbstractSpectrum
{
#region Constructors
public RawSpectrumProvider(float[] data)
{
int dataReferenceCount = (data.Length - 1) * 2;
Bands = new Band[data.Length];
for (int i = 0; i < Bands.Length; i++)
Bands[i] = new Band(FrequencyHelper.GetFrequencyOfIndex(i, dataReferenceCount), FrequencyHelper.GetFrequencyOfIndex(i, dataReferenceCount), new[] { data[i] });
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/VisualizationPRovider/FrequencyBarsVisualizationProvider.cs
================================================
using System;
using KeyboardAudioVisualizer.AudioProcessing.Equalizer;
using KeyboardAudioVisualizer.AudioProcessing.Spectrum;
using KeyboardAudioVisualizer.Configuration;
namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
{
#region Configuration
public enum ValueMode { Max, Average, Sum }
public enum SpectrumMode { Gamma, Logarithmic, Linear }
public class FrequencyBarsVisualizationProviderConfiguration : AbstractConfiguration
{
private ValueMode _valueMode = ValueMode.Sum;
public ValueMode ValueMode
{
get => _valueMode;
set => SetProperty(ref _valueMode, value);
}
private SpectrumMode _spectrumMode = SpectrumMode.Logarithmic;
public SpectrumMode SpectrumMode
{
get => _spectrumMode;
set => SetProperty(ref _spectrumMode, value);
}
private int _bars = 48;
public int Bars
{
get => _bars;
set => SetProperty(ref _bars, value);
}
private double _smoothing = 3;
public double Smoothing
{
get => _smoothing;
set => SetProperty(ref _smoothing, value);
}
private double _minFrequency = 60;
public double MinFrequency
{
get => _minFrequency;
set => SetProperty(ref _minFrequency, value);
}
private double _maxFrequency = 15000;
public double MaxFrequency
{
get => _maxFrequency;
set => SetProperty(ref _maxFrequency, value);
}
private double _referenceLevel = 90;
public double ReferenceLevel
{
get => _referenceLevel;
set => SetProperty(ref _referenceLevel, value);
}
private double _emphasisePeaks = 0.5f;
public double EmphasisePeaks
{
get => _emphasisePeaks;
set => SetProperty(ref _emphasisePeaks, value);
}
private int _gamma = 2;
public int Gamma
{
get => _gamma;
set => SetProperty(ref _gamma, value);
}
}
#endregion
public class FrequencyBarsVisualizationProvider : AbstractAudioProcessor, IVisualizationProvider
{
#region Properties & Fields
private readonly FrequencyBarsVisualizationProviderConfiguration _configuration;
private readonly ISpectrumProvider _spectrumProvider;
private double _smoothingFactor;
private double _emphasiseFactor;
public IEqualizer Equalizer { get; set; }
public IConfiguration Configuration => _configuration;
public float[] VisualizationData { get; private set; }
#endregion
#region Constructors
public FrequencyBarsVisualizationProvider(FrequencyBarsVisualizationProviderConfiguration configuration, ISpectrumProvider spectrumProvider)
{
this._configuration = configuration;
this._spectrumProvider = spectrumProvider;
configuration.PropertyChanged += (sender, args) => RecalculateConfigValues(args.PropertyName);
}
#endregion
#region Methods
public override void Initialize() => RecalculateConfigValues(null);
private void RecalculateConfigValues(string changedPropertyName)
{
if ((changedPropertyName == null) || (changedPropertyName == nameof(FrequencyBarsVisualizationProviderConfiguration.Bars)))
VisualizationData = new float[_configuration.Bars];
if ((changedPropertyName == null) || (changedPropertyName == nameof(FrequencyBarsVisualizationProviderConfiguration.Smoothing)))
_smoothingFactor = Math.Log10(_configuration.Smoothing);
if ((changedPropertyName == null) || (changedPropertyName == nameof(FrequencyBarsVisualizationProviderConfiguration.EmphasisePeaks)))
_emphasiseFactor = (0.75 * (1 + _configuration.EmphasisePeaks));
}
public override void Update()
{
ISpectrum spectrum = GetSpectrum();
if (spectrum == null) return;
float[] equalizerValues = Equalizer?.IsEnabled == true ? Equalizer.CalculateValues(spectrum.BandCount) : null;
for (int i = 0; i < spectrum.BandCount; i++)
{
double binPower = GetBandValue(spectrum[i]);
if (equalizerValues != null)
{
float equalizerValue = equalizerValues[i];
equalizerValue *= 10; //TODO DarthAffe 13.08.2017: Equalizer-Scale through setting?
if (Math.Abs(equalizerValue) > 0.000001)
{
bool lower = equalizerValue < 0;
equalizerValue = 1 + (equalizerValue * equalizerValue);
binPower *= lower ? 1f / equalizerValue : equalizerValue;
}
}
binPower = Math.Max(0, 20 * Math.Log10(binPower));
binPower = Math.Max(0, binPower);
binPower /= _configuration.ReferenceLevel;
if (_configuration.EmphasisePeaks > 0.001)
binPower = Math.Pow(binPower, 1 + _configuration.EmphasisePeaks) * _emphasiseFactor;
if (i < VisualizationData.Length)
{
VisualizationData[i] = (float)((VisualizationData[i] * _smoothingFactor) + (binPower * (1.0 - _smoothingFactor)));
if (float.IsNaN(VisualizationData[i])) VisualizationData[i] = 0;
}
}
}
private ISpectrum GetSpectrum()
{
switch (_configuration.SpectrumMode)
{
case SpectrumMode.Gamma:
return _spectrumProvider.GetGammaSpectrum(_configuration.Bars, _configuration.Gamma, (float)_configuration.MinFrequency, (float)_configuration.MaxFrequency);
case SpectrumMode.Logarithmic:
return _spectrumProvider.GetLogarithmicSpectrum(_configuration.Bars, (float)_configuration.MinFrequency, (float)_configuration.MaxFrequency);
case SpectrumMode.Linear:
return _spectrumProvider.GetLinearSpectrum(_configuration.Bars, (float)_configuration.MinFrequency, (float)_configuration.MaxFrequency);
default:
return null;
}
}
private float GetBandValue(Band band)
{
switch (_configuration.ValueMode)
{
case ValueMode.Max: return band.Max;
case ValueMode.Average: return band.Average;
case ValueMode.Sum: return band.Sum;
default: return 0;
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/VisualizationPRovider/IVisualizationProvider.cs
================================================
using KeyboardAudioVisualizer.Configuration;
namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
{
public interface IVisualizationProvider
{
IConfiguration Configuration { get; }
float[] VisualizationData { get; }
void Initialize();
void Update();
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/VisualizationProvider/BeatVisualizationProvider.cs
================================================
using KeyboardAudioVisualizer.AudioProcessing.Spectrum;
using KeyboardAudioVisualizer.Configuration;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
{
#region Configuration
public class BeatVisualizationProviderConfiguration : AbstractConfiguration
{
//TODO DarthAffe 12.08.2017: Check if there is something usefull to configure here
}
#endregion
public class BeatVisualizationProvider : AbstractAudioProcessor, IVisualizationProvider
{
#region Properties & Fields
private readonly BeatVisualizationProviderConfiguration _configuration;
private readonly ISpectrumProvider _specturProvider;
private RingBuffer[] _history;
public IConfiguration Configuration => _configuration;
public float[] VisualizationData { get; } = new float[1];
#endregion
#region Constructors
public BeatVisualizationProvider(BeatVisualizationProviderConfiguration configuration, ISpectrumProvider specturProvider)
{
this._configuration = configuration;
this._specturProvider = specturProvider;
}
#endregion
#region Methods
public override void Initialize()
{
_history = new RingBuffer[64];
for (int i = 0; i < _history.Length; i++)
_history[i] = new RingBuffer(32);
}
public override void Update()
{
VisualizationData[0] = 0;
ISpectrum spectrum = _specturProvider.GetLogarithmicSpectrum(64);
for (int i = 0; i < 32; i++)
{
float currentEnergy = spectrum[i].Average;
float averageEnergy = _history[i].Average;
_history[i].Put(currentEnergy);
if (currentEnergy > (35 * averageEnergy))
{
VisualizationData[0] = 1;
break;
}
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/VisualizationProvider/LevelVisualizationProvider.cs
================================================
using System;
using System.Linq;
using KeyboardAudioVisualizer.AudioCapture;
using KeyboardAudioVisualizer.Configuration;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
{
#region Configuration
public enum ConversionMode
{
Linear, Logarithmic, Exponential
}
public class LevelVisualizationProviderConfiguration : AbstractConfiguration
{
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 = 8;
public double Scale
{
get => _scale;
set => SetProperty(ref _scale, value);
}
private double _referenceLevel = 90;
public double ReferenceLevel
{
get => _referenceLevel;
set => SetProperty(ref _referenceLevel, value);
}
}
#endregion
public class LevelVisualizationProvider : AbstractAudioProcessor, IVisualizationProvider
{
#region Properties & Fields
private readonly LevelVisualizationProviderConfiguration _configuration;
private readonly AudioBuffer _audioBuffer;
private float[] _sampleDataLeft;
private float[] _sampleDataRight;
private float[] _sampleDataMix;
private double _smoothingFactor;
private double _scalingFactor;
public IConfiguration Configuration => _configuration;
public float[] VisualizationData { get; } = new float[3];
#endregion
#region Constructors
public LevelVisualizationProvider(LevelVisualizationProviderConfiguration configuration, AudioBuffer audioBuffer)
{
this._configuration = configuration;
this._audioBuffer = audioBuffer;
configuration.PropertyChanged += (sender, args) => RecalculateConfigValues(args.PropertyName);
}
#endregion
#region Methods
public override void Initialize()
{
_sampleDataLeft = new float[2048];
_sampleDataRight = new float[2048];
_sampleDataMix = new float[2048];
RecalculateConfigValues(null);
}
private void RecalculateConfigValues(string changedPropertyName)
{
if ((changedPropertyName == null) || (changedPropertyName == nameof(LevelVisualizationProviderConfiguration.Smoothing)))
_smoothingFactor = Math.Log10(MathHelper.Clamp(_configuration.Smoothing, 0.001, 9.5));
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 override void Update()
{
_audioBuffer.CopyLeftInto(ref _sampleDataLeft, 0);
_audioBuffer.CopyRightInto(ref _sampleDataRight, 0);
_audioBuffer.CopyMixInto(ref _sampleDataMix, 0);
float levelLeft = Convert(GetRms(ref _sampleDataLeft));
float levelRight = Convert(GetRms(ref _sampleDataRight));
float levelMix = Convert(GetRms(ref _sampleDataMix));
UpdateData(0, levelLeft);
UpdateData(1, levelRight);
UpdateData(2, levelMix);
}
private float GetRms(ref float[] data) => (float)Math.Sqrt(data.Average(x => x * x));
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, (Math.Pow(_configuration.ReferenceLevel, level) - 1) / _configuration.ReferenceLevel);
default: return level;
}
}
private void UpdateData(int index, float level)
{
VisualizationData[index] = (float)((VisualizationData[index] * _smoothingFactor) + (level * _scalingFactor * (1.0 - _smoothingFactor)));
if (double.IsNaN(VisualizationData[index])) VisualizationData[index] = 0;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/AudioProcessing/VisualizationProvider/VisualizationType.cs
================================================
using KeyboardAudioVisualizer.Attributes;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider
{
public enum VisualizationType
{
None,
[VisualizerFor(RGBDeviceType.Keyboard | RGBDeviceType.LedMatrix)]
[DisplayName("Frequency Bars")]
FrequencyBars,
[VisualizerFor(RGBDeviceType.Keyboard | RGBDeviceType.LedMatrix | RGBDeviceType.LedStripe | RGBDeviceType.Mousepad)]
Level,
Beat,
}
}
================================================
FILE: KeyboardAudioVisualizer/Configuration/AbstractConfiguration.cs
================================================
using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.Configuration
{
public class AbstractConfiguration : AbstractBindable, IConfiguration, INotifyPropertyChanged
{
#region Methods
protected override bool SetProperty(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if ((typeof(T) == typeof(double)) || (typeof(T) == typeof(float)))
{
if (Math.Abs((double)(object)storage - (double)(object)value) < 0.000001) return false;
}
else
{
if (Equals(storage, value)) return false;
}
storage = value;
// ReSharper disable once ExplicitCallerInfoArgument
OnPropertyChanged(propertyName);
return true;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Configuration/ColorSerializer.cs
================================================
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.Configuration
{
public class ColorSerializer : JsonConverter
{
#region Methods
public override bool CanConvert(Type objectType) => objectType == typeof(Color);
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
if (!(value is Color color)) return;
writer.WriteStartObject();
writer.WritePropertyName("A");
writer.WriteValue(color.A);
writer.WritePropertyName("R");
writer.WriteValue(color.R);
writer.WritePropertyName("G");
writer.WriteValue(color.G);
writer.WritePropertyName("B");
writer.WriteValue(color.B);
writer.WriteEndObject();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject jsonObject = JObject.Load(reader);
if (jsonObject.Property("A").Value.ToObject() > 1.0) //DarthAffe 09.06.2019: Convert old Settings
return new Color(jsonObject.Property("A").Value.ToObject(),
jsonObject.Property("R").Value.ToObject(),
jsonObject.Property("G").Value.ToObject(),
jsonObject.Property("B").Value.ToObject());
else
return new Color(jsonObject.Property("A").Value.ToObject(),
jsonObject.Property("R").Value.ToObject(),
jsonObject.Property("G").Value.ToObject(),
jsonObject.Property("B").Value.ToObject());
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Configuration/EqualizerConfiguration.cs
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using KeyboardAudioVisualizer.AudioProcessing.Equalizer;
namespace KeyboardAudioVisualizer.Configuration
{
public class EqualizerConfiguration : AbstractConfiguration
{
#region Properties & Fields
public bool IsEnabled { get; set; } = false;
public List Bands { get; set; } = new List();
#endregion
#region Methods
public void LoadInto(IEqualizer equalizer)
{
equalizer.IsEnabled = IsEnabled;
foreach (EqualizerBand band in Bands)
{
if (band.IsFixedOffset)
{
EqualizerBand bandToUpdate = equalizer.Bands.FirstOrDefault(b => b.IsFixedOffset && (Math.Abs(b.Offset - band.Offset) < 0.01));
if (bandToUpdate != null)
bandToUpdate.Value = band.Value;
}
else
equalizer.AddBand(band.Offset, band.Value);
}
}
public void SaveFrom(IEqualizer equalizer)
{
IsEnabled = equalizer.IsEnabled;
Bands.Clear();
foreach (EqualizerBand band in equalizer.Bands)
Bands.Add(new EqualizerBand(band.Offset, band.Value, band.IsFixedOffset));
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Configuration/IConfiguration.cs
================================================
using System.ComponentModel;
namespace KeyboardAudioVisualizer.Configuration
{
public interface IConfiguration : INotifyPropertyChanged
{ }
}
================================================
FILE: KeyboardAudioVisualizer/Configuration/Settings.cs
================================================
using System;
using System.Collections.Generic;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.Configuration
{
public class Settings
{
#region Constants
public const int CURRENT_VERSION = 1;
#endregion
#region Properties & Fields
public int Version { get; set; } = 0;
public double UpdateRate { get; set; } = 40.0;
public bool EnableAudioPrescale { get; set; } = false;
public LinearGradient Background { get; set; }
public Dictionary Visualizations { get; set; } = new Dictionary();
public VisualizationSettings this[VisualizationIndex visualizationIndex]
{
get
{
if (!Visualizations.TryGetValue(visualizationIndex, out VisualizationSettings settings))
Visualizations[visualizationIndex] = (settings = new VisualizationSettings(visualizationIndex));
return settings;
}
}
#endregion
}
public class VisualizationSettings
{
#region Properties & Fields
public VisualizationType SelectedVisualization { get; set; }
public LinearGradient Gradient { get; set; }
public EqualizerConfiguration EqualizerConfiguration { get; set; } = new EqualizerConfiguration();
public FrequencyBarsVisualizationProviderConfiguration FrequencyBarsConfiguration { get; set; } = new FrequencyBarsVisualizationProviderConfiguration();
public LevelVisualizationProviderConfiguration LevelConfiguration { get; set; } = new LevelVisualizationProviderConfiguration();
public BeatVisualizationProviderConfiguration BeatConfiguration { get; set; } = new BeatVisualizationProviderConfiguration();
public IConfiguration this[VisualizationType visualizationType]
{
get
{
switch (visualizationType)
{
case VisualizationType.None:
return null;
case VisualizationType.FrequencyBars:
return FrequencyBarsConfiguration;
case VisualizationType.Level:
return LevelConfiguration;
case VisualizationType.Beat:
return BeatConfiguration;
default:
throw new ArgumentOutOfRangeException(nameof(visualizationType), visualizationType, null);
}
}
}
#endregion
#region Constructors
public VisualizationSettings(VisualizationIndex visualizationIndex)
{
switch (visualizationIndex)
{
case VisualizationIndex.Primary:
SelectedVisualization = VisualizationType.FrequencyBars;
Gradient = new LinearGradient(new GradientStop(0, HSVColor.Create(300, 1, 1)),
new GradientStop(0.20, HSVColor.Create(225, 1, 1)),
new GradientStop(0.35, HSVColor.Create(180, 1, 1)),
new GradientStop(0.50, HSVColor.Create(135, 1, 1)),
new GradientStop(0.65, HSVColor.Create(90, 1, 1)),
new GradientStop(0.80, HSVColor.Create(45, 1, 1)),
new GradientStop(0.95, HSVColor.Create(0, 1, 1)));
break;
case VisualizationIndex.Secondary:
SelectedVisualization = VisualizationType.Beat;
Gradient = new LinearGradient(new GradientStop(0.5, new Color(255, 255, 255)));
break;
case VisualizationIndex.Tertiary:
SelectedVisualization = VisualizationType.Level;
Gradient = new LinearGradient(new GradientStop(0, new Color(0, 0, 255)),
new GradientStop(1, new Color(255, 0, 0)));
break;
}
}
#endregion
#region Methods
public T GetConfiguration(VisualizationType visualizationType)
where T : IConfiguration, new() => (T)this[visualizationType];
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Controls/BlurredDecorationWindow.cs
================================================
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
namespace KeyboardAudioVisualizer.Controls
{
[TemplatePart(Name = "PART_Decoration", Type = typeof(FrameworkElement))]
[TemplatePart(Name = "PART_Content", Type = typeof(FrameworkElement))]
[TemplatePart(Name = "PART_CloseButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_MinimizeButton", Type = typeof(Button))]
[TemplatePart(Name = "PART_IconButton", Type = typeof(Button))]
public class BlurredDecorationWindow : Window
{
#region DependencyProperties
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty BackgroundImageProperty = DependencyProperty.Register(
"BackgroundImage", typeof(ImageSource), typeof(BlurredDecorationWindow), new PropertyMetadata(default(ImageSource)));
public ImageSource BackgroundImage
{
get => (ImageSource)GetValue(BackgroundImageProperty);
set => SetValue(BackgroundImageProperty, value);
}
public static readonly DependencyProperty DecorationHeightProperty = DependencyProperty.Register(
"DecorationHeight", typeof(double), typeof(BlurredDecorationWindow), new PropertyMetadata(20.0));
public double DecorationHeight
{
get => (double)GetValue(DecorationHeightProperty);
set => SetValue(DecorationHeightProperty, value);
}
public static readonly DependencyProperty IconToolTipProperty = DependencyProperty.Register(
"IconToolTip", typeof(string), typeof(BlurredDecorationWindow), new PropertyMetadata(default(string)));
public string IconToolTip
{
get => (string)GetValue(IconToolTipProperty);
set => SetValue(IconToolTipProperty, value);
}
public static readonly DependencyProperty IconCommandProperty = DependencyProperty.Register(
"IconCommand", typeof(ICommand), typeof(BlurredDecorationWindow), new PropertyMetadata(default(ICommand)));
public ICommand IconCommand
{
get => (ICommand)GetValue(IconCommandProperty);
set => SetValue(IconCommandProperty, value);
}
// ReSharper restore InconsistentNaming
#endregion
#region Constructors
static BlurredDecorationWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(BlurredDecorationWindow), new FrameworkPropertyMetadata(typeof(BlurredDecorationWindow)));
}
#endregion
#region Methods
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
FrameworkElement decoration = GetTemplateChild("PART_Decoration") as FrameworkElement;
if (decoration != null)
decoration.MouseLeftButtonDown += (sender, args) => DragMove();
Button closeButton = GetTemplateChild("PART_CloseButton") as Button;
if (closeButton != null)
closeButton.Click += (sender, args) => ApplicationManager.Instance.ExitCommand.Execute(null);
Button minimizeButton = GetTemplateChild("PART_MinimizeButton") as Button;
if (minimizeButton != null)
minimizeButton.Click += (sender, args) => Hide();
Button iconButton = GetTemplateChild("PART_IconButton") as Button;
if (iconButton != null)
iconButton.Click += (sender, args) => IconCommand?.Execute(null);
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Controls/ColorSelector.cs
================================================
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using RGB.NET.Core;
using Color = RGB.NET.Core.Color;
using Point = System.Windows.Point;
using Rectangle = System.Windows.Shapes.Rectangle;
using WpfColor = System.Windows.Media.Color;
namespace KeyboardAudioVisualizer.Controls
{
[TemplatePart(Name = "PART_Selector", Type = typeof(Panel))]
[TemplatePart(Name = "PART_SliderAlpha", Type = typeof(Slider))]
[TemplatePart(Name = "PART_SliderRed", Type = typeof(Slider))]
[TemplatePart(Name = "PART_SliderGreen", Type = typeof(Slider))]
[TemplatePart(Name = "PART_SliderBlue", Type = typeof(Slider))]
[TemplatePart(Name = "PART_SliderHue", Type = typeof(Slider))]
[TemplatePart(Name = "PART_SliderSaturation", Type = typeof(Slider))]
[TemplatePart(Name = "PART_SliderValue", Type = typeof(Slider))]
[TemplatePart(Name = "PART_Preview", Type = typeof(Rectangle))]
public class ColorSelector : Control
{
#region Properties & Fields
private bool _ignorePropertyChanged;
private bool _dragSelector;
private byte _a;
private byte _r;
private byte _g;
private byte _b;
private double _hue;
private double _saturation;
private double _value;
private Panel _selector;
private Rectangle _selectorColor;
private Grid _selectorGrip;
private Slider _sliderAlpha;
private Slider _sliderRed;
private Slider _sliderGreen;
private Slider _sliderBlue;
private Slider _sliderHue;
private Slider _sliderSaturation;
private Slider _sliderValue;
private Rectangle _preview;
private SolidColorBrush _previewBrush;
private SolidColorBrush _selectorBrush;
private LinearGradientBrush _alphaBrush;
private LinearGradientBrush _redBrush;
private LinearGradientBrush _greenBrush;
private LinearGradientBrush _blueBrush;
private LinearGradientBrush _hueBrush;
private LinearGradientBrush _saturationBrush;
private LinearGradientBrush _valueBrush;
#endregion
#region DependencyProperties
public static readonly DependencyProperty SelectedColorProperty = DependencyProperty.Register(
"SelectedColor", typeof(Color), typeof(ColorSelector), new FrameworkPropertyMetadata(new Color(255, 0, 0),
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
SelectedColorChanged));
public Color SelectedColor
{
get => (Color)GetValue(SelectedColorProperty);
set => SetValue(SelectedColorProperty, value);
}
#endregion
#region Methods
public override void OnApplyTemplate()
{
if ((_selector = GetTemplateChild("PART_Selector") as Panel) != null)
{
_selectorBrush = new SolidColorBrush();
_selectorColor = new Rectangle
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
SnapsToDevicePixels = true,
StrokeThickness = 0,
Fill = _selectorBrush
};
_selector.Children.Add(_selectorColor);
Rectangle selectorWhite = new Rectangle
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
SnapsToDevicePixels = true,
StrokeThickness = 0,
Fill = new LinearGradientBrush(WpfColor.FromRgb(255, 255, 255), WpfColor.FromArgb(0, 255, 255, 255), new Point(0, 0.5), new Point(1, 0.5))
};
_selector.Children.Add(selectorWhite);
Rectangle selectorBlack = new Rectangle
{
VerticalAlignment = VerticalAlignment.Stretch,
HorizontalAlignment = HorizontalAlignment.Stretch,
SnapsToDevicePixels = true,
StrokeThickness = 0,
Fill = new LinearGradientBrush(WpfColor.FromRgb(0, 0, 0), WpfColor.FromArgb(0, 0, 0, 0), new Point(0.5, 1), new Point(0.5, 0))
};
_selector.Children.Add(selectorBlack);
_selectorGrip = new Grid
{
VerticalAlignment = VerticalAlignment.Bottom,
HorizontalAlignment = HorizontalAlignment.Left,
SnapsToDevicePixels = true
};
_selectorGrip.Children.Add(new Ellipse
{
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
SnapsToDevicePixels = true,
Stroke = new SolidColorBrush(WpfColor.FromRgb(0, 0, 0)),
StrokeThickness = 2,
Fill = null,
Width = 12,
Height = 12
});
_selectorGrip.Children.Add(new Ellipse
{
VerticalAlignment = VerticalAlignment.Center,
HorizontalAlignment = HorizontalAlignment.Center,
SnapsToDevicePixels = true,
Stroke = new SolidColorBrush(WpfColor.FromRgb(255, 255, 255)),
StrokeThickness = 1,
Fill = null,
Width = 10,
Height = 10
});
_selector.Children.Add(_selectorGrip);
_selector.SizeChanged += (sender, args) => UpdateSelector();
_selector.MouseLeftButtonDown += (sender, args) =>
{
_dragSelector = true;
UpdateSelectorValue(args.GetPosition(_selector));
};
_selector.MouseEnter += (sender, args) =>
{
if (args.LeftButton == MouseButtonState.Pressed)
{
_dragSelector = true;
UpdateSelectorValue(args.GetPosition(_selector));
}
};
_selector.MouseLeftButtonUp += (sender, args) => _dragSelector = false;
_selector.MouseLeave += (sender, args) => _dragSelector = false;
_selector.MouseMove += (sender, args) => UpdateSelectorValue(args.GetPosition(_selector));
_selector.ClipToBounds = true;
}
if ((_sliderAlpha = GetTemplateChild("PART_SliderAlpha") as Slider) != null)
{
_alphaBrush = new LinearGradientBrush(new GradientStopCollection(new[] { new GradientStop(new WpfColor(), 0),
new GradientStop(new WpfColor(), 1) }));
_sliderAlpha.Background = _alphaBrush;
_sliderAlpha.ValueChanged += AChanged;
}
if ((_sliderRed = GetTemplateChild("PART_SliderRed") as Slider) != null)
{
_redBrush = new LinearGradientBrush(new GradientStopCollection(new[] { new GradientStop(new WpfColor(), 0),
new GradientStop(new WpfColor(), 1) }));
_sliderRed.Background = _redBrush;
_sliderRed.ValueChanged += RChanged;
}
if ((_sliderGreen = GetTemplateChild("PART_SliderGreen") as Slider) != null)
{
_greenBrush = new LinearGradientBrush(new GradientStopCollection(new[] { new GradientStop(new WpfColor(), 0),
new GradientStop(new WpfColor(), 1) }));
_sliderGreen.Background = _greenBrush;
_sliderGreen.ValueChanged += GChanged;
}
if ((_sliderBlue = GetTemplateChild("PART_SliderBlue") as Slider) != null)
{
_blueBrush = new LinearGradientBrush(new GradientStopCollection(new[] { new GradientStop(new WpfColor(), 0),
new GradientStop(new WpfColor(), 1) }));
_sliderBlue.Background = _blueBrush;
_sliderBlue.ValueChanged += BChanged;
}
if ((_sliderHue = GetTemplateChild("PART_SliderHue") as Slider) != null)
{
_hueBrush = new LinearGradientBrush(new GradientStopCollection(new[] { new GradientStop(new WpfColor(), 0),
new GradientStop(new WpfColor(), 1.0 / 6.0),
new GradientStop(new WpfColor(), 2.0 / 6.0),
new GradientStop(new WpfColor(), 3.0 / 6.0),
new GradientStop(new WpfColor(), 4.0 / 6.0),
new GradientStop(new WpfColor(), 5.0 / 6.0),
new GradientStop(new WpfColor(), 1) }));
_sliderHue.Background = _hueBrush;
_sliderHue.ValueChanged += HueChanged;
}
if ((_sliderSaturation = GetTemplateChild("PART_SliderSaturation") as Slider) != null)
{
_saturationBrush = new LinearGradientBrush(new GradientStopCollection(new[] { new GradientStop(new WpfColor(), 0),
new GradientStop(new WpfColor(), 1) }));
_sliderSaturation.Background = _saturationBrush;
_sliderSaturation.ValueChanged += SaturationChanged;
}
if ((_sliderValue = GetTemplateChild("PART_SliderValue") as Slider) != null)
{
_valueBrush = new LinearGradientBrush(new GradientStopCollection(new[] { new GradientStop(new WpfColor(), 0),
new GradientStop(new WpfColor(), 1) }));
_sliderValue.Background = _valueBrush;
_sliderValue.ValueChanged += ValueChanged;
}
if ((_preview = GetTemplateChild("PART_Preview") as Rectangle) != null)
{
_previewBrush = new SolidColorBrush();
_preview.Fill = _previewBrush;
}
SetColor(SelectedColor);
}
private static void SelectedColorChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is ColorSelector cs) || !(dependencyPropertyChangedEventArgs.NewValue is Color color)) return;
cs.SetColor(color);
}
private void SetColor(Color color)
{
if (_ignorePropertyChanged) return;
SetA(color);
SetRGB(color);
SetHSV(color);
UpdateSelector();
UpdateUIColors();
}
private void AChanged(object sender, RoutedPropertyChangedEventArgs routedPropertyChangedEventArgs)
{
if (_ignorePropertyChanged) return;
_a = (byte)routedPropertyChangedEventArgs.NewValue.Clamp(0, byte.MaxValue);
Color color = new Color(_a, _r, _g, _b);
UpdateSelectedColor(color);
UpdateUIColors();
UpdateSelector();
}
private void RChanged(object sender, RoutedPropertyChangedEventArgs routedPropertyChangedEventArgs)
{
if (_ignorePropertyChanged) return;
_r = (byte)routedPropertyChangedEventArgs.NewValue.Clamp(0, byte.MaxValue);
RGBChanged();
}
private void GChanged(object sender, RoutedPropertyChangedEventArgs routedPropertyChangedEventArgs)
{
if (_ignorePropertyChanged) return;
_g = (byte)routedPropertyChangedEventArgs.NewValue.Clamp(0, byte.MaxValue);
RGBChanged();
}
private void BChanged(object sender, RoutedPropertyChangedEventArgs routedPropertyChangedEventArgs)
{
if (_ignorePropertyChanged) return;
_b = (byte)routedPropertyChangedEventArgs.NewValue.Clamp(0, byte.MaxValue);
RGBChanged();
}
private void RGBChanged()
{
Color color = new Color(_a, _r, _g, _b);
UpdateSelectedColor(color);
SetHSV(color);
UpdateUIColors();
UpdateSelector();
}
private void HueChanged(object sender, RoutedPropertyChangedEventArgs routedPropertyChangedEventArgs)
{
if (_ignorePropertyChanged) return;
_hue = routedPropertyChangedEventArgs.NewValue.Clamp(0, 360);
HSVChanged();
}
private void SaturationChanged(object sender, RoutedPropertyChangedEventArgs routedPropertyChangedEventArgs)
{
if (_ignorePropertyChanged) return;
_saturation = routedPropertyChangedEventArgs.NewValue.Clamp(0, 1);
HSVChanged();
}
private void ValueChanged(object sender, RoutedPropertyChangedEventArgs routedPropertyChangedEventArgs)
{
if (_ignorePropertyChanged) return;
_value = routedPropertyChangedEventArgs.NewValue.Clamp(0, 1);
HSVChanged();
}
private void HSVChanged()
{
Color color = HSVColor.Create(_a, _hue, _saturation, _value);
UpdateSelectedColor(color);
SetRGB(color);
UpdateUIColors();
UpdateSelector();
}
private void SetA(Color color)
{
_ignorePropertyChanged = true;
_a = color.GetA();
if (_sliderAlpha != null)
_sliderAlpha.Value = _a;
_ignorePropertyChanged = false;
}
private void SetRGB(Color color)
{
_ignorePropertyChanged = true;
_r = color.GetR();
if (_sliderRed != null)
_sliderRed.Value = _r;
_g = color.GetG();
if (_sliderGreen != null)
_sliderGreen.Value = _g;
_b = color.GetB();
if (_sliderBlue != null)
_sliderBlue.Value = _b;
_ignorePropertyChanged = false;
}
private void SetHSV(Color color)
{
_ignorePropertyChanged = true;
_hue = color.GetHue();
if (_sliderHue != null)
_sliderHue.Value = _hue;
_saturation = color.GetSaturation();
if (_sliderSaturation != null)
_sliderSaturation.Value = _saturation;
_value = color.GetValue();
if (_sliderValue != null)
_sliderValue.Value = _value;
_ignorePropertyChanged = false;
}
private void UpdateSelectedColor(Color color)
{
_ignorePropertyChanged = true;
SelectedColor = color;
_ignorePropertyChanged = false;
}
private void UpdateSelector()
{
if (_selector == null) return;
double selectorX = (_selector.ActualWidth * _saturation) - (_selectorGrip.ActualWidth / 2);
double selectorY = (_selector.ActualHeight * _value) - (_selectorGrip.ActualHeight / 2);
if (!double.IsNaN(selectorX) && !double.IsNaN(selectorY))
_selectorGrip.Margin = new Thickness(selectorX, 0, 0, selectorY);
}
private void UpdateSelectorValue(Point mouseLocation)
{
if (!_dragSelector) return;
double saturation = mouseLocation.X / _selector.ActualWidth;
double value = 1 - (mouseLocation.Y / _selector.ActualHeight);
if (!double.IsNaN(saturation) && !double.IsNaN(value))
{
_saturation = saturation;
_value = value;
HSVChanged();
}
}
private void UpdateUIColors()
{
Color hueColor = HSVColor.Create(_hue, 1, 1);
if (_previewBrush != null)
_previewBrush.Color = WpfColor.FromArgb(_a, _r, _g, _b);
if (_selectorBrush != null)
_selectorBrush.Color = WpfColor.FromRgb(hueColor.GetR(), hueColor.GetG(), hueColor.GetB());
if (_alphaBrush != null)
{
_alphaBrush.GradientStops[0].Color = WpfColor.FromArgb(0, _r, _g, _b);
_alphaBrush.GradientStops[1].Color = WpfColor.FromArgb(255, _r, _g, _b);
}
if (_redBrush != null)
{
_redBrush.GradientStops[0].Color = WpfColor.FromArgb(_a, 0, _g, _b);
_redBrush.GradientStops[1].Color = WpfColor.FromArgb(_a, 255, _g, _b);
}
if (_greenBrush != null)
{
_greenBrush.GradientStops[0].Color = WpfColor.FromArgb(_a, _r, 0, _b);
_greenBrush.GradientStops[1].Color = WpfColor.FromArgb(_a, _r, 255, _b);
}
if (_blueBrush != null)
{
_blueBrush.GradientStops[0].Color = WpfColor.FromArgb(_a, _r, _g, 0);
_blueBrush.GradientStops[1].Color = WpfColor.FromArgb(_a, _r, _g, 255);
}
if (_hueBrush != null)
{
Color referenceColor1 = HSVColor.Create(0, _saturation, _value);
Color referenceColor2 = HSVColor.Create(60, _saturation, _value);
Color referenceColor3 = HSVColor.Create(120, _saturation, _value);
Color referenceColor4 = HSVColor.Create(180, _saturation, _value);
Color referenceColor5 = HSVColor.Create(240, _saturation, _value);
Color referenceColor6 = HSVColor.Create(300, _saturation, _value);
_hueBrush.GradientStops[0].Color = WpfColor.FromArgb(_a, referenceColor1.GetR(), referenceColor1.GetG(), referenceColor1.GetB());
_hueBrush.GradientStops[1].Color = WpfColor.FromArgb(_a, referenceColor2.GetR(), referenceColor2.GetG(), referenceColor2.GetB());
_hueBrush.GradientStops[2].Color = WpfColor.FromArgb(_a, referenceColor3.GetR(), referenceColor3.GetG(), referenceColor3.GetB());
_hueBrush.GradientStops[3].Color = WpfColor.FromArgb(_a, referenceColor4.GetR(), referenceColor4.GetG(), referenceColor4.GetB());
_hueBrush.GradientStops[4].Color = WpfColor.FromArgb(_a, referenceColor5.GetR(), referenceColor5.GetG(), referenceColor5.GetB());
_hueBrush.GradientStops[5].Color = WpfColor.FromArgb(_a, referenceColor6.GetR(), referenceColor6.GetG(), referenceColor6.GetB());
_hueBrush.GradientStops[6].Color = WpfColor.FromArgb(_a, referenceColor1.GetR(), referenceColor1.GetG(), referenceColor1.GetB());
}
if (_saturationBrush != null)
{
Color referenceColor = HSVColor.Create(_hue, 1, _value);
_saturationBrush.GradientStops[0].Color = WpfColor.FromArgb(_a, 255, 255, 255);
_saturationBrush.GradientStops[1].Color = WpfColor.FromArgb(_a, referenceColor.GetR(), referenceColor.GetG(), referenceColor.GetB());
}
if (_valueBrush != null)
{
Color referenceColor = HSVColor.Create(_hue, _saturation, 1);
_valueBrush.GradientStops[0].Color = WpfColor.FromArgb(_a, 0, 0, 0);
_valueBrush.GradientStops[1].Color = WpfColor.FromArgb(_a, referenceColor.GetR(), referenceColor.GetG(), referenceColor.GetB());
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Controls/Form.cs
================================================
using System;
using System.Windows;
using System.Windows.Controls;
namespace KeyboardAudioVisualizer.Controls
{
public class Form : Panel
{
#region DependencyProperties
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty RowHeightProperty = DependencyProperty.Register("RowHeight", typeof(double), typeof(Form),
new FrameworkPropertyMetadata(24.0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public double RowHeight
{
get => (double)GetValue(RowHeightProperty);
set
{
if (value < 0) throw new ArgumentOutOfRangeException(nameof(RowHeight), "Row height can't be negative");
SetValue(RowHeightProperty, value);
}
}
public static readonly DependencyProperty LabelWidthProperty = DependencyProperty.Register("LabelWidth", typeof(double), typeof(Form),
new FrameworkPropertyMetadata(100.0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public double LabelWidth
{
get => (double)GetValue(LabelWidthProperty);
set
{
if (value < 0) throw new ArgumentOutOfRangeException(nameof(RowHeight), "Label width can't be negative");
SetValue(LabelWidthProperty, value);
}
}
public static readonly DependencyProperty ElementSpacingProperty = DependencyProperty.Register("ElementSpacing", typeof(double), typeof(Form),
new FrameworkPropertyMetadata(default(double), FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public double ElementSpacing
{
get => (double)GetValue(ElementSpacingProperty);
set => SetValue(ElementSpacingProperty, value);
}
public static readonly DependencyProperty RowSpacingProperty = DependencyProperty.Register("RowSpacing", typeof(double), typeof(Form),
new FrameworkPropertyMetadata(default(double), FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public double RowSpacing
{
get => (double)GetValue(RowSpacingProperty);
set => SetValue(RowSpacingProperty, value);
}
// ReSharper restore InconsistentNaming
#endregion
#region AttachedProperties
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty IsLabelProperty = DependencyProperty.RegisterAttached("IsLabel", typeof(bool), typeof(Form),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static void SetIsLabel(UIElement element, bool value) => element.SetValue(IsLabelProperty, value);
public static bool GetIsLabel(UIElement element) => (bool)element.GetValue(IsLabelProperty);
public static readonly DependencyProperty LineBreaksProperty = DependencyProperty.RegisterAttached("LineBreaks", typeof(int), typeof(Form),
new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static void SetLineBreaks(UIElement element, int value) => element.SetValue(LineBreaksProperty, value);
public static int GetLineBreaks(UIElement element) => (int)element.GetValue(LineBreaksProperty);
public static readonly DependencyProperty RowSpanProperty = DependencyProperty.RegisterAttached("RowSpan", typeof(int), typeof(Form),
new FrameworkPropertyMetadata(1, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static void SetRowSpan(DependencyObject element, int value) => element.SetValue(RowSpanProperty, value);
public static int GetRowSpan(DependencyObject element) => (int)element.GetValue(RowSpanProperty);
public static readonly DependencyProperty FillProperty = DependencyProperty.RegisterAttached("Fill", typeof(bool), typeof(Form),
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsArrange | FrameworkPropertyMetadataOptions.AffectsMeasure));
public static void SetFill(DependencyObject element, bool value) => element.SetValue(FillProperty, value);
public static bool GetFill(DependencyObject element) => (bool)element.GetValue(FillProperty);
// ReSharper restore InconsistentNaming
#endregion
#region Methods
protected override Size MeasureOverride(Size availableSize)
{
if (InternalChildren.Count == 0) return new Size(0, 0);
FormLayout layout = new FormLayout(RowHeight, LabelWidth, ElementSpacing, RowSpacing);
foreach (UIElement child in InternalChildren)
{
child.Measure(availableSize);
layout.AddElement(child, 0);
}
return new Size(layout.Width, layout.Height);
}
protected override Size ArrangeOverride(Size finalSize)
{
if (InternalChildren.Count == 0) return new Size(0, 0);
FormLayout layout = new FormLayout(RowHeight, LabelWidth, ElementSpacing, RowSpacing);
foreach (UIElement child in InternalChildren)
child.Arrange(layout.AddElement(child, finalSize.Width));
return new Size(finalSize.Width, layout.Height);
}
#endregion
#region Data
private class FormLayout
{
#region Properties & Fields
private readonly double _rowHeight;
private readonly double _labelWidth;
private readonly double _elementSpacing;
private readonly double _rowSpacing;
private double _currentRowWidth;
private int _newRows = 0;
private int _rows = -1;
private double _currentMaxWidth;
public double Width => Math.Max((Math.Max(_currentMaxWidth, _currentRowWidth) - _elementSpacing), 0);
public double Height => ((_rows + 1) * _rowHeight) + (_rows * _rowSpacing);
#endregion
#region Constructors
public FormLayout(double rowHeight, double labelWidth, double elementSpacing, double rowSpacing)
{
this._rowHeight = rowHeight;
this._labelWidth = labelWidth;
this._elementSpacing = elementSpacing;
this._rowSpacing = rowSpacing;
}
#endregion
#region Methods
public Rect AddElement(UIElement element, double targetWidth)
{
bool isLabel = GetIsLabel(element);
int lineBreaks = GetLineBreaks(element);
int rowSpan = GetRowSpan(element);
double elementWidth = isLabel ? _labelWidth : element.DesiredSize.Width;
double height = _rowHeight;
if (_newRows > 0)
{
AddLineBreaks(_newRows);
_newRows = 0;
}
if (lineBreaks > 0) AddLineBreaks(lineBreaks);
else if (isLabel) AddLineBreaks(1);
else if (_rows < 0) _rows = 0;
if (!isLabel && (_currentRowWidth < _labelWidth))
_currentRowWidth = _labelWidth + _elementSpacing;
if (rowSpan > 1)
{
height = (rowSpan * _rowHeight) + ((rowSpan - 1) * _rowSpacing);
_newRows = Math.Max(_newRows, rowSpan - 1);
}
if (element is FrameworkElement fe)
fe.MaxHeight = height;
double width = elementWidth;
if ((targetWidth >= 1) && GetFill(element))
width = targetWidth - _currentRowWidth;
Rect rect = new Rect(new Point(_currentRowWidth, (_rows * _rowHeight) + (_rows * _rowSpacing)), new Size(width, height));
_currentRowWidth += width + _elementSpacing;
return rect;
}
private void AddLineBreaks(int count)
{
if (count <= 0) return;
_currentMaxWidth = Math.Max(_currentMaxWidth, _currentRowWidth);
_currentRowWidth = 0;
_rows += count;
}
#endregion
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Controls/GradientEditor.cs
================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
using Color = System.Windows.Media.Color;
using Point = System.Windows.Point;
using Size = System.Windows.Size;
using Rectangle = System.Windows.Shapes.Rectangle;
using GradientStop = RGB.NET.Brushes.Gradients.GradientStop;
namespace KeyboardAudioVisualizer.Controls
{
[TemplatePart(Name = "PART_Gradient", Type = typeof(Canvas))]
[TemplatePart(Name = "PART_Stops", Type = typeof(Canvas))]
public class GradientEditor : Control
{
#region Properties & Fields
private Canvas _gradientContainer;
private Canvas _stopContainer;
private readonly List _previewRectangles = new List();
private readonly Dictionary _stops = new Dictionary();
private ContentControl _draggingStop;
private AdornerLayer _adornerLayer;
private ColorPickerAdorner _adorner;
private Window _window;
#endregion
#region DepdencyProperties
public static readonly DependencyProperty GradientProperty = DependencyProperty.Register(
"Gradient", typeof(LinearGradient), typeof(GradientEditor), new FrameworkPropertyMetadata(null,
FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
OnGradientChanged));
public LinearGradient Gradient
{
get => (LinearGradient)GetValue(GradientProperty);
set => SetValue(GradientProperty, value);
}
public static readonly DependencyProperty GradientStopStyleProperty = DependencyProperty.Register(
"GradientStopStyle", typeof(Style), typeof(GradientEditor), new PropertyMetadata(default(Style)));
public Style GradientStopStyle
{
get => (Style)GetValue(GradientStopStyleProperty);
set => SetValue(GradientStopStyleProperty, value);
}
public static readonly DependencyProperty SelectedStopProperty = DependencyProperty.Register(
"SelectedStop", typeof(GradientStop), typeof(GradientEditor), new PropertyMetadata(default(GradientStop), SelectedStopChanged));
public GradientStop SelectedStop
{
get => (GradientStop)GetValue(SelectedStopProperty);
set => SetValue(SelectedStopProperty, value);
}
public static readonly DependencyProperty ColorSelectorTemplateProperty = DependencyProperty.Register(
"ColorSelectorTemplate", typeof(DataTemplate), typeof(GradientEditor), new PropertyMetadata(default(DataTemplate)));
public DataTemplate ColorSelectorTemplate
{
get => (DataTemplate)GetValue(ColorSelectorTemplateProperty);
set => SetValue(ColorSelectorTemplateProperty, value);
}
public static readonly DependencyProperty CanAddOrDeleteStopsProperty = DependencyProperty.Register(
"CanAddOrDeleteStops", typeof(bool), typeof(GradientEditor), new PropertyMetadata(true));
public bool CanAddOrDeleteStops
{
get => (bool)GetValue(CanAddOrDeleteStopsProperty);
set => SetValue(CanAddOrDeleteStopsProperty, value);
}
#endregion
#region AttachedProperties
public static readonly DependencyProperty IsSelectedProperty = DependencyProperty.RegisterAttached(
"IsSelected", typeof(bool), typeof(GradientEditor), new PropertyMetadata(default(bool)));
public static void SetIsSelected(DependencyObject element, bool value) => element.SetValue(IsSelectedProperty, value);
public static bool GetIsSelected(DependencyObject element) => (bool)element.GetValue(IsSelectedProperty);
#endregion
#region Constructors
public GradientEditor()
{
if (Gradient == null)
Gradient = new LinearGradient();
}
#endregion
#region Methods
public override void OnApplyTemplate()
{
if ((_gradientContainer = GetTemplateChild("PART_Gradient") as Canvas) != null)
{
_gradientContainer.SizeChanged += (sender, args) => UpdateGradientPreview();
_gradientContainer.MouseDown += GradientContainerOnMouseDown;
}
if ((_stopContainer = GetTemplateChild("PART_Stops") as Canvas) != null)
_stopContainer.SizeChanged += (sender, args) => UpdateGradientStops();
_adornerLayer = AdornerLayer.GetAdornerLayer(this);
_window = Window.GetWindow(this);
if (_window != null)
{
_window.PreviewMouseDown += WindowMouseDown;
_window.PreviewKeyDown += (sender, args) =>
{
if (args.Key == Key.Escape)
SelectedStop = null;
};
}
UpdateGradientPreview();
UpdateGradientStops();
}
private void UpdateGradientPreview()
{
if ((_gradientContainer == null) || (Gradient == null)) return;
List gradientStops = Gradient.GradientStops.OrderBy(x => x.Offset).ToList();
if (gradientStops.Count == 0)
UpdatePreviewRectangleCount(gradientStops.Count);
else if (gradientStops.Count == 1)
{
UpdatePreviewRectangleCount(gradientStops.Count);
GradientStop firstStop = gradientStops[0];
UpdatePreviewRectangle(_previewRectangles[0], _gradientContainer.ActualWidth, _gradientContainer.ActualHeight, 0, 1, firstStop.Color, firstStop.Color);
}
else
{
UpdatePreviewRectangleCount(gradientStops.Count + 1);
GradientStop firstStop = gradientStops[0];
UpdatePreviewRectangle(_previewRectangles[0], _gradientContainer.ActualWidth, _gradientContainer.ActualHeight, 0, firstStop.Offset, firstStop.Color, firstStop.Color);
for (int i = 0; i < (gradientStops.Count - 1); i++)
{
GradientStop stop = gradientStops[i];
GradientStop nextStop = gradientStops[i + 1];
Rectangle rect = _previewRectangles[i + 1];
UpdatePreviewRectangle(rect, _gradientContainer.ActualWidth, _gradientContainer.ActualHeight, stop.Offset, nextStop.Offset, stop.Color, nextStop.Color);
}
GradientStop lastStop = gradientStops[gradientStops.Count - 1];
UpdatePreviewRectangle(_previewRectangles[_previewRectangles.Count - 1], _gradientContainer.ActualWidth, _gradientContainer.ActualHeight, lastStop.Offset, 1, lastStop.Color, lastStop.Color);
}
}
private void UpdatePreviewRectangle(Rectangle rect, double referenceWidth, double referenceHeight, double from, double to,
RGB.NET.Core.Color startColor, RGB.NET.Core.Color endColor)
{
rect.Fill = new LinearGradientBrush(Color.FromArgb(startColor.GetA(), startColor.GetR(), startColor.GetG(), startColor.GetB()),
Color.FromArgb(endColor.GetA(), endColor.GetR(), endColor.GetG(), endColor.GetB()),
new Point(0, 0.5), new Point(1, 0.5));
//DarthAffe 09.02.2018: Forced rounding to prevent render issues on resize
Canvas.SetLeft(rect, Math.Floor(referenceWidth * from.Clamp(0, 1)));
rect.Width = Math.Ceiling(referenceWidth * (to.Clamp(0, 1) - from.Clamp(0, 1)));
Canvas.SetTop(rect, 0);
rect.Height = referenceHeight;
}
private void UpdatePreviewRectangleCount(int gradientCount)
{
int countDiff = gradientCount - _previewRectangles.Count;
if (countDiff > 0)
for (int i = 0; i < countDiff; i++)
{
Rectangle rect = new Rectangle { VerticalAlignment = VerticalAlignment.Stretch };
_previewRectangles.Add(rect);
_gradientContainer.Children.Add(rect);
}
if (countDiff < 0)
for (int i = 0; i < Math.Abs(countDiff); i++)
{
int index = _previewRectangles.Count - i - 1;
Rectangle rect = _previewRectangles[index];
_previewRectangles.RemoveAt(index);
_gradientContainer.Children.Remove(rect);
}
}
private void UpdateGradientStops()
{
if (Gradient == null) return;
List gradientStops = Gradient.GradientStops.OrderBy(x => x.Offset).ToList();
UpdateGradientStopsCount(gradientStops);
foreach (GradientStop stop in gradientStops)
UpdateGradientStop(_stops[stop], _stopContainer.ActualWidth, _stopContainer.ActualHeight, stop);
}
private void UpdateGradientStop(ContentControl control, double referenceWidth, double referenceHeight, GradientStop stop)
{
control.Background = new SolidColorBrush(Color.FromArgb(stop.Color.GetA(), stop.Color.GetR(), stop.Color.GetG(), stop.Color.GetB()));
Canvas.SetLeft(control, (referenceWidth * stop.Offset.Clamp(0, 1)) - (control.Width / 2.0));
Canvas.SetTop(control, 0);
control.Height = referenceHeight;
}
private void UpdateGradientStopsCount(List gradientStops)
{
foreach (GradientStop stop in gradientStops)
{
if (!_stops.ContainsKey(stop))
{
ContentControl control = new ContentControl
{
VerticalAlignment = VerticalAlignment.Stretch,
Style = GradientStopStyle,
Content = stop
};
control.MouseDown += GradientStopOnMouseDown;
_stops.Add(stop, control);
_stopContainer.Children.Add(control);
}
}
List stopsToRemove = new List();
foreach (KeyValuePair stopPair in _stops)
if (!gradientStops.Contains(stopPair.Key))
{
ContentControl control = stopPair.Value;
control.MouseDown -= GradientStopOnMouseDown;
stopsToRemove.Add(stopPair.Key);
_stopContainer.Children.Remove(control);
}
foreach (GradientStop stop in stopsToRemove)
_stops.Remove(stop);
}
private void AttachGradient(AbstractGradient gradient) => gradient.GradientChanged += GradientChanged;
private void DetachGradient(AbstractGradient gradient) => gradient.GradientChanged -= GradientChanged;
private void GradientChanged(object o, EventArgs eventArgs)
{
UpdateGradientPreview();
UpdateGradientStops();
}
private static void OnGradientChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is GradientEditor ge)) return;
if (dependencyPropertyChangedEventArgs.OldValue is AbstractGradient oldGradient)
ge.DetachGradient(oldGradient);
if (dependencyPropertyChangedEventArgs.NewValue is AbstractGradient newGradient)
ge.AttachGradient(newGradient);
}
private void GradientContainerOnMouseDown(object o, MouseButtonEventArgs mouseButtonEventArgs)
{
if ((mouseButtonEventArgs.ChangedButton != MouseButton.Left) || (Gradient == null) || !CanAddOrDeleteStops) return;
double offset = mouseButtonEventArgs.GetPosition(_gradientContainer).X / _gradientContainer.ActualWidth;
RGB.NET.Core.Color color = Gradient.GetColor(offset);
GradientStop newStop = new GradientStop(offset, color);
Gradient.GradientStops.Add(newStop);
SelectedStop = newStop;
}
private void GradientStopOnMouseDown(object o, MouseButtonEventArgs mouseButtonEventArgs)
{
if (!((o as ContentControl)?.Content is GradientStop stop) || (Gradient == null)) return;
if (mouseButtonEventArgs.ChangedButton == MouseButton.Right)
{
if (CanAddOrDeleteStops)
Gradient.GradientStops.Remove(stop);
}
else if (mouseButtonEventArgs.ChangedButton == MouseButton.Left)
{
SelectedStop = stop;
_draggingStop = (ContentControl)o;
}
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_draggingStop?.Content is GradientStop stop)
{
double location = e.GetPosition(_gradientContainer).X;
stop.Offset = (location / _gradientContainer.ActualWidth).Clamp(0, 1);
}
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
_draggingStop = null;
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
_draggingStop = null;
}
private static void SelectedStopChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is GradientEditor gradientEditor)) return;
if (gradientEditor._adorner != null)
gradientEditor._adornerLayer.Remove(gradientEditor._adorner);
if (dependencyPropertyChangedEventArgs.OldValue is GradientStop oldStop)
{
if (gradientEditor._stops.TryGetValue(oldStop, out ContentControl oldcontrol))
SetIsSelected(oldcontrol, false);
}
if (dependencyPropertyChangedEventArgs.NewValue is GradientStop stop)
{
ContentControl stopContainer = gradientEditor._stops[stop];
SetIsSelected(stopContainer, true);
if (gradientEditor._adornerLayer != null)
{
ContentControl contentControl = new ContentControl
{
ContentTemplate = gradientEditor.ColorSelectorTemplate,
Content = stop
};
ColorPickerAdorner adorner = new ColorPickerAdorner(stopContainer, contentControl);
gradientEditor._adorner = adorner;
gradientEditor._adornerLayer.Add(adorner);
}
}
}
private void WindowMouseDown(object o, MouseButtonEventArgs mouseButtonEventArgs)
{
if ((_adorner != null) && (VisualTreeHelper.HitTest(_adorner, mouseButtonEventArgs.GetPosition(_adorner)) == null))
SelectedStop = null;
}
#endregion
}
public class ColorPickerAdorner : Adorner
{
#region Properties & Fields
private readonly VisualCollection _visualChildren;
private readonly FrameworkElement _colorSelector;
protected override int VisualChildrenCount => 1;
protected override Visual GetVisualChild(int index) => _colorSelector;
#endregion
#region Constructors
public ColorPickerAdorner(UIElement adornedElement, FrameworkElement colorSelector)
: base(adornedElement)
{
this._colorSelector = colorSelector;
_visualChildren = new VisualCollection(this) { colorSelector };
}
#endregion
#region Methods
protected override Size ArrangeOverride(Size finalSize)
{
Window referenceWindow = Window.GetWindow(AdornedElement);
Point referenceLocation = AdornedElement.TranslatePoint(new Point(0, 0), referenceWindow);
double referenceWidth = ((FrameworkElement)AdornedElement).ActualWidth / 2.0;
double referenceHeight = ((FrameworkElement)AdornedElement).Height;
double referenceX = referenceLocation.X + referenceWidth;
double halfWidth = finalSize.Width / 2.0;
double maxOffset = referenceWindow.Width - halfWidth;
double offset = (referenceX < halfWidth ? referenceX
: (((referenceX + (referenceWidth * 2)) > maxOffset)
? halfWidth - ((maxOffset - referenceX) - (referenceWidth * 2))
: halfWidth));
_colorSelector.Arrange(new Rect(new Point(referenceWidth - offset, referenceHeight), finalSize));
return _colorSelector.RenderSize;
}
protected override Size MeasureOverride(Size constraint)
{
_colorSelector.Measure(constraint);
return _colorSelector.DesiredSize;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Controls/ImageButton.cs
================================================
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
namespace KeyboardAudioVisualizer.Controls
{
public class ImageButton : Button
{
#region Properties & Fields
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty ImageProperty = DependencyProperty.Register(
"Image", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(default(ImageSource)));
public ImageSource Image
{
get => (ImageSource)GetValue(ImageProperty);
set => SetValue(ImageProperty, value);
}
public static readonly DependencyProperty HoverImageProperty = DependencyProperty.Register(
"HoverImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(default(ImageSource)));
public ImageSource HoverImage
{
get => (ImageSource)GetValue(HoverImageProperty);
set => SetValue(HoverImageProperty, value);
}
public static readonly DependencyProperty PressedImageProperty = DependencyProperty.Register(
"PressedImage", typeof(ImageSource), typeof(ImageButton), new PropertyMetadata(default(ImageSource)));
public ImageSource PressedImage
{
get => (ImageSource)GetValue(PressedImageProperty);
set => SetValue(PressedImageProperty, value);
}
// ReSharper restore InconsistentNaming
#endregion
#region Constructors
static ImageButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/BoolToVisibilityConverter.cs
================================================
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace KeyboardAudioVisualizer.Converter
{
[ValueConversion(typeof(bool), typeof(Visibility))]
public class BoolToVisibilityConverter : IValueConverter
{
#region Methods
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> (value as bool?) == true ? Visibility.Visible
: (string.Equals(parameter?.ToString(), "true", StringComparison.OrdinalIgnoreCase) ? Visibility.Hidden : Visibility.Collapsed);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => value as Visibility? == Visibility.Visible;
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/EqualizerBandsToPointsConverter.cs
================================================
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Data;
using System.Windows.Media;
using KeyboardAudioVisualizer.AudioProcessing.Equalizer;
namespace KeyboardAudioVisualizer.Converter
{
public class EqualizerBandsToPointsConverter : IMultiValueConverter
{
#region Methods
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
PointCollection points = new PointCollection();
if ((values.Length != 4) || (parameter == DependencyProperty.UnsetValue)
|| (values[0] == null) || (values[0] == DependencyProperty.UnsetValue)
|| (values[1] == null) || (values[1] == DependencyProperty.UnsetValue) //HACK DarthAffe 13.08.2017: I need this only to update the binding
|| (values[2] == null) || (values[2] == DependencyProperty.UnsetValue)
|| (values[3] == null) || (values[3] == DependencyProperty.UnsetValue))
return points;
IEqualizer equalizer = (IEqualizer)values[0];
double width = (double)values[2];
double height = (double)values[3];
int valueCount = int.Parse(parameter.ToString());
double halfHeight = height / 2.0;
List<(float offset, float value)> pointValues = equalizer.Bands.Select(b => (b.Offset, b.Value)).ToList();
float[] calculatedValues = equalizer.CalculateValues(valueCount);
for (int i = 0; i < calculatedValues.Length; i++)
pointValues.Add(((float)i / calculatedValues.Length, calculatedValues[i]));
foreach ((float offset, float value) in pointValues.OrderBy(x => x.offset))
points.Add(new Point(offset * width, GetPosY(value, halfHeight)));
return points;
}
private double GetPosY(float offset, double halfHeight)
{
if (offset < 0)
return halfHeight + (-offset * halfHeight);
if (offset > 0)
return halfHeight - (offset * halfHeight);
return halfHeight;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotSupportedException();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/EqualsToBoolConverter.cs
================================================
using System;
using System.Globalization;
using System.Windows.Data;
namespace KeyboardAudioVisualizer.Converter
{
[ValueConversion(typeof(object), typeof(bool))]
public class EqualsToBoolConverter : IValueConverter
{
#region Methods
public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => Equals(value, parameter);
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/OffsetToPosXConverter.cs
================================================
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace KeyboardAudioVisualizer.Converter
{
public class OffsetToPosXConverter : IMultiValueConverter
{
#region Methods
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if ((values.Length != 2) || (parameter == DependencyProperty.UnsetValue)
|| (values[0] == null) || (values[0] == DependencyProperty.UnsetValue)
|| (values[1] == null) || (values[1] == DependencyProperty.UnsetValue))
return 0;
float offset = (float)values[0];
double width = (double)values[1];
double correction = double.Parse(parameter.ToString());
return (offset * width) - correction;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/ValueToPosYConverter.cs
================================================
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
namespace KeyboardAudioVisualizer.Converter
{
public class ValueToPosYConverter : IMultiValueConverter
{
#region Methods
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if ((values.Length != 2) || (parameter == DependencyProperty.UnsetValue)
|| (values[0] == null) || (values[0] == DependencyProperty.UnsetValue)
|| (values[1] == null) || (values[1] == DependencyProperty.UnsetValue))
return 0;
float offset = (float)values[0];
double height = (double)values[1];
double correction = double.Parse(parameter.ToString());
double halfHeight = height / 2.0;
if (offset < 0)
return (halfHeight + (-offset * halfHeight)) - correction;
if (offset > 0)
return (halfHeight - (offset * halfHeight)) - correction;
return halfHeight - correction;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture) => throw new NotImplementedException();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/VisualizationProviderDisplayNameConverter.cs
================================================
using System;
using System.Globalization;
using System.Windows.Data;
using KeyboardAudioVisualizer.Attributes;
using KeyboardAudioVisualizer.Helper;
using VisualizationType = KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider.VisualizationType;
namespace KeyboardAudioVisualizer.Converter
{
[ValueConversion(typeof(VisualizationType), typeof(string))]
public class VisualizationProviderDisplayNameConverter : IValueConverter
{
#region Methods
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is VisualizationType visualizationType)) return null;
return visualizationType.GetAttribute()?.DisplayName ?? visualizationType.ToString();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/VisualizationToLastChildFillConverter.cs
================================================
using System;
using System.Globalization;
using System.Windows.Data;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.UI.Visualization;
namespace KeyboardAudioVisualizer.Converter
{
[ValueConversion(typeof(IVisualizationProvider), typeof(bool))]
public class VisualizationToLastChildFillConverter : IValueConverter
{
#region Methods
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return value is FrequencyBarsVisualizationProvider;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Converter/VisualizationTypeSelectableConverter.cs
================================================
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Windows.Data;
using KeyboardAudioVisualizer.Attributes;
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Core;
using VisualizationType = KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider.VisualizationType;
namespace KeyboardAudioVisualizer.Converter
{
[ValueConversion(typeof(IEnumerable), typeof(IEnumerable))]
public class VisualizationTypeSelectableConverter : IValueConverter
{
#region Methods
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (!(value is IEnumerable visualizationProviders) || !(parameter is RGBDeviceType targetDevice)) return new List();
return visualizationProviders.Where(x => x.GetAttribute()?.VisualizerFor.HasFlag(targetDevice) ?? true).ToList();
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => throw new NotSupportedException();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Decorators/BeatDecorator.cs
================================================
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.Decorators
{
public class BeatDecorator : AbstractUpdateAwareDecorator, IBrushDecorator
{
#region Properties & Fields
private readonly IVisualizationProvider _visualizationProvider;
#endregion
#region Constructors
public BeatDecorator(IVisualizationProvider visualizationProvider)
{
this._visualizationProvider = visualizationProvider;
}
#endregion
#region Methods
protected override void Update(double deltaTime) => _visualizationProvider.Update();
#endregion
public Color ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, Color color) => color.SetA(color.A * _visualizationProvider.VisualizationData[0]);
}
}
================================================
FILE: KeyboardAudioVisualizer/Decorators/FrequencyBarsDecorator.cs
================================================
using System;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using RGB.NET.Core;
using Color = RGB.NET.Core.Color;
using Rectangle = RGB.NET.Core.Rectangle;
namespace KeyboardAudioVisualizer.Decorators
{
public class FrequencyBarsDecorator : AbstractUpdateAwareDecorator, IBrushDecorator
{
#region Properties & Fields
private readonly IVisualizationProvider _visualizationProvider;
#endregion
#region Constructors
public FrequencyBarsDecorator(IVisualizationProvider visualizationProvider)
{
this._visualizationProvider = visualizationProvider;
}
#endregion
#region Methods
protected override void Update(double deltaTime) => _visualizationProvider.Update();
public Color ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, Color color)
{
int barSampleIndex = Math.Min(_visualizationProvider.VisualizationData.Length, (int)Math.Floor(_visualizationProvider.VisualizationData.Length * (renderTarget.Point.X / (rectangle.Location.X + rectangle.Size.Width))));
double curBarHeight = 1.0 - Math.Max(0f, _visualizationProvider.VisualizationData[barSampleIndex]);
double verticalPos = (renderTarget.Point.Y / rectangle.Size.Height);
return curBarHeight > verticalPos ? color.SetA(0) : color;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Decorators/LevelBarDecorator.cs
================================================
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.Decorators
{
public class LevelBarDecorator : AbstractUpdateAwareDecorator, IBrushDecorator
{
#region Properties & Fields
private readonly IVisualizationProvider _visualizationProvider;
public LevelBarDirection Direction { get; set; }
public int DataIndex { get; set; }
public LinearGradient Gradient { get; set; }
#endregion
#region Constructors
public LevelBarDecorator(IVisualizationProvider visualizationProvider, LevelBarDirection direction, int dataIndex, LinearGradient gradient)
{
this._visualizationProvider = visualizationProvider;
this.Direction = direction;
this.DataIndex = dataIndex;
this.Gradient = gradient;
}
#endregion
#region Methods
protected override void Update(double deltaTime) => _visualizationProvider.Update();
public Color ManipulateColor(Rectangle rectangle, BrushRenderTarget renderTarget, Color color)
{
double offset = CalculateOffset(rectangle, renderTarget);
if (Direction == LevelBarDirection.Horizontal)
{
if (offset < 0)
{
offset = (-offset * 2);
if (offset >= _visualizationProvider.VisualizationData[0])
return color.SetA(0);
else
return Gradient.GetColor(offset);
}
else
{
offset *= 2;
if (offset >= _visualizationProvider.VisualizationData[1])
return color.SetA(0);
else
return Gradient.GetColor(offset);
}
}
else
{
if (offset >= _visualizationProvider.VisualizationData[DataIndex])
return color.SetA(0);
}
return color;
}
private double CalculateOffset(Rectangle rectangle, BrushRenderTarget renderTarget)
{
switch (Direction)
{
case LevelBarDirection.Left:
return (rectangle.Size.Width - renderTarget.Rectangle.Center.X) / rectangle.Size.Width;
case LevelBarDirection.Right:
return renderTarget.Rectangle.Center.X / rectangle.Size.Width;
case LevelBarDirection.Top:
return (rectangle.Size.Height - renderTarget.Rectangle.Center.Y) / rectangle.Size.Height;
case LevelBarDirection.Bottom:
return renderTarget.Rectangle.Center.Y / rectangle.Size.Height;
case LevelBarDirection.Horizontal:
return (renderTarget.Rectangle.Center.X / rectangle.Size.Width) - 0.5;
default:
return -1;
}
}
#endregion
}
#region Data
public enum LevelBarDirection
{
Left, Right, Top, Bottom,
//HACK DarthAffe 12.09.2017: Just a bad workaround ...
Horizontal
}
#endregion
}
================================================
FILE: KeyboardAudioVisualizer/Helper/ActionCommand.cs
================================================
using System;
using System.Windows.Input;
namespace KeyboardAudioVisualizer.Helper
{
public class ActionCommand : ICommand
{
#region Properties & Fields
private readonly Func _canExecute;
private readonly Action _command;
#endregion
#region Events
public event EventHandler CanExecuteChanged;
#endregion
#region Constructors
public ActionCommand(Action command, Func canExecute = null)
{
this._command = command;
this._canExecute = canExecute;
}
#endregion
#region Methods
public bool CanExecute(object parameter)
{
return _canExecute?.Invoke() ?? true;
}
public void Execute(object parameter)
{
_command?.Invoke();
}
public void RaiseCanExecuteChanged()
{
CanExecuteChanged?.Invoke(this, new EventArgs());
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/EnumExtension.cs
================================================
using System;
using System.Linq;
namespace KeyboardAudioVisualizer.Helper
{
public static class EnumExtension
{
#region Methods
public static T GetAttribute(this Enum e)
where T : Attribute => e.GetType().GetMember(e.ToString()).FirstOrDefault()?.GetCustomAttributes(typeof(T), false).FirstOrDefault() as T;
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/ExceptionExtension.cs
================================================
using System;
namespace KeyboardAudioVisualizer.Helper
{
public static class ExceptionExtension
{
#region Methods
public static string GetFullMessage(this Exception ex, string message = "")
{
if (ex == null) return string.Empty;
message += ex.Message;
if (ex.InnerException != null)
message += "\r\nInnerException: " + GetFullMessage(ex.InnerException);
return message;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/FrequencyHelper.cs
================================================
using System;
namespace KeyboardAudioVisualizer.Helper
{
public static class FrequencyHelper
{
#region Constants
private const int SAMPLE_RATE = 44100; // Right now this is fixed
#endregion
#region Methods
public static float GetFrequencyOfIndex(int index, int count) => index * ((float)SAMPLE_RATE / count);
public static int GetIndexOfFrequency(float frequency, int count) => (int)(frequency / ((float)SAMPLE_RATE / count));
public static double CalculatedBAForFrequency(float frequency)
{
double ra = (Math.Pow(12194, 2) * Math.Pow(frequency, 4)) / ((Math.Pow(frequency, 2) + Math.Pow(20.6, 2)) * Math.Sqrt((Math.Pow(frequency, 2) + Math.Pow(107.7, 2)) * (Math.Pow(frequency, 2) + Math.Pow(737.9, 2))) * (Math.Pow(frequency, 2) + Math.Pow(12194, 2)));
return (20 * Math.Log10(ra)) + 2;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/MathHelper.cs
================================================
using System;
namespace KeyboardAudioVisualizer.Helper
{
public static class MathHelper
{
#region Methods
public static double Clamp(double value, double min, double max) => Math.Max(min, Math.Min(max, value));
public static float Clamp(float value, float min, float max) => (float)Clamp((double)value, min, max);
public static int Clamp(int value, int min, int max) => Math.Max(min, Math.Min(max, value));
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/ObservableDictionary.cs
================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using RGB.NET.Core;
// Taken from https://codereview.stackexchange.com/questions/116562/custom-implementation-of-observabledictionary
namespace KeyboardAudioVisualizer.Helper
{
public class ObservableDictionary : AbstractBindable, IDictionary, INotifyCollectionChanged
{
#region Constants
private const string INDEXER_NAME = "Item[]";
#endregion
#region Properties & Fields
private readonly IList _values;
private readonly IDictionary _indexMap;
private readonly SimpleMonitor _monitor = new SimpleMonitor();
#endregion
#region Constructor
public ObservableDictionary()
{
_values = new List();
_indexMap = new Dictionary();
}
public ObservableDictionary(IDictionary dictionary)
{
_values = new List();
_indexMap = new Dictionary();
int idx = 0;
foreach (KeyValuePair kvp in dictionary)
{
_indexMap.Add(kvp.Key, idx);
_values.Add(kvp.Value);
idx++;
}
}
public ObservableDictionary(int capacity)
{
_values = new List(capacity);
_indexMap = new Dictionary(capacity);
}
#endregion
#region Virtual Add/Remove/Change Control Methods
protected virtual void AddItem(TKey key, TValue value)
{
CheckReentrancy();
int index = _values.Count;
_indexMap.Add(key, index);
_values.Add(value);
OnPropertyChanged(nameof(Count));
OnPropertyChanged(nameof(Keys));
OnPropertyChanged(nameof(Values));
OnPropertyChanged(INDEXER_NAME);
OnCollectionChanged(NotifyCollectionChangedAction.Add, key, value, index);
}
protected virtual bool RemoveItem(TKey key)
{
CheckReentrancy();
int index = _indexMap[key];
TValue value = _values[index];
if (_indexMap.Remove(key))
{
_values.RemoveAt(index);
List keys = _indexMap.Keys.ToList();
foreach (TKey existingKey in keys)
{
if (_indexMap[existingKey] > index)
_indexMap[existingKey]--;
}
OnPropertyChanged(nameof(Count));
OnPropertyChanged(nameof(Keys));
OnPropertyChanged(nameof(Values));
OnPropertyChanged(INDEXER_NAME);
OnCollectionChanged(NotifyCollectionChangedAction.Remove, key, value, index);
return true;
}
return false;
}
protected virtual bool RemoveItem(KeyValuePair item)
{
CheckReentrancy();
if (_indexMap.ContainsKey(item.Key) && _values[_indexMap[item.Key]].Equals(item.Value))
{
int index = _indexMap[item.Key];
TValue value = _values[index];
_indexMap.Remove(item.Key);
_values.RemoveAt(index);
List keys = _indexMap.Keys.ToList();
foreach (TKey existingKey in keys)
{
if (_indexMap[existingKey] > index)
_indexMap[existingKey]--;
}
OnPropertyChanged(nameof(Count));
OnPropertyChanged(nameof(Keys));
OnPropertyChanged(nameof(Values));
OnPropertyChanged(INDEXER_NAME);
OnCollectionChanged(NotifyCollectionChangedAction.Remove, item.Key, item.Value, index);
return true;
}
return false;
}
protected virtual void RemoveAllItems()
{
CheckReentrancy();
_values.Clear();
_indexMap.Clear();
OnPropertyChanged(nameof(Count));
OnPropertyChanged(nameof(Keys));
OnPropertyChanged(nameof(Values));
OnPropertyChanged(INDEXER_NAME);
OnCollectionChanged(NotifyCollectionChangedAction.Reset);
}
protected virtual void ChangeItem(TKey key, TValue newValue)
{
CheckReentrancy();
if (!_indexMap.ContainsKey(key))
AddItem(key, newValue);
else
{
int index = _indexMap[key];
TValue oldValue = _values[index];
_values[index] = newValue;
OnPropertyChanged(nameof(Values));
OnPropertyChanged(INDEXER_NAME);
OnCollectionChanged(NotifyCollectionChangedAction.Replace, key, oldValue, newValue, index);
}
}
protected IDisposable BlockReentrancy()
{
_monitor.Enter();
return (IDisposable)_monitor;
}
protected void CheckReentrancy()
{
// ISSUE: reference to a compiler-generated field
// ISSUE: reference to a compiler-generated field
if (_monitor.Busy && CollectionChanged != null && CollectionChanged.GetInvocationList().Length > 1)
throw new InvalidOperationException("ObservableCollectionReentrancyNotAllowed");
}
#endregion
#region IDictionary Members
public void Add(TKey key, TValue value) => AddItem(key, value);
public bool ContainsKey(TKey key) => _indexMap.ContainsKey(key);
public bool Remove(TKey key) => RemoveItem(key);
public bool TryGetValue(TKey key, out TValue value)
{
if (_indexMap.TryGetValue(key, out int index))
{
value = _values[index];
return true;
}
else
{
value = default;
return false;
}
}
public ICollection Keys => _indexMap.Keys;
public ICollection Values => _values;
public TValue this[TKey key]
{
get
{
int index = _indexMap[key];
return _values[index];
}
set => ChangeItem(key, value);
}
#endregion
#region ICollection> Members
public void Clear() => RemoveAllItems();
public int Count => _indexMap.Count;
void ICollection>.Add(KeyValuePair item) => Add(item.Key, item.Value);
bool ICollection>.Contains(KeyValuePair item) => _indexMap.ContainsKey(item.Key) && _values[_indexMap[item.Key]].Equals(item.Value);
void ICollection>.CopyTo(KeyValuePair[] array, int arrayIndex)
{
foreach (KeyValuePair kvp in _indexMap)
{
array[arrayIndex] = new KeyValuePair(kvp.Key, _values[kvp.Value]);
arrayIndex++;
}
}
bool ICollection>.IsReadOnly => false;
bool ICollection>.Remove(KeyValuePair item) => RemoveItem(item);
#endregion
#region IEnumerable> Members
public IEnumerator> GetEnumerator()
{
foreach (KeyValuePair kvp in _indexMap)
{
KeyValuePair pair = new KeyValuePair(kvp.Key, _values[kvp.Value]);
yield return pair;
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
#region INotifyCollectionChanged Members
public event NotifyCollectionChangedEventHandler CollectionChanged;
protected void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
NotifyCollectionChangedEventHandler handler = CollectionChanged;
using (BlockReentrancy())
handler?.Invoke(this, e);
}
protected void OnCollectionChanged(NotifyCollectionChangedAction action) => OnCollectionChanged(new NotifyCollectionChangedEventArgs(action));
protected void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue value, int index) => OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, new KeyValuePair(key, value), index));
protected void OnCollectionChanged(NotifyCollectionChangedAction action, TKey key, TValue oldValue, TValue newValue, int index)
{
KeyValuePair newPair = new KeyValuePair(key, newValue);
KeyValuePair oldPair = new KeyValuePair(key, oldValue);
OnCollectionChanged(new NotifyCollectionChangedEventArgs(action, newPair, oldPair, index));
}
#endregion
private class SimpleMonitor : IDisposable
{
private int _busyCount;
public bool Busy => _busyCount > 0;
public void Enter() => _busyCount = _busyCount + 1;
public void Dispose() => _busyCount = _busyCount - 1;
}
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/RingBuffer.cs
================================================
using System.Linq;
namespace KeyboardAudioVisualizer.Helper
{
public class RingBuffer
{
#region Properties & Fields
private readonly int _capacity;
private readonly float[] _buffer;
private int _currentIndex;
public int Size => _capacity;
public float Average => _buffer.Average();
public float Min => _buffer.Min();
public float Max => _buffer.Max();
public float Sum => _buffer.Sum();
#endregion
#region Constructors
public RingBuffer(int capacity)
{
this._capacity = capacity;
_buffer = new float[capacity];
}
#endregion
#region Methods
public void Put(float value) => Put(new[] { value }, 0, 1);
public void Put(float[] src, int offset, int count)
{
lock (_buffer)
{
if (count > _capacity)
{
offset += count - _capacity;
count = _capacity;
}
for (int i = 0; i < count; i++)
{
_currentIndex++;
if (_currentIndex >= _capacity) _currentIndex = 0;
_buffer[_currentIndex] = src[offset + i];
}
}
}
public void CopyInto(ref float[] data, int offset) => CopyInto(ref data, offset, _capacity);
public void CopyInto(ref float[] data, int offset, int count)
{
lock (_buffer)
for (int i = _capacity - count; i < count; i++)
data[offset + i] = _buffer[(_currentIndex + i) % _capacity];
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/VisualizationIndex.cs
================================================
namespace KeyboardAudioVisualizer.Helper
{
public enum VisualizationIndex
{
Primary,
Secondary,
Tertiary
}
}
================================================
FILE: KeyboardAudioVisualizer/Helper/WPFHelper.cs
================================================
using System.Windows;
using System.Windows.Media;
namespace KeyboardAudioVisualizer.Helper
{
public static class WPFHelper
{
#region Methods
public static T GetVisualChild(this DependencyObject parent)
where T : Visual
{
T child = default(T);
int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
for (int i = 0; i < numVisuals; i++)
{
Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
child = v as T;
if (child == null)
child = GetVisualChild(v);
if (child != null)
break;
}
return child;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/KeyboardAudioVisualizer.csproj
================================================
Debug
AnyCPU
{0AC4E8B1-4D4D-447F-B9FD-38A74ED1F243}
WinExe
KeyboardAudioVisualizer
KeyboardAudioVisualizer
v4.6.1
512
{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}
4
true
x86
true
full
false
bin\Debug\
DEBUG;TRACE
prompt
4
latest
x86
pdbonly
true
bin\Release\
TRACE
prompt
4
latest
Resources\Icon.ico
..\packages\CSCore.1.2.1.2\lib\net35-client\CSCore.dll
..\packages\Hardcodet.NotifyIcon.Wpf.1.0.8\lib\net451\Hardcodet.Wpf.TaskbarNotification.dll
..\packages\HidSharp.2.0.5\lib\net35\HidSharp.dll
..\packages\RGB.NET.Devices.Asus.0.1.31\lib\net45\Interop.AuraServiceLib.dll
True
..\packages\MathNet.Numerics.4.7.0\lib\net461\MathNet.Numerics.dll
..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll
..\packages\RGB.NET.Brushes.0.1.31\lib\net45\RGB.NET.Brushes.dll
..\packages\RGB.NET.Core.0.1.31\lib\net45\RGB.NET.Core.dll
..\packages\RGB.NET.Decorators.0.1.31\lib\net45\RGB.NET.Decorators.dll
..\packages\RGB.NET.Devices.Asus.0.1.31\lib\net45\RGB.NET.Devices.Asus.dll
..\packages\RGB.NET.Devices.CoolerMaster.0.1.31\lib\net45\RGB.NET.Devices.CoolerMaster.dll
..\packages\RGB.NET.Devices.Corsair.0.1.31\lib\net45\RGB.NET.Devices.Corsair.dll
..\packages\RGB.NET.Devices.Logitech.0.1.31\lib\net45\RGB.NET.Devices.Logitech.dll
..\packages\RGB.NET.Devices.Novation.0.1.31\lib\net45\RGB.NET.Devices.Novation.dll
..\packages\RGB.NET.Devices.Razer.0.1.31\lib\net45\RGB.NET.Devices.Razer.dll
..\packages\RGB.NET.Devices.SteelSeries.0.1.31\lib\netstandard2.0\RGB.NET.Devices.SteelSeries.dll
..\packages\RGB.NET.Groups.0.1.31\lib\net45\RGB.NET.Groups.dll
..\packages\Sanford.Multimedia.Midi.6.6.0\lib\net20\Sanford.Multimedia.Midi.dll
..\packages\System.ValueTuple.4.5.0\lib\net461\System.ValueTuple.dll
4.0
..\libs\Transitionals.dll
MSBuild:Compile
Designer
App.xaml
Code
ConfigurationWindow.xaml
Code
True
True
Resources.resx
True
Settings.settings
True
ResXFileCodeGenerator
Resources.Designer.cs
Designer
Designer
SettingsSingleFileGenerator
Settings.Designer.cs
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
Designer
MSBuild:Compile
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
MSBuild:Compile
Designer
This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
================================================
FILE: KeyboardAudioVisualizer/KeyboardAudioVisualizer.csproj.DotSettings
================================================
CSharp71
================================================
FILE: KeyboardAudioVisualizer/Legacy/ConfigurationMigrator.cs
================================================
using System.IO;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.Legacy
{
public static class ConfigurationMigrator
{
#region Constants
private const string PATH_V1_SETTINGS = "Settings.xml";
#endregion
#region Constructors
#endregion
#region Methods
public static Configuration.Settings MigrateOldConfig()
{
if (!File.Exists(PATH_V1_SETTINGS)) return null;
try
{
Settings oldSettings = SerializationHelper.LoadObjectFromFile(PATH_V1_SETTINGS);
Configuration.Settings settings = new Configuration.Settings { UpdateRate = oldSettings.UpdateRate };
settings[VisualizationIndex.Primary].SelectedVisualization = VisualizationType.FrequencyBars;
settings[VisualizationIndex.Primary].FrequencyBarsConfiguration = oldSettings.FrequencyBarsVisualizationProviderConfiguration;
settings[VisualizationIndex.Primary].EqualizerConfiguration = oldSettings.EqualizerConfiguration;
settings[VisualizationIndex.Secondary].SelectedVisualization = VisualizationType.Beat;
settings[VisualizationIndex.Secondary].BeatConfiguration = oldSettings.BeatVisualizationProviderConfiguration;
settings[VisualizationIndex.Tertiary].SelectedVisualization = VisualizationType.Level;
settings[VisualizationIndex.Tertiary].LevelConfiguration = oldSettings.LevelVisualizationProviderConfiguration;
return settings;
}
catch
{
return null;
}
}
public static void CleanupOldConfigs()
{
if (File.Exists(PATH_V1_SETTINGS))
File.Delete(PATH_V1_SETTINGS);
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Legacy/ConfigurationUpdates.cs
================================================
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.Legacy
{
public static class ConfigurationUpdates
{
#region Methods
public static void PerformOn(Configuration.Settings settings)
{
if (settings.Version < 1)
UpdateTo1(settings);
}
private static void UpdateTo1(Configuration.Settings settings)
{
settings.Visualizations[VisualizationIndex.Primary].Gradient = new LinearGradient(new GradientStop(0, HSVColor.Create(300, 1, 1)),
new GradientStop(0.20, HSVColor.Create(225, 1, 1)),
new GradientStop(0.35, HSVColor.Create(180, 1, 1)),
new GradientStop(0.50, HSVColor.Create(135, 1, 1)),
new GradientStop(0.65, HSVColor.Create(90, 1, 1)),
new GradientStop(0.80, HSVColor.Create(45, 1, 1)),
new GradientStop(0.95, HSVColor.Create(0, 1, 1)));
settings.Visualizations[VisualizationIndex.Secondary].Gradient = new LinearGradient(new GradientStop(0.5, new Color(255, 255, 255)));
settings.Visualizations[VisualizationIndex.Tertiary].Gradient = new LinearGradient(new GradientStop(0, new Color(0, 0, 255)),
new GradientStop(1, new Color(255, 0, 0)));
settings.Version = 1;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Legacy/SerializationHelper.cs
================================================
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace KeyboardAudioVisualizer.Legacy
{
public static class SerializationHelper
{
#region Methods
public static void SaveObjectToFile(T serializableObject, string path)
{
if (serializableObject == null) return;
try
{
XmlDocument xmlDocument = new XmlDocument();
XmlSerializer serializer = new XmlSerializer(serializableObject.GetType());
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(stream, serializableObject);
stream.Seek(0, SeekOrigin.Begin);
xmlDocument.Load(stream);
xmlDocument.Save(path);
}
}
catch {/* Catch'em all */}
}
public static T LoadObjectFromFile(string fileName)
{
if (string.IsNullOrEmpty(fileName)) return default(T);
try
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(fileName);
string xmlString = xmlDocument.OuterXml;
using (StringReader sr = new StringReader(xmlString))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (XmlReader reader = new XmlTextReader(sr))
return (T)serializer.Deserialize(reader);
}
}
catch {/* Catch'em all */}
return default(T);
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Legacy/Settings.cs
================================================
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Configuration;
namespace KeyboardAudioVisualizer.Legacy
{
public class Settings
{
#region General
public double UpdateRate { get; set; } = 40.0;
#endregion
#region AudioProcessing
public EqualizerConfiguration EqualizerConfiguration { get; set; } = new EqualizerConfiguration();
public FrequencyBarsVisualizationProviderConfiguration FrequencyBarsVisualizationProviderConfiguration { get; set; } = new FrequencyBarsVisualizationProviderConfiguration();
public LevelVisualizationProviderConfiguration LevelVisualizationProviderConfiguration { get; set; } = new LevelVisualizationProviderConfiguration();
public BeatVisualizationProviderConfiguration BeatVisualizationProviderConfiguration { get; set; } = new BeatVisualizationProviderConfiguration();
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Properties/AssemblyInfo.cs
================================================
using System.Reflection;
using System.Runtime.InteropServices;
using System.Windows;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("KeyboardAudioVisualizer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("KeyboardAudioVisualizer")]
[assembly: AssemblyCopyright("Copyright © Wyrez 2017")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
//In order to begin building localizable applications, set
//CultureYouAreCodingWith in your .csproj file
//inside a . For example, if you are using US english
//in your source files, set the to en-US. Then uncomment
//the NeutralResourceLanguage attribute below. Update the "en-US" in
//the line below to match the UICulture setting in the project file.
//[assembly: NeutralResourcesLanguage("en-US", UltimateResourceFallbackLocation.Satellite)]
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.3.1.1")]
[assembly: AssemblyFileVersion("1.3.1.1")]
================================================
FILE: KeyboardAudioVisualizer/Properties/Resources.Designer.cs
================================================
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
namespace KeyboardAudioVisualizer.Properties
{
///
/// A strongly-typed resource class, for looking up localized strings, etc.
///
// This class was auto-generated by the StronglyTypedResourceBuilder
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "4.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
internal class Resources
{
private static global::System.Resources.ResourceManager resourceMan;
private static global::System.Globalization.CultureInfo resourceCulture;
[global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal Resources()
{
}
///
/// Returns the cached ResourceManager instance used by this class.
///
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager
{
get
{
if ((resourceMan == null))
{
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("KeyboardAudioVisualizer.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
///
/// Overrides the current thread's CurrentUICulture property for all
/// resource lookups using this strongly typed resource class.
///
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Globalization.CultureInfo Culture
{
get
{
return resourceCulture;
}
set
{
resourceCulture = value;
}
}
}
}
================================================
FILE: KeyboardAudioVisualizer/Properties/Resources.resx
================================================
text/microsoft-resx
2.0
System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
================================================
FILE: KeyboardAudioVisualizer/Properties/Settings.Designer.cs
================================================
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
//
//------------------------------------------------------------------------------
namespace KeyboardAudioVisualizer.Properties
{
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "11.0.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase
{
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));
public static Settings Default
{
get
{
return defaultInstance;
}
}
}
}
================================================
FILE: KeyboardAudioVisualizer/Properties/Settings.settings
================================================
================================================
FILE: KeyboardAudioVisualizer/Resources/KeyboardAudioVisualizer.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/BlurredDecorationWindow.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/Button.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/CachedResourceDictionary.cs
================================================
using System;
using System.Collections;
using System.Collections.Generic;
using System.Windows;
namespace KeyboardAudioVisualizer.Styles
{
public class CachedResourceDictionary : ResourceDictionary
{
#region Properties & Fields
// ReSharper disable InconsistentNaming
private static readonly List _cachedDictionaries = new List();
private static readonly ResourceDictionary _innerDictionary = new ResourceDictionary();
// ReSharper restore
public new Uri Source
{
get => null;
set
{
lock (_innerDictionary)
{
UpdateCache(value);
MergedDictionaries.Clear();
MergedDictionaries.Add(_innerDictionary);
}
}
}
#endregion
#region Methods
private static void UpdateCache(Uri source)
{
string uriPath = source.OriginalString;
if (_cachedDictionaries.Contains(uriPath)) return;
_cachedDictionaries.Add(uriPath);
ResourceDictionary newDictionary = new ResourceDictionary { Source = new Uri(uriPath, source.IsAbsoluteUri ? UriKind.Absolute : UriKind.Relative) };
CopyDictionaryEntries(newDictionary, _innerDictionary);
}
private static void CopyDictionaryEntries(IDictionary source, IDictionary target)
{
foreach (object key in source.Keys)
if (!target.Contains(key))
target.Add(key, source[key]);
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/Styles/ColorSelector.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/ComboBox.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/Form.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/FrameworkElement.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/GradientEditor.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/GroupBox.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/ImageButton.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/Navigation.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/Slider.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/Styles/Theme.xaml
================================================
#FFDCDCDC
#FF2A2A2A
#B82A2A2A
#111111
#B8111111
#60111111
#50000000
#FFE135
40
14
14
22
================================================
FILE: KeyboardAudioVisualizer/Styles/ToolTip.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/Configuration/BeatConfiguration.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/Configuration/FrequencyBarsConfiguration.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/Configuration/LevelConfiguration.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/ConfigurationViewModel.cs
================================================
using System;
using System.Diagnostics;
using System.Reflection;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.UI
{
public class ConfigurationViewModel : AbstractBindable
{
#region Properties & Fields
public Version Version => Assembly.GetEntryAssembly().GetName().Version;
public double UpdateRate
{
get => 1.0 / ApplicationManager.Instance.UpdateTrigger.UpdateFrequency;
set
{
double val = MathHelper.Clamp(value, 1, 60);
ApplicationManager.Instance.Settings.UpdateRate = val;
ApplicationManager.Instance.UpdateTrigger.UpdateFrequency = 1.0 / val;
OnPropertyChanged();
}
}
public bool EnableAudioPrescale
{
get => ApplicationManager.Instance.Settings.EnableAudioPrescale;
set
{
ApplicationManager.Instance.Settings.EnableAudioPrescale = value;
OnPropertyChanged();
}
}
public VisualizationType SelectedPrimaryVisualization
{
get => ApplicationManager.Instance.Settings[VisualizationIndex.Primary].SelectedVisualization;
set
{
ApplicationManager.Instance.Settings[VisualizationIndex.Primary].SelectedVisualization = value;
ApplicationManager.Instance.ApplyVisualization(VisualizationIndex.Primary, value);
}
}
public VisualizationType SelectedSecondaryVisualization
{
get => ApplicationManager.Instance.Settings[VisualizationIndex.Secondary].SelectedVisualization;
set
{
ApplicationManager.Instance.Settings[VisualizationIndex.Secondary].SelectedVisualization = value;
ApplicationManager.Instance.ApplyVisualization(VisualizationIndex.Secondary, value);
}
}
public VisualizationType SelectedTertiaryVisualization
{
get => ApplicationManager.Instance.Settings[VisualizationIndex.Tertiary].SelectedVisualization;
set
{
ApplicationManager.Instance.Settings[VisualizationIndex.Tertiary].SelectedVisualization = value;
ApplicationManager.Instance.ApplyVisualization(VisualizationIndex.Tertiary, value);
}
}
#endregion
#region Commands
private ActionCommand _openHomepageCommand;
public ActionCommand OpenHomepageCommand => _openHomepageCommand ?? (_openHomepageCommand = new ActionCommand(OpenHomepage));
#endregion
#region Constructors
#endregion
#region Methods
private void OpenHomepage() => Process.Start("https://github.com/DarthAffe/KeyboardAudioVisualizer");
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/UI/ConfigurationWindow.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/ConfigurationWindow.xaml.cs
================================================
using System;
using KeyboardAudioVisualizer.Controls;
namespace KeyboardAudioVisualizer.UI
{
public partial class ConfigurationWindow : BlurredDecorationWindow
{
public ConfigurationWindow() => InitializeComponent();
//DarthAffe 07.02.2018: This prevents the applicaiton from not shutting down and crashing afterwards if 'close' is selected in the taskbar-context-menu
private void ConfigurationWindow_OnClosed(object sender, EventArgs e)
{
ApplicationManager.Instance.ExitCommand.Execute(null);
}
}
}
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/BeatVisualization.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/BeatVisualizer.cs
================================================
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
namespace KeyboardAudioVisualizer.UI.Visualization
{
public class BeatVisualizer : Control
{
#region Properties & Fields
private LinearGradient _gradient;
#endregion
#region DependencyProperties
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty VisualizationProviderProperty = DependencyProperty.Register(
"VisualizationProvider", typeof(IVisualizationProvider), typeof(BeatVisualizer), new PropertyMetadata(default(IVisualizationProvider)));
public IVisualizationProvider VisualizationProvider
{
get => (IVisualizationProvider)GetValue(VisualizationProviderProperty);
set => SetValue(VisualizationProviderProperty, value);
}
public static readonly DependencyProperty VisualizationIndexProperty = DependencyProperty.Register(
"VisualizationIndex", typeof(VisualizationIndex?), typeof(BeatVisualizer), new PropertyMetadata(null, VisualizationIndexChanged));
public VisualizationIndex? VisualizationIndex
{
get => (VisualizationIndex?)GetValue(VisualizationIndexProperty);
set => SetValue(VisualizationIndexProperty, value);
}
public static readonly DependencyProperty BrushProperty = DependencyProperty.Register(
"Brush", typeof(Brush), typeof(BeatVisualizer), new PropertyMetadata(default(Brush)));
public Brush Brush
{
get => (Brush)GetValue(BrushProperty);
set => SetValue(BrushProperty, value);
}
public static readonly DependencyProperty BeatValueProperty = DependencyProperty.Register(
"BeatValue", typeof(float), typeof(BeatVisualizer), new PropertyMetadata(default(float)));
public float BeatValue
{
get => (float)GetValue(BeatValueProperty);
set => SetValue(BeatValueProperty, value);
}
// ReSharper restore InconsistentNaming
#endregion
#region Constructors
public BeatVisualizer()
{
RGBSurface.Instance.Updated += args => Dispatcher.BeginInvoke(new Action(Update), DispatcherPriority.Normal);
//TODO DarthAffe 12.08.2017: Create brush from config
}
#endregion
#region Methods
private void Update()
{
IVisualizationProvider visualizationProvider = VisualizationProvider;
if ((visualizationProvider == null) || (Visibility != Visibility.Visible)) return;
if (visualizationProvider.VisualizationData[0] > 0.9)
BeatValue = 1f;
else if (BeatValue > 0.01f)
{
float newValue = BeatValue * 0.625f;
if (newValue > 0.01f)
BeatValue = newValue;
else BeatValue = 0;
}
}
private static void VisualizationIndexChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is BeatVisualizer visualizer)) return;
visualizer.UpdateGradient();
}
private void UpdateGradient()
{
void GradientChanged(object sender, EventArgs args) => UpdateColor();
if (_gradient != null)
_gradient.GradientChanged -= GradientChanged;
_gradient = VisualizationIndex.HasValue ? ApplicationManager.Instance.Settings[VisualizationIndex.Value].Gradient : null;
if (_gradient != null)
_gradient.GradientChanged += GradientChanged;
UpdateColor();
}
private void UpdateColor()
{
if (_gradient == null) return;
GradientStopCollection gradientStops = new GradientStopCollection();
foreach (RGB.NET.Brushes.Gradients.GradientStop stop in _gradient.GradientStops)
gradientStops.Add(new System.Windows.Media.GradientStop(System.Windows.Media.Color.FromArgb(stop.Color.GetA(), stop.Color.GetR(), stop.Color.GetG(), stop.Color.GetB()), stop.Offset));
Brush = new LinearGradientBrush(gradientStops, new System.Windows.Point(0, 0.5), new System.Windows.Point(1, 0.5));
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/EqualizerVisualization.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/EqualizerVisualizer.cs
================================================
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using KeyboardAudioVisualizer.AudioProcessing.Equalizer;
using KeyboardAudioVisualizer.Helper;
namespace KeyboardAudioVisualizer.UI.Visualization
{
[TemplatePart(Name = "PART_Grips", Type = typeof(ItemsControl))]
public class EqualizerVisualizer : Control
{
#region DependencyProperties
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty EqualizerProperty = DependencyProperty.Register(
"Equalizer", typeof(IEqualizer), typeof(EqualizerVisualizer), new PropertyMetadata(default(IEqualizer)));
public IEqualizer Equalizer
{
get => (IEqualizer)GetValue(EqualizerProperty);
set => SetValue(EqualizerProperty, value);
}
public static readonly DependencyProperty ResetCommandProperty = DependencyProperty.Register(
"ResetCommand", typeof(ICommand), typeof(EqualizerVisualizer), new PropertyMetadata(null));
public ICommand ResetCommand
{
get => (ICommand)GetValue(ResetCommandProperty);
set => SetValue(ResetCommandProperty, value);
}
// ReSharper restore InconsistentNaming
#endregion
#region Properties & Fields
private ItemsControl _grips;
private EqualizerBand _draggingBand;
#endregion
#region Constructors
public EqualizerVisualizer()
{
ResetCommand = new ActionCommand(() => Equalizer?.Reset());
}
#endregion
#region Methods
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_grips = GetTemplateChild("PART_Grips") as ItemsControl;
}
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
EqualizerBand band = GetClickedBand();
if (band != null)
_draggingBand = band;
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
{
base.OnMouseLeftButtonUp(e);
_draggingBand = null;
}
protected override void OnMouseLeave(MouseEventArgs e)
{
base.OnMouseLeave(e);
_draggingBand = null;
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove(e);
if (_draggingBand == null) return;
UpdateBand(_draggingBand, e.GetPosition(_grips));
}
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
{
base.OnMouseRightButtonDown(e);
if (_grips == null) return;
EqualizerBand band = GetClickedBand();
if (band == null)
{
EqualizerBand newBand = Equalizer.AddBand(0, 0);
UpdateBand(newBand, e.GetPosition(_grips));
}
else
Equalizer.RemoveBandBand(band);
}
private void UpdateBand(EqualizerBand band, Point position)
{
double halfHeight = _grips.ActualHeight / 2.0;
band.Offset = (float)(position.X / _grips.ActualWidth);
band.Value = (float)(-(position.Y - halfHeight) / halfHeight);
}
private EqualizerBand GetClickedBand()
{
ItemsPresenter itemsPresenter = _grips.GetVisualChild();
if (itemsPresenter == null) return null;
Panel panel = VisualTreeHelper.GetChild(itemsPresenter, 0) as Panel;
if (panel == null) return null;
foreach (UIElement element in panel.Children)
if (element.IsMouseOver)
{
EqualizerBand band = ((element as ContentPresenter)?.Content as EqualizerBand);
if (band != null) return band;
}
return null;
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/FrequencyBarsVisualization.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/FrequencyBarsVisualizer.cs
================================================
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
using Color = System.Windows.Media.Color;
using Rectangle = System.Windows.Shapes.Rectangle;
namespace KeyboardAudioVisualizer.UI.Visualization
{
[TemplatePart(Name = "PART_BarsPanel", Type = typeof(Panel))]
public class FrequencyBarsVisualizer : Control
{
#region DependencyProperties
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty VisualizationProviderProperty = DependencyProperty.Register(
"VisualizationProvider", typeof(IVisualizationProvider), typeof(FrequencyBarsVisualizer),
new PropertyMetadata(default(IVisualizationProvider), VisualizationProviderChanged));
public IVisualizationProvider VisualizationProvider
{
get => (IVisualizationProvider)GetValue(VisualizationProviderProperty);
set => SetValue(VisualizationProviderProperty, value);
}
public static readonly DependencyProperty VisualizationIndexProperty = DependencyProperty.Register(
"VisualizationIndex", typeof(VisualizationIndex?), typeof(FrequencyBarsVisualizer), new PropertyMetadata(null, VisualizationIndexChanged));
public VisualizationIndex? VisualizationIndex
{
get => (VisualizationIndex?)GetValue(VisualizationIndexProperty);
set => SetValue(VisualizationIndexProperty, value);
}
// ReSharper restore InconsistentNaming
#endregion
#region Properties & Fields
private LinearGradient _gradient;
private Panel _panel;
private Rectangle[] _bars = new Rectangle[0];
#endregion
#region Constructors
public FrequencyBarsVisualizer()
{
RGBSurface.Instance.Updated += args => Dispatcher.BeginInvoke(new Action(Update), DispatcherPriority.Normal);
SizeChanged += (sender, args) => UpdateSizes();
}
#endregion
#region Methods
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
_panel = GetTemplateChild("PART_BarsPanel") as Panel;
InitializeBars();
}
private static void VisualizationProviderChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is FrequencyBarsVisualizer visualizer)) return;
void ConfigurationOnPropertyChanged(object sender, PropertyChangedEventArgs args) => visualizer.ConfigurationChanged(args.PropertyName);
if (dependencyPropertyChangedEventArgs.OldValue is IVisualizationProvider oldVisualizationProvider)
oldVisualizationProvider.Configuration.PropertyChanged -= ConfigurationOnPropertyChanged;
if (dependencyPropertyChangedEventArgs.NewValue is IVisualizationProvider newVisualizationProvider)
newVisualizationProvider.Configuration.PropertyChanged += ConfigurationOnPropertyChanged;
}
private static void VisualizationIndexChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is FrequencyBarsVisualizer visualizer)) return;
visualizer.UpdateGradient();
}
private void UpdateGradient()
{
void GradientChanged(object sender, EventArgs args) => UpdateColors();
if (_gradient != null)
_gradient.GradientChanged -= GradientChanged;
_gradient = VisualizationIndex.HasValue ? ApplicationManager.Instance.Settings[VisualizationIndex.Value].Gradient : null;
if (_gradient != null)
_gradient.GradientChanged += GradientChanged;
UpdateColors();
}
private void ConfigurationChanged(string changedPropertyName)
{
if ((changedPropertyName == null) || (changedPropertyName == nameof(FrequencyBarsVisualizationProviderConfiguration.Bars)))
InitializeBars();
}
private void InitializeBars()
{
if (_panel == null) return;
_panel.Children.Clear();
if (VisualizationProvider == null) return;
_bars = new Rectangle[((FrequencyBarsVisualizationProviderConfiguration)VisualizationProvider.Configuration).Bars];
for (int i = 0; i < _bars.Length; i++)
{
_bars[i] = new Rectangle { VerticalAlignment = VerticalAlignment.Bottom, Width = 0 };
_panel.Children.Add(_bars[i]);
}
UpdateSizes();
UpdateColors();
}
private void UpdateSizes()
{
if (_bars.Length == 0) return;
double barSpacing = ActualWidth / _bars.Length;
double barWidth = (barSpacing * 3.0) / 4.0;
double margin = barSpacing - barWidth;
for (int i = 0; i < _bars.Length; i++)
{
_bars[i].Width = barWidth;
_bars[i].Margin = new Thickness(margin / 2, 0, margin / 2, 0);
}
}
private void UpdateColors()
{
if (_gradient == null) return;
for (int i = 0; i < _bars.Length; i++)
{
RGB.NET.Core.Color color = _gradient.GetColor((double)i / _bars.Length);
_bars[i].Fill = new SolidColorBrush(Color.FromRgb(color.GetR(), color.GetG(), color.GetB()));
}
}
private void Update()
{
IVisualizationProvider visualizationProvider = VisualizationProvider;
if ((visualizationProvider == null) || (Visibility != Visibility.Visible)) return;
int count = Math.Min(_bars.Length, visualizationProvider.VisualizationData.Length);
for (int i = 0; i < count; i++)
{
_bars[i].Height = (float)((_bars[i].Height * 0.5) + ((ActualHeight * visualizationProvider.VisualizationData[i]) * (1.0 - 0.5)));
if (double.IsNaN(_bars[i].Height)) _bars[i].Height = 0;
}
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/LevelVisualization.xaml
================================================
================================================
FILE: KeyboardAudioVisualizer/UI/Visualization/LevelVisualizer.cs
================================================
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using KeyboardAudioVisualizer.AudioProcessing.VisualizationProvider;
using KeyboardAudioVisualizer.Helper;
using RGB.NET.Brushes.Gradients;
using RGB.NET.Core;
using Color = System.Windows.Media.Color;
using GradientStop = RGB.NET.Brushes.Gradients.GradientStop;
using Point = System.Windows.Point;
namespace KeyboardAudioVisualizer.UI.Visualization
{
public class LevelVisualizer : Control
{
#region Properties & Fields
private LinearGradient _gradient;
#endregion
#region DependencyProperties
// ReSharper disable InconsistentNaming
public static readonly DependencyProperty VisualizationProviderProperty = DependencyProperty.Register(
"VisualizationProvider", typeof(IVisualizationProvider), typeof(LevelVisualizer), new PropertyMetadata(default(IVisualizationProvider)));
public IVisualizationProvider VisualizationProvider
{
get => (IVisualizationProvider)GetValue(VisualizationProviderProperty);
set => SetValue(VisualizationProviderProperty, value);
}
public static readonly DependencyProperty VisualizationIndexProperty = DependencyProperty.Register(
"VisualizationIndex", typeof(VisualizationIndex?), typeof(LevelVisualizer), new PropertyMetadata(null, VisualizationIndexChanged));
public VisualizationIndex? VisualizationIndex
{
get => (VisualizationIndex?)GetValue(VisualizationIndexProperty);
set => SetValue(VisualizationIndexProperty, value);
}
public static readonly DependencyProperty BrushLeftProperty = DependencyProperty.Register(
"BrushLeft", typeof(Brush), typeof(LevelVisualizer), new PropertyMetadata(default(Brush)));
public Brush BrushLeft
{
get => (Brush)GetValue(BrushLeftProperty);
set => SetValue(BrushLeftProperty, value);
}
public static readonly DependencyProperty BrushRightProperty = DependencyProperty.Register(
"BrushRight", typeof(Brush), typeof(LevelVisualizer), new PropertyMetadata(default(Brush)));
public Brush BrushRight
{
get => (Brush)GetValue(BrushRightProperty);
set => SetValue(BrushRightProperty, value);
}
public static readonly DependencyProperty SizeLeftProperty = DependencyProperty.Register(
"SizeLeft", typeof(int), typeof(LevelVisualizer), new PropertyMetadata(default(int)));
public int SizeLeft
{
get => (int)GetValue(SizeLeftProperty);
set => SetValue(SizeLeftProperty, value);
}
public static readonly DependencyProperty SizeRightProperty = DependencyProperty.Register(
"SizeRight", typeof(int), typeof(LevelVisualizer), new PropertyMetadata(default(int)));
public int SizeRight
{
get => (int)GetValue(SizeRightProperty);
set => SetValue(SizeRightProperty, value);
}
// ReSharper restore InconsistentNaming
#endregion
#region Constructors
public LevelVisualizer()
{
RGBSurface.Instance.Updated += args => Dispatcher.BeginInvoke(new Action(Update), DispatcherPriority.Normal);
}
#endregion
#region Methods
private void Update()
{
IVisualizationProvider visualizationProvider = VisualizationProvider;
if ((visualizationProvider == null) || (Visibility != Visibility.Visible)) return;
int horizontalSizeLeft = (int)(visualizationProvider.VisualizationData[0] * (ActualWidth / 2));
if (Math.Abs(SizeLeft - horizontalSizeLeft) > 1)
SizeLeft = horizontalSizeLeft;
int horizontalSizeRight = (int)(visualizationProvider.VisualizationData[1] * (ActualWidth / 2));
if (Math.Abs(SizeRight - horizontalSizeRight) > 1)
SizeRight = horizontalSizeRight;
}
private void SetBrushes()
{
if (_gradient == null) return;
GradientStopCollection gradientStops = new GradientStopCollection();
foreach (GradientStop stop in _gradient.GradientStops)
gradientStops.Add(new System.Windows.Media.GradientStop(Color.FromArgb(stop.Color.GetA(), stop.Color.GetR(), stop.Color.GetG(), stop.Color.GetB()), stop.Offset));
BrushLeft = new LinearGradientBrush(gradientStops, new Point(1, 0.5), new Point(0, 0.5));
BrushRight = new LinearGradientBrush(gradientStops, new Point(0, 0.5), new Point(1, 0.5));
}
private static void VisualizationIndexChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
{
if (!(dependencyObject is LevelVisualizer visualizer)) return;
visualizer.UpdateGradient();
}
private void UpdateGradient()
{
void GradientChanged(object sender, EventArgs args) => SetBrushes();
if (_gradient != null)
_gradient.GradientChanged -= GradientChanged;
_gradient = VisualizationIndex.HasValue ? ApplicationManager.Instance.Settings[VisualizationIndex.Value].Gradient : null;
if (_gradient != null)
_gradient.GradientChanged += GradientChanged;
SetBrushes();
}
#endregion
}
}
================================================
FILE: KeyboardAudioVisualizer/packages.config
================================================
================================================
FILE: KeyboardAudioVisualizer.sln
================================================
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.26430.15
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeyboardAudioVisualizer", "KeyboardAudioVisualizer\KeyboardAudioVisualizer.csproj", "{0AC4E8B1-4D4D-447F-B9FD-38A74ED1F243}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "libs", "libs", "{9A053D4E-5BC6-4179-8F82-9B5C8C7E4127}"
ProjectSection(SolutionItems) = preProject
libs\Transitionals.dll = libs\Transitionals.dll
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{0AC4E8B1-4D4D-447F-B9FD-38A74ED1F243}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AC4E8B1-4D4D-447F-B9FD-38A74ED1F243}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AC4E8B1-4D4D-447F-B9FD-38A74ED1F243}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AC4E8B1-4D4D-447F-B9FD-38A74ED1F243}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
================================================
FILE: KeyboardAudioVisualizer.sln.DotSettings
================================================
True
================================================
FILE: LICENSE
================================================
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Lesser General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
{description}
Copyright (C) {year} {fullname}
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) year name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
{signature of Ty Coon}, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Lesser General
Public License instead of this License.
================================================
FILE: NuGet.Config
================================================
================================================
FILE: README.md
================================================
This software is no longer actively developed.
Consider checking out [Artemis](https://github.com/Artemis-RGB/Artemis) for a even more feature rich replacement.
# KeyboardAudioVisualizer
It's colorful - I like it!
**Example-Video:**
[](https://www.youtube.com/watch?v=mby2NYN0V1o)
## Visualizations
#### Keyboard
- **"Frequency Bars"** - Simple spectrum visualizer.
- **"Level"** - Shows the overall volume.
- **"Beat detection"** - Pulses to the beat of the music. (This isn't working really well right now, depending on the music. But in general not satisfying, sorry.)
#### Mouse/Headset
- **"Beat detection"** - Pulses to the beat of the music. (This isn't working really well right now, depending on the music. But in general not satisfying, sorry.)
#### Mousepad/Lightbar (K95 Platinum)
- **"Level"** - Shows the overall volume.
- **"Beat detection"** - Pulses to the beat of the music. (This isn't working really well right now, depending on the music. But in general not satisfying, sorry.)
## Supported devices
- All Corsair RGB-devices.
- Logitech G910 and G610 with physical EU layout. (Untested but should work)
- (In theory every device with SDK support could be include, open an issue if you want to help with increasing the range of supported devices!)
## Settings
#### Frequency Bars
- **Spectrum:** The way the spectrum is grouped together. Values are: _(default: Logarithmic)_
- **_Linear_**: Each bar represents the same range of frequencies. Most of the time this doesn't look good since low frequencies are underrepresented.
- **_Logarithmic_**: The higher the frequencies get the wider the range of grouped frequencies. This is close to the way humans perceive sound and therfore most of the time looks quite good as long as the range of used frequencies is big enough.
- **_Gamma_**: While Gamma-correction is known from image-processing it still applies quite well to audio-data, grouping not as extreme as logarithmic but still better than linear. The used Gamma-value can be configured.
- **Value:** The way the value of a frequency bar is determined. Values are: _(default: Sum)_
- **_Sum_**: Sums the power of all frequencies grouped in the bar using all available data. Combining this with logarithmic grouping gives the most realistic representation.
- **_Max_**: Uses the maximum value in each group making sure peaks are caught well. This works quite good with gamma-grouping.
- **_Average_**: Uses the average of all frequencies grouped in the bar. This smooths out the whole graph quite a lot.
- **Bars:** The number of bars used to represent the spectrum. _(default: 48)_
- **Min Freq.:** The minimum frequency used in the graph. This value shouldn't be modified. _(default: 60)_
- **Max Freq.:** The maximum frequency used in the graph. This value can be lowered to increase the value of lower frequencies. Using high values might lead to death bars if the audio is mastered with an low-pass filter cutting high frequencies. _(default: 15000)_
- **Gamma:** The correction value used for gamma-grouping (disabled if any other grouping is selected). High values lead to a stronger compression of high frequencies. _(default: 2)_
- **Reference:** The reference value used to calculate the power of each bar. Adjust this to your audio volume. Low volume -> low reference, high volume -> higher reference. _(default: 90)_
- **Smoothing:** Smooths the graph to prevent flickering. Low values will cause a hectic fast plot, high values a slow one without peaks. _(default: 3)_
- **Emphasize:** Emphasizes peaks. The higher the value, the bigger the difference between a "loud-bar" and a "quiet-bar". _(default: 0.5)_
**Equalizer**
Allows to finetune the graph by slective increasing/decresing the value.
You can add new pivots by rightclicking on the visualization-window.
Existing pivots can be deleted by rightclicking on them or moved by leftclicking and dragging around.
#### Beat detection
_No configuration right now_
#### Level
- **Calculation:** Defines how the RMS of the audio is plotted. Values are _Linear_, _Logarithmic_ and _Exponential_. The used range of the plott increases in that order (exponential has the widest range of peaks). _(default: Logarithmic)_
- **Scale:** Scales the whole graph. Use this to to fit your audio volume. _(default: 3)_
- **Smoothing:** Smooths the plot to prevent flickering. Low values will cause a hectic fast plot, high values a slow one without peaks. _(default: 8)_