Skip to content

Commit

Permalink
Merge pull request #34 from stevehjohn/sab
Browse files Browse the repository at this point in the history
Sab
  • Loading branch information
stevehjohn authored Aug 11, 2024
2 parents d14262b + 134405c commit 303cbf8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
65 changes: 42 additions & 23 deletions src/Zen.Desktop.Host/Features/SpectrumAnalyser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ public class SpectrumAnalyser

private int _bufferPosition;

private int _beeperBufferPosition;

private bool _rendering;

private readonly Color[] _palette;

private readonly float[] _magnitudes;

private readonly float[] _groupedMagnitudes;

private readonly FrequencyRange[] _frequencyRanges =
[
new FrequencyRange(30, 40),
Expand Down Expand Up @@ -71,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 @@ -114,6 +124,18 @@ public void ReceiveSignals(float[] signals)
}
}

public void ReceiveSignal(float signal)
{
_buffers[3][_beeperBufferPosition] = new Complex(-signal / 1.5, 0);

_beeperBufferPosition++;

if (_beeperBufferPosition >= BufferSize)
{
_beeperBufferPosition = 0;
}
}

private void RenderSpectrum()
{
_rendering = true;
Expand All @@ -123,6 +145,7 @@ private void RenderSpectrum()
RenderSpectrumChannel(0);
RenderSpectrumChannel(1);
RenderSpectrumChannel(2);
RenderSpectrumChannel(3);

_spectrum.SetData(_data);

Expand All @@ -135,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 @@ -204,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 @@ -256,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;
}
}
}
5 changes: 5 additions & 0 deletions src/Zen.Desktop.Host/Infrastructure/Host.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ private void SetMotherboard(Model model)
if (_spectrumAnalyser != null)
{
_motherboard.AyAudio.AySignalHook = _spectrumAnalyser.ReceiveSignals;

_motherboard.AyAudio.BeeperSignalHook = _spectrumAnalyser.ReceiveSignal;
}
}

Expand Down Expand Up @@ -187,6 +189,8 @@ protected override void LoadContent()
_spectrumAnalyser = new SpectrumAnalyser(_graphicsDeviceManager);

_motherboard.AyAudio.AySignalHook = _spectrumAnalyser.ReceiveSignals;

_motherboard.AyAudio.BeeperSignalHook = _spectrumAnalyser.ReceiveSignal;
}

if (AppSettings.Instance.Visualisation == Visualisation.VideoRam)
Expand Down Expand Up @@ -362,6 +366,7 @@ private void MenuFinished(MenuResult result, object arguments)

_spectrumAnalyser = new SpectrumAnalyser(_graphicsDeviceManager);
_motherboard.AyAudio.AySignalHook = _spectrumAnalyser.ReceiveSignals;
_motherboard.AyAudio.BeeperSignalHook = _spectrumAnalyser.ReceiveSignal;
_videoRamVisualiser = null;
_waveVisualiser = null;

Expand Down

0 comments on commit 303cbf8

Please sign in to comment.