spotifyd icon indicating copy to clipboard operation
spotifyd copied to clipboard

Stuttering and pops during playback on macOS Catalina

Open gcohara opened this issue 3 years ago • 21 comments

First, let me start by thanking everyone for their work on this project - It's great to see a viable alternative to the official Spotify client. However, I've been trying to use this and finding that I'm getting pops and stuttering during playback. It's particularly bad during periods of high CPU usage, e.g when compiling something. I installed using Homebrew.

I find this odd because I never have this problem with the Spotify desktop client. Is it related to using PortAudio rather than CoreAudio? Is there a configuration option I'm missing?

gcohara avatar Nov 21 '21 16:11 gcohara

On macOS Monterey, I get the same problem, sound is laggy and not really good to listen... I've tried changing the bitrate but it still there.

SailorSnoW avatar Nov 29 '21 10:11 SailorSnoW

I actually fixed it by changing my device sample rate. I guess portaudio don't like 96 000Hz, when I lower it to 48 000Hz the problem disappear for me

SailorSnoW avatar Nov 29 '21 10:11 SailorSnoW

@SailorSnoW How did you do this? I had a look online but couldn't find anything helpful.

yougotwill avatar Dec 02 '21 00:12 yougotwill

Solution:

Disabling 'volume_normalization' and commenting out 'normalization_pregain' fixed it for me, my config now looks like this

# If set to true, enables volume normalization between songs.
volume_normalization = false

# The normalization pregain that is applied for each song.
# normalization_pregain = -10

System Info: macOS 12.1 Beta (21C5045a) MacBook Pro (16-inch, 2021) Apple M1 Max

kiralabs avatar Dec 06 '21 19:12 kiralabs

@yougotwill You should be able to adjust the sample rate in Audio MIDI Setup

CaidanS avatar Jan 27 '22 00:01 CaidanS

I still get massive stuttering on macOS Monterey (Macbook Pro 2017) despite having applied all the suggestions above:

  • output device has 48kHz sampling rate
  • volume normalization is disabled
  • normalization pregain is commented out
  • bitrate is 160 I noticed that stutter and crackles is connected to UI activity. For example, when I switch windows or tabs, stuttering increases. What other settings can influence stutter & crackles?

christophberger avatar Mar 09 '22 15:03 christophberger

Experiencing the exact same thing as @christophberger, on MacOS Monterey as a 2017 Macbook Pro as well.

B1T3X avatar Mar 10 '22 14:03 B1T3X

Is there a way to increase the buffer size used to send audio to coreaudio?

pfultz2 avatar Mar 16 '22 17:03 pfultz2

I personally have no Macbook so I can't test this myself. Would someone be able to try running the upstream project librespot that spotifyd uses and see if the problem is still there? (see here or here for installation instructions)

eladyn avatar Mar 17 '22 17:03 eladyn

I personally have no Macbook so I can't test this myself. Would someone be able to try running the upstream project librespot that spotifyd uses and see if the problem is still there? (see here or here for installation instructions)

Going to try this and post my findings.

B1T3X avatar Mar 18 '22 14:03 B1T3X

Ok, so I think I managed to solve this. First of all - librespot worked flawlessly with the following config librespot -u <user> -p <password> -n "MyLIbreSpot" --device-type "computer" -b 160 --backend rodio --mixer softvol

I played around a little with the feature flags and managed to get it working. I managed to solve the problem by cloning the repo and running the following commands:

cargo build --release --locked --no-default-features --features="portaudio_backend" cargo install --path . --locked --no-default-features --features="portaudio_backend"

This solved the problem but prompted me that portaudio is a backend prone to panicking so I re-ran the build & install commands with rodio_backend instead.

B1T3X avatar Mar 18 '22 15:03 B1T3X

Awesome, thanks for trying and reporting the results!

I'm going to assume that the spotifyd version, where the stuttering occurred, was installed via homebrew? If not, you can probably skip all what I've written below :sweat_smile:.

The brew formula actually does something very similar to what you did. Something that I noticed is that they are still on 0.3.2, so the reason why it is working for you if you compile it yourself might be the newer version. (0.3.3 vs 0.3.2)

One possibility to check if that is indeed the case, would be to download and run the newest version directly from the releases page here and see if the stuttering is still there with this version.

There has been an attempt to bump the version at https://github.com/Homebrew/homebrew-core/pull/90676, but it failed due to the tests failing (there is indeed a bug in the current version that is related to that). I personally don't have much experience with brew, so if anyone would be willing to give bumping the version over there another shot that would be great!

eladyn avatar Mar 18 '22 17:03 eladyn

Thanks @eladyn and @B1T3X for providing steps and hints. Unfortunately, I still encounter the stuttering.

