From 1065a75d1fa6fd825f7b7027594f7d83d6d1d445 Mon Sep 17 00:00:00 2001 From: Rapandrasmus <78219215+Rapandrasmus@users.noreply.github.com> Date: Wed, 4 Oct 2023 17:14:05 +0200 Subject: [PATCH] Giraffe sound now works properly --- .../Games/AnimalAcrobat/GiraffeInput.cs | 44 ++++++------------- Assets/Scripts/Util/Sound.cs | 16 ++++++- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/Assets/Scripts/Games/AnimalAcrobat/GiraffeInput.cs b/Assets/Scripts/Games/AnimalAcrobat/GiraffeInput.cs index af969fd8a..0795359e3 100644 --- a/Assets/Scripts/Games/AnimalAcrobat/GiraffeInput.cs +++ b/Assets/Scripts/Games/AnimalAcrobat/GiraffeInput.cs @@ -54,7 +54,7 @@ namespace HeavenStudio.Games.Scripts_AnimalAcrobat double beat = caller.startBeat + caller.timer; _drumRollSound = SoundByte.PlayOneShotGame("animalAcrobat/giraffeDrumroll", beat, 1, 1, true); - StartCoroutine(GiraffeDrumRollLoop(beat)); + GiraffeDrumRollLoop(beat); MultiSound.Play(new MultiSound.Sound[] { @@ -64,37 +64,21 @@ namespace HeavenStudio.Games.Scripts_AnimalAcrobat }); } - private IEnumerator GiraffeDrumRollLoop(double beat) + private void GiraffeDrumRollLoop(double beat) { - float fadeOutNormalized = 0; - var cond = Conductor.instance; - while (fadeOutNormalized <= 1) + _drumRollSound.LerpVolume(beat, 1, 1, 0.125f); + + BeatAction.New(this, new List() { - fadeOutNormalized = cond.GetPositionFromBeat(beat, 1); - _drumRollSound.SetVolume(Mathf.Lerp(1, 0.125f, fadeOutNormalized)); - yield return null; - } - - float fadeInNormalized = 0; - - while (fadeInNormalized <= 1) - { - fadeInNormalized = cond.GetPositionFromBeat(beat + 1, 3); - _drumRollSound.SetVolume(Mathf.Lerp(0.125f, 1, fadeOutNormalized)); - yield return null; - } - - float fadeOutFinal = 0; - double fadeOutLength = cond.SecsToBeats(0.587, cond.GetBpmAtBeat(beat + 4)); - - while (fadeOutFinal <= 1) - { - fadeOutFinal = cond.GetPositionFromBeat(beat + 4, fadeOutLength); - _drumRollSound.SetVolume(Mathf.Lerp(1, 0, fadeOutNormalized)); - yield return null; - } - _drumRollSound.Stop(); - _drumRollSound = null; + new BeatAction.Action(beat + 1, delegate + { + _drumRollSound.LerpVolume(beat + 1, 3, 0.125f, 1); + }), + new BeatAction.Action(beat + 4, delegate + { + _drumRollSound.KillLoop(0.587); + }) + }); } private void Empty(PlayerActionEvent caller) { } diff --git a/Assets/Scripts/Util/Sound.cs b/Assets/Scripts/Util/Sound.cs index 0201f13e0..09c3b538f 100644 --- a/Assets/Scripts/Util/Sound.cs +++ b/Assets/Scripts/Util/Sound.cs @@ -43,9 +43,21 @@ namespace HeavenStudio.Util { } - public void SetVolume(float volume) + public void LerpVolume(double beat, double length, float volumeStart, float volumeEnd) { - audioSource.volume = volume; + if (!gameObject.activeSelf) return; + StartCoroutine(LerpVolumeCo(beat, length, volumeStart, volumeEnd)); + } + + private IEnumerator LerpVolumeCo(double beat, double length, float volumeStart, float volumeEnd) + { + float normalized = 0; + while (normalized <= 1) + { + normalized = cond.GetPositionFromBeat(beat, length); + audioSource.volume = Mathf.Lerp(volumeStart, volumeEnd, normalized); + yield return null; + } } public void Play()