From cdbd1c9b7d085f3ea174f9ba5ede1161ad3c11da Mon Sep 17 00:00:00 2001 From: minenice55 Date: Tue, 21 Feb 2023 11:26:08 -0500 Subject: [PATCH] Expose Song Pitch to Minigames (#301) * expose song pitch to minigames * clean up new additions * make new functions actually set music pitch * fix playback speed slider bug --- Assets/Scripts/Conductor.cs | 24 ++++++++++++++----- .../Scripts/LevelEditor/Timeline/Timeline.cs | 6 +++-- Assets/Scripts/Util/Sound.cs | 6 ++--- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Assets/Scripts/Conductor.cs b/Assets/Scripts/Conductor.cs index fa3898027..1e03946af 100644 --- a/Assets/Scripts/Conductor.cs +++ b/Assets/Scripts/Conductor.cs @@ -17,7 +17,7 @@ namespace HeavenStudio public float secPerBeat; // The number of seconds for each song beat, inversely scaled to song pitch (higer pitch = shorter time) - public float pitchedSecPerBeat => (secPerBeat / musicSource.pitch); + public float pitchedSecPerBeat => (secPerBeat / SongPitch); // Current song position, in seconds private double songPos; // for Conductor use only @@ -64,11 +64,23 @@ namespace HeavenStudio public bool metronome = false; Util.Sound metronomeSound; - public float timeSinceLastTempoChange = Single.MinValue; + // pitch values + private float timelinePitch = 1f; + private float minigamePitch = 1f; + public float SongPitch { get => timelinePitch * minigamePitch; } - private bool beat; + public void SetTimelinePitch(float pitch) + { + timelinePitch = pitch; + musicSource.pitch = SongPitch; + } - // private AudioDspTimeKeeper timeKeeper; + public void SetMinigamePitch(float pitch) + { + minigamePitch = pitch; + musicSource.pitch = SongPitch; + } + void Awake() { @@ -130,7 +142,7 @@ namespace HeavenStudio if (musicStartTime < 0f) { musicSource.time = (float) startPos; - musicSource.PlayScheduled(AudioSettings.dspTime - firstBeatOffset / musicSource.pitch); + musicSource.PlayScheduled(AudioSettings.dspTime - firstBeatOffset / SongPitch); } else { @@ -185,7 +197,7 @@ namespace HeavenStudio if (isPlaying) { double absTime = Time.realtimeSinceStartupAsDouble; - double dt = (absTime - lastAbsTime) * musicSource.pitch; + double dt = (absTime - lastAbsTime) * SongPitch; lastAbsTime = absTime; time += dt; diff --git a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs index 79d240c0c..87e07d124 100644 --- a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs +++ b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs @@ -900,11 +900,12 @@ namespace HeavenStudio.Editor.Track return LayersRect.rect.height / 5f; } + const float SpeedSnap = 0.25f; public void SetPlaybackSpeed(float speed) { - float spd = Mathp.Round2Nearest(speed, Timeline.SnapInterval()); + float spd = Mathp.Round2Nearest(speed, SpeedSnap); PlaybackSpeed.transform.GetChild(3).GetComponent().text = $"Playback Speed: {spd}x"; - Conductor.instance.musicSource.pitch = spd; + Conductor.instance.SetTimelinePitch(spd); PlaybackSpeed.value = spd; } @@ -914,6 +915,7 @@ namespace HeavenStudio.Editor.Track { PlaybackSpeed.transform.GetChild(3).GetComponent().text = $"Playback Speed: 1x"; PlaybackSpeed.value = 1f; + Conductor.instance.SetTimelinePitch(PlaybackSpeed.value); } } diff --git a/Assets/Scripts/Util/Sound.cs b/Assets/Scripts/Util/Sound.cs index 1c4a2f970..22ba0edbd 100644 --- a/Assets/Scripts/Util/Sound.cs +++ b/Assets/Scripts/Util/Sound.cs @@ -51,7 +51,7 @@ namespace HeavenStudio.Util else { playInstant = false; - scheduledPitch = cnd.musicSource.pitch; + scheduledPitch = cnd.SongPitch; startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch); audioSource.PlayScheduled(startTime); } @@ -79,9 +79,9 @@ namespace HeavenStudio.Util } else { - if (!played && scheduledPitch != cnd.musicSource.pitch) + if (!played && scheduledPitch != cnd.SongPitch) { - scheduledPitch = cnd.musicSource.pitch; + scheduledPitch = cnd.SongPitch; startTime = (AudioSettings.dspTime + (cnd.GetSongPosFromBeat(beat) - cnd.songPositionAsDouble)/(double)scheduledPitch); audioSource.SetScheduledStartTime(startTime); }