aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2024-04-15 20:30:41 +0200
committerdec05eba <dec05eba@protonmail.com>2024-04-15 20:30:41 +0200
commit7e239c8aa289bbe71bcbb0bcc5908ca02cee4861 (patch)
treefda770d5c0bacfe1db4d5593174fa0b4fcfe9299
parent21a2e3c3fa7f91e06a8cfd0b1d3920e2c6083ff5 (diff)
Test latency comp
-rw-r--r--include/sound.hpp2
-rw-r--r--src/main.cpp13
-rw-r--r--src/sound.cpp16
3 files changed, 25 insertions, 6 deletions
diff --git a/include/sound.hpp b/include/sound.hpp
index 731e787..77bec99 100644
--- a/include/sound.hpp
+++ b/include/sound.hpp
@@ -55,7 +55,7 @@ void sound_device_close(SoundDevice *device);
Returns the next chunk of audio into @buffer.
Returns the number of frames read, or a negative value on failure.
*/
-int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec);
+int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec, double *latency_seconds);
std::vector<AudioInput> get_pulseaudio_inputs();
diff --git a/src/main.cpp b/src/main.cpp
index 5aacd8b..c716e47 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -2417,12 +2417,17 @@ int main(int argc, char **argv) {
while(running) {
void *sound_buffer;
int sound_buffer_size = -1;
- if(audio_device.sound_device.handle)
- sound_buffer_size = sound_device_read_next_chunk(&audio_device.sound_device, &sound_buffer, timeout_sec);
+ const double time_before_read_seconds = clock_get_monotonic_seconds();
+ if(audio_device.sound_device.handle) {
+ // TODO: use this instead of calculating time to read. But this can fluctuate and we dont want to go back in time
+ double latency_seconds = 0.0;
+ sound_buffer_size = sound_device_read_next_chunk(&audio_device.sound_device, &sound_buffer, timeout_sec, &latency_seconds);
+ }
const bool got_audio_data = sound_buffer_size >= 0;
-
- const double this_audio_frame_time = clock_get_monotonic_seconds() - paused_time_offset;
+ const double time_after_read_seconds = clock_get_monotonic_seconds();
+ const double time_to_read_seconds = time_after_read_seconds - time_before_read_seconds;
+ const double this_audio_frame_time = time_after_read_seconds - paused_time_offset - time_to_read_seconds;
if(paused) {
if(got_audio_data)
diff --git a/src/sound.cpp b/src/sound.cpp
index be9c196..25f1080 100644
--- a/src/sound.cpp
+++ b/src/sound.cpp
@@ -41,6 +41,7 @@ struct pa_handle {
size_t output_index, output_length;
int operation_success;
+ double latency_seconds;
};
static void pa_sound_device_free(pa_handle *s) {
@@ -79,6 +80,7 @@ static pa_handle* pa_sound_device_new(const char *server,
p->read_data = NULL;
p->read_length = 0;
p->read_index = 0;
+ p->latency_seconds = 0.0;
const int buffer_size = attr->fragsize;
void *buffer = malloc(buffer_size);
@@ -161,6 +163,9 @@ static int pa_sound_device_read(pa_handle *p, double timeout_seconds) {
bool success = false;
int r = 0;
int *rerror = &r;
+ pa_usec_t latency = 0;
+ int negative = 0;
+
CHECK_DEAD_GOTO(p, rerror, fail);
while (p->output_index < p->output_length) {
@@ -193,6 +198,13 @@ static int pa_sound_device_read(pa_handle *p, double timeout_seconds) {
CHECK_DEAD_GOTO(p, rerror, fail);
continue;
}
+
+ if(pa_stream_get_latency(p->stream, &latency, &negative) >= 0) {
+ p->latency_seconds = negative ? -(int64_t)latency : latency;
+ p->latency_seconds *= 0.0000001;
+ if(p->latency_seconds < 0.0)
+ p->latency_seconds = 0.0;
+ }
}
const size_t space_free_in_output_buffer = p->output_length - p->output_index;
@@ -276,13 +288,15 @@ void sound_device_close(SoundDevice *device) {
device->handle = NULL;
}
-int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec) {
+int sound_device_read_next_chunk(SoundDevice *device, void **buffer, double timeout_sec, double *latency_seconds) {
pa_handle *pa = (pa_handle*)device->handle;
if(pa_sound_device_read(pa, timeout_sec) < 0) {
//fprintf(stderr, "pa_simple_read() failed: %s\n", pa_strerror(error));
+ *latency_seconds = 0.0;
return -1;
}
*buffer = pa->output_data;
+ *latency_seconds = pa->latency_seconds;
return device->frames;
}