Skip to content

Commit

Permalink
Fix a bug for errorly calculating RMS since buffer format changed
Browse files Browse the repository at this point in the history
  • Loading branch information
Juniverse committed Jun 25, 2024
1 parent 68e8d26 commit a3cee24
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions Runtime/Scripts/Audio/AudioCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ public bool AutoDetectPlayerSpeaking
public bool IsRecording
{
get => m_IsRecording || Input.GetKey(m_PushToTalkKey);
set => m_IsRecording = value;
set
{
m_IsRecording = value;
if (m_IsRecording)
m_ProcessedWaveData.Clear();
}
}
/// <summary>
/// Signifies if user is speaking based on audio amplitude and threshold.
Expand Down Expand Up @@ -629,11 +634,15 @@ protected virtual byte[] Output(int nSize)
// Root Mean Square, used to measure the variation of the noise.
protected float CalculateRMS()
{
// List<short> inputTmp = new List<short>();
// inputTmp.AddRange(m_InputBuffer);
int nCount = m_InputBuffer.Count > 0 ? m_InputBuffer.Count : 1;
long nMaxSample = m_InputBuffer.Aggregate<short, long>(0, (current, sample) => current + sample * sample);
return Mathf.Sqrt(nMaxSample / (float)nCount);
if (m_RawInput == null || m_RawInput.Length == 0)
return 0;
int nCount = m_RawInput.Length > 0 ? m_RawInput.Length : 1;
double nMaxSample = 0;
foreach (float sample in m_RawInput)
{
nMaxSample += (sample * sample);
}
return Mathf.Sqrt((float)nMaxSample / nCount);
}
// Sound Noise Ratio (dB). Used to check how loud the input voice is.
protected float CalculateSNR()
Expand Down

0 comments on commit a3cee24

Please sign in to comment.