[GUIDE] NEWB guide to Bit-Perfect Linux audio.
Jan 15, 2014 at 11:14 AM Thread Starter Post #1 of 16

yay101

100+ Head-Fier
Joined
Feb 24, 2013
Posts
449
Likes
68
My guide to Linux audio for beginners.
 
This applies to many, many distributions and should work regardless of the tools you prefer. I am not going into a great deal of detail to keep it short and easy to read for average users, don’t expect alsa vs oss vs jack vs pipe discussions.
 

 
Output options:
There are several options for audio output in Linux. Where windows has kmixer (mixing, lossy) and wasapi (direct control, lossless) linux uses pulseaudio (mixing, lossy) and alsa (direct control, lossless).
 
The general rule is to have pulseaudio only on when you are not listening to music, this means you can still watch youtube videos and hear notifications from skype at the same time.
 
But when you are listening to music the aim is to have only one source, your player, having priority control over your sound card.
 


   


The only requirement is nano, no gui or desktop environment is necessary. Installing nano on debian or ubuntu is as simple as:
 
sudo apt-get install nano

 

 
Setting up Alsa and gstreamer support:
Firstly make sure alsa, alsa-utils gstreamer0.10-alsa are all installed and up to date.
 
For debian/ ubuntu:
 
sudo apt-get update && sudo apt-get install -y alsa alsa-utils gstreamer0.10-alsa

  Detecting your proper sound card:
For the uses of this guide i will assume you are using a USB DAC.
Running the below in a terminal will list your devices detected by alsa as valid output devices:
 



 
The output on my machine is:
 
**** List of PLAYBACK Hardware Devices ****
card 0: NVidia [HDA NVidia], device 3: HDMI 0 [HDMI 0]
 Subdevices: 1/1
 Subdevice #0: subdevice #0
card 0: NVidia [HDA NVidia], device 7: HDMI 1 [HDMI 1]
 Subdevices: 1/1
 Subdevice #0: subdevice #0
card 1: DAC [UAC1 DAC], device 0: USB Audio [USB Audio]
 Subdevices: 0/1
 Subdevice #0: subdevice #0

 
Here you can easily see that card 1, device 0 is my usb sound card. If i was to use HDMI output i would set it to card 0 device 3 or card 0 device 7.
 
Note: Sometimes if you get two HDMI outputs one is actually your DVI output, its a cheap trick hardware OEMs use to add ports. You will have to try both to find out which one is actually your HDMI device.
  Testing a device is as simple as using:
  aplay -D hw:0,0 test.wav
 
Where hw:0,0 refers to your hardware detected above with aplay -l.

 
For Alsa playback applications we provide hardware addresses in the form:
 
 hw:card#,device#

 

So in my case it is:
 

 
Setting defaults is the final step. Run the following in a terminal:
 
sudo nano /etc/asound.conf

 
I suggest cleaning out this entire file so it is blank then putting the below contents in.
 
 
pcm.!default {
    type hw
    card 0
device 0
}

ctl.!default {
    type hw           
    card 0
device 0
}
 

 
Simply replace the card and device numbers with the ones you found above.
 
For instance my config reads:
 
 
pcm.!default {
    type hw
    card 1
device 0
}

ctl.!default {
    type hw           
    card 1
device 0
}

 
 
You are done with alsa!
 

 
Turning off pulseaudio:
 
As of 14.04 the version of pulseaudio used is much easier to disable with only a single line needing to be edited.
 
Using the command:
sudo nano /etc/pulse/client.conf

 
Change the autospawn line to read:
autospawn = no

 
That's it. No joke, you are done. Just restart your machine.
 
 
 
Playback!
Setting up your music player to use alsa.
  There are many options out there for playing back bitperfect audio in linux as far as music players go.

This post by Rizlaw is a great summary, albeit rather outdated. If you have more questions feel free to ask via pm, post a reply, or simply head over to the forums for your specific distro.

 
Jan 15, 2014 at 12:32 PM Post #2 of 16
 There are several options for audio output in linux. Where windows has kmixer (mixing, lossy) and wasapi (direct control, lossless) linux uses pulseaudio (mixing, lossy) and alsa (direct control, lossless).

 
Could you go into more detail about PulseAudio being "lossy"?  I don't find support of that anywhere.  The closest I got was this post on Computer Audiophile but the maintainer comment that's linked to is dead: http://www.computeraudiophile.com/f11-software/ubuntu-how-achieve-bitperfect-playback-8480/#post107417
 
Q. Why does pulseaudio need Alsa installed?
A. Pulseaudio is just a super advanced mixer that can handle any input and output or loop or network stream. Alsa is simply the default output and supports almost all hardware.

 
I get what you're saying that a user can choose to use PulseAudio or ALSA directly, but you may want to make it clear PulseAudio is in fact a layer on top of ALSA and doesn't exist in its stead.  https://en.wikipedia.org/wiki/File:pulseaudio-diagram.svg
 
Why should someone use PulseAudio at all instead of ALSA's dmix plugin?  Does ALSA's mixing also degrade audio quality?  if the concern is avoiding any software mixing why is switching between PulseAudio and ALSA better than always using ALSA directly and toggling the dmix plugin?  http://www.alsa-project.org/main/index.php/Asoundrc#Software_mixing
 
I think just a little bit more information and the guide will be even more helpful to beginners.
 
Jan 15, 2014 at 12:49 PM Post #3 of 16
All of that was left out for the purpose of it not being 30 pages long. I will update it tomorrow with spoiler sections to explain perhaps.

Edit: and about lossy. Anything that is not bit perfect, is by definition lossy as it has lost information. By that reasoning all software mixing is Lossy, the thing we wish to avoid.

Also, as requested. Here is the reasoning behind pulseaudio instead of dmix for instance. Pulseaudio has to be installed anyway. Its not optional, dmix is. Many apps won't even output audio if pulseaudio is not detected as it is their only output method these days. Several video players are guilty of that sadly. I miss the days of plugins and dmix sometimes, but I also see the value of a universal mixer.

There are ways to increase audio quality as well as still use pulseaudio as its entire mixing interface is configured by a single file. The downside of course is still not bit exact audio, but also massive kernel io and CPU limitations. Much like jack.
 
Jan 16, 2014 at 10:42 AM Post #4 of 16
 Edit: and about lossy. Anything that is not bit perfect, is by definition lossy as it has lost information. By that reasoning all software mixing is Lossy, the thing we wish to avoid.

 
Ok, I get you, thanks.

 
Also, as requested. Here is the reasoning behind pulseaudio instead of dmix for instance. Pulseaudio has to be installed anyway. Its not optional, dmix is 

 
As a Slackware user I've never had to install it.  I'm guessing PulseAudio-by-default distros have all of the media/audio players in their repositories compiled to support it, but again I don't want beginners to have the wrong impression.  Even in those PulseAudio-by-default distros I imagine it's possible to get along without it.  If you want to assume that the reader's life will be easier the PulseAudio route then it may be prudent to include a list of popular distros that you know rely on PulseAudio more than others rather than this blanket statement.
 
Jan 16, 2014 at 11:03 AM Post #5 of 16
That's a good point, I may add more information soon. The main problem is Pulseaudio is installed by default on a great majority of prepackaged distros. Even a lot of Arch's packages assume pulseaudio is present, and adding a flag for that on arch would be like 5 lines in a pkgbuild. Laziness I guess.
 
Apr 1, 2014 at 5:27 AM Post #7 of 16
That a really cool script ronalde, I am going to rewrite this guide very soon with a long and short version with the short version being "mostly" automated. Recent changes to the gnome stack and the release of the next ubuntu and mint builds coming up mean some things will change,  and possibly more people will shortly be looking for just this.
 
Apr 1, 2014 at 10:50 AM Post #8 of 16
Originally Posted by yay101 /img/forum/go_quote.gif
 
Q. Why can’t i just remove pulseaudio completely?
A. Most modern desktops have deep requirements for pulseaudio libraries, some only need a single “emulator” type library and pulseaudio is never actually on the machine.

 
I removed the pulseaudio server, and kept the libraries. This is enough to get rid of the mixing and pulseaudio related bugs/problems, but applications that depend on the libraries still run.

 
May 26, 2016 at 11:08 AM Post #12 of 16
I know it's an outdated thread but being a guide I think it will be good to maintain it updated.
 
I'm using Ubuntu 16.04 with gnome and if I specify the device in ctl.!default I can't change the volume or even open alsamixer. Deleting only this line works like it suppose to do.
 
This extension (https://extensions.gnome.org/extension/835/alsa-mixer/) adds an icon in the top bar. With alsa the notifications don't work but I don't really care, the are useless after all.
If you want to adjust the volume with your keyboard, the commands are:
amixer -q sset Master 5+
amixer -q sset Master 5-
amixer -q sset Master toggle

 
To check if it works, when you're playing something open a terminal and execute:
 
 cat /proc/asound/card0/pcm0p/sub0/hw_params

 
You have to change the card number according with your devices and you can open the file with a text editor if you want.
 
Edit: With that config I can't play html5 videos in Firefox but I found a solution:
defaults.pcm.card 0
defaults.pcm.device 0
defaults.ctl.card 0

With these options you'll be using dmix but what I do is having it commented in the same file, and using it when I have to. Or you can do the opposite if you don't listen to music 24/7 like I do.
 
Jun 17, 2018 at 9:34 AM Post #14 of 16
You are correct noknok23, changing the volume digitally does defeat the purpose of bit perfect audio as it by definition has to do modifications to the stream to adjust the volume and calculate the new bits.

HOWEVER the difference may very well be inaudible right down to less than half volume (when playing back 16bit padded to 24 or 32).

It's person choice really, I like my bits as they are in the record all the way to the DAC. Others are welcome to disagree.
 
Dec 31, 2018 at 12:25 PM Post #15 of 16
Thanks so much for the guide @yay101!

I'd assumed that setting Pulseaudio autospawn to no (last step in your guide) would disable it entirely, however (on my setup at least), it simply prevents it spawning on KDE Plasma startup and hogging all the soundcards; it still wakes up whenever an app specifically wants to use it.

This was the last piece in the puzzle for me, because I wanted to retain the convenience of Pulseaudio for most stuff (web browsing, system sounds, watching movies in VLC etc.) whilst retaining pure ALSA-only bitperfect playback in gmusicbrowser. Whilst I'm not playing music, all sound is mixed via Pulseaudio, then when I want bitperfect playback I disable my USB DAC in Pulseaudio Volume Control, and gmusicbrowser can hog it and set any sampling rate the DAC can support. Volume is then managed only via the slider in gmusicbrowser.

For those who're on this path, here's a few other tidbits I've picked up along the way that are of interest:

- As per the current ALSA documentation, I do not have any ASLA configuration files at all; no ~/.asoundrc nor /etc/asound.conf file whatsoever. Instead, I configured gmusicbrowser according to Rizlaw's guide and ALSA does a perfect job of passing audio to the DAC without any further configuration.
- Pulseaudio defaults to 48kHz internally and will up- or down-sample all other streams to that rate in order to mix them. That frequency was chosen since a. it is the DVD native rate and b. it divides well for other common rates (except 44.1, natch).
- For reference, my setup is Gentoo linux with gmusicbrowser 1.1.15.2, KDE Plasma 5, Pulseaudio 12.2 and ALSA 1.1.6

Thanks again @yay101!!
 

Users who are viewing this thread

Back
Top