mikebrady / shairport-sync

AirPlay and AirPlay 2 audio player
Other
7.17k stars 569 forks source link

Controlling latency with "audio_backend_latency_offset_in_seconds" #608

Closed LadnerJamie closed 6 years ago

LadnerJamie commented 6 years ago

Hi, I'm having some problems adjusting the latency on my RPi3 and am hoping you can help or explain where I'm going wrong.

I have my Pi connected via HDMI to my Samsung Soundbar & TV and am experiencing a delay when I play audio from my Forked-daapd server. I have multiple Pis and other airplay devices connected via ethernet and wirelessly, all play sync'ed audio.

I think the problem may be due to some home theater weirdness that the TV / Soundbar is introducing and giving me a 1/2 - 1/4 second delay. I thought I could make shairport-sync compensate for this using the "audio_backend_latency_offset_in_seconds" setting in the config file. I've tried adjusting the setting but it doesn't seem to have any effect whatsoever.

When I set the log verbosity at "2" I see the messages: userSuppliedLatency is 0. AirPlayLatency is -1. iTunesLatency is -1. forkedDaapdLatency is -1.

The "UserSuppliedLatency" is always 0, no matter what change I make to the audio backend latency setting. Is this normal?

Is it even possible to have shairport-sync play the audio on this Pi "sooner" by a couple hundred milliseconds? Maybe I'm going about this the wrong way, but I'd appreciate any insight that you may be able to offer.

Thanks for your help, and thanks for such a making such a useful program.

mikebrady commented 6 years ago

Thanks for the post. It's often the case that audio processing systems like TV sound bars and home theatres introduce a delay, typically tens to hundreds of milliseconds.

Those userSuppliedLatency setting is for completely ignoring the latency agreed by Shairport Sync with the audio source. It's special-purpose only and is inappropriate here. Similarly, AirPlayLatency, iTunesLatency, and forkedDaapdLatency are obsolete and no longer directly settable.

I just tried setting the audio_backend_latency_offset_in_seconds. In the configuration file, I set the offset to minus half a second – i.e. to output the audio half a second early (notice that it's uncommented):

//      interface = "name"; // Use this advanced setting to specify the interface on which Shairport Sync should provide its service. Leave it commented out to get the default, which is to select the interface(s) automatically.
  audio_backend_latency_offset_in_seconds = -0.5; // Set this offset to compensate for a fixed delay in the audio back end. E.g. if the output device delays by 100 ms, set this to -0.1.
//  audio_backend_buffer_desired_length_in_seconds = 0.15; // If set too small, buffer underflow occurs on low-powered machines. Too long and the response time to volume changes becomes annoying. Default is 0.15 seconds in the alsa backend, 0.35 seconds in

When you restart, the offset should show up in the log if you set verbosity to 1 or more:

Oct 25 08:25:02 raspberrypi shairport-sync[8485]: use_mmap_if_available is 1.
Oct 25 08:25:02 raspberrypi shairport-sync[8485]: output_rate is 44100.
Oct 25 08:25:02 raspberrypi shairport-sync[8485]: output_format is 7 (0-unknown, 1-S8, 2-U8, 3-S16, 4-S24, 5-S24_3LE, 6-S24_3BE, 7-S32).
Oct 25 08:25:02 raspberrypi shairport-sync[8485]: audio backend desired buffer length is 0.150000 seconds.
Oct 25 08:25:02 raspberrypi shairport-sync[8485]: audio backend latency offset is -0.500000 seconds.
Oct 25 08:25:02 raspberrypi shairport-sync[8485]: audio backend silence lead-in time is -1.000000 seconds. A value -1.0 means use the default.
Oct 25 08:25:02 raspberrypi shairport-sync[8485]: volume range in dB (zero means use the range specified by the mixer): 40.

A time difference of this size is very easy to hear, I promise!

(By the way, I'm assuming you're using the latest version of Shairport Sync.)

LadnerJamie commented 6 years ago

Thanks Mike,

I had tried changing the "audio_backend_buffer_desired_length_in_seconds" setting, but wasn't able to hear any noticeable difference, and when I saw the offset in the log it was always set to 0.

I had assumed I was running the latest version as well, but just in case I wasn't I cloned your repository and re-installed from scratch. That has solved my issue. I had assumed that Raspbian Stretch would have the most recent version but I now see that it wasn't.

Thanks again for creating such a great piece of software and all your help.

mikebrady commented 6 years ago

I'm glad the problem is solved. The repositories are always a bit behind, I'm afraid, and it's still developing rather quickly.

mikebrady commented 6 years ago

By the way, is it okay to close this issue now?

LadnerJamie commented 6 years ago

Yes, please close this issue.

mikebrady commented 6 years ago

Thanks.