2018-11-20 03:07:23 +00:00
|
|
|
|
using Microsoft.Xna.Framework;
|
|
|
|
|
using Microsoft.Xna.Framework.Graphics;
|
2019-01-13 05:56:30 +00:00
|
|
|
|
using RecrownedAthenaeum.Camera;
|
|
|
|
|
using RecrownedAthenaeum.Render;
|
2018-12-04 05:05:54 +00:00
|
|
|
|
using RecrownedAthenaeum.UI.Modular;
|
2018-11-20 03:07:23 +00:00
|
|
|
|
using System;
|
|
|
|
|
|
2019-01-23 01:20:13 +00:00
|
|
|
|
namespace RhythmBullet.Audio.Visualizer
|
2018-11-20 03:07:23 +00:00
|
|
|
|
{
|
2019-01-23 01:20:13 +00:00
|
|
|
|
internal class HorizontalVisualizer : UIModule, IDisposable
|
2018-11-20 03:07:23 +00:00
|
|
|
|
{
|
2019-01-23 01:20:13 +00:00
|
|
|
|
bool disposed;
|
2019-02-26 03:40:07 +00:00
|
|
|
|
private RectangleRenderer rectangleRenderer;
|
2018-11-20 03:07:23 +00:00
|
|
|
|
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;
|
|
|
|
|
|
2019-01-13 05:56:30 +00:00
|
|
|
|
internal HorizontalVisualizer(TransparentSampleProvider transparentSampleProvider, GraphicsDevice graphicsDevice, Camera2D camera2D)
|
2018-11-20 03:07:23 +00:00
|
|
|
|
{
|
|
|
|
|
tsp = transparentSampleProvider;
|
|
|
|
|
bar.Width = (int)(graphicsDevice.Viewport.Width / 70f);
|
|
|
|
|
spaceBetweenBars = (int)(0.25f * bar.Width);
|
|
|
|
|
bar.Width -= spaceBetweenBars;
|
|
|
|
|
|
|
|
|
|
binsPerBar = tsp.GetCurrentSpectrum().Length / BAR_COUNT;
|
|
|
|
|
barValue = new int[BAR_COUNT];
|
2019-02-26 03:40:07 +00:00
|
|
|
|
rectangleRenderer = new RectangleRenderer();
|
2018-11-20 03:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Update(GameTime gameTime)
|
|
|
|
|
{
|
2019-01-23 01:20:13 +00:00
|
|
|
|
if (disposed) throw new ObjectDisposedException(this.Name);
|
2018-11-20 03:07:23 +00:00
|
|
|
|
UpdateBars((float)gameTime.ElapsedGameTime.TotalSeconds);
|
|
|
|
|
AverageBars();
|
|
|
|
|
base.Update(gameTime);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Draw(SpriteBatch batch)
|
|
|
|
|
{
|
2019-01-23 01:20:13 +00:00
|
|
|
|
if (disposed) throw new ObjectDisposedException(this.Name);
|
2019-02-26 03:40:07 +00:00
|
|
|
|
rectangleRenderer.Begin(true);
|
2018-11-20 03:07:23 +00:00
|
|
|
|
for (int i = 0; i < BAR_COUNT; i++)
|
|
|
|
|
{
|
2018-11-21 00:12:02 +00:00
|
|
|
|
bar.X = (i * (bar.Width + spaceBetweenBars)) + bounds.X;
|
|
|
|
|
bar.Y = bounds.Y;
|
2018-11-20 03:07:23 +00:00
|
|
|
|
|
|
|
|
|
bar.Height = barValue[i];
|
2019-02-26 03:40:07 +00:00
|
|
|
|
rectangleRenderer.Draw(bar.X, bar.Y, bar.Width, bar.Height, color);
|
2018-11-20 03:07:23 +00:00
|
|
|
|
|
|
|
|
|
bar.Height = -barValue[BAR_COUNT - i - 1];
|
2019-02-26 03:40:07 +00:00
|
|
|
|
rectangleRenderer.Draw(bar.X, bar.Y, bar.Width, bar.Height, color);
|
2018-11-20 03:07:23 +00:00
|
|
|
|
}
|
2019-02-26 03:40:07 +00:00
|
|
|
|
rectangleRenderer.End();
|
2018-11-20 03:07:23 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-23 01:20:13 +00:00
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (disposed) throw new ObjectDisposedException(this.Name);
|
|
|
|
|
Dispose(true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public virtual void Dispose(bool disposing)
|
|
|
|
|
{
|
|
|
|
|
if (disposing && !disposed)
|
|
|
|
|
{
|
2019-02-26 03:40:07 +00:00
|
|
|
|
rectangleRenderer.Dispose();
|
2019-01-23 01:20:13 +00:00
|
|
|
|
}
|
|
|
|
|
disposed = true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-14 04:08:58 +00:00
|
|
|
|
~HorizontalVisualizer()
|
2018-11-20 03:07:23 +00:00
|
|
|
|
{
|
2019-01-23 01:20:13 +00:00
|
|
|
|
Dispose(false);
|
2018-11-20 03:07:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|