check current Microphone input volume

Hello! My output decibels are between -160db (quiet) to 0db (close to screaming). Does someone know why?
(They should be posetive, right?)

I copied the “full script” posted by A_munim with just one modification. I removed = new AudioClip(). To fix thoose errors as lilshaque mentioned.

(Unity 2018.3.3f1)

3 Likes

I’m getting a constant -infinty on my MicLoudnessinDecibels.

I checked to make sure the mic was detected and it was.

EDIT: okay so I didn’t have an audio source lol, once I added that I was able to get some db data, however I’m having the same issue as Mogge858 where 0 is the highest number.

ARG EDIT: Right so 0 db is unity gain. Data checks out. Anything above would mean that the data is being amplified. And I call myself an audio engineer…

Hi guys. I’m really sorry to awake a dead post but i’m stuck. I’ve tried everything and i can’t find the issue. I CAN’T get consistent results for my volume. Sometimes a whisper gives me a higher result than a shout and vice versa. Please can anybody help me?!

private void Update()
    {
        micInputVolume = LevelMax();
    }

  

    float LevelMax()
    {
        int _sampleWindow = 128;
        float levelMax = 0;
        float[] waveData = new float[_sampleWindow];
        int micPosition = Microphone.GetPosition(null) - (_sampleWindow + 1); // null means the first microphone
        if (micPosition < 0) return 0;
        microphoneInput.GetData(waveData, micPosition);

        // Getting a peak on the last 128 samples
        for (int i = 0; i < _sampleWindow; i++)
        {
            float wavePeak = waveData[i] * waveData[i];
            if (levelMax < wavePeak)
            {
                levelMax = wavePeak;
            }
        }
        return levelMax;
    }

Is it possibly to do with the fact that in decibels a whisper can often be louder that a raised voice. Not sure it it’s true but I read somewhere a babys scream is intended for high decibel range to get attention. Also a whisper can sometimes be louder than a seargent major whaling at his platoon. It might not carry RANGE but in decibels a whisper is greater. Correct me if this is wrong. Cheers

Can someone kindly explain how to get the audio playback from this mic input script. I know there might be delay but that might be to my advantage as Im using the script to action lipsync on a character using blendshapes based on volume levels. Reason I want the audio playback is to change the pitch to a cartoon voice. The delay in playback will likely sync up with the small delay in mouth actions from the volume inputs. Many Thanks!

Just stumbled in here, but loudness is a very complicated thing because it’s time and delta based. I think what you want is to calculate the RMS of the audio then convert that to dbs. Specify a period (length)

   public static float ComputeRMS( float[] buffer, int offset, ref int length )
    {
        // sum of squares
        float sos = 0f;
        float val;
        if( offset + length > buffer.Length )
        {
            length = buffer.Length - offset;
        }
        for( int i = 0; i < length; i++ )
        {
            val = buffer[ offset ];
            sos += val * val;
            offset ++;
        }
        // return sqrt of average
        return Mathf.Sqrt( sos / length );
    }
    public static float ComputeDB( float[] buffer, int offset, ref int length )
    {
        float rms;
        rms = ComputeRMS( buffer, offset, ref length );
        // could divide rms by reference power, simplified version here with ref power of 1f.
        // will return negative values: 0db is the maximum.
        return 10 * Mathf.Log10( rms );
    }
1 Like

in light of the example further up which has _sampleWindow - what would offset and length be here @nykwil ?

The rms is calculated against a certain chunk of time. length is the number of samples and offset is the time into the buffer that it starts calculating it.

1 Like

I followed the same step as mentioned by @Mogge858 , but was still getting a constant -infinity for MicLoudnessinDecibels. I also tried adding an audio source to the GameManager with the script as mentioned by @AmarBagh ; however, the MicLoudnessinDecibels remained as -infinity. Does anyone know how to resolve this problem? For example, do I need to change anything in the code or tune the properties of audio source? Thanks!

1 Like

also having the same problem as awinder

As @nykwil noted above, you want to use RMS and then convert to dBs. This old post has a great explanation and the code still works. It even lets you tweak the level so that you can get dBs that make sense (i.e., a whisper at around 20). https://answers.unity.com/questions/157940/getoutputdata-and-getspectrumdata-they-represent-t.html?childToView=158800#answer-158800

@awinder i am facing the same issue too if you have found out the solution can you share it

i am facing a issue where my MicLoudnessinDecibels reamins - infinity

yeah thanks guys! I’m almost there.
@nykwil Yes the RMS would make a lot of sense. But I don’t know how to call the two methods:
ComputeRMS(…) and ComputeDB(…)
and what to put into the first two arguments?

To kind of RMS I helped myself with MathF.smoothdamp.

I get this error from the microphone :frowning: on the .GetData() line

./Modules/Audio/Public/sound/SoundManager.cpp(716) : Error executing result = instance->m_Sound->lock(offsetBytes, lengthBytes, &ptr1, &ptr2, &len1, &len2) (An invalid parameter was passed to this function. )
UnityEngine.AudioClip:GetData (single[ ],int)