x


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[i] * data[i];
    }

    result /= data.Length;

    return Mathf.Sqrt(result);
}
more ▼

asked Dec 18 '10 at 01:04 AM

Tyler 2 gravatar image

Tyler 2
1.1k 211 246 264

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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?

more ▼

answered Dec 18 '10 at 01:14 AM

BerggreenDK gravatar image

BerggreenDK
2.4k 54 62 75

How would I do that?

Dec 18 '10 at 01:48 AM Tyler 2

How would you do what?

Dec 18 '10 at 02:19 AM BerggreenDK

"sum all the samples within a RMS and check if it makes an abnormally large jump in the sum?"

Dec 18 '10 at 02:29 AM Tyler 2

I believe the RMS function already does it for you. Do you understand what the script does or how much practice do you have? Not to be rude, but I am trying to understand your question.

Dec 18 '10 at 02:44 AM BerggreenDK

You're not being rude.I think the script I posted takes the volume of of the audio in the scene every interval? I am not very good at scripting. I dont know how I would "store the value from each update in a "previousVol" somewhere globally and then compare the previous to the new vol."

Dec 18 '10 at 06:18 AM Tyler 2
(comments are locked)
10|3000 characters needed characters left

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[i]) > threshold) {
             return true;
        }
    }
    return false;
}

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.

more ▼

answered Dec 18 '10 at 11:54 AM

_Petroz gravatar image

_Petroz
3.6k 27 35 57

Thanks. I added that to the script I posted above, and even though I am getting no error, I cannot get anything to happen when the audio increases (even if I set the threshold to .1). Tyler 0 secs ago

Dec 18 '10 at 05:50 PM Tyler 2

This is a function that you call like the RMS funcion. something like: if (ExceedsVolumeThreshold(window))

Dec 18 '10 at 10:49 PM _Petroz

Thank you. Is there a way to analyze audio BEFORE it plays? I know that is odd, but in music generated games like audio surf and rhythm zone, that is done (I presume it would help performance)?

Dec 19 '10 at 06:46 AM Tyler 2

Also, this is going to need some fine tunning, do you have any idea on how I could accuratly test to see if it is detecting beats (I want to eventualy make something like this http://youtube.com/watch?v=xH20F6Q3trY or this http://www.youtube.com/watch?v=cInI1DDpVjM&feature=related if that helps put it into context)

Dec 19 '10 at 07:29 AM Tyler 2
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1026
x74

asked: Dec 18 '10 at 01:04 AM

Seen: 1178 times

Last Updated: Dec 18 '10 at 01:04 AM