Skip to content

Commit

Permalink
Fix memory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehjohn committed Aug 11, 2024
1 parent 5fbb06c commit 134405c
Showing 1 changed file with 27 additions and 23 deletions.
50 changes: 27 additions & 23 deletions src/Zen.Desktop.Host/Features/SpectrumAnalyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ public class SpectrumAnalyser

private readonly Color[] _palette;

private readonly float[] _magnitudes;

private readonly float[] _groupedMagnitudes;

private readonly FrequencyRange[] _frequencyRanges =
[
new FrequencyRange(30, 40),
Expand Down Expand Up @@ -73,6 +77,10 @@ public SpectrumAnalyser(GraphicsDeviceManager graphicsDeviceManager)

_peakTimes = new long[4][];

_magnitudes = new float[BufferSize];

_groupedMagnitudes = new float[_frequencyRanges.Length];

for (var i = 0; i < 4; i++)
{
_buffers[i] = new Complex[BufferSize];
Expand Down Expand Up @@ -150,35 +158,31 @@ private void RenderSpectrumChannel(int channel)

var height = Constants.ScreenHeightPixels / 4;

var magnitudes = new float[_buffers[channel].Length];

var axis = height * Constants.SpectrumVisualisationPanelWidth * (channel + 1) - Constants.SpectrumVisualisationPanelWidth;

for (var i = 0; i < magnitudes.Length; i++)
for (var i = 0; i < _magnitudes.Length; i++)
{
magnitudes[i] = (float) Math.Sqrt(_buffers[channel][i].Real * _buffers[channel][i].Real + _buffers[channel][i].Imaginary * _buffers[channel][i].Imaginary);
_magnitudes[i] = (float) Math.Sqrt(_buffers[channel][i].Real * _buffers[channel][i].Real + _buffers[channel][i].Imaginary * _buffers[channel][i].Imaginary);
}

var groupedMagnitudes = new float[_frequencyRanges.Length];

for (var i = 0; i < _frequencyRanges.Length; i++)
{
var lowBin = (int)Math.Round(_frequencyRanges[i].Low * BufferSize / System.Modules.Audio.Constants.SampleRate);
var highBin = (int)Math.Round(_frequencyRanges[i].High * BufferSize / System.Modules.Audio.Constants.SampleRate);
var lowBin = (int) Math.Round(_frequencyRanges[i].Low * BufferSize / System.Modules.Audio.Constants.SampleRate);
var highBin = (int) Math.Round(_frequencyRanges[i].High * BufferSize / System.Modules.Audio.Constants.SampleRate);

float sum = 0;

for (var j = lowBin; j <= highBin; j++)
{
sum += magnitudes[j];
sum += _magnitudes[j];
}

groupedMagnitudes[i] = sum / (highBin - lowBin + 1);
_groupedMagnitudes[i] = sum / (highBin - lowBin + 1);
}

for (var i = 0; i < _frequencyRanges.Length; i++)
{
var dataPoint = groupedMagnitudes[i] / MagnitudeDivisor;
var dataPoint = _groupedMagnitudes[i] / MagnitudeDivisor;

var peak = (int) (dataPoint * height * (channel == 3 ? 1 : 4));

Expand Down Expand Up @@ -219,18 +223,6 @@ private void RenderSpectrumChannel(int channel)
}
}
}

private class FrequencyRange
{
public float Low { get; }
public float High { get; }

public FrequencyRange(float low, float high)
{
Low = low;
High = high;
}
}

private static class PaletteGenerator
{
Expand Down Expand Up @@ -271,4 +263,16 @@ public static Color[] GetPalette(int steps, Color[] markers)
return palette;
}
}

private class FrequencyRange
{
public float Low { get; }
public float High { get; }

public FrequencyRange(float low, float high)
{
Low = low;
High = high;
}
}
}

0 comments on commit 134405c

Please sign in to comment.