Properly queue critical VO clips

This commit is contained in:
journaliciouz
2025-10-30 14:17:47 +01:00
parent 64da60dadd
commit e81879959e
10 changed files with 280 additions and 259 deletions

View File

@@ -35,6 +35,7 @@ public class AudioManager : MonoBehaviour, IPausable
public List<AppleAudioSource> SFXSources;
private IAudioEventSource _eventSource;
private bool wasInterrupted;
/// <summary>
/// Singleton instance of the AudioManager.
@@ -141,33 +142,61 @@ public class AudioManager : MonoBehaviour, IPausable
/// </summary>
public bool RequestPlayVO(AppleAudioSource requestedAudioSource)
{
//Debug.Log($"[AUDIOMANAGER] CurrentVO source prio: {currentlyPlayingVO.sourcePriority}, clip prio: {currentlyPlayingVO.clipPriority} requested VO prio: {requestedAudioSource.sourcePriority}, clip prio: {clipPriority}");
// If nothing is playing, let the requested audio source play
if (currentlyPlayingVO == null)
{
currentlyPlayingVO = requestedAudioSource;
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
RegisterStartStopEvents(requestedAudioSource.audioSource);
SetupNewAudioSource(requestedAudioSource);
Debug.Log($"[AUDIOMANAGER] Playing {currentlyPlayingVO.name} as nothing is currently playing.");
return true;
}
if(currentlyPlayingVO.audioSourceType != AppleAudioSource.AudioSourceType.CriticalVO)
// If the requested audio source is the same, interrupt and trigger it again
if (currentlyPlayingVO == requestedAudioSource)
{
InterruptAudioSource(requestedAudioSource);
SetupNewAudioSource(requestedAudioSource);
Debug.Log($"[AUDIOMANAGER] {currentlyPlayingVO.name} is the same as {requestedAudioSource.name}. Triggering it again.");
return true;
}
// if the currently playing audio source is not critical, interrupt it and play the requested audio source
if (currentlyPlayingVO.audioSourceType != AppleAudioSource.AudioSourceType.CriticalVO)
{
InterruptAudioSource(requestedAudioSource);
SetupNewAudioSource(requestedAudioSource);
Debug.Log($"[AUDIOMANAGER] {currentlyPlayingVO.name} is not critical. Playing {requestedAudioSource.name} instead because it is critical.");
return true;
}
// If the requested audio source has the same priority as currently playing source, check the priority of the requested clip
if (currentlyPlayingVO.audioSourceType == AppleAudioSource.AudioSourceType.CriticalVO && currentlyPlayingVO.sourcePriority == requestedAudioSource.sourcePriority)
{
if (currentlyPlayingVO.clipPriority > requestedAudioSource.clipPriority)
{
InterruptAudioSource(requestedAudioSource);
SetupNewAudioSource(requestedAudioSource);
Debug.Log($"[AUDIOMANAGER] Interrupted {currentlyPlayingVO.name} because it has same priority as {requestedAudioSource.name} but the requested clip has higher priority");
return true;
}
else
{
return false;
}
}
// If the requested audio source has higher priority than the currently playing source, interrupt the current source and let the requested one play
if (currentlyPlayingVO.audioSourceType == AppleAudioSource.AudioSourceType.CriticalVO && currentlyPlayingVO.sourcePriority > requestedAudioSource.sourcePriority)
{
currentlyPlayingVO.InterruptAudio(requestedAudioSource.name);
currentlyPlayingVO = requestedAudioSource;
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
RegisterStartStopEvents(requestedAudioSource.audioSource);
return true;
}
if (currentlyPlayingVO.audioSourceType == AppleAudioSource.AudioSourceType.CriticalVO && currentlyPlayingVO.priority > requestedAudioSource.priority)
{
currentlyPlayingVO.InterruptAudio(requestedAudioSource.name);
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
currentlyPlayingVO = requestedAudioSource;
RegisterStartStopEvents(requestedAudioSource.audioSource);
Debug.Log($"[AUDIOMANAGER] Interrupted {currentlyPlayingVO.name} because {requestedAudioSource.name} has higher priority");
InterruptAudioSource(requestedAudioSource);
SetupNewAudioSource(requestedAudioSource);
return true;
}
// If the requested audio source didn't clear any of the above cases, tell it to get rekt.
else
{
Debug.Log($"CurrentVO prio: {currentlyPlayingVO.priority} requested VO prio: {requestedAudioSource.priority}");
{
Debug.Log($"[AUDIOMANAGER] {currentlyPlayingVO.name} is still playing. {requestedAudioSource.name} has lower priority");
return false;
}
}
@@ -177,15 +206,16 @@ public class AudioManager : MonoBehaviour, IPausable
// TODO: Release the handles safely ReleaseAllHandles();
}
private void RegisterStartStopEvents(AudioSource audioSource)
private void SetupNewAudioSource(AppleAudioSource audioSource)
{
if (audioSource.resource == null)
if (audioSource.audioSource.resource == null)
{
Debug.Log($"AppleAudioSource {audioSource.name} could not register Start and Stop events.");
Debug.Log($"[AUDIOMANAGER] Faled to setup {audioSource.name}. Invalid resource");
}
else
{
_eventSource = audioSource.RequestEventHandlers();
currentlyPlayingVO = audioSource;
_eventSource = audioSource.audioSource.RequestEventHandlers();
_eventSource.AudioStopped += OnAudioStopped;
_eventSource.AudioStarted += OnAudioStarted;
}
@@ -193,14 +223,34 @@ public class AudioManager : MonoBehaviour, IPausable
private void OnAudioStopped(object sender, EventArgs e)
{
currentlyPlayingVO = null;
_eventSource.AudioStopped -= OnAudioStopped;
_eventSource.AudioStarted -= OnAudioStarted;
if (wasInterrupted)
{
ResetAudioSource();
}
else
{
currentlyPlayingVO = null;
ResetAudioSource();
}
}
private void OnAudioStarted(object sender, EventArgs e)
{
}
private void ResetAudioSource()
{
_eventSource.AudioStopped -= OnAudioStopped;
_eventSource.AudioStarted -= OnAudioStarted;
wasInterrupted = false;
}
private void InterruptAudioSource(AppleAudioSource newAudioSource)
{
wasInterrupted = true;
currentlyPlayingVO.InterruptAudio(newAudioSource.name);
ResetAudioSource();
currentlyPlayingVO = newAudioSource;
}
}