soundvision/UnityProject/Assets/Scripts/PdConnection/Spectrogram.cs

56 lines
1.5 KiB
C#
Raw Normal View History

2019-10-05 14:49:30 +00:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace cylvester
{
public interface ISpectrogram
{
Texture2D Texture { get; }
int Index { get; }
2019-10-05 14:49:30 +00:00
}
public class Spectrogram : MonoBehaviour, ISpectrogram
{
[SerializeField] private PdBackend pdBackend;
[SerializeField, Range(1, 16)] private int channel = 1;
[SerializeField] int arrayLength_ = PdConstant.BlockSize;
2019-10-05 14:49:30 +00:00
2019-10-05 16:09:24 +00:00
private IPdArraySelector spectrumArraySelector_;
2019-10-05 14:49:30 +00:00
private Texture2D texture_;
private int index_;
void Start()
{
2019-10-05 16:09:24 +00:00
spectrumArraySelector_ = new PdArraySelector(pdBackend.SpectrumArrayContainer);
texture_ = new Texture2D(PdConstant.BlockSize, arrayLength_, TextureFormat.R8, false);
2019-10-05 14:49:30 +00:00
var pixels = texture_.GetPixels();
for (var i = 0;i < pixels.Length; ++i)
pixels[i] = Color.black;
texture_.SetPixels(pixels);
texture_.Apply();
}
void Update()
{
spectrumArraySelector_.Selection = channel - 1;
var array = spectrumArraySelector_.SelectedArray;
2019-10-05 16:09:24 +00:00
for (var i = 0; i < PdConstant.BlockSize; i++)
2019-10-05 14:49:30 +00:00
{
texture_.SetPixel(i, index_, new Color(array[i], 0f, 0f));
}
texture_.Apply();
index_++;
index_ %= arrayLength_;
2019-10-05 14:49:30 +00:00
}
public Texture2D Texture => texture_;
public int Index => index_;
2019-10-05 14:49:30 +00:00
}
}