Spectrum Data

Hey there

I wanted to try and do some visualization of music data and I was told to start by looking at the AudioSource.GetSpectrumData(…) method but the data that it spits out at me doesn’t really make sense to me :confused:

And looking at the example code doesn’t really educate me any further:

public class GetSpectrumDataExample : MonoBehaviour
{
    AudioSource audio;
    float[] spectrum = new float[256];

    void Start()
    {
        audio = GetComponent<AudioSource>();
    }

    // Update is called once per frame
    void Update()
    {
        audio.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);
        int i = 1;
        while (i < spectrum.Length - 1)
        {
            Debug.DrawLine(new Vector3(i - 1, spectrum[i] + 10, 0), new Vector3(i, spectrum[i + 1] + 10, 0), Color.red);
            Debug.DrawLine(new Vector3(i - 1, Mathf.Log(spectrum[i - 1]) + 10, 2), new Vector3(i, Mathf.Log(spectrum[i]) + 10, 2), Color.cyan);
            Debug.DrawLine(new Vector3(Mathf.Log(i - 1), spectrum[i - 1] - 10, 1), new Vector3(Mathf.Log(i), spectrum[i] - 10, 1), Color.green);
            Debug.DrawLine(new Vector3(Mathf.Log(i - 1), Mathf.Log(spectrum[i - 1]), 3), new Vector3(Mathf.Log(i), Mathf.Log(spectrum[i]), 3), Color.yellow);
            i++;
        }
    }
}

But despite this, the code produces this:

Could anyone in here shed some light on how the Spectrum Data works?

What is the spectrum: What is spectrum data? audio.GetSpectrumData - Unity Engine - Unity Discussions
How can it be used: GetOutputData and GetSpectrumData, what represent the values returned? - Questions & Answers - Unity Discussions
More:
https://www.youtube.com/watch?v=V_8JSWVT36k

1 Like

Thanks alucardj. I’ll look it over :slight_smile: