Update waiting logic for audio thread.

This commit is contained in:
Skyth 2024-12-22 14:36:56 +03:00
parent 6bf029b0bb
commit 6921a54dc6

View file

@ -40,8 +40,6 @@ static void AudioThread()
GuestThreadContext ctx(0);
size_t channels = g_downMixToStereo ? 2 : XAUDIO_NUM_CHANNELS;
auto start = std::chrono::steady_clock::now();
int64_t iteration = 1;
while (true)
{
@ -55,19 +53,14 @@ static void AudioThread()
g_clientCallback(ctx.ppcContext, reinterpret_cast<uint8_t*>(g_memory.base));
}
auto next = start + std::chrono::nanoseconds((iteration * XAUDIO_NUM_SAMPLES * 1000000000ll) / XAUDIO_SAMPLES_HZ);
auto now = std::chrono::steady_clock::now();
constexpr auto INTERVAL = 1000000000ns * XAUDIO_NUM_SAMPLES / XAUDIO_SAMPLES_HZ;
auto next = now + (INTERVAL - now.time_since_epoch() % INTERVAL);
if ((next - now) < 1s)
{
std::this_thread::sleep_for(std::chrono::floor<std::chrono::milliseconds>(next - now));
std::this_thread::sleep_for(std::chrono::floor<std::chrono::milliseconds>(next - now));
while ((now = std::chrono::steady_clock::now()) < next)
std::this_thread::yield();
}
int64_t elapsed = std::chrono::nanoseconds(std::chrono::steady_clock::now() - start).count();
iteration = ((elapsed * XAUDIO_SAMPLES_HZ) / (XAUDIO_NUM_SAMPLES * 1000000000ll)) + 1;
while (std::chrono::steady_clock::now() < next)
std::this_thread::yield();
}
}