Giraffe sound now works properly

This commit is contained in:
Rapandrasmus 2023-10-04 17:14:05 +02:00
parent 3047ea74f1
commit 1065a75d1f
2 changed files with 28 additions and 32 deletions

View file

@ -54,7 +54,7 @@ namespace HeavenStudio.Games.Scripts_AnimalAcrobat
double beat = caller.startBeat + caller.timer; double beat = caller.startBeat + caller.timer;
_drumRollSound = SoundByte.PlayOneShotGame("animalAcrobat/giraffeDrumroll", beat, 1, 1, true); _drumRollSound = SoundByte.PlayOneShotGame("animalAcrobat/giraffeDrumroll", beat, 1, 1, true);
StartCoroutine(GiraffeDrumRollLoop(beat)); GiraffeDrumRollLoop(beat);
MultiSound.Play(new MultiSound.Sound[] 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; _drumRollSound.LerpVolume(beat, 1, 1, 0.125f);
var cond = Conductor.instance;
while (fadeOutNormalized <= 1) BeatAction.New(this, new List<BeatAction.Action>()
{ {
fadeOutNormalized = cond.GetPositionFromBeat(beat, 1); new BeatAction.Action(beat + 1, delegate
_drumRollSound.SetVolume(Mathf.Lerp(1, 0.125f, fadeOutNormalized)); {
yield return null; _drumRollSound.LerpVolume(beat + 1, 3, 0.125f, 1);
} }),
new BeatAction.Action(beat + 4, delegate
float fadeInNormalized = 0; {
_drumRollSound.KillLoop(0.587);
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;
} }
private void Empty(PlayerActionEvent caller) { } private void Empty(PlayerActionEvent caller) { }

View file

@ -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() public void Play()