Skip to content

Commit

Permalink
Add timer for preventing freq start/stop audio
Browse files Browse the repository at this point in the history
  • Loading branch information
Juniverse committed Jun 27, 2024
1 parent a3cee24 commit d945523
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Runtime/Scripts/Audio/AudioCapture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class AudioCapture : MonoBehaviour
[SerializeField] protected int m_AudioToPushCapacity = 100;
[SerializeField] protected string m_DeviceName;
[SerializeField] protected bool m_DetectPlayerSpeaking = true;
[Range(0.1f, 1f)] [SerializeField] protected float m_SwitchingAudioTimer = 0.5f;
[Space(10)]
[SerializeField] protected AudioEvent m_AudioEvent;

Expand All @@ -55,6 +56,7 @@ public class AudioCapture : MonoBehaviour
protected bool m_IsCapturing;
protected float m_BackgroundNoise;
protected float m_CalibratingTime;
protected float m_CurrentAudioSwitchingTimer;
// Last known position in AudioClip buffer.
protected int m_LastPosition;
// Size of audioclip used to collect information, need to be big enough to keep up with collect.
Expand Down Expand Up @@ -87,6 +89,7 @@ public AudioSource Recording
return m_RecordingSource;
}
}

/// <summary>
/// Gets the event handler of AudioCapture.
/// </summary>
Expand Down Expand Up @@ -194,6 +197,11 @@ protected set
Event.onPlayerStopSpeaking?.Invoke();
}
}

/// <summary>
/// Gets if is switching audio.
/// </summary>
public bool IsSwitchingAudio => m_CurrentAudioSwitchingTimer != 0;
/// <summary>
/// Signifies it's currently capturing.
/// </summary>
Expand All @@ -202,9 +210,12 @@ public bool IsCapturing
get => m_IsCapturing;
set
{
if (IsSwitchingAudio)
return;
if (m_IsCapturing == value)
return;
m_IsCapturing = value;
m_CurrentAudioSwitchingTimer = m_SwitchingAudioTimer;
if (m_IsCapturing)
{
Event.onRecordingStart?.Invoke();
Expand Down Expand Up @@ -398,14 +409,15 @@ protected virtual void OnDestroy()
}
protected void Update()
{
TimerCountDown();
if (m_AudioToPush.Count > m_AudioToPushCapacity)
m_AudioToPush.TryDequeue(out AudioChunk chunk);
}

#endregion

#region Protected Functions

protected virtual void Init()
{
AudioConfiguration audioSetting = AudioSettings.GetConfiguration();
Expand Down Expand Up @@ -444,6 +456,12 @@ protected virtual IEnumerator _Calibrate()
}
Event.onStopCalibrating?.Invoke();
}

protected virtual void TimerCountDown()
{
m_CurrentAudioSwitchingTimer -= Time.deltaTime;
m_CurrentAudioSwitchingTimer = m_CurrentAudioSwitchingTimer < 0 ? 0 : m_CurrentAudioSwitchingTimer;
}
/// <summary>
/// Resample all the incoming audio data to the Inworld server supported data (16000 * 1).
/// </summary>
Expand Down

0 comments on commit d945523

Please sign in to comment.