Hi all…
I try to monitor sound decibels using this [scripts][1]
but why i am getting Negative Decibels ?
in [theory][2], sound from Normal Conversation will result 60db and even silent room will make 10-20dB…
any idea ?
Result of Decibels Calculation :
![alt text][3]
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 <em>* _samples*; // sum squared samples*</em>
* }*
//===>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().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 > maxV) || !(spectrum > Threshold))
// continue;
//
// maxV = spectrum*;*
// 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*
*[1]: http://answers.unity3d.com/questions/157940/getoutputdata-and-getspectrumdata-they-represent-t.html*_
*[2]: http://www.noisehelp.com/noise-level-chart.html*_
*[3]: http://i.imgur.com/wrCgUCO.png*_