diff --git a/src/Zen.Desktop.Host/Features/WaveVisualiser.cs b/src/Zen.Desktop.Host/Features/WaveVisualiser.cs index a9e797df..a1c6ce1a 100644 --- a/src/Zen.Desktop.Host/Features/WaveVisualiser.cs +++ b/src/Zen.Desktop.Host/Features/WaveVisualiser.cs @@ -16,6 +16,8 @@ public class WaveVisualiser private readonly float[][] _buffers; + private readonly float[] _centreBuffer; + private readonly Texture2D _waves; private int _bufferPosition; @@ -40,6 +42,8 @@ public WaveVisualiser(GraphicsDeviceManager graphicsDeviceManager) { _buffers[i] = new float[BufferSize]; } + + _centreBuffer = new float[BufferSize]; } public void ReceiveSignals(float[] signals) @@ -102,9 +106,7 @@ private void RenderChannel(int channel) var axis = channel == 3 ? height * width * channel + height * width / 2 : height * width * (channel + 1) - width; - var buffer = _buffers[channel]; - - var length = buffer.Length; + CentreChannel(_buffers[channel]); var lastOffset = 0; @@ -112,7 +114,7 @@ private void RenderChannel(int channel) for (var x = 0; x < width; x++) { - var dataPoint = buffer[(int) (x * ((float) length / width))]; + var dataPoint = _centreBuffer[(int) (x * ((float) BufferSize / width))]; var offset = -(int) (dataPoint * height * (channel == 3 ? 1 : 4)); @@ -131,4 +133,28 @@ private void RenderChannel(int channel) lastOffset = offset; } } + + private void CentreChannel(float[] buffer) + { + var max = float.MinValue; + + var maxPos = int.MinValue; + + for (var i = 0; i < BufferSize; i++) + { + if (buffer[i] > max) + { + max = buffer[i]; + + maxPos = i; + } + } + + var startPos = BufferSize / 2 + maxPos; + + for (var i = 0; i < BufferSize; i++) + { + _centreBuffer[i] = buffer[(startPos + i) % BufferSize]; + } + } } \ No newline at end of file