How it works...

The key component of this recipe is the AudioSource.GetOutputData(float[] samples, int channel) function. It grabs a number of samples from the currently playing audio. The number of samples is specified by the length of the float[] samples array. This length has to be a power of 2. We get the samples every frame in the IEnumerator SampleAudio() coroutine. We average the values of squared samples to calculate an RMS of the audio (an average volume of a number of samples). The more samples we average, the smoother the changes of the RMS. For most sounds, 1024 is a good number. We use the calculated RMS to scale the Cube game object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.40.189