Skip to content

Commit

Permalink
Filter graph merging to convolutions
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed May 24, 2024
1 parent 696abc9 commit 9749eab
Show file tree
Hide file tree
Showing 23 changed files with 311 additions and 157 deletions.
49 changes: 49 additions & 0 deletions Cavern/Filters/Utilities/FilterGraphNodeUtils.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
using System.Collections.Generic;
using System.Linq;

namespace Cavern.Filters.Utilities {
/// <summary>
/// Special functions for handling <see cref="FilterGraphNode"/>s.
/// </summary>
public static class FilterGraphNodeUtils {
/// <summary>
/// Convert the filter graph's filters to convolutions, and merge chains together to a single filter.
/// </summary>
public static void ConvertToConvolution(IEnumerable<FilterGraphNode> rootNodes, int filterLength) {
HashSet<FilterGraphNode> visited = new HashSet<FilterGraphNode>();
Queue<FilterGraphNode> queue = new Queue<FilterGraphNode>(rootNodes);
while (queue.Count > 0) {
FilterGraphNode currentNode = queue.Dequeue();
if (visited.Contains(currentNode)) {
continue;
}

if (!(currentNode.Filter is BypassFilter || currentNode.Filter.GetType() == typeof(FastConvolver))) {
ConvertToConvolution(currentNode, filterLength);
}
visited.Add(currentNode);
foreach (FilterGraphNode child in currentNode.Children) {
queue.Enqueue(child);
}
}
}

/// <summary>
/// Check if the graph has cycles.
/// </summary>
Expand Down Expand Up @@ -44,6 +67,32 @@ public static HashSet<FilterGraphNode> MapGraph(IEnumerable<FilterGraphNode> roo
return visited;
}

/// <summary>
/// Converts this filter to a convolution and upmerges all children until possible.
/// </summary>
static void ConvertToConvolution(FilterGraphNode node, int filterLength) {
float[] impulse = new float[filterLength];
impulse[0] = 1;

FilterGraphNode downmergeUntil = node;
while (true) {
downmergeUntil.Filter.Process(impulse);
if (downmergeUntil.Children.Count != 1 || downmergeUntil.Children[0].Parents.Count != 1 ||
downmergeUntil.Children[0].Filter is BypassFilter) {
break;
}
downmergeUntil = downmergeUntil.Children[0];
}

FilterGraphNode[] newChildren = downmergeUntil.Children.ToArray();
downmergeUntil.DetachChildren();
node.Filter = new FastConvolver(impulse);
node.DetachChildren();
for (int i = 0; i < newChildren.Length; i++) {
node.AddChild(newChildren[i]);
}
}

/// <summary>
/// Starting from a single node, checks if the graph has cycles.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions Cavern/Utilities/ArrayExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ public static float Nearest(this float[] source, float value) {
return closest;
}

/// <summary>
/// Removes a <paramref name="value"/> from an <paramref name="array"/> if it contains it.
/// </summary>
public static void Remove<T>(ref T[] array, T value) {
for (int i = 0; i < array.Length; i++) {
if (!array[i].Equals(value)) {
continue;
}

T[] replacement = new T[array.Length - 1];
Array.Copy(array, 0, replacement, 0, i);
Array.Copy(array, i + 1, replacement, i, array.Length - i - 1);
array = replacement;
return;
}
}

/// <summary>
/// Remove the 0 or default elements from the end of an array.
/// </summary>
Expand Down
24 changes: 0 additions & 24 deletions CavernSamples/EQAPOtoFIR/Controls/FFTSize.xaml

This file was deleted.

103 changes: 0 additions & 103 deletions CavernSamples/EQAPOtoFIR/Controls/FFTSize.xaml.cs

This file was deleted.

8 changes: 4 additions & 4 deletions CavernSamples/EQAPOtoFIR/Dialogs/SegmentsDialog.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EQAPOtoFIR.Dialogs"
xmlns:EQAPOtoFIR="clr-namespace:EQAPOtoFIR" x:Class="EQAPOtoFIR.Dialogs.SegmentsDialog"
xmlns:voidx="clr-namespace:VoidX.WPF"
x:Class="EQAPOtoFIR.Dialogs.SegmentsDialog"
mc:Ignorable="d"
Title="C array splitting" Height="100" Width="300">
<Grid>
<Label Content="Select the amount of output segments:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
<EQAPOtoFIR:FFTSize x:Name="segments" HorizontalAlignment="Left" Margin="10,0,0,10" Width="63"
Minimum="1" Value="1" Maximum="65536" Height="20" VerticalAlignment="Bottom"/>
<voidx:FFTSize x:Name="segments" HorizontalAlignment="Left" Margin="10,0,0,10" Width="63"
Minimum="1" Value="1" Maximum="65536" Height="20" VerticalAlignment="Bottom"/>
<Button Content="OK" Margin="0,0,10,10" Height="20" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="75" Click="OK"/>
</Grid>
</Window>
1 change: 1 addition & 0 deletions CavernSamples/EQAPOtoFIR/EQAPOtoFIR.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="..\VoidX.WPF\FFTSize.cs" Link="Controls\FFTSize.cs" />
<Compile Include="..\VoidX.WPF\NumericUpDown.xaml.cs" Link="Controls\NumericUpDown.xaml.cs" />
</ItemGroup>

Expand Down
3 changes: 1 addition & 2 deletions CavernSamples/EQAPOtoFIR/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:EQAPOtoFIR"
xmlns:voidx="clr-namespace:VoidX.WPF"
mc:Ignorable="d"
Title="Equalizer APO to FIR" Height="286" Width="404">
Expand All @@ -19,7 +18,7 @@
<voidx:NumericUpDown x:Name="sampleRate" Minimum="8000" Value="48000" Maximum="384000" />
</GroupBox>
<GroupBox Header="FFT size" HorizontalAlignment="Left" Height="45" Margin="10,107,0,0" VerticalAlignment="Top" Width="100">
<local:FFTSize x:Name="fftSize" Minimum="512" Value="65536" Maximum="1024576" />
<voidx:FFTSize x:Name="fftSize" Minimum="512" Value="65536" Maximum="1024576" />
</GroupBox>
<GroupBox Header="Bit depth" HorizontalAlignment="Left" Height="95" Margin="115,57,0,0" VerticalAlignment="Top" Width="100">
<Grid>
Expand Down
5 changes: 4 additions & 1 deletion CavernSamples/FilterStudio/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ public partial class App : Application {
/// <summary>
/// Set the language strings with the app launch.
/// </summary>
public App() => Resources.MergedDictionaries.Add(Consts.Language.GetMainWindowStrings());
public App() {
Resources.MergedDictionaries.Add(Consts.Language.GetMainWindowStrings());
Resources.MergedDictionaries.Add(Consts.Language.GetDialogStrings());
}
}
}
10 changes: 10 additions & 0 deletions CavernSamples/FilterStudio/Consts/Language.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ static class Language {
/// </summary>
public static ResourceDictionary GetMainWindowStrings() => mainWindowCache ??= GetFor("MainWindowStrings");

/// <summary>
/// Get the translations of dialogs.
/// </summary>
public static ResourceDictionary GetDialogStrings() => dialogCache ??= GetFor("DialogStrings");

/// <summary>
/// Get the translation of a resource file in the user's language, or in English if a translation couldn't be found.
/// </summary>
Expand All @@ -33,5 +38,10 @@ static ResourceDictionary GetFor(string resource) {
/// The loaded translation of the <see cref="MainWindow"/> for reuse.
/// </summary>
static ResourceDictionary mainWindowCache;

/// <summary>
/// The loaded translation of the dialogs for reuse.
/// </summary>
static ResourceDictionary dialogCache;
}
}
5 changes: 5 additions & 0 deletions CavernSamples/FilterStudio/FilterStudio.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
<ApplicationIcon>..\Icon.ico</ApplicationIcon>
</PropertyGroup>
<ItemGroup>
<Compile Include="..\VoidX.WPF\FFTSize.cs" Link="VoidX.WPF\FFTSize.cs" />
<Compile Include="..\VoidX.WPF\NumericUpDown.xaml.cs" Link="VoidX.WPF\NumericUpDown.xaml.cs" />
<Compile Include="..\VoidX.WPF\ObjectToDataGrid.cs" Link="VoidX.WPF\ObjectToDataGrid.cs" />
<Compile Include="..\VoidX.WPF\QuickContextMenu.cs" Link="VoidX.WPF\QuickContextMenu.cs" />
</ItemGroup>
Expand All @@ -28,6 +30,9 @@
<Resource Include="..\_Common\CavernLogo.png" Link="Resources\CavernLogo.png" />
</ItemGroup>
<ItemGroup>
<Page Include="..\VoidX.WPF\NumericUpDown.xaml" Link="VoidX.WPF\NumericUpDown.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="..\_Common\Styles.xaml" Link="Resources\Styles.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down
17 changes: 17 additions & 0 deletions CavernSamples/FilterStudio/FilterStudio.csproj.user
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,31 @@
</ApplicationDefinition>
</ItemGroup>
<ItemGroup>
<Compile Update="Windows\ConvolutionLengthDialog.xaml.cs">
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Page Update="..\VoidX.WPF\NumericUpDown.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="..\_Common\Styles.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="MainWindow.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="Resources\DialogStrings.hu-HU.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="Resources\DialogStrings.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="Resources\MainWindowStrings.xaml">
<SubType>Designer</SubType>
</Page>
<Page Update="Windows\ConvolutionLengthDialog.xaml">
<SubType>Designer</SubType>
</Page>
</ItemGroup>
</Project>
Loading

0 comments on commit 9749eab

Please sign in to comment.