Skip to content

Commit

Permalink
INTG-1769 Add VAD Dll solution
Browse files Browse the repository at this point in the history
  • Loading branch information
Juniverse committed Jun 21, 2024
1 parent f65a489 commit fd297fd
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 26 deletions.
2 changes: 1 addition & 1 deletion Runtime/Data/UserSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 146eba7038a14426a7d95a5b80da5291, type: 3}
m_Name: UserSettings
m_EditorClassIdentifier:
m_PlayerName:
m_PlayerName: Player
m_PlayerData:
- fieldId:
fieldValue:
Expand Down
8 changes: 6 additions & 2 deletions Runtime/Scenes/Sample2D.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -245,6 +245,10 @@ PrefabInstance:
serializedVersion: 3
m_TransformParent: {fileID: 0}
m_Modifications:
- target: {fileID: -4074574829800160846, guid: 3a0885e96397c414f80c73ab7dc0c72d, type: 3}
propertyPath: m_SceneFullName
value: workspaces/unitytest/scenes/innequin_gallery
objectReference: {fileID: 0}
- target: {fileID: 2395919966041133306, guid: 3a0885e96397c414f80c73ab7dc0c72d, type: 3}
propertyPath: m_AutoStart
value: 0
Expand Down Expand Up @@ -365,7 +369,7 @@ PrefabInstance:
objectReference: {fileID: 0}
- target: {fileID: 162201157703380547, guid: d3611a2c36d56be4dbb74eaf7b5de58e, type: 3}
propertyPath: m_AnchoredPosition.y
value: -729.576
value: -655.3221
objectReference: {fileID: 0}
- target: {fileID: 401423953070606001, guid: d3611a2c36d56be4dbb74eaf7b5de58e, type: 3}
propertyPath: m_Name
Expand Down
52 changes: 29 additions & 23 deletions Runtime/Scripts/Audio/AudioCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class AudioCapture : MonoBehaviour
protected readonly ConcurrentQueue<AudioChunk> m_AudioToPush = new ConcurrentQueue<AudioChunk>();
protected List<AudioDevice> m_Devices = new List<AudioDevice>();
protected List<short> m_InputBuffer = new List<short>();
protected float[] m_RawInput;
protected List<short> m_ProcessedWaveData = new List<short>();
static int m_nPosition;
#if UNITY_WEBGL
Expand Down Expand Up @@ -249,6 +250,11 @@ public List<AudioDevice> Devices
/// Get if aec is enabled. The parent class by default is false.
/// </summary>
public virtual bool EnableAEC => false;

/// <summary>
/// Get if VAD is enabled. The parent class by default is false.
/// </summary>
public virtual bool EnableVAD => false;
#endregion

#if UNITY_WEBGL && !UNITY_EDITOR
Expand Down Expand Up @@ -417,17 +423,20 @@ protected virtual IEnumerator _Calibrate()
StartMicrophone(m_DeviceName);
#endif
Event.onStartCalibrating?.Invoke();
Recording.volume = 0.5f;
while (m_BackgroundNoise == 0 || m_CalibratingTime < m_BufferSeconds)
if (!EnableVAD) // Use local method to calculate SNR.
{
int nSize = GetAudioData();
m_CalibratingTime += 0.1f;
yield return new WaitForSecondsRealtime(0.1f);
float rms = CalculateRMS();
if (rms > m_BackgroundNoise)
m_BackgroundNoise = rms;
Recording.volume = 0.5f;
while (m_BackgroundNoise == 0 || m_CalibratingTime < m_BufferSeconds)
{
int nSize = GetAudioData();
m_CalibratingTime += 0.1f;
yield return new WaitForSecondsRealtime(0.1f);
float rms = CalculateRMS();
if (rms > m_BackgroundNoise)
m_BackgroundNoise = rms;
}
Recording.volume = 1f;
}
Recording.volume = 1f;
Event.onStopCalibrating?.Invoke();
}
/// <summary>
Expand Down Expand Up @@ -486,13 +495,13 @@ protected virtual void Collect()
{
if (m_SamplingMode == MicSampleMode.NO_MIC)
return;
if (m_SamplingMode != MicSampleMode.PUSH_TO_TALK && m_BackgroundNoise == 0)
if (m_SamplingMode != MicSampleMode.PUSH_TO_TALK && !EnableVAD && m_BackgroundNoise == 0)
return;
int nSize = GetAudioData();
if (nSize <= 0)
return;
IsPlayerSpeaking = true;
IsCapturing = IsRecording || AutoDetectPlayerSpeaking && IsPlayerSpeaking;
IsPlayerSpeaking = DetectPlayerSpeaking();
IsCapturing = IsRecording || IsPlayerSpeaking;
if (IsCapturing)
{
string charName = InworldController.CharacterHandler.CurrentCharacter ? InworldController.CharacterHandler.CurrentCharacter.BrainName : "";
Expand All @@ -504,9 +513,10 @@ protected virtual void Collect()
chunk = audioData,
targetName = charName
});

}
}
protected virtual bool DetectPlayerSpeaking() => AutoDetectPlayerSpeaking;

protected virtual IEnumerator OutputData()
{
if (InworldController.Client && InworldController.Client.Status == InworldConnectionStatus.Connected)
Expand Down Expand Up @@ -534,12 +544,12 @@ protected int GetAudioData()
if (!WebGLGetAudioData(m_LastPosition))
return -1;
#else
float[] buffer = new float[nSize];
m_RawInput = new float[nSize];
if (!Recording.clip)
return -1;
#endif
Recording.clip.GetData(buffer, m_LastPosition);
PreProcessAudioData(ref m_InputBuffer, buffer, 1);
Recording.clip.GetData(m_RawInput, m_LastPosition);
PreProcessAudioData(ref m_InputBuffer, m_RawInput, 1);
m_LastPosition = m_nPosition % m_BufferSize;
return nSize;
}
Expand Down Expand Up @@ -619,14 +629,10 @@ 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);
long nMaxSample = 0;
// List<short> inputTmp = new List<short>();
// inputTmp.AddRange(m_InputBuffer);
int nCount = m_InputBuffer.Count > 0 ? m_InputBuffer.Count : 1;
foreach (var sample in inputTmp)
{
nMaxSample += sample * sample;
}
long nMaxSample = m_InputBuffer.Aggregate<short, long>(0, (current, sample) => current + sample * sample);
return Mathf.Sqrt(nMaxSample / (float)nCount);
}
// Sound Noise Ratio (dB). Used to check how loud the input voice is.
Expand Down

0 comments on commit fd297fd

Please sign in to comment.