Skip to content

Commit b221e13

Browse files
committed
in audio_jack.c, return the delay in terms of frames at Shairport Sync's output rate, not the Jack Audio server's internal rate.
1 parent 616b6e7 commit b221e13

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

audio_jack.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* jack output driver. This file is part of Shairport Sync.
3-
* Copyright (c) 2019 -- 2022 Mike Brady <[email protected]>,
3+
* Copyright (c) 2019 -- 2024 Mike Brady <[email protected]>,
44
* Jörn Nettingsmeier <[email protected]>
55
*
66
* All rights reserved.
@@ -50,8 +50,11 @@ pthread_mutex_t client_mutex = PTHREAD_MUTEX_INITIALIZER;
5050
jack_port_t *port[NPORTS];
5151
const char *port_name[NPORTS] = {"out_L", "out_R"};
5252

53+
54+
int sps_sample_rate;
55+
5356
jack_client_t *client;
54-
jack_nframes_t sample_rate;
57+
jack_nframes_t jack_sample_rate;
5558
jack_nframes_t jack_latency;
5659

5760
jack_ringbuffer_t *jackbuf;
@@ -235,17 +238,17 @@ static int jack_init(__attribute__((unused)) int argc, __attribute__((unused)) c
235238
if (!client) {
236239
die("Could not start JACK server. JackStatus is %x", status);
237240
}
238-
sample_rate = jack_get_sample_rate(client);
241+
jack_sample_rate = jack_get_sample_rate(client);
239242
#ifdef CONFIG_SOXR
240243
if (config.jack_soxr_resample_quality >= SOXR_QQ) {
241244
quality_spec = soxr_quality_spec(config.jack_soxr_resample_quality, 0);
242245
io_spec = soxr_io_spec(SOXR_INT16_I, SOXR_FLOAT32_I);
243246
} else
244247
#endif
245-
if (sample_rate != 44100) {
248+
if (jack_sample_rate != 44100) {
246249
die("The JACK server is running at the wrong sample rate (%d) for Shairport Sync."
247250
" Must be 44100 Hz.",
248-
sample_rate);
251+
jack_sample_rate);
249252
}
250253
jack_set_process_callback(client, &process, NULL);
251254
jack_set_graph_order_callback(client, &graph, NULL);
@@ -329,14 +332,15 @@ static void jack_start(int i_sample_rate, __attribute__((unused)) int i_sample_f
329332
// Nothing to do, JACK client has already been set up at jack_init().
330333
// Also, we have no say over the sample rate or sample format of JACK,
331334
// We convert the 16bit samples to float, and die if the sample rate is != 44k1 without soxr.
335+
sps_sample_rate = i_sample_rate;
332336
#ifdef CONFIG_SOXR
333337
if (config.jack_soxr_resample_quality >= SOXR_QQ) {
334338
// we might improve a bit with soxr_clear if the sample_rate doesn't change
335339
if (soxr) {
336340
soxr_delete(soxr);
337341
}
338342
soxr_error_t e = NULL;
339-
soxr = soxr_create(i_sample_rate, sample_rate, NPORTS, &e, &io_spec, &quality_spec, NULL);
343+
soxr = soxr_create(sps_sample_rate, jack_sample_rate, NPORTS, &e, &io_spec, &quality_spec, NULL);
340344
if (!soxr) {
341345
die("Unable to create soxr resampler for JACK: %s", e);
342346
}
@@ -366,13 +370,15 @@ static int jack_delay(long *the_delay) {
366370
debug(2, "audio_occupancy_now is %d.", audio_occupancy_now);
367371
pthread_mutex_unlock(&buffer_mutex);
368372

369-
int64_t frames_processed_since_latest_latency_check = (delta * sample_rate) / 1000000000;
373+
int64_t frames_processed_since_latest_latency_check = (delta * jack_sample_rate) / 1000000000;
370374
// debug(1,"delta: %" PRId64 " frames.",frames_processed_since_latest_latency_check);
371375
// jack_latency is set by the graph() callback, it's the average of the maximum
372376
// latencies of all our output ports. Adjust this constant baseline delay according
373377
// to the buffer fill level:
374-
*the_delay = jack_latency + audio_occupancy_now - frames_processed_since_latest_latency_check;
375-
// debug(1,"reporting a delay of %d frames",*the_delay);
378+
int64_t the_delay_in_jack_frames = jack_latency + audio_occupancy_now - frames_processed_since_latest_latency_check;
379+
int64_t the_delay_in_sps_frames = (the_delay_in_jack_frames * sps_sample_rate) / jack_sample_rate;
380+
*the_delay = the_delay_in_sps_frames;
381+
// debug(2, "reporting a delay of %ld frames at Shairport Sync's rate of %d FPS.",*the_delay, sps_sample_rate);
376382
return 0;
377383
}
378384

0 commit comments

Comments
 (0)