Skip to content

Commit

Permalink
Add option to use Real+Imaginary average value
Browse files Browse the repository at this point in the history
  • Loading branch information
Sekoree committed Jul 10, 2022
1 parent c174ff1 commit 9e51636
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
18 changes: 18 additions & 0 deletions RendererUI/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,28 @@ public bool UseImaginaryFFTValues
get => _renderer.UseImaginaryFFTValues;
set
{
if (value)
{
UseImaginaryAndRealAverage = false;
}
_renderer.UseImaginaryFFTValues = value;
this.RaisePropertyChanged();
}
}

public bool UseImaginaryAndRealAverage
{
get => _renderer.UseImaginaryAndRealAverage;
set
{
if (value)
{
UseImaginaryFFTValues = false;
}
_renderer.UseImaginaryAndRealAverage = value;
this.RaisePropertyChanged();
}
}

[Reactive] public ObservableCollection<string> FFTWindows { get; set; } = new();
[Reactive] public string SelectedFFTWindow { get; set; }
Expand Down
22 changes: 17 additions & 5 deletions RendererUI/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</Design.DataContext>

<Grid ColumnDefinitions="2*,Auto"
RowDefinitions="Auto,Auto,Auto,Auto"
RowDefinitions="Auto,Auto,Auto,Auto,Auto"
Margin="10">
<TextBox Grid.Column="0"
Grid.Row="0"
Expand Down Expand Up @@ -54,19 +54,31 @@
Spacing="10">
<ComboBox Items="{Binding FFTWindows}"
SelectedItem="{Binding SelectedFFTWindow}"/>
<CheckBox IsChecked="{Binding UseImaginaryAndRealAverage}"
Content="Use Real + Imaginary FFT Average"/>
</StackPanel>

<StackPanel Grid.Column="0"
Grid.ColumnSpan="2"
Grid.Row="3"
Margin="10"
VerticalAlignment="Center"
HorizontalAlignment="Stretch"
Orientation="Horizontal"
Spacing="10">
<CheckBox IsChecked="{Binding UseImaginaryFFTValues}"
Content="Use Imaginary FFTs"/>
<CheckBox IsChecked="{Binding UseFFTPositions}"
Content="Use FFT Positions"/>
<CheckBox IsChecked="{Binding UseImaginaryFFTValues}"
Content="Use Imaginary FFT Values"/>
</StackPanel>

<TextBlock Grid.Column="0"
Grid.Row="3"
Grid.Row="4"
Margin="10"
VerticalAlignment="Center"
Text="{Binding OutputText, StringFormat=Output: {0}}"/>
<Button Grid.Column="1"
Grid.Row="3"
Grid.Row="4"
Margin="10"
HorizontalAlignment="Stretch"
Content="Render"
Expand Down
46 changes: 37 additions & 9 deletions SekoRenderer/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@ public class Renderer
{
public bool UseFFTPositions { get; set; }
public bool UseImaginaryFFTValues { get; set; }
public bool UseImaginaryAndRealAverage { get; set; }

public delegate double _fftWindow(int n, int frameSize);
public _fftWindow FFTWindow { get; set; }

public Renderer()
private int _fftResolution;

public Renderer(int fftResolution = 512)
{
UseFFTPositions = true;
UseImaginaryFFTValues = true;
UseImaginaryAndRealAverage = false;
FFTWindow = FastFourierTransform.HannWindow;
_fftResolution = fftResolution;
}

public Renderer(ref _fftWindow fftWindow, bool useFFTPositions = true, bool useImaginaryFFTValues = true)
public Renderer(ref _fftWindow fftWindow, bool useFFTPositions = true, bool useImaginaryFFTValues = true, bool useImaginaryAndRealAverage = false, int fftResolution = 512)
{
UseFFTPositions = useFFTPositions;
UseImaginaryFFTValues = useImaginaryFFTValues;
UseImaginaryAndRealAverage = useImaginaryAndRealAverage;
FFTWindow = fftWindow;
_fftResolution = fftResolution;
}

public string Md5HashFile(string fileName)
Expand Down Expand Up @@ -69,7 +76,7 @@ public void WriteAshFile(string path, string outputFolder, List<float> sums)
}

sbyte[]? shape = null;
int numShapeNodes = 256;
int numShapeNodes = _fftResolution / 2;
Console.WriteLine("Creating shape");
if (fileStream != null)
{
Expand Down Expand Up @@ -111,7 +118,7 @@ public void WriteAshFile(string path, string outputFolder, List<float> sums)

public List<float> DecodeSongSums(string path)
{
var fft = new float[512];
var fft = new float[_fftResolution];

var could = Bass.Init();
Console.WriteLine("Bass.Init returned " + could);
Expand All @@ -138,19 +145,35 @@ public List<float> DecodeSongSums(string path)
private float FastDecodeStepAsync(int chan, float[] ffts)
{
int fftPos = 0;
var data = Bass.ChannelGetData(chan, ffts, (int)DataFlags.FFT1024);
var defaultData = DataFlags.FFT1024;
if (_fftResolution == 128)
defaultData = DataFlags.FFT256;
else if (_fftResolution == 256)
defaultData = DataFlags.FFT512;
else if (_fftResolution == 1024)
defaultData = DataFlags.FFT2048;
else if (_fftResolution == 2048)
defaultData = DataFlags.FFT4096;
else if (_fftResolution == 4096)
defaultData = DataFlags.FFT8192;
else if (_fftResolution == 8192)
defaultData = DataFlags.FFT16384;
else if (_fftResolution == 16384)
defaultData = DataFlags.FFT32768;

var data = Bass.ChannelGetData(chan, ffts, (int)defaultData);

var complexFFTs = new Complex[ffts.Length];
for (int i = 0; i < ffts.Length; i++)
{
complexFFTs[i] = new Complex(ffts[i] * FFTWindow.Invoke(fftPos, 512), 0);
complexFFTs[i] = new Complex(ffts[i] * FFTWindow.Invoke(fftPos, _fftResolution), 0);
if (UseFFTPositions)
{
fftPos++;
}
}

FastFourierTransform.FFT(false, (int)Math.Log(512, 2.0), ref complexFFTs);
FastFourierTransform.FFT(false, (int)Math.Log(_fftResolution, 2.0), ref complexFFTs);

int fFT512KISS = data;
if (fFT512KISS < 1)
Expand All @@ -159,9 +182,14 @@ private float FastDecodeStepAsync(int chan, float[] ffts)
}

float num = 0f;
for (int i = 1; i < 512; i++)
for (int i = 1; i < _fftResolution; i++)
{
if (UseImaginaryFFTValues)
if (UseImaginaryAndRealAverage)
{
var avg = (complexFFTs[i].Real + complexFFTs[i].Imaginary) / 2;
num += (float)Math.Sqrt(Math.Max(0f, avg));
}
else if (UseImaginaryFFTValues)
{
num += (float)Math.Sqrt(Math.Max(0f, complexFFTs[i].Imaginary));
}
Expand Down

0 comments on commit 9e51636

Please sign in to comment.