Volume increase?

Hello, I got this script from this thread I started. How can I use it to detect a sudden increase in volume, (like a drum beat) and then tie it to another function (like increasing the velocity of a particle emitter or spawning an enemy)?

var vol: float;
var sampleRate: float;
var timeWindow: float;

private var numSamples: int;
private var window: float[];

function Start() {
    numSamples = sampleRate * timeWindow;
    window = new float[numSamples];
    InvokeRepeating("UpdateVolume", 0, timeWindow);
}

function UpdateVolume() {
window = audio.GetOutputData(numSamples, 0);
    vol = RMS(window);
}

function RMS(data: float[]) {
    var result = 0.0;

    for (i = 0; i < data.Length; i++) {
        result += data _* data*;*_
 _*}*_
 _*result /= data.Length;*_
 _*return Mathf.Sqrt(result);*_
_*}*_
_*```*_

I havent tried it, but perhaps you could sum all the samples within a RMS and check if it makes an abnormally large jump in the sum?

The RMS function seems to be summing your samples already? so the "vol" variable, isnt that the thing you should be watching?

perhaps if you store the value from each update in a "previousVol" somewhere globally and then compare the previous to the new vol. If the difference is... lets say 30 then you have a spike. Then you call your "volumeter trigger"?

Wouldnt that do the trick?

If you want to detect a sudden peak just check the absolute value of the samples:

function ExceedsVolumeThreshold(data: float[]) {
    var threshold = 0.7;

    for (i = 0; i < data.Length; i++) {
        if (Mathf.Abs(data*) > threshold) {*
 *return true;*
 *}*
 *}*
 *return false;*
*}*
*```*
*<p>This is simplified, better analysis would require more complex code. You would probably have to develop some heuristics to distinguish beats from other sounds in the music.</p>*