I uninstalled spotifyd and ran brew install --HEAD spotifyd, which basically clones the master branch and runs cargo to install spotifyd v0.3.3. However, the stuttering was still there. (bitrate=160, backend=portaudio, mixer=softvol, output device sample rate set to 48kHz 16 bit (and also tried all other settings))

Then I tested librespot 0.3.1. No stuttering anymore, even with heavy UI activity (which used to increase the stuttering with spotifyd).

So brewing spotifyd from HEAD seems not sufficient to resolve the issue. I then built spotifyd myself (with the steps @B1T3X posted here), but no improvement either.

Finally, I ran spotifyd as a launchd service. But the result is the same. (AFAIK, brew service start is only a frontend to launchd, so this result was kind of expected.)

Maybe some other problem is at work here that I have not been able to narrow down.

christophberger avatar Mar 20 '22 19:03 christophberger

Thanks @eladyn and @B1T3X for providing steps and hints. Unfortunately, I still encounter the stuttering.

I uninstalled spotifyd and ran brew install --HEAD spotifyd, which basically clones the master branch and runs cargo to install spotifyd v0.3.3. However, the stuttering was still there. (bitrate=160, backend=portaudio, mixer=softvol, output device sample rate set to 48kHz 16 bit (and also tried all other settings))

Then I tested librespot 0.3.1. No stuttering anymore, even with heavy UI activity (which used to increase the stuttering with spotifyd).

So brewing spotifyd from HEAD seems not sufficient to resolve the issue. I then built spotifyd myself (with the steps @B1T3X posted here), but no improvement either.

Finally, I ran spotifyd as a launchd service. But the result is the same. (AFAIK, brew service start is only a frontend to launchd, so this result was kind of expected.)

Maybe some other problem is at work here that I have not been able to narrow down.

What method did you use to install Rust and what version of Rust are you using?

B1T3X avatar Mar 20 '22 20:03 B1T3X

@B1T3X Rust came with the Homebrew installation of spotifyd HEAD. Version is 1.59.0

christophberger avatar Mar 21 '22 08:03 christophberger

@B1T3X Rust came with the Homebrew installation of spotifyd HEAD. Version is 1.59.0

Can you try uninstalling spotifyd & Rust using Homebrew and then installing Rust via the rustup method and spotifyd using the way I described of compiling?

B1T3X avatar Mar 21 '22 08:03 B1T3X

@B1T3X I can, but recently I discovered that librespot also has stuttering when macOS does heavy GUI updates. It is not as much as with my instance of spotifyd (this is why I noticed this so late), but this means that the core of the problem lies within librespot.

christophberger avatar Mar 23 '22 09:03 christophberger

on M1 mac, with spotifyd 0.3.2: no stuttering. Even if I run GTA five while playing music, there is no stuttering

aviwad avatar Apr 15 '22 13:04 aviwad

Ok.

I just found out that the daemon is responsible for the stuttering in my case.

It makes no difference if I install spotifyd via homebrew or via rust (like @B1T3X says).

As soon as I set up a daemon with launchctl or use the homebrew formula for it, the stuttering starts.

alexchaichan avatar Dec 15 '22 13:12 alexchaichan

I was experiencing stuttering when macOS was under sudden CPU:I/O load like when launching a bunch of apps simultaneously. It happen both with a librespot daemon as well as a spotifyd daemon (brew or custom). To fix this for either I've set up the proprety list keys: ProcessType, LowPriorityIO, and LowPriorityBackgroundIO as seen below.

<key>ProcessType</key>
<string>Interactive</string>
<key>LowPriorityIO</key>
<false/>
<key>LowPriorityBackgroundIO</key>
<false/>

In the launchd.plist(5) man page it says this about ProcessType and the setting Interactive.

ProcessType ... If left unspecified, the system will apply light resource limits to the job, throttling its CPU usage and I/O bandwidth. ... Interactive Interactive jobs run with the same resource limitations as apps, that is to say, none. Interactive jobs are critical to maintaining a responsive user experience, and this key should only be used if an app's ability to be responsive depends on it, and cannot be made Adaptive.

While I saw no information about a default setting for LowPriorityIO and LowPriorityBackgroundIO being true, when testing without them set the stuttering was less often but still some times there. With them set to false, I've noticed no more stuttering.

philipsdean avatar Feb 27 '23 20:02 philipsdean

Rebuilding spotifyd with rodio backend fixes crackling for me.

I noticed problems mostly when pausing/unpausing song.

If somebody needs quick "one-liner" to test it out:

cd /tmp && \
git clone https://github.com/Spotifyd/spotifyd.git && \
cd spotifyd && \
cargo build --release --locked --no-default-features --features=rodio_backend  && \
./target/release/spotifyd --no-daemon  

nazriel avatar Aug 26 '23 16:08 nazriel