From 1a90ba28d9dc966747b2ce75a871b3c7bee464df Mon Sep 17 00:00:00 2001 From: Jenny Crowe Date: Wed, 9 Mar 2022 20:59:48 -0700 Subject: [PATCH] Jukebox/Sound code cleanup --- .../Games/BuiltToScaleDS/BuiltToScaleDS.cs | 2 +- .../Scripts/Games/RhythmTweezers/LongHair.cs | 2 +- Assets/Scripts/Util/Jukebox.cs | 27 ++++++------------- Assets/Scripts/Util/Sound.cs | 11 ++++++++ 4 files changed, 21 insertions(+), 21 deletions(-) diff --git a/Assets/Scripts/Games/BuiltToScaleDS/BuiltToScaleDS.cs b/Assets/Scripts/Games/BuiltToScaleDS/BuiltToScaleDS.cs index 099eea22..7a1b5838 100644 --- a/Assets/Scripts/Games/BuiltToScaleDS/BuiltToScaleDS.cs +++ b/Assets/Scripts/Games/BuiltToScaleDS/BuiltToScaleDS.cs @@ -190,7 +190,7 @@ namespace RhythmHeavenMania.Games.BuiltToScaleDS var pianoPitch = Mathf.Pow(2f, (1f / 12f) * semiTones) *Conductor.instance.musicSource.pitch; var pianoSource = Jukebox.PlayOneShotGame("builtToScaleDS/Piano", -1, pianoPitch, 0.8f, true); - Jukebox.SetLoopParams(pianoSource, beat + length, 0.1f); + pianoSource.SetLoopParams(beat + length, 0.1f); } } } \ No newline at end of file diff --git a/Assets/Scripts/Games/RhythmTweezers/LongHair.cs b/Assets/Scripts/Games/RhythmTweezers/LongHair.cs index f201c07a..ee7ac149 100644 --- a/Assets/Scripts/Games/RhythmTweezers/LongHair.cs +++ b/Assets/Scripts/Games/RhythmTweezers/LongHair.cs @@ -19,7 +19,7 @@ namespace RhythmHeavenMania.Games.RhythmTweezers public GameObject holder; public GameObject loop; - private AudioSource pullSound; + private Sound pullSound; private void Awake() { diff --git a/Assets/Scripts/Util/Jukebox.cs b/Assets/Scripts/Util/Jukebox.cs index 09d0c5e2..77da9c19 100644 --- a/Assets/Scripts/Util/Jukebox.cs +++ b/Assets/Scripts/Util/Jukebox.cs @@ -40,7 +40,7 @@ namespace RhythmHeavenMania.Util FindJukebox().GetComponent().volume = volume; } - public static AudioSource PlayOneShot(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false) + public static Sound PlayOneShot(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false) { GameObject oneShot = new GameObject("oneShot"); @@ -59,10 +59,10 @@ namespace RhythmHeavenMania.Util GameManager.instance.SoundObjects.Add(oneShot); - return audioSource; + return snd; } - public static AudioSource PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false) + public static Sound PlayOneShotScheduled(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false) { GameObject oneShot = new GameObject("oneShotScheduled"); @@ -84,10 +84,10 @@ namespace RhythmHeavenMania.Util GameManager.instance.SoundObjects.Add(oneShot); - return audioSource; + return snd; } - public static AudioSource PlayOneShotGame(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false) + public static Sound PlayOneShotGame(string name, float beat = -1, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false) { if (GameManager.instance.currentGame == name.Split('/')[0] || forcePlay) { @@ -97,7 +97,7 @@ namespace RhythmHeavenMania.Util return null; } - public static AudioSource PlayOneShotScheduledGame(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false) + public static Sound PlayOneShotScheduledGame(string name, double targetTime, float pitch = 1f, float volume = 1f, bool looping = false, bool forcePlay = false) { if (GameManager.instance.currentGame == name.Split('/')[0] || forcePlay) { @@ -107,24 +107,13 @@ namespace RhythmHeavenMania.Util return null; } - // Loops play forever by default unless you set their params via this method. - public static void SetLoopParams(AudioSource source, float endBeat, float fadeTime) - { - if (source == null) - return; - - var snd = source.GetComponent(); - snd.loopEndBeat = endBeat; - snd.fadeTime = fadeTime; - } - - public static void KillLoop(AudioSource source, float fadeTime) + public static void KillLoop(Sound source, float fadeTime) { // Safeguard against previously-destroyed sounds. if (source == null) return; - source.GetComponent().KillLoop(fadeTime); + source.KillLoop(fadeTime); } } diff --git a/Assets/Scripts/Util/Sound.cs b/Assets/Scripts/Util/Sound.cs index 6094cfee..6700e71b 100644 --- a/Assets/Scripts/Util/Sound.cs +++ b/Assets/Scripts/Util/Sound.cs @@ -99,6 +99,17 @@ namespace RhythmHeavenMania.Util } } + public void SetLoopParams(float endBeat, float fadeTime) + { + loopEndBeat = endBeat; + this.fadeTime = fadeTime; + } + + public void Stop() + { + audioSource.Stop(); + } + public void Delete() { GameManager.instance.SoundObjects.Remove(gameObject);