diff --git a/Assets/Scripts/Conductor.cs b/Assets/Scripts/Conductor.cs index 3c7a81897..8cf07b7f9 100644 --- a/Assets/Scripts/Conductor.cs +++ b/Assets/Scripts/Conductor.cs @@ -1,7 +1,6 @@ -using System.Collections; +using System; using System.Collections.Generic; using UnityEngine; -using UnityEngine.Audio; using Starpelly; @@ -21,15 +20,15 @@ namespace HeavenStudio public float pitchedSecPerBeat => (secPerBeat / musicSource.pitch); // Current song position, in seconds - private float songPos; // for Conductor use only - public float songPosition; + private double songPos; // for Conductor use only + public float songPosition => (float) songPos; // Current song position, in beats - private float songPosBeat; // for Conductor use only - public float songPositionInBeats; + private double songPosBeat; // for Conductor use only + public float songPositionInBeats => (float) songPosBeat; // Current time of the song - private float time; + private double time; double lastAbsTime; @@ -54,7 +53,7 @@ namespace HeavenStudio // Metronome tick sound enabled public bool metronome = false; - public float timeSinceLastTempoChange = 0; + public float timeSinceLastTempoChange = Single.MinValue; private bool beat; @@ -67,7 +66,7 @@ namespace HeavenStudio public void SetBeat(float beat) { - float secFromBeat = GetSongPosFromBeat(beat); + float secFromBeat = (float) GetSongPosFromBeat(beat); if (musicSource.clip != null) { @@ -79,7 +78,6 @@ namespace HeavenStudio GameManager.instance.SetCurrentEventToClosest(beat); songPosBeat = beat; - songPositionInBeats = songPosBeat; } public void Play(float beat) @@ -119,12 +117,12 @@ namespace HeavenStudio if (musicStartTime < 0f) { - musicSource.time = startPos; + musicSource.time = (float) startPos; musicSource.PlayScheduled(AudioSettings.dspTime - firstBeatOffset / musicSource.pitch); } else { - musicSource.time = musicStartTime; + musicSource.time = (float) musicStartTime; musicSource.PlayScheduled(AudioSettings.dspTime); } } @@ -132,11 +130,11 @@ namespace HeavenStudio { if (negativeStartTime) { - musicSource.time = startPos; + musicSource.time = (float) startPos; } else { - musicSource.time = startPos + firstBeatOffset; + musicSource.time = (float) startPos + firstBeatOffset; } musicSource.PlayScheduled(AudioSettings.dspTime); @@ -160,7 +158,6 @@ namespace HeavenStudio this.time = time; songPosBeat = 0; - songPositionInBeats = 0; isPlaying = false; isPaused = false; @@ -182,10 +179,8 @@ namespace HeavenStudio time += dt; songPos = time; - songPosition = songPos; - songPosBeat += (dt / secPerBeat); - songPositionInBeats = songPosBeat; + songPosBeat = GetBeatFromSongPos(songPos - firstBeatOffset); // songPositionInBeats = Time.deltaTime / secPerBeat; if (metronome) @@ -258,13 +253,13 @@ namespace HeavenStudio return tempoChanges; } - public float GetSongPosFromBeat(float beat) + public double GetSongPosFromBeat(float beat) { var chart = GameManager.instance.Beatmap; SetBpm(chart.bpm); //initial counter - float counter = 0f; + double counter = 0f; //time of last tempo change, to know how much to add to counter float lastTempoChangeBeat = 0f; @@ -296,31 +291,31 @@ namespace HeavenStudio } //thank you @wooningcharithri#7419 for the psuedo-code - private float BeatsToSecs(float beats, float bpm) + private double BeatsToSecs(double beats, float bpm) { // Debug.Log("BeatsToSecs returning " + beats / bpm * 60); return beats / bpm * 60f; } - private float SecsToBeats(float s, float bpm) + private double SecsToBeats(double s, float bpm) { // Debug.Log("SecsToBeats returning " + s / 60f / bpm); return s / 60f * bpm; } - public float GetBeatFromSongPos(float seconds) + public double GetBeatFromSongPos(double seconds) { // Debug.Log("Getting beat of seconds " + seconds); var chart = GameManager.instance.Beatmap; - float lastTempoChangeBeat = 0f; + double lastTempoChangeBeat = 0f; + double counterSeconds = -firstBeatOffset; float lastBpm = chart.bpm; - float counterSeconds = -firstBeatOffset; var tempoChanges = GetSortedTempoChanges(chart); foreach (var t in tempoChanges) { - float beatToNext = t.beat - lastTempoChangeBeat; - float secToNext = BeatsToSecs(beatToNext, lastBpm); - float nextSecs = counterSeconds + secToNext; + double beatToNext = t.beat - lastTempoChangeBeat; + double secToNext = BeatsToSecs(beatToNext, lastBpm); + double nextSecs = counterSeconds + secToNext; // Debug.Log("nextSecs is " + nextSecs + ", seconds " + seconds); if (nextSecs >= seconds) @@ -357,7 +352,7 @@ namespace HeavenStudio public float SongLengthInBeats() { if (!musicSource.clip) return 0; - return GetBeatFromSongPos(musicSource.clip.length); + return (float) GetBeatFromSongPos(musicSource.clip.length); } public bool SongPosLessThanClipLength(float t) @@ -368,6 +363,14 @@ namespace HeavenStudio return false; } + public bool SongPosLessThanClipLength(double t) + { + if (musicSource.clip != null) + return t < musicSource.clip.length; + else + return false; + } + public bool NotStopped() { return Conductor.instance.isPlaying == true || Conductor.instance.isPaused == true; diff --git a/Assets/Scripts/GameManager.cs b/Assets/Scripts/GameManager.cs index 69818d02e..35d028c2f 100644 --- a/Assets/Scripts/GameManager.cs +++ b/Assets/Scripts/GameManager.cs @@ -238,7 +238,6 @@ namespace HeavenStudio if (Conductor.instance.songPositionInBeats >= tempoChanges[currentTempoEvent]) { Conductor.instance.SetBpm(Beatmap.tempoChanges[currentTempoEvent].tempo); - Conductor.instance.timeSinceLastTempoChange = Time.time; currentTempoEvent++; } } diff --git a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs index 83c59fbdd..bca6a5b34 100644 --- a/Assets/Scripts/LevelEditor/Timeline/Timeline.cs +++ b/Assets/Scripts/LevelEditor/Timeline/Timeline.cs @@ -332,7 +332,7 @@ namespace HeavenStudio.Editor.Track if (!Conductor.instance.isPlaying && !Conductor.instance.isPaused) { SongBeat.text = $"Beat {string.Format("{0:0.000}", TimelineSlider.localPosition.x)}"; - SongPos.text = FormatTime(Conductor.instance.GetSongPosFromBeat(TimelineSlider.localPosition.x)); + SongPos.text = FormatTime((float) Conductor.instance.GetSongPosFromBeat(TimelineSlider.localPosition.x)); } else {