You can use AudioClip.GetData to get what you need. Just write a script like this:
using UnityEngine;
public class AudioSourceLoudnessTester : MonoBehaviour {
public AudioSource audioSource;
public float updateStep = 0.1f;
public int sampleDataLength = 1024;
private float currentUpdateTime = 0f;
private float clipLoudness;
private float[] clipSampleData;
// Use this for initialization
void Awake () {
if (!audioSource) {
Debug.LogError(GetType() + ".Awake: there was no audioSource set.");
}
clipSampleData = new float[sampleDataLength];
}
// Update is called once per frame
void Update () {
currentUpdateTime += Time.deltaTime;
if (currentUpdateTime >= updateStep) {
currentUpdateTime = 0f;
audioSource.clip.GetData(clipSampleData, audioSource.timeSamples); //I read 1024 samples, which is about 80 ms on a 44khz stereo clip, beginning at the current sample position of the clip.
clipLoudness = 0f;
foreach (var sample in clipSampleData) {
clipLoudness += Mathf.Abs(sample);
}
clipLoudness /= sampleDataLength; //clipLoudness is what you are looking for
}
}
}
I update the loudness value only once every 100ms to keep its CPU usage down, and make sure to declare the float for the sample data globally, since it could annoy the garbage collector, if you declare a huge array and destroy it again every update step.