Making An Audio Visualizer In Unity With Blend Shapes & Audio API - Part 2

There is a Part 1, check that out before going through this one.
So now we will look at Unity's Audio API, not the entire thing just the part that allows us to do stuff like this.
Audio Frequency Spectrum Visualizer
This is a representation of the audio data based on it's frequency and the how loud (amplitude) that particular part of the frequency spectrum is.
Let's start coding the thing.
We will make class called AudioVisualizer.
Member variables:
 [Range(1.0f,4500.0f)]  
 public float multiplier;  
 public int minRange = 0;  
 public int maxRange = 64;  
 private SkinnedMeshRenderer skinnedMeshRenderer;  
 private float prevAvg = 0.0f;
Nothing special here, we will get to know what these are used for as we go on.
Start Function:
void Start ()   
{  
   skinnedMeshRenderer = GetComponent<SkinnedMeshRenderer> ();  
}  
Update Function:
void Update ()   
{      
     float[] spectrum = new float[64];  
     AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);  
     if (maxRange < minRange)  
       maxRange = minRange + 1;  
     minRange = Mathf.Clamp(minRange, 0, 63);  
     maxRange = Mathf.Clamp(maxRange, 0, 63);  
     float avg = 0;  
     for (int i = minRange; i < maxRange; i++)  
       avg += Mathf.Abs(spectrum[i]);      
     avg = avg / (float)Mathf.Abs(maxRange - minRange);  
     if (avg - prevAvg > 0.0012f)
            avg = prevAvg + 0.0012f;
     else if (avg - prevAvg < -0.0012f)
            avg = prevAvg - 0.0012f;
     skinnedMeshRenderer.SetBlendShapeWeight(0, avg*multiplier);  
     prevAvg = avg;
}  
Now time to break it down.
float[] spectrum = new float[64];  
AudioListener.GetSpectrumData(spectrum, 0, FFTWindow.BlackmanHarris);  
We allocate an array of 64 floats.
We then pass in that array as parameter to 'GetSpectrumData'.
'GetSpectrumData' requires 3 parameters -
1) The float array (has be power of 2 in size eg :32,64,128 etc).
2) Which audio channel it is. In case of stereo audio 2 channels exist, we will use the first one.
3) Type of FFT Window, We will use BlackmanHarris which is more expensive to compute when compared to Window type of Triangles or Hamming but has better quality.
So each index of the float[] is a higher frequency bracket.
With index 0 being lowest frequency bracket and 63 being highest frequency bracket.
The float value at each index represents how much amplitude that frequency had. 
 if (maxRange < minRange)  
       maxRange = minRange + 1;  
minRange = Mathf.Clamp(minRange, 0, 63);  
maxRange = Mathf.Clamp(maxRange, 0, 63);  
The above code keep parameters within the correct range.
float avg = 0;  
for (int i = minRange; i < maxRange; i++)  
     avg += Mathf.Abs(spectrum[i]);      
avg = avg / (float)Mathf.Abs(maxRange - minRange);  
if (avg - prevAvg > 0.0012f)
      avg = prevAvg + 0.0012f;
else if (avg - prevAvg < -0.0012f)
      avg = prevAvg - 0.0012f;
I am going through the specified range of frequencies and averaging them and also limiting how much change can happen each frame so that the motion is no that erratic.
skinnedMeshRenderer.SetBlendShapeWeight(0, avg*multiplier);  
prevAvg = avg;
Here we are setting the blend shape weight and assigning prevAvg as the currently calculated average.
That's it! Get source code HERE.
Get Unity package HERE.
For more Unity development tutorials go HERE
Support Bitshift Programmer by leaving a like on Bitshift Programmer Facebook Page and be updated as soon as there is a new blog post.
If you have any questions that you might have about shaders or unity development in general don't be shy and leave a message on my facebook page or down in the comments.