- Home /
Unity Float to db
hi! As you all know, i hope, Unity has a function where you can read the ... let's call it "volume" of every sample of an audio file. the problem is: its given in float -1 to +1. I found some sites where they convert float to db. But only 0 to +1 floats. what to do with negative floats? Is there any "unity-compatible" conversion from float to db?
I guess if you want to convert a range of "-1 to 1" into a range of 0 to 1 you would do this:
(sampleVolume + 1.0f) * 0.5f
To convert back again you would do:
db * 2.0f - 1.0f
This would give you the 0 to 1 range but I guess there would be some amount of loss by doing this.
Let me know if I got your question the wrong way.
are you sure this is correct? i mean there must be a reason why the value is negative float value. i wonder if its really correct to convert it this way
I think I did something wrong there sorry. I'm not sure how to help except from just googling for an answer.
http://answers.unity3d.com/questions/283192/how-to-convert-decibel-number-to-audio-source-volu.html
found this by googling "volume to decibel" I don't know if it will help.
Answer by Bunny83 · May 10, 2015 at 02:22 PM
The data you get from Unity are PCM samples which are by nature positive and negative. In a wave file they are stored as an unsigned integer but represent the same information, just shifted into the positive range (like xortrox mentioned above).
Not sure why you want it in decibel but the conversion for volume is usually:
float db = 20f * Mathf.Log10(amplidude);
amplitude should be between 0 and 1. You get that by taking the absolute value of each sample (Mathf.Abs()).
Note that decibel is just a relation compared to a reference value. Depending on the usage a db value could mean totally different things. See Decibel.
so its really this simple huh... okay than i'll try this maybe it works this time ^_^ i'm trying to create a script that builds a road via an audio file like that game "Audiosurf" does...
$$anonymous$$eep in $$anonymous$$d that you probably want to take the average of a period and not a single sample.
yeah i got this in my $$anonymous$$d... still... it's so complicated to copy audiosurf... i mean just to build a road with curves is... nearly impossible.. >.<
Well, how you actually approach this? I never played audiosurf (but i've seen it). If the track is actually derived from the audio you probably want to measure the average volume in equal intervals and use the derivative (change between two measured levels) as indication if the track should go up or down.
maybe... well but a short trip back to the db Problem... i analyzed an audio file with unity... but the samples where NOTHING is playing so ... a pause. is -6db. while other stuff is -7db... -11db... what the hell is going on there...
Answer by Catlard · May 10, 2015 at 02:09 PM
I think you're misunderstanding the meaning of "float". A float is a kind of number the computer recognizes that takes up a certain amount of memory in the computer, where as db is just a name for the number. I assume that however they measure the decibels in an audio file at one time, it is stored either as a float, or something else that can handle decimals. The name of a VARIABLE is probably db, but the variable is an object the computer calls a "float". Does that make sense?
however the assigned range, unity gives is from -1 to +1. thats definitly not the value in db! maybe its a factor or something i don't know. whether or not i need a conversion from the unity values into db values!
Does it matter what they've done to the values, as long as you can convert them back to the same range using algebra? Clearly 0 must correspond to -1 in this situation, right?
Also, I have seen some decibel values on stereos as negative numbers. Just saying.
http://www.animations.physics.unsw.edu.au/jw/images/dB_files/LI.gif
i don't know thats why i'm asking! there must be a reason why unity is using negative numbers. if they just want a higher range they could've taken 0 to +2 but why -1 to +1?
Your answer
![](https://koobas.hobune.stream/wayback/20220613195247im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Math formula to set volume levels nicely 0 Answers
Multiple Cars not working 1 Answer
C# divide float by integer 2 Answers
Why can't I add to a float? 1 Answer
convert AudioClip.GetData values into AudioSource.GetOutputData values 1 Answer