- Home /
Trying to make an audio visualizer, not sure how to distinguish frequencies with GetSpectrumData
If I'm understanding correctly, spectrum data is generally returned on a logarithmic scale. This is fine but I'm unable to figure out how to make sense of it because I don't know how this logarithmic scale works or how it relates to all the elements of the array. As for what I'm doing: I instantiate 1024 small blocks (sprites) then I space them apart and scale them appropriately so I can see them all. I then use get spectrum data to assign a value to each element in an array of 1024 float values. I match them 1:1 every frame. meaning all the values in my spectrum array are assigned linearly as the y scale of each of my blocks (arranged in a line). The problem with this is that it doesn't account for the fact that the spectrum data is meant to be read on a logarithmic scale so I end up not visualizing the audio in any meaningful way. Here is my code:
int numBlocks = 1024;
public float intrvl;
public float scale;
GameObject block;
GameObject[] blocks;
public float Boost;
void Start() {
blocks = new GameObject[numBlocks];
CreateBlocks ();
assignBlocks ();
//StartCoroutine (Visualize ());
}
void CreateBlocks() {
block = transform.GetChild (0).gameObject;
block.transform.position = new Vector3 (intrvl, block.transform.position.y, block.transform.position.z);
block.transform.localScale = new Vector3 (scale, 0, 0);
for (int i = 1; i < numBlocks; i++) {
GameObject newBlock = Instantiate (block, block.transform.position, Quaternion.Euler (0, 0, 0), transform);
newBlock.transform.Translate (new Vector3 (intrvl * i,0));
}
}
void assignBlocks() {
for (int i = 0; i < numBlocks; i++) {
blocks [i] = transform.GetChild (i).gameObject;
}
}
void Update() {
float[] spectrum = new float[numBlocks];
AudioListener.GetSpectrumData (spectrum, 0, FFTWindow.BlackmanHarris);
for (int i = 1; i < numBlocks - 1; i++) {
blocks [i].transform.localScale = new Vector3 (blocks [i].transform.localScale.x, spectrum [i]*Boost,blocks [i].transform.localScale.z);
}
}
intrvl is the distance between blocks and scale is the x size of blocks. Boost just increases the amplitude.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Cannot implicitly cinvert type 'float' to 'UnityEngine.Camera' ? 0 Answers
Distribute terrain in zones 3 Answers
AudioSource.GetSpectrumData Failure 0 Answers
[SOLVED] Issue with music and Don't Destroy on load 2 Answers