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 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;

View file

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

View file

@ -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
{