improve deferred timekeep even more

This commit is contained in:
minenice55 2023-12-23 16:07:55 -05:00
parent 1414988527
commit 29e3bafffe

View file

@ -189,7 +189,7 @@ namespace HeavenStudio
startPos = GetSongPosFromBeat(beat); startPos = GetSongPosFromBeat(beat);
firstBeatOffset = offset; firstBeatOffset = offset;
time = startPos - (3 * dspSizeSeconds); time = startPos;
if (musicSource.clip != null && startPos < musicSource.clip.length - offset) if (musicSource.clip != null && startPos < musicSource.clip.length - offset)
{ {
@ -197,13 +197,13 @@ namespace HeavenStudio
double musicStartDelay = -offset - startPos; double musicStartDelay = -offset - startPos;
if (musicStartDelay > 0) if (musicStartDelay > 0)
{ {
musicScheduledTime = dspTime + (musicStartDelay / SongPitch); musicScheduledTime = dspTime + (musicStartDelay / SongPitch) + 2*dspSizeSeconds;
dspStart = dspTime; dspStart = dspTime + 2*dspSizeSeconds;
} }
else else
{ {
musicScheduledTime = dspTime + (3 * dspSizeSeconds); musicScheduledTime = dspTime + 2*dspSizeSeconds;
dspStart = dspTime + (3 * dspSizeSeconds); dspStart = dspTime + 2*dspSizeSeconds;
} }
musicScheduledPitch = SongPitch; musicScheduledPitch = SongPitch;
musicSource.PlayScheduled(musicScheduledTime); musicSource.PlayScheduled(musicScheduledTime);
@ -214,12 +214,12 @@ namespace HeavenStudio
dspStart = dspTime; dspStart = dspTime;
} }
songPosBeat = GetBeatFromSongPos(time); songPosBeat = beat;
startBeat = songPosBeat; startBeat = songPosBeat;
_metronomeTally = 0; _metronomeTally = 0;
startTime = DateTime.Now; startTime = DateTime.Now;
absTimeAdjust = -(3 * dspSizeSeconds); absTimeAdjust = 0;
deferTimeKeeping = (musicSource.clip != null); deferTimeKeeping = (musicSource.clip != null);
isPlaying = true; isPlaying = true;
@ -228,15 +228,17 @@ namespace HeavenStudio
void OnAudioFilterRead(float[] data, int channels) void OnAudioFilterRead(float[] data, int channels)
{ {
if (!deferTimeKeeping) return;
// don't actually do anything with the data // don't actually do anything with the data
// wait until we get a dsp update before starting to keep time // wait until we get a dsp update before starting to keep time
if (deferTimeKeeping && AudioSettings.dspTime >= dspStart) double dsp = AudioSettings.dspTime;
if (dsp >= dspStart - dspSizeSeconds)
{ {
Debug.Log($"dsptime: {AudioSettings.dspTime}, deferred timekeeping for {DateTime.Now - startTime} seconds");
deferTimeKeeping = false; deferTimeKeeping = false;
startTime = DateTime.Now; Debug.Log($"dsptime: {dsp}, deferred timekeeping for {DateTime.Now - startTime} seconds (delta dsp {dsp - dspStart})");
dspStart = AudioSettings.dspTime; startTime += TimeSpan.FromSeconds(dsp - dspStart);
absTimeAdjust = 0; absTimeAdjust = 0;
dspStart = dsp;
} }
} }
@ -370,7 +372,8 @@ namespace HeavenStudio
{ {
if (isPlaying) if (isPlaying)
{ {
if (AudioSettings.dspTime < musicScheduledTime && musicScheduledPitch != SongPitch) double dsp = AudioSettings.dspTime;
if (dsp < musicScheduledTime && musicScheduledPitch != SongPitch)
{ {
if (SongPitch == 0f) if (SongPitch == 0f)
{ {
@ -382,30 +385,33 @@ namespace HeavenStudio
musicSource.UnPause(); musicSource.UnPause();
musicScheduledPitch = SongPitch; musicScheduledPitch = SongPitch;
musicScheduledTime = (AudioSettings.dspTime + (-GameManager.instance.Beatmap.data.offset - songPositionAsDouble) / (double)SongPitch); musicScheduledTime = (dsp + (-GameManager.instance.Beatmap.data.offset - songPositionAsDouble) / (double)SongPitch);
musicSource.SetScheduledStartTime(musicScheduledTime); musicSource.SetScheduledStartTime(musicScheduledTime);
} }
} }
absTime = (DateTime.Now - startTime).TotalSeconds; if (!deferTimeKeeping)
//dspTime to sync with audio thread in case of drift
dspTime = AudioSettings.dspTime - dspStart;
if (Math.Abs(absTime + absTimeAdjust - dspTime) > dspMargin)
{ {
int i = 0; absTime = (DateTime.Now - startTime).TotalSeconds;
while (Math.Abs(absTime + absTimeAdjust - dspTime) > dspMargin)
//dspTime to sync with audio thread in case of drift
dspTime = dsp - dspStart;
if (Math.Abs(absTime + absTimeAdjust - dspTime) > dspMargin)
{ {
i++; int i = 0;
absTimeAdjust = (dspTime - absTime + absTimeAdjust) * 0.5; while (Math.Abs(absTime + absTimeAdjust - dspTime) > dspMargin)
if (i > 8) break; {
i++;
absTimeAdjust = (dspTime - absTime + absTimeAdjust) * 0.5;
if (i > 8) break;
}
} }
time = MapTimeToPitchChanges(absTime + absTimeAdjust);
songPos = startPos + time;
songPosBeat = GetBeatFromSongPos(songPos);
} }
time = MapTimeToPitchChanges(absTime + absTimeAdjust);
songPos = startPos + time;
songPosBeat = GetBeatFromSongPos(songPos);
} }
} }