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 );
}
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.
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!
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?