more liberal use of Double in conductor

beatkeeping is now absolute instead of being additive
This commit is contained in:
minenice55 2022-09-22 20:40:05 -04:00
parent 91cda204e4
commit f82ef76de6
3 changed files with 33 additions and 31 deletions

View file

@ -1,7 +1,6 @@
using System.Collections; using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.Audio;
using Starpelly; using Starpelly;
@ -21,15 +20,15 @@ namespace HeavenStudio
public float pitchedSecPerBeat => (secPerBeat / musicSource.pitch); public float pitchedSecPerBeat => (secPerBeat / musicSource.pitch);
// Current song position, in seconds // Current song position, in seconds
private float songPos; // for Conductor use only private double songPos; // for Conductor use only
public float songPosition; public float songPosition => (float) songPos;
// Current song position, in beats // Current song position, in beats
private float songPosBeat; // for Conductor use only private double songPosBeat; // for Conductor use only
public float songPositionInBeats; public float songPositionInBeats => (float) songPosBeat;
// Current time of the song // Current time of the song
private float time; private double time;
double lastAbsTime; double lastAbsTime;
@ -54,7 +53,7 @@ namespace HeavenStudio
// Metronome tick sound enabled // Metronome tick sound enabled
public bool metronome = false; public bool metronome = false;
public float timeSinceLastTempoChange = 0; public float timeSinceLastTempoChange = Single.MinValue;
private bool beat; private bool beat;
@ -67,7 +66,7 @@ namespace HeavenStudio
public void SetBeat(float beat) public void SetBeat(float beat)
{ {
float secFromBeat = GetSongPosFromBeat(beat); float secFromBeat = (float) GetSongPosFromBeat(beat);
if (musicSource.clip != null) if (musicSource.clip != null)
{ {
@ -79,7 +78,6 @@ namespace HeavenStudio
GameManager.instance.SetCurrentEventToClosest(beat); GameManager.instance.SetCurrentEventToClosest(beat);
songPosBeat = beat; songPosBeat = beat;
songPositionInBeats = songPosBeat;
} }
public void Play(float beat) public void Play(float beat)
@ -119,12 +117,12 @@ namespace HeavenStudio
if (musicStartTime < 0f) if (musicStartTime < 0f)
{ {
musicSource.time = startPos; musicSource.time = (float) startPos;
musicSource.PlayScheduled(AudioSettings.dspTime - firstBeatOffset / musicSource.pitch); musicSource.PlayScheduled(AudioSettings.dspTime - firstBeatOffset / musicSource.pitch);
} }
else else
{ {
musicSource.time = musicStartTime; musicSource.time = (float) musicStartTime;
musicSource.PlayScheduled(AudioSettings.dspTime); musicSource.PlayScheduled(AudioSettings.dspTime);
} }
} }
@ -132,11 +130,11 @@ namespace HeavenStudio
{ {
if (negativeStartTime) if (negativeStartTime)
{ {
musicSource.time = startPos; musicSource.time = (float) startPos;
} }
else else
{ {
musicSource.time = startPos + firstBeatOffset; musicSource.time = (float) startPos + firstBeatOffset;
} }
musicSource.PlayScheduled(AudioSettings.dspTime); musicSource.PlayScheduled(AudioSettings.dspTime);
@ -160,7 +158,6 @@ namespace HeavenStudio
this.time = time; this.time = time;
songPosBeat = 0; songPosBeat = 0;
songPositionInBeats = 0;
isPlaying = false; isPlaying = false;
isPaused = false; isPaused = false;
@ -182,10 +179,8 @@ namespace HeavenStudio
time += dt; time += dt;
songPos = time; songPos = time;
songPosition = songPos;
songPosBeat += (dt / secPerBeat); songPosBeat = GetBeatFromSongPos(songPos - firstBeatOffset);
songPositionInBeats = songPosBeat;
// songPositionInBeats = Time.deltaTime / secPerBeat; // songPositionInBeats = Time.deltaTime / secPerBeat;
if (metronome) if (metronome)
@ -258,13 +253,13 @@ namespace HeavenStudio
return tempoChanges; return tempoChanges;
} }
public float GetSongPosFromBeat(float beat) public double GetSongPosFromBeat(float beat)
{ {
var chart = GameManager.instance.Beatmap; var chart = GameManager.instance.Beatmap;
SetBpm(chart.bpm); SetBpm(chart.bpm);
//initial counter //initial counter
float counter = 0f; double counter = 0f;
//time of last tempo change, to know how much to add to counter //time of last tempo change, to know how much to add to counter
float lastTempoChangeBeat = 0f; float lastTempoChangeBeat = 0f;
@ -296,31 +291,31 @@ namespace HeavenStudio
} }
//thank you @wooningcharithri#7419 for the psuedo-code //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); // Debug.Log("BeatsToSecs returning " + beats / bpm * 60);
return beats / bpm * 60f; 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); // Debug.Log("SecsToBeats returning " + s / 60f / bpm);
return s / 60f * bpm; return s / 60f * bpm;
} }
public float GetBeatFromSongPos(float seconds) public double GetBeatFromSongPos(double seconds)
{ {
// Debug.Log("Getting beat of seconds " + seconds); // Debug.Log("Getting beat of seconds " + seconds);
var chart = GameManager.instance.Beatmap; var chart = GameManager.instance.Beatmap;
float lastTempoChangeBeat = 0f; double lastTempoChangeBeat = 0f;
double counterSeconds = -firstBeatOffset;
float lastBpm = chart.bpm; float lastBpm = chart.bpm;
float counterSeconds = -firstBeatOffset;
var tempoChanges = GetSortedTempoChanges(chart); var tempoChanges = GetSortedTempoChanges(chart);
foreach (var t in tempoChanges) foreach (var t in tempoChanges)
{ {
float beatToNext = t.beat - lastTempoChangeBeat; double beatToNext = t.beat - lastTempoChangeBeat;
float secToNext = BeatsToSecs(beatToNext, lastBpm); double secToNext = BeatsToSecs(beatToNext, lastBpm);
float nextSecs = counterSeconds + secToNext; double nextSecs = counterSeconds + secToNext;
// Debug.Log("nextSecs is " + nextSecs + ", seconds " + seconds); // Debug.Log("nextSecs is " + nextSecs + ", seconds " + seconds);
if (nextSecs >= seconds) if (nextSecs >= seconds)
@ -357,7 +352,7 @@ namespace HeavenStudio
public float SongLengthInBeats() public float SongLengthInBeats()
{ {
if (!musicSource.clip) return 0; if (!musicSource.clip) return 0;
return GetBeatFromSongPos(musicSource.clip.length); return (float) GetBeatFromSongPos(musicSource.clip.length);
} }
public bool SongPosLessThanClipLength(float t) public bool SongPosLessThanClipLength(float t)
@ -368,6 +363,14 @@ namespace HeavenStudio
return false; return false;
} }
public bool SongPosLessThanClipLength(double t)
{
if (musicSource.clip != null)
return t < musicSource.clip.length;
else
return false;
}
public bool NotStopped() public bool NotStopped()
{ {
return Conductor.instance.isPlaying == true || Conductor.instance.isPaused == true; return Conductor.instance.isPlaying == true || Conductor.instance.isPaused == true;

View file

@ -238,7 +238,6 @@ namespace HeavenStudio
if (Conductor.instance.songPositionInBeats >= tempoChanges[currentTempoEvent]) if (Conductor.instance.songPositionInBeats >= tempoChanges[currentTempoEvent])
{ {
Conductor.instance.SetBpm(Beatmap.tempoChanges[currentTempoEvent].tempo); Conductor.instance.SetBpm(Beatmap.tempoChanges[currentTempoEvent].tempo);
Conductor.instance.timeSinceLastTempoChange = Time.time;
currentTempoEvent++; currentTempoEvent++;
} }
} }

View file

@ -332,7 +332,7 @@ namespace HeavenStudio.Editor.Track
if (!Conductor.instance.isPlaying && !Conductor.instance.isPaused) if (!Conductor.instance.isPlaying && !Conductor.instance.isPaused)
{ {
SongBeat.text = $"Beat {string.Format("{0:0.000}", TimelineSlider.localPosition.x)}"; 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 else
{ {