switch to less stuttery timing method

This commit is contained in:
msk
2023-09-29 17:04:02 -07:00
parent 2a6e64dbfd
commit 0dfb606216
3 changed files with 31 additions and 18 deletions
+20
View File
@@ -4,6 +4,19 @@ using System;
public partial class BGM : AudioStreamPlayer
{
// latency compensation
private ulong timeStartUsec;
private float timeDelay;
public float CurTime
{
get
{
float time = (Time.GetTicksUsec() - timeStartUsec) / 1000000f;
return time - timeDelay;
}
}
public void LoadFromUser(string path)
{
if (!path.StartsWith("user://"))
@@ -43,4 +56,11 @@ public partial class BGM : AudioStreamPlayer
break;
}
}
public void Play()
{
timeStartUsec = Time.GetTicksUsec();
timeDelay = (float) (AudioServer.GetTimeToNextMix() + AudioServer.GetOutputLatency());
base.Play();
}
}