2019-09-29 10:00:16 +00:00
|
|
|
using UnityEngine;
|
|
|
|
|
|
|
|
namespace cylvester
|
|
|
|
{
|
|
|
|
interface ISpectrumGenerator
|
|
|
|
{
|
|
|
|
Texture2D Spectrum { get; }
|
2019-09-29 16:04:26 +00:00
|
|
|
void Update(float[] fftData, ref Rect selectionRect);
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public class SpectrumGenerator : ISpectrumGenerator
|
|
|
|
{
|
|
|
|
private Texture2D texture_;
|
|
|
|
private readonly int height_;
|
|
|
|
public Texture2D Spectrum => texture_;
|
|
|
|
|
|
|
|
public SpectrumGenerator(int width, int height)
|
|
|
|
{
|
|
|
|
texture_ = new Texture2D(width, height);
|
|
|
|
height_ = height;
|
|
|
|
}
|
|
|
|
|
2019-09-29 16:04:26 +00:00
|
|
|
public void Update(float[] fftData, ref Rect selectionRect)
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
2019-09-29 16:04:26 +00:00
|
|
|
for (var x = 0; x < texture_.width; x++)
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
2019-09-29 16:04:26 +00:00
|
|
|
var magnitude = fftData[x];
|
|
|
|
for (var y = 0; y < texture_.height; y++)
|
2019-09-29 10:00:16 +00:00
|
|
|
{
|
2019-09-29 16:04:26 +00:00
|
|
|
|
2019-09-29 10:00:16 +00:00
|
|
|
var alpha = 0.4f;
|
|
|
|
if ((selectionRect.x < x && x < (selectionRect.x + selectionRect.width)) &&
|
|
|
|
(selectionRect.y < y && y < (selectionRect.y + selectionRect.height)))
|
|
|
|
{
|
|
|
|
alpha = 1f;
|
|
|
|
}
|
2019-09-29 16:04:26 +00:00
|
|
|
|
|
|
|
var color = y > magnitude ? Color.white : Color.gray;
|
|
|
|
color.a = alpha;
|
|
|
|
texture_.SetPixel(x, height_, color);
|
2019-09-29 10:00:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
texture_.Apply();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|