Sunday, January 04, 2015

Tips to fix sound server issues on linux (Alsa, Pulseaudio)

Usually everything else works out of the box on linux but the audio. I think analog audio would work just fine but I always use digital audio over SPDIF or HDMI so that could be the reason why I have always some audio problems.

So I list here some common tips that I will not have to google those again in next time. As KDE is mine default desktop environment so there will be some specific tips for just KDE.

TIP 1 - Check that you have all ALSA related packages installed


I have installed these:

dpkg --get-selections | grep 'alsa'
alsa-base                                       install
alsa-oss                                        install
alsa-tools                                      install
alsa-tools-gui                                  install
alsa-utils                                      install
alsamixergui                                    install
alsaplayer-alsa                                 install
alsaplayer-common                               install
alsaplayer-gtk                                  install
cairo-dock-alsamixer-plug-in                    install
gstreamer0.10-alsa:amd64                        install
gstreamer1.0-alsa:amd64                         install
libalsaplayer0                                  install
libsox-fmt-alsa:amd64                           install

TIP 2 - Check your GUI settings 


Try to unmute channels and try to different audio devices. In KDE try to modify mixer and KDE audio settings.


TIP 3 - Disable PulseAudio server


Usually mine problems are solved when I disable pulseaudio or at least I have to do it to get it audio first working with Alsa.

So you don't have to totally remove pulseaudio you can disable it first.

If you need to remove it totally then purge the installation. Sometimes it may uninstall some dependencies that are needed, so be careful when purging it.

Disabling automatic spawning of PulseAudio server

Some users may prefer to manually start the PulseAudio server before running certain programs and then stop the PulseAudio server when they are finished. A simple way to accomplish this is to add or edit following file:
/home/<username>/.config/pulse/client.conf 

and add or uncomment following attribute:
autospawn = no

If you want later undo this just comment that line:

#autospawn = no

or remove the file:

rm /home/<username>/.config/pulse/client.conf

autospawn = no

Now you can manually start the pulseaudio server with

$ pulseaudio --start

and stop it with

$ pulseaudio -k

If you have problems to kill pulseaudio then you can try following commands:

$ pkill -f pulseaudio
$ ps aux | grep pulse


You should now find that KDE audio settings are different for ALSA and PULSEAUDIO. When you stop/kill PULSEAUDIO then KDE's mixer and audio settings should view ALSA settings.


ALSA active (pulseaudio stopped)


PULSEAUDIO active

TIP 4 - Found your working sound cards and devices (ALSA)

Following command can be used to list your cards and devices:

$ aplay -l (list all soundcards and digital audio devices)
$ aplay -L (list device names)

And test audio files can be found in folder:
/usr/share/sounds/alsa/

Aplay can be used to test, if your pcm outputs are working. Be sure to unmute and set volume for master and pcm channels with mixer first. It might be a good idea to set a moderate volume level for not damaging your boxes. For hw:0,0  means hw:<card>,<device>. List of available numbers can be found using the "$ aplay -l" -command. Then let it play the test file. Try all the combinations of cards and devices.

$ aplay -D hw:0,0 Noise.wav

If you receive a cryptic error message about "Channels count non available" then the wav file uses a sample rate different to the hardware. You can add a resampling layer with the 'plug' plugin, e.g.

$ aplay -D plughw:0,0 Noise.wav

Many don't know that you can play files using the devices names too. Using the command "$ aplay -L" it lists devices by names, example:

iec958:CARD=PCH,DEV=0
  HDA Intel PCH, ALC892 Digital
  IEC958 (S/PDIF) Digital Audio Output

To play that device you can use following command:

$ aplay -D iec958:CARD=PCH,DEV=0 Noise.wav


If you receive a cryptic error message about "Channels count non available" then the wav file uses a sample rate different to the hardware. You can add resampling layer with the 'plug' plugin.

 $ aplay -D plughw:CARD=PCH,DEV=0 Noise.wav

The "Channels count non available"can be fixed by defining your "ctl" (short for "control") entry in /etc/asound.conf -file. Example:
   
defaults.ctl.card 0
defaults.pcm.card 0
defaults.pcm.device 0

TIP 5 - Audio works in KDE but not in the Firefox etc...


Then you should define default audio card and device. Check tip 6.


TIP 6 - Modify ALSA configs


There could be two config files for ALSA. The system-wide file /etc/asound.conf or the user-specific file ~/.asoundrc.

First you should try to comment out all the lines that you have in those files and try again if you have some audio problems.

If that didn't fixed then I try explain different configuration options shortly.

To set digital out as default

On most soundcards the digital output emits the same sound signal as the analog output. If this is not the case for you then you can set the default playback device by using .asoundrc or /etc/asound.conf. If you want your digital output to be the default you might need to add this:

pcm.!default {
        type hw
        card <the card number you worked out above>
        device <the device number you worked out above>
}
This is the most simple configuration that I try usually first. Just edit the card and device values that worked for you in the aplay sound testing (TIP4).

defaults.ctl.card 1
defaults.pcm.card 1
defaults.pcm.device 7

This way I fixed mine audio problems in one machine:

pcm.!default {

   type plug
   slave {
       pcm "iec958"
   }
}

Here is other config that I have used:

pcm.!default {
  type plug
  slave {
    pcm "hw:1,7"
  }
}
ctl.!default {
  type hw
  card 1
}

Or that can be define also this way:

pcm.!default {
  type hw
  card 1
  device 7
}
ctl.!default {
  type hw
  card 1
  device 7
}
On the pulseaudio side you can always define default audio device as well by editing following config file: /etc/pulse/default.pa (define/edit those in bold text with your device numbers)

#load-module module-alsa-sink device=hw:1,1
#load-module module-alsa-source device=hw:1,1

Usually I have been able to fix my audio problems following these tips. I will define this article again when I next time have some audio problems. At the moment I have 4 Debian or Ubuntu installations and audio worked out of the box only one of those installations. Now I have two systems without pulseaudio. I think I'm not expert in these audio issues but I have always been able to solve those. I think this is the area that needs some attention in linux development. It just shouldn't be this difficult to solve always these issues.

When the audio works then I usually set the on-the-fly AC3 encoding:
http://shellscreen.blogspot.fi/2014/11/6-channel-aac-to-ac3-on-fly-encoding-on.html

1 comment: