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
This commit is contained in:
minenice55 2023-02-21 11:26:08 -05:00 committed by GitHub
parent f413ce9f98
commit cdbd1c9b7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 11 deletions

View file

@ -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;
}
public void SetMinigamePitch(float pitch)
{
minigamePitch = pitch;
musicSource.pitch = SongPitch;
}
// private AudioDspTimeKeeper timeKeeper;
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;

View file

@ -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<TMP_Text>().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<TMP_Text>().text = $"Playback Speed: 1x";
PlaybackSpeed.value = 1f;
Conductor.instance.SetTimelinePitch(PlaybackSpeed.value);
}
}

View file

@ -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);
}