refactoring to comply with c# conventions
This commit is contained in:
109
RhythmBullet/Audio/Visualizer/HorizontalVisualizer.cs
Normal file
109
RhythmBullet/Audio/Visualizer/HorizontalVisualizer.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using Microsoft.Xna.Framework.Graphics;
|
||||
using RhythmBullet.UI.Modular;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RhythmBullet.Audio.Visualizer
|
||||
{
|
||||
internal class HorizontalVisualizer : UIModule, IDisposable
|
||||
{
|
||||
GraphicsDevice graphicsDevice;
|
||||
Texture2D barTexture;
|
||||
private const int BAR_COUNT = 70;
|
||||
private const int SMOOTH_RANGE = 3;
|
||||
private readonly int binsPerBar;
|
||||
private readonly int spaceBetweenBars;
|
||||
private Rectangle bar;
|
||||
private TransparentSampleProvider tsp;
|
||||
private int[] barValue;
|
||||
|
||||
internal HorizontalVisualizer(TransparentSampleProvider transparentSampleProvider, GraphicsDevice graphicsDevice)
|
||||
{
|
||||
this.graphicsDevice = graphicsDevice;
|
||||
tsp = transparentSampleProvider;
|
||||
bar.Width = (int)(graphicsDevice.Viewport.Width / 70f);
|
||||
spaceBetweenBars = (int)(0.25f * bar.Width);
|
||||
bar.Width -= spaceBetweenBars;
|
||||
|
||||
barTexture = new Texture2D(graphicsDevice, 1, 1);
|
||||
barTexture.SetData(new[] { Color.White });
|
||||
binsPerBar = tsp.GetCurrentSpectrum().Length / BAR_COUNT;
|
||||
barValue = new int[BAR_COUNT];
|
||||
}
|
||||
|
||||
public override void Update(GameTime gameTime)
|
||||
{
|
||||
UpdateBars((float)gameTime.ElapsedGameTime.TotalSeconds);
|
||||
AverageBars();
|
||||
base.Update(gameTime);
|
||||
}
|
||||
|
||||
public override void Draw(SpriteBatch batch)
|
||||
{
|
||||
for (int i = 0; i < BAR_COUNT; i++)
|
||||
{
|
||||
bar.X = (i * (bar.Width + spaceBetweenBars)) + bounds.X;
|
||||
bar.Y = bounds.Y;
|
||||
|
||||
bar.Height = barValue[i];
|
||||
batch.Draw(barTexture, bar, color);
|
||||
|
||||
bar.Height = -barValue[BAR_COUNT - i - 1];
|
||||
batch.Draw(barTexture, bar, color);
|
||||
}
|
||||
base.Draw(batch);
|
||||
}
|
||||
|
||||
private void UpdateBars(float delta)
|
||||
{
|
||||
const float ALPHA = 0.5f;
|
||||
float[] spectrum = tsp.GetCurrentSpectrum();
|
||||
|
||||
for (int barID = 0; barID < BAR_COUNT; barID++)
|
||||
{
|
||||
int targetBarHeight = 0;
|
||||
|
||||
for (int bin = barID * binsPerBar; bin < (barID + 1) * (binsPerBar); bin++)
|
||||
{
|
||||
targetBarHeight += (int)spectrum[bin];
|
||||
}
|
||||
targetBarHeight /= binsPerBar;
|
||||
|
||||
int distance = targetBarHeight - barValue[barID];
|
||||
distance *= (int)Math.Round((1.0f - Math.Pow(1 - ALPHA, delta / 0.02f)));
|
||||
barValue[barID] += distance;
|
||||
}
|
||||
}
|
||||
|
||||
private void AverageBars()
|
||||
{
|
||||
for (int barID = 0; barID < BAR_COUNT; barID++)
|
||||
{
|
||||
int terms = 0;
|
||||
for (int pos = 0; pos < SMOOTH_RANGE; pos++)
|
||||
{
|
||||
if (barID + pos < BAR_COUNT)
|
||||
{
|
||||
barValue[barID] += barValue[barID + pos];
|
||||
terms++;
|
||||
}
|
||||
if (barID - pos > 0)
|
||||
{
|
||||
barValue[barID] += barValue[barID - pos];
|
||||
terms++;
|
||||
}
|
||||
}
|
||||
barValue[barID] /= terms;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
barTexture.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user