• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Kokujou · May 10, 2015 at 01:44 PM · c#audiomathfloat

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?

Comment

People who like this

0 Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image xortrox · May 10, 2015 at 02:06 PM 0
Share

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.

avatar image Kokujou · May 10, 2015 at 02:07 PM 0
Share

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

avatar image xortrox · May 10, 2015 at 02:09 PM 0
Share

I think I did something wrong there sorry. I'm not sure how to help except from just googling for an answer.

avatar image Kokujou · May 10, 2015 at 02:15 PM 0
Share

well i tried that >.<

avatar image xortrox · May 10, 2015 at 02:17 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image

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.

Comment
xortrox
Catlard
DMGregory

People who like this

3 Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kokujou · May 10, 2015 at 02:26 PM 0
Share

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...

avatar image Bunny83 · May 10, 2015 at 02:30 PM 0
Share

Keep in mind that you probably want to take the average of a period and not a single sample.

avatar image Kokujou · May 10, 2015 at 02:35 PM 0
Share

yeah i got this in my mind... still... it's so complicated to copy audiosurf... i mean just to build a road with curves is... nearly impossible.. >.<

avatar image Bunny83 · May 10, 2015 at 02:46 PM 0
Share

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.

avatar image Kokujou · May 10, 2015 at 02:48 PM 0
Share

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...

Show more comments
avatar image

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?

Comment
DMGregory

People who like this

-1 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Kokujou · May 10, 2015 at 02:15 PM 0
Share

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!

avatar image Catlard · May 10, 2015 at 02:19 PM 0
Share

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

avatar image Kokujou · May 10, 2015 at 02:21 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Math formula to set volume levels nicely 0 Answers

Distribute terrain in zones 3 Answers

convert AudioClip.GetData values into AudioSource.GetOutputData values 1 Answer

Multiple Cars not working 1 Answer

C# divide float by integer 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges