How to get the best sound with and properly configure PulseAudio

To improve audio quality with pulseaudio present: Everything in a code box is done in a terminal

1) Make a backup of default settings:

Code:
sudo cp /etc/pulse/daemon.conf /etc/pulse/daemon.conf.old

2) edit the daemon.conf to use the best resampler and the highest quality sample format:

Code:
sudo nano /etc/pulse/daemon.conf

3) find the following:
resample-method =
default-sample-format =

4) change both to: (make sure to remove the “;” before default-sample-format)

Code:
resample-method = src-sinc-best-quality
default-sample-format = [S]float32ne[/S]

Here you should put the results from running pacmd list-sinks and the information found on the line called sample spec. The first piece of information should be the sample format your card will work best with. For my case the output was

Quote:

sample spec: s32le 2ch 44100Hz

thus my sample format is s32le. The float32ne is simply a generic method.

SECOND ISSUE: If you know that you will be using audio of a higher frequency (AKA something above 48KHz such as a Bluray HD audio track). Then you will likely want to change this area.

Code:
alternate-sample-rate = 48000

to this

Code:
alternate-sample-rate = 96000

This will prevent resampling of audio that is @96KHz but it will create resampling of any audio @48KHz. As of right now I have no idea if more than 2 default sample frequencies/rates can be used. So you’re stuck with the choice of of which you want to avoid resampling. In my experience most sources even Bluray tracks seem to top out at 48kHz so I’d say leave it at 48000.

5) FOR MORE THAN JUST 2 SPEAKERS
change this accordingly (make sure to remove the “;” before each setting)
3 for 2.1 aka 2 speakers and a sub and (While 3 for 2.1 is valid and supported most 2.1 speakers don’t work properly with such a setting so you should tinker with that to figure out what works best for you)so 6 would equal 5.1 and 8 would equal 7.1 (you’ll have to add the locational name of each speaker you have in the channel map to make it work properly)
(a generic 5.1 setup edit to your needs)

Code:
default-sample-channels = 6
default-channel-map = front-left,front-right,rear-left,rear-right,center,subwoofer

SIDE NOTE: You might want to experiment with swapping subwoofer with lfe to see which one sounds best for you and your specific sound hardware. But make sure your hardware can handle the lfe as it is noted on occasion the lfe can destroy speakers or subwoofers.

6) IF YOU WANT TO MESS WITH LATENCY
Lower these numbers as much as you can (at 0 pulse will no longer work and ALSA will be used instead) but be careful they may introduce problems and flash will likely become a problem or may not work at all.
Apparently I goofed up here…though this is purely subjective to each person; the recommended thing to do is set a higher fragment number and size. For me I personally have had better results (lower CPU usage) with as few fragments as pulse will allow me (in my case it was 2) and as large a fragment size as I felt necessary (I’m at 400 right now). The changes here are really up to you.
This section has been a source of problems as there’s much misinformation out on the net. The safest bet outside of default is to use more fragments and a fragment size of 50 or greater. This however is a generic method. If you want to know how to figure out the specific for your configuration setup for this part of audio you’ll need to be ready to do some math and have a read from the steps laid out by these guys at the mint forum: http://forums.linuxmint.com/viewtopic.php?f=42&t=44862
One note is that you should use your info from running pacmd list-sinks and then follow steps 2-3 in the mint guide.

Code:
default-fragments = 1 (4 is default)
default-fragment-size-msec = 1 (25 is default)

6.1) IF YOU WANT TO TWEAK THE PRIORITY TO INCREASE QUALITY
Find “nice-level” and change it from the default -11 to a high negative (remember -20 is max and not recommended). Remember as usual to remove the ; before “nice-level”.

Code:
nice-level = -15 #this setting increases the frequency/share of CPU time that Pulseaudio has access to

SIDE NOTE: Not confirmed by anyone else yet but it seems changing the priority has a negative effect on KDE’s system sounds so they’ll cause a second or two of glitching/blocky/ugly sound if something else is using audio. No idea why but the priority change might negatively effect sounds from DEs such as Gnome, KDE. Doesn’t seem to cause problems with other applications using sound though. UPDATE: Skype is another thing that doesn’t like the nice level or latency changes. If you make changes similar to this then make sure not have anything trying to use the sound server at the same time as Skype as it’s again one program that just doesn’t play nice with others. It works perfectly fine if nothing else tries to use the sound card so don’t worry there.

IF YOU WISH TO AVOID PULSEAUDIO AND INSTEAD USE ALSA ALONE
Warning: Spoiler! (Click to show)

Make the fragment section of the config look like this:

Code:
default-fragments = 0
default-fragment-size-msec = 0

It should be noted that this will negate any of the changes made prior to this point as this is a lazy way of getting rid of pulseaudio without having to actually uninstall pulse audio. It works by simply causing pulse not to load as this is an incorrect setting. Thus, pulse should no longer show up in your process list or return a follow from running

Code:
ps -A | grep "pulse"

. This means that ALSA will be in charge of everything and configuring it is a whole separate topic. However, it seems most people believe that one does not have to configure ALSA at all as it should “just work” by default so you shouldn’t need to do any config work with it.

An alternative and likely better way to disable Pulseaudio is to change the following in the /etc/pulse/client.conf file:

Code:
; autospawn = yes

to

Code:
 autospawn = no

7) exit and save changes

8) Next you need to kill pulse so the old config settings are no longer in effect

Code:
pulseaudio --kill

9) Then you should check that your config is valid

Code:
pulseaudio --check

10) If it returns nothing then your config is fine and you can restart pulse with your changes implemented

Code:
pulseaudio --start

NOTE: These changes may increase CPU load and the sample format may not be supported by the audio hardware on your mobo or card. Lower the number from 32 to 24 to 16 until it works. Otherwise use the original default setting. It should be noted that using a float setting will always result in lower CPU load and better sound quality.

Example of how it should look. Remember any changes made need to have the # or ; before each line removed in order to take effect. This file has real time enabled which requires changes to /etc/security/limits.conf before working; an example of that file will be shown as well.
Example daemon.conf (Click to show)

Code:
# This file is part of PulseAudio.
#
# PulseAudio is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PulseAudio is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with PulseAudio; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA.

## Configuration file for the PulseAudio daemon. See pulse-daemon.conf(5) for
## more information. Default values are commented out.  Use either ; or # for
## commenting.

; daemonize = no
; fail = yes
; allow-module-loading = yes
; allow-exit = yes
; use-pid-file = yes
; system-instance = no
; local-server-type = user
; enable-shm = yes
; shm-size-bytes = 0 # setting this 0 will use the system-default, usually 64 MiB
; lock-memory = no
; cpu-limit = no

; high-priority = yes
; nice-level = -11

#This is the realtime setting section. The ";" are gone meaning these are active
 realtime-scheduling = yes
 realtime-priority = 49 #9 is the recommended setting use a higher number at your own risk 49 is my setting.

; exit-idle-time = 20
; scache-idle-time = 20

; dl-search-path = (depends on architecture)

; load-default-script-file = yes
; default-script-file = /etc/pulse/default.pa

; log-target = auto
; log-level = notice
; log-meta = no
; log-time = no
; log-backtrace = 0

resample-method = src-sinc-best-quality #this is the best resampler there is.
#If your CPU is weaker see secret rabbit code or stick to speex-float-10
; enable-remixing = yes
; enable-lfe-remixing = no

flat-volumes = no #this disables system wide EQ IIRC which you want for fidelity sake 

; rlimit-fsize = -1
; rlimit-data = -1
; rlimit-stack = -1
; rlimit-core = -1
; rlimit-as = -1
; rlimit-rss = -1
; rlimit-nproc = -1
; rlimit-nofile = 256
; rlimit-memlock = -1
; rlimit-locks = -1
; rlimit-sigpending = -1
; rlimit-msgqueue = -1
; rlimit-nice = 31
 rlimit-rtprio = 49 #this must match the realtime-priority you set above
#...you will also need to edit the /etc/security/limits.conf file to allow realtime. See that file example.
; rlimit-rttime = 1000000

; default-sample-format = s16le #s32le, float-32 are other available methods. AFAIK s24le implies s32le.
; default-sample-rate = 44100
; alternate-sample-rate = 48000 #change this the highest kHZ rate of anything you own IE 96000 if a true bluray.
; default-sample-channels = 2 #configure this to match the number of speakers you have...
#...6 equals a 5.1 system. 8 equals a 7.1 system.
; default-channel-map = front-left,front-right

default-fragments = 25 #see the guide for a link on figuring this out. Best to leave it at 5 or increase it if...
#...you notice dropped or clipped playback
default-fragment-size-msec = 364 #see the guide for a link on figuring this out. Best to leave it at 25 or increase...
#...it if you notice dropped or clipped playback

; enable-deferred-volume = yes
deferred-volume-safety-margin-usec = 1
; deferred-volume-extra-delay-usec = 0

Example /etc/security/limits.conf. For this change the rez name to match your user name when you add those two lines.
Limits.conf example (Click to show)

Code:
# /etc/security/limits.conf
#
#Each line describes a limit for a user in the form:
#
#<domain>        <type>  <item>  <value>
#
#Where:
#<domain> can be:
#        - an user name
#        - a group name, with @group syntax
#        - the wildcard *, for default entry
#        - the wildcard %, can be also used with %group syntax,
#                 for maxlogin limit
#        - NOTE: group and wildcard limits are not applied to root.
#          To apply a limit to the root user, <domain> must be
#          the literal username root.
#
#<type> can have the two values:
#        - "soft" for enforcing the soft limits
#        - "hard" for enforcing hard limits
#
#<item> can be one of the following:
#        - core - limits the core file size (KB)
#        - data - max data size (KB)
#        - fsize - maximum filesize (KB)
#        - memlock - max locked-in-memory address space (KB)
#        - nofile - max number of open files
#        - rss - max resident set size (KB)
#        - stack - max stack size (KB)
#        - cpu - max CPU time (MIN)
#        - nproc - max number of processes
#        - as - address space limit (KB)
#        - maxlogins - max number of logins for this user
#        - maxsyslogins - max number of logins on the system
#        - priority - the priority to run user process with
#        - locks - max number of file locks the user can hold
#        - sigpending - max number of pending signals
#        - msgqueue - max memory used by POSIX message queues (bytes)
#        - nice - max nice priority allowed to raise to values: [-20, 19]
#        - rtprio - max realtime priority
#        - chroot - change root to directory (Debian-specific)
#
#<domain>      <type>  <item>         <value>
#

#*               soft    core            0
#root            hard    core            100000
#*               hard    rss             10000
#@student        hard    nproc           20
#@faculty        soft    nproc           20
#@faculty        hard    nproc           50
#ftp             hard    nproc           0
#ftp             -       chroot          /ftp
#@student        -       maxlogins       4
rez              -       nice            -18 #this must match or exceed what is in your daemon.conf so it must be >= to nice-level
rez              -       rtprio          49 #this must match what is in you daemon.conf file so if you use 9 this must be 9

# End of file

A possibly helpful suggestion for laptop users with either NVIDIA and/or ALC889 onboard soundcard
Added October 30, 2014

Quote:

From @Evil Penguin
Welp, looks like I finally got the audio performing the way it should.
It literally took me several hours of trial/error and research to get these pesky audio issues sorted.

The ALC889 can’t have a default sample rate of 44100 if the sample format is higher than s16le (doing so will result in a huge cluster of issues).
s16le and s32le appear to be the only sample formats that work. src-sinc-best-quality resampler appears to run into a CPU bottleneck and causes distortion so I changed it to the next best thing (src-sinc-medium-quality) and it works without issue. I have NVIDIA HDMI audio and when it’s on, it has issues with the modified PA settings and crashes/crackles so I turned it off via pavucontrol (configuration tab).

I’ve settled with:

resample-method = src-sinc-medium-quality
default-sample-format = s32le
default-sample-rate = 48000
alternate-sample-rate = 96000

I now have a better understanding of why there’s popping noises when messing around with the desktop environment while listening to music.

Taken from the Gentoo wiki:

Quote:

No guarantees on actual latencies

Currently PA provides whatever latency at that moment is possible be it some milliseconds to hundreds of milliseconds without regard to what applications ask for.

In case of buffer under-run latencies are never decreased
Currently, if a buffer under-run occurs, PA buffers for longer increasing latency, but it then never tries to buffer for less until restart.

If the buffer isn’t large enough, you’ll likely hear audio distortion (in my case crackling).
PulseAudio automatically increases the latency/buffer size every time there’s a buffer under-run.
Alternatively you can set the latency yourself by disabling timer-based scheduling but I wouldn’t advise that unless you know what you’re doing.

25 thoughts on “How to get the best sound with and properly configure PulseAudio”

  1. Nice article with crucial information for people who have to dapple with PulseAudio, thanks!

    However, “best sound” is debatable here, because if changing the sample rate alters the waveform in a manner that is perceptible with ears only, it is definitely doing something wrong. Transparent (and correct) resampling would result in identical output files after converting back to the original sample rate. Anything else but the output that results in the waveform originally in the file is by definition different from the source track and thus wrong. You cannot conjure back ultrasound data lost in the anti-aliasing aka lowpass filtering of a CD quality signal, and quite frankly you do not need to. 44.1KHz is all that you need as it alone exceeds the upper limit of human hearing as per it's Nyquist frequency, and there has been zero legitimate studies proving higher sample rates would matter. Even if they did, you would have to be in possession of the higher samplerate master files and upconverting serves no purpose (except in the cases of Benchmark DAC1 where the internal clock happens to function the best at it's resampling frequency and thus it converts everything to that).

    Resampling CAN sound different, it can even sound “better” subjectively. However, in reality you're not improving anything but simply altering the original signal to the point where it's no longer original. You might like the chance, but it is in no way higher fidelity (= truthful to the original).

  2. Well, because I avoided mentioning changing the KHz freqs which is an option available I thought I was avoiding confusion on that issue. Here if one does only what is listed there is no frequency alteration unless they play something with a frequency higher than 48KHz (the second default that pulseaudio comes with) or lower than 44.1KHz (the first default). Changing the resample method simply helps people if they encounter anything outside of those ranges retain the best quality that they can get on a downmix. But, I will admit, I perhaps should include a mention that if people want to enjoy higher frequency sources on hardware that can handle it then they should change the second default to 96KHz (the most common HF format). I think I'll include that change. In short I'm agreeing with your statements but explaining that what was explained in the article creates a situation in which no one would encounter resampling unless they had a source below 44.1KHz or above 48KHz.

    As for the float-32ne part that is simply to use the best quality and best performing default sampler which just about every source I found made it clear that speex(the default) was just a middle of the road sampler and unless set to speex-float9 would use more CPU power for lower quality sound for ALL audio even that which wasn't under the resample range (AKA <44.1KHz or >48KHz in default).

    Thanks. I'll get to work on the frequency section though as I'm sure some people (myself now that I think of it with my blurays) will encounter things of higher frequency than 48KHz.

  3. I think it could be useful to provide a brief overview of the use of pacmd; which is the pulse command line. It allows you to directly interface with the server and update configuration options on the fly (the changes are supposedly persistent too, and have been in my experience as well) It also provides a level of verbosity that the PulseAudio stdout/stderr streams on startup generally don't.

  4. Thanks Xaero. I'm looking into using it and how it can be helpful but I'm not sure I'm going to have time to get into it for a while. Right now the most useful thing I can see from it is the more accurate info it can get on the card/sink properties so that you can better configure the daemon.conf file for things like fragments, sampler, and latency. Never thought audio would be so complicated but boy was I wrong. LOL.

  5. It has to do with that supposedly being a safe “higher quality” format. It should be covered under point #4 in the guide. Basically, if you can't figure out your sample format from running pacmd list-sinks it's just a generic method.

  6. Oh, after doing some reading it seems that floating point sampling method is more efficient and may yield higher audio quality.
    I think in this case, float32le and float32ne are the same thing.

  7. Yup. Another thing I didn't really cover is that even better results can be combined when prioritizing your IRQs through the threadirqs boot option and my script in the Linux section. I'd recommend then putting PulseAudio after the hd-audio0 process. But I haven't tested that fully.

  8. Commenting out:
    load-module module-suspend-on-idle
    in:
    /etc/pulse/default.pa
    Seems to have fixed/worked around the remainder of my sound crackling issues…

    5 different settings in config files that I've had to change in order to get my audio working properly, sheesh.

  9. Were you having crackling issues during playback or just when you'd startup the computer/start/stop playback? I have issues with just ALSA but it's only at start/stop of playback and mainly on my laptop. What were the 5 fixes you needed to do?

  10. Starting and stopping playback would cause a pop sound.
    When music is playing and I'm browsing the net or using other programs, it'll randomly pop as well.

    These are all the settings I changed for my chipset (ALC889):

    #load-module module-suspend-on-idle

    resample-method = src-sinc-best-quality
    default-sample-format = float32ne
    default-sample-rate = 48000
    alternate-sample-rate = 192000

  11. *extra note*
    I *think* changing the priority to real-time actually introduced audio issues in my case so I reversed it.
    I noticed it while playing CSGO, sounds would skip or be distorted.

  12. Interesting. I know some (especially “new”) onboards had that issue with Pulse but I hadn't heard about anyone having it for a while. Any reason you set the default to 48KHz? You're basically forcing all your 44.1KHz music to be resampled for no reason and even though float is the lightest on CPU it still is an unnecessary load. Also, the 192KHz is beyond any format I've come across. I think the highest I've seen yet was 96KHz Bluray stuff.

  13. I used 48 KHz initially because Defense Grid 2 had issues with 44.1 KHz default.
    I've tried s24-32le and 44.1 KHz and that failed to playback audio for some reason.

    I think it might be worth looking at the sample rates again now that I've settled suspend on idle issues.
    I chose 192 KHz as the alternative in the event that I ever *needed* it but perhaps 96 KHz would be a better alternative.
    Videos tend to have 48 KHz audio…

    Regardless, the CPU usage isn't all that high so I don't mind.

  14. Ah…I forgot about videos being that way. I kind of wish Pulse could handle 3 settings just so you could have no resampling of music and videos and then whatever KHz you gave the third setting. I guess Lennart couldn't figure that out?

    If you're feeling up to it I'd love for someone to try out PulseAudio with my RT script/guide. Also, I have a system tunning script as well that could help make Pulse better sounding.

  15. Welp, looks like I finally got the audio performing the way it should.
    It literally took me several hours of trial/error and research to get these pesky audio issues sorted.

    The ALC889 can't have a default sample rate of 44100 if the sample format is higher than s16le (doing so will result in a huge cluster of issues).
    s16le, s32le and float32le appear to be the only sample formats that work. src-sinc-best-quality resampler appears to run into a CPU bottleneck and causes distortion so I changed it to the next best thing (src-sinc-medium-quality) and it works without issue. I have NVIDIA HDMI audio and when it's on, it has issues with the modified PA settings and crashes/crackles so I turned it off via pavucontrol (configuration tab). Lastly, I still have suspend on idle disabled so my playback doesn't pop when I start or stop it.

    I've settled with:
    #load-module module-suspend-on-idle

    resample-method = src-sinc-medium-quality
    default-sample-format = float32ne
    default-sample-rate = 48000
    alternate-sample-rate = 96000

    I hope someone finds this info useful.

  16. Thanks Penguin. I'm sure someone will but I'm going to try and add your comment (giving credit of course) to this how to.

    My best guess is that your issue is somehow related to the whole mess of NVIDIA HDMI sound and probably a little of the whole GPU switching effect as well. Really wish NVIDIA and Intel would get that one hammered down.

  17. One little correction, float32le isn't actually supported by my cards.
    After further trials and looking through PA's log, it seems that only s16le and s32le sample formats work (that are worth using).
    Though to be fair I haven't tried u8, ulaw and alaw.

    I now have a better understanding of why there's popping noises when messing around with the desktop environment while listening to music.

    Taken from the Gentoo wiki:
    No guarantees on actual latencies

    Currently PA provides whatever latency at that moment is possible be it some milliseconds to hundreds of milliseconds without regard to what applications ask for.

    In case of buffer under-run latencies are never decreased
    Currently, if a buffer under-run occurs, PA buffers for longer increasing latency, but it then never tries to buffer for less until restart.

    If the buffer isn't large enough, you'll likely hear audio distortion (in my case crackling).
    PulseAudio automatically increases the latency/buffer size every time there's a buffer under-run.
    Alternatively you can set the latency yourself by disabling timer-based scheduling but I wouldn't advise that unless you know what you're doing.

  18. From my understanding Pulse was supposed to be able to take your incorrect settings and step them down to the next highest correct one. But it's been a while since I've read up on it. I do know that u8, ulaw, and alaw are not something you should use unless you absolutely need guarantees on network bandwidth since I believe they are all formats for long distance communication (telephone,VIOP).

    Since this article is open for all to the edit could you add this buffer information? I'm not sure I've encountered that before so I'd rather someone with the experience on it write about it.

  19. Just joined to say this has helped me to fix popping / crackling noises on Ubuntu 15.10 with a USB creative X-FI, where all my other Google results haven't helped at all.

    And with those keywords I hope this leads someone else to this page.

    Thanks a lot!

Leave a Reply

Your email address will not be published. Required fields are marked *

Recommended