- Home /
Change variable value based on perceived audio volume
If we know that Unity uses the calculation volume = 1.0 / (1.0 + rolloff * (distance 1.0));
to calculate volume based on distance, is it possible to use this in some way to manipulate the value of a variable based on the current 'perceived' volume? Such as changing the alpha value of a GUITexture's color.
I half-suspect the answer is a flat 'no', but if we know the rolloff value, can't we use this in some way? I guess an easier way is to fake it by checking the player's distance to the audiosource or using triggers, but that would surely mean a lot of tweaking the radius to match up with the sound rolloff. Just curious if there's another way.
Promise this will be my last question of the day!
Thanks.
[EDIT] I've been playing around with the formula to see what I can get from it, but i'll be honest and say i'm not 100% sure what i'm doing here. I couldn't get the right syntax to decalre a variable with this formula on a single line, so i've attacked it in two parts, then combined the result. The value returned is too large to be useful (ie, above 1.0) so I need a way to convert it to a value between 0 and 1 (I believe Mathf.Clamp is what i'm looking for but using Mathf.Clamp((1.0 / rolloff * dist),0.0,1.0);
doesn't map it between 0.0 and 1.0). As I said, i'm fumbling around in the dark here so excuse my ignorance!
var player : GameObject; var rolloff : float;
function Start(){
// temporary, i'll assign these from the editor later audioSource = gameObject.Find("Sphere"); player = gameObject.Find("First Person Controller");
}
function Update(){
//the (distance - 1.0) part of the equation var dist = Vector3.Distance(audioSource.transform.position, player.transform.position) -1.0;
// the 1.0 + rolloff part of the equation rolloff = 1.0 + audioSource.audio.rolloffFactor;
// combining the two currentVol = 1.0 / rolloff * dist;
guiTexture.color.a = currentVol;
print(currentVol);
}
I can confirm that the above does have an effect so long as I am close to the object (ie, while currentVol is a float less than 1.0). It's constantly changing because the sphere is moving, but is definitely changing the alpha of the GUITexture as I get closer (it's actually fading out the closer I get so I might have something back to front). Still need to figure out how to map currentVol to a value between 0.0 and 1.0 however.
You're not going about this quite right anyway. The volume is NOT the "perceived volume". Humans don't hear on a linear scale. You can check out http://www.unifycommunity.com/wiki/index.php?title=Listener for something that will match better. I'd put some effort into helping you, but I think Unity 3.0 is going to make all this boring fixing of the audio curves unnecessary, so I'm waiting for that version to see what happens. Hopefully everything I put on the wiki becomes obsolete.
Hi Jessy, I know that volume is not the perceived volume, and therein lay my problem: trying to get some representation of the perceived volume which I can use to alter the value of something like a GUITexture's alpha. I found your scripts on Unify yesterday but i'll be honest, all this is over my head so I wasn't sure how I could use them to help me here. I do appreciate that it's meaningless to find an answer here since Unity 3.0 is on the way, but my project will be completed before then. However, I might just change the alpha based on distance for simplicity. Thank you for your insights.
Answer by Lucas Meijer 1 · Apr 02, 2010 at 11:14 PM
If you know that that is the formula, nothing is stopping you from running the same formula, and doing something with the resulting value.
You can ask as many questions on a day as you like :)
Hi Lucas, i'm trying to do that but I wasn't able to combine the entire formula on a single line. I've amended my question above to show you what i've got.
Your answer
Follow this Question
Related Questions
webgl volume just doesnt work 0 Answers
How to get current volume of an AudioSource. 3 Answers
Audio Volume Help 1 Answer
cannot implicitly convert type 'int' to 'bool' problem 1 Answer
Need help with AudioSources 1 Answer