Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by zukinet · Aug 18, 2017 at 01:50 PM · c#audiosourceaudioclipmicrophoneaudio listener

Why I am getting Negative Decibels (around -70dB) where Normal Speech around 60dB

Hi all....

I try to monitor sound decibels using this scripts but why i am getting Negative Decibels ?

in theory, sound from Normal Conversation will result 60db and even silent room will make 10-20dB...

any idea ?

Result of Decibels Calculation : alt text

Below are my source :

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class VR_SoundMonitor : MonoBehaviour {
 
 //===>PUBLIC
     public float DecibleValue;
     public float RmsValue; // { get; set; }
     public float PitchValue;// { get; set; }
     private AudioSource audioSourceObject;
 
 //===>PRIVATE
     private float[] _spectrum;
     private float[] _samples;
     private float _fSample;
 
 //===>KONSTANTA
     const int QSamples = 1024;
     const float RefValue = 0.1f;
     const float Threshold = 0.02f;
 
     // Pre Initialization...
     void Awake(){
 
     }
 
     // Use this for initialization
     void Start () {
         audioSourceObject = GetComponent<AudioSource> ();
         InitVariable ();
         InitMicrophone ();
     }
 
     //Nyalakan Microphone
     private void InitMicrophone()
     {
         
 //        audioSourceObject.pitch = 1.0f;
         audioSourceObject.clip = Microphone.Start ("SoundMonitoring", false, 60, 44100);
         while (!(Microphone.GetPosition(null) > 0)) { }    //TODO: untoggle
         audioSourceObject.Play();
 //        audioSourceObject.mute = true;
 
     }
 
     private void InitVariable()
     {
         _samples = new float[QSamples]; //use constanta (3 below)
         _spectrum = new float[QSamples];
         _fSample = AudioSettings.outputSampleRate;
     }
 
     // Update is called once per frame
     void Update () {
         CalculateAnalyzeSound (audioSourceObject);
         Debug.Log ("Decibels="+DecibleValue+" ## RmsVal="+RmsValue);
     }
 
     public void CalculateAnalyzeSound(AudioSource audio)
     {
 //===>VARIABLE...
          float _RmsValue;
          float _DbValue;
          float _PitchValue;
 
 //===>IMPORT DATA...
         audio.GetOutputData(_samples, 0);
         int i = 0;
         float sum = 0;
 
         for (i = 0; i < QSamples; i++)
         {
             sum += _samples[i] * _samples[i]; // sum squared samples
         }
 
 //===>CALCULATE 
         _RmsValue = Mathf.Sqrt(sum / QSamples); // rms = square root of average
         _DbValue = 20 * Mathf.Log10(_RmsValue / RefValue); // calculate dB
         if (_DbValue < -160) _DbValue = -160; // clamp it to -160dB min
             
 //        //(OLD)    GetComponent<AudioSource>().GetSpectrumData(_spectrum, 0, FFTWindow.BlackmanHarris);
 //        audio.GetSpectrumData (_spectrum, 0, FFTWindow.BlackmanHarris);
 //
 //        float maxV = 0;
 //        var maxN = 0;
 //        for (i = 0; i < QSamples; i++)
 //        { // find max 
 //            if (!(_spectrum[i] > maxV) || !(_spectrum[i] > Threshold))
 //                continue;
 //
 //            maxV = _spectrum[i];
 //            maxN = i; // maxN is the index of max
 //        }
 //        float freqN = maxN; // pass the index to a float variable
 //        if (maxN > 0 && maxN < QSamples - 1)
 //        { // interpolate index using neighbours
 //            var dL = _spectrum[maxN - 1] / _spectrum[maxN];
 //            var dR = _spectrum[maxN + 1] / _spectrum[maxN];
 //            freqN += 0.5f * (dR * dR - dL * dL);
 //        }
 //        _PitchValue = freqN * (_fSample / 2) / QSamples; // convert index to frequency
 
 //===>RETURN VALUES (FINAL)
         RmsValue = _RmsValue;
         DecibleValue = _DbValue;
 //        PitchValue = _PitchValue;
     }//end_CoreAnalyzeSound(...)        
 }//end_All


Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by aldonaletto · Aug 21, 2017 at 03:28 AM

Decibels are relative measurement units: 10 times the log10 of output power/reference power, or 20 times log10 of output voltage/reference voltage. Since the samples are directly proportional to the output voltage, I've adopted 20*log10(samplesRMS/refValue). But the value adopted as refValue is totally arbitrary - there's no fixed relationship between sample value and the actual output power because it depends on the hardware, the master volume, the speakers etc.
You can get bigger dB values by simply reducing refValue: if refValue is reduced from 0.1f to 0.01f, for instance, the results will get 20dB higher; if reduced to 0.001f, the results will grow 40dB.

Comment
Add comment · Show 4 · 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 zukinet · Aug 21, 2017 at 05:03 AM 0
Share

Hi @aldonaletto, thanks for replying.

so, there's no way to find accurate value for refValue or what''re average for most device ? How about frequency (Hertz), will be same relative as Decibel ?

avatar image aldonaletto zukinet · Aug 22, 2017 at 07:26 PM 0
Share

No, there's no standard on that subject: the samples are floats that vary from -1.0 to 1.0, but there's no defined relationship between the sample value and the actual sound pressure levels - all that stuff varies wildly among different computers and speaker systems.
And things get even worse when one tries to measure input levels: since many systems have Automatic Gain Control, quiet sounds may be more amplified than the louder ones - without AGC, the system could be calibrated using some reference sound.
EDITED: Different from sound level, frequencies are not modified by the record or play system, thus one can measure them accurately. The routine above just finds the more intense spectrum value and convert it to frequency.

avatar image uditkhiraiya · Jan 27, 2018 at 01:20 PM 0
Share

@aldonaletto Thank you so much for your expertise in this. I have been trying to find the decibel value of an audio input through my $$anonymous$$icrophone but get a value of -160(it shows a value -infinity when clamping is removed). What do you think I must change ? Reducing the RefValue doesn't make a difference.

Hoping for your quick response, Thanks in advance.

avatar image Bunny83 uditkhiraiya · Jan 27, 2018 at 01:45 PM 0
Share

If you get -inf as a result of the log it means your actual rms value is "0". That means compared to the reference rms value your signal is "infinitly more silent". He clamps the value to -160 since that would meen your signal is "0.00000001" times the reference value, so basically just silent.

avatar image
0

Answer by uditkhiraiya · Jan 31, 2018 at 07:24 AM

@Bunny83 Thank you for your response ! Yes, that makes sense. So it turns out that I have to play the audio clip I recorded from the microphone with a delay of at least one second to see some sort of an increase in Db Value. Is there a way I can analyse the microphone input to calculate the Db value without a play back ?

Thanks, Udit Khiraiya.

Comment
Add comment · 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

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.

Follow this Question

Answers Answers and Comments

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

How to record audio in chunks and send through websocket? 0 Answers

AudioClip dont listened in script component 1 Answer

Multiple Microphone Inputs 0 Answers

Help with Null Reference Exception please. 1 Answer

PlayClipAtPoint Qualify with Type Name 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