From d90626f583d7860ae060856f3935cf2845beb440 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Wed, 13 Jan 2021 17:50:56 +1030 Subject: [PATCH] PulseAudio: Remove get_latency() caching --- doc/classes/AudioServer.xml | 2 +- doc/classes/Performance.xml | 2 +- .../pulseaudio/audio_driver_pulseaudio.cpp | 27 +++++++++---------- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/doc/classes/AudioServer.xml b/doc/classes/AudioServer.xml index 7f3b446d82d..f95568ba5d5 100644 --- a/doc/classes/AudioServer.xml +++ b/doc/classes/AudioServer.xml @@ -132,7 +132,7 @@ - Returns the audio driver's output latency. + Returns the audio driver's output latency. This can be expensive, it is not recommended to call this every frame. diff --git a/doc/classes/Performance.xml b/doc/classes/Performance.xml index 4345594527c..4264360c94e 100644 --- a/doc/classes/Performance.xml +++ b/doc/classes/Performance.xml @@ -114,7 +114,7 @@ Number of islands in the 3D physics engine. - Output latency of the [AudioServer]. + Output latency of the [AudioServer]. Equivalent to calling [method AudioServer.get_output_latency], it is not recommended to call this every frame. Represents the size of the [enum Monitor] enum. diff --git a/drivers/pulseaudio/audio_driver_pulseaudio.cpp b/drivers/pulseaudio/audio_driver_pulseaudio.cpp index d960acd0bc4..df1b2edd67f 100644 --- a/drivers/pulseaudio/audio_driver_pulseaudio.cpp +++ b/drivers/pulseaudio/audio_driver_pulseaudio.cpp @@ -353,27 +353,24 @@ Error AudioDriverPulseAudio::init() { } float AudioDriverPulseAudio::get_latency() { - if (latency == 0) { //only do this once since it's approximate anyway - lock(); + lock(); - pa_usec_t palat = 0; - if (pa_stream_get_state(pa_str) == PA_STREAM_READY) { - int negative = 0; + pa_usec_t pa_lat = 0; + if (pa_stream_get_state(pa_str) == PA_STREAM_READY) { + int negative = 0; - if (pa_stream_get_latency(pa_str, &palat, &negative) >= 0) { - if (negative) { - palat = 0; - } + if (pa_stream_get_latency(pa_str, &pa_lat, &negative) >= 0) { + if (negative) { + pa_lat = 0; } } - - if (palat > 0) { - latency = double(palat) / 1000000.0; - } - - unlock(); } + if (pa_lat > 0) { + latency = double(pa_lat) / 1000000.0; + } + + unlock(); return latency; }