- Home /
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);
}
Answer by BerggreenDK · Dec 18, 2010 at 01:14 AM
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?
"sum all the samples within a R$$anonymous$$S and check if it makes an abnormally large jump in the sum?"
I believe the R$$anonymous$$S 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.
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."
Answer by _Petroz · Dec 18, 2010 at 11:54 AM
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.
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
This is a function that you call like the R$$anonymous$$S funcion. something like: if (ExceedsVolumeThreshold(window))
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)?
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 youtube.com/watch?v=xH20F6Q3trY or this http://www.youtube.com/watch?v=cInI1DDpVj$$anonymous$$&feature=related if that helps put it into context)
Your answer
Follow this Question
Related Questions
sound distance between camera and audio source 2 Answers
Rigidbody crate collide sound. 0 Answers
Music volume decreases after one loop 1 Answer
AudioListener.volume doesn't change the volume 4 Answers
Urgent! Need to animate volume 1 Answer