Saturday, October 19, 2013

Lubuntu, Kubuntu, Xubuntu or Ubuntu: Sound not working after resume from suspend

This post might as well be cataloged as a summary of a continuous frustration that has be fed by the lack of decent audio support in Linux.

I first saw Linux installed in 1999-2000 and it was a RedHat 6.1/6.2 from a computer magazine. From the start one of the first problems was getting the sound card working. I discovered I had to 'modprobe' and when I first heard a sound I loved it. Years went on and I switched to a Slackware 8.0 (booted from floppy :) ). I stayed for 1 month without sound until I found out what I need to do in order to make it run. To this date I consider slackware one of the best operating systems I've ever tried. Later on I switched to Ubuntu (6.04) 'cause I was tired of kernel compilations and lack of a package dependency tool. It was a miracle! Everything worked. I kept using Ubuntu and I never had any problems. I thought the desktop evolved, it was usable. But, I was wrong!

I installed the latest 13.10 Ubuntu/Lubuntu and found out that I had no sound after resume from suspend . I said to myself: "Come on, it must be a mistake, I haven't got a problem like this in years!"

But it seems that this is a known problem for quite a while. And I started searching on the Internet for (at least local) solutions.

My conclusions were that when suspended the audio card/driver remains in an inconsistent state and needs to be somehow restarted. In case of Alsa this is done via:

sudo alsa force-reload


But in my case I couldn't do that without errors that were telling me something like this:

me@WS12:~$ sudo alsa force-reload
Unloading ALSA sound driver modules: snd-hda-codec-analog snd-hda-intel snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-seq-midi snd-seq-midi-event snd-rawmidi snd-seq snd-seq-device snd-timer (failed: modules still loaded: snd-hda-codec-analog snd-hda-intel snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-timer).
Loading ALSA sound driver modules: snd-hda-codec-analog snd-hda-intel snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-seq-midi snd-seq-midi-event snd-rawmidi snd-seq snd-seq-device snd-timer.

Doing a lsof on the sound devices reveals why:

me@WS12:~$ lsof /dev/snd/*
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
lxpanel 1268  ins    9u   CHR  116,6      0t0 8923 /dev/snd/controlC0

For other Ubuntu flavors (I use Lubuntu) you might get pulseaudio being the one keeping sound devices open.

What I did?

Step 1a: I killed lxpanel

killall lxpanel

OR

Step 1b: I removed the Volume Bar from the panel and made a successful 'sudo alsa force-reload' (called twice ... - see below) - seems like the volume bar keeps some /dev/snd/* devices open

Step 2: Restart the lxpanel

lxpanel --profile Lubuntu


And after this the sound works. You need to do something similar if you want to do this when using portaudio:

1. Create this file with the following contents (so that portaudio doesn't restart when killed):

me@WS12:~$ cat ~/.pulse/client.conf
autospawn = no
me@WS12:~$


2. Kill pulseaudio

pulseaudio --kill

3. Also kill other programs that have some /dev/snd/* files open

4. Then restart alsa. But make sure you type 'sudo alsa force-reload' twice because ... I don't know (maybe the module unload order is not the good one ... really don't know)

me@WS12:~$ sudo alsa force-reload
Unloading ALSA sound driver modules: snd-seq-midi snd-seq-midi-event snd-seq snd-rawmidi snd-seq-device snd-hda-codec-analog snd-hda-intel snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-timer (failed: modules still loaded: snd-hda-codec-analog snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-timer).
Loading ALSA sound driver modules: snd-seq-midi snd-seq-midi-event snd-seq snd-rawmidi snd-seq-device snd-hda-codec-analog snd-hda-intel snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-timer.
me@WS12:~$ sudo alsa force-reload
Unloading ALSA sound driver modules: snd-hda-intel snd-seq-midi snd-seq-midi-event snd-seq snd-rawmidi snd-seq-device snd-hda-codec-analog snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-timer.
Loading ALSA sound driver modules: snd-hda-intel snd-seq-midi snd-seq-midi-event snd-seq snd-rawmidi snd-seq-device snd-hda-codec-analog snd-hda-codec snd-hwdep snd-pcm snd-page-alloc snd-timer.
me@WS12:~$

5. After that remove the ~/.pulse/client.conf

rm ~/.pulse/client.conf

6. Restart pulseaudio

pulseaudio --start

You're done!

Well this is what I call "evolution"! NOT.

Other choices in my opinion would be:

1. Fix the driver (if this is really the case)
2. When executing pm-suspend do the steps that I did... (see man page for scripts executed on suspend)
3. Use a sound server (pulseaudio/jack/esd...) that when going to sleep disconnects gracefully from the /dev/snd/* files and lets the 'alsa force-reload' do the job on resume - before it connects again)

The best choice I believe it is 3 (if 1 is not the issue) because the applications using sound (playing or recording) cannot see any real difference since they are routed through the sound server.

If you know some distros that work in this scenario (after suspend+resume I can hear music), please let me know.

5 comments:

Unknown said...

Hello, does the issue resolve when you plug the headphones in and out?

undergraver said...

Hi Roman,

Thanks for the advice. It really works.

Now can you shed some light on this issue?

All the best.

Tobiáš Potoček said...
This comment has been removed by the author.
Tobiáš Potoček said...

I had the same problem on my Sony Vaio Pro but only when the laptop was unplugged. At first I tried the steps described in this article. They worked but for some reason they were messing up the wifi card after resume. So I decided to simply disable pm powersave hooks related to soundcard and it fixed the problem:

sudo touch /etc/pm/power.d/pci_devices
sudo touch /etc/pm/power.d/intel-audio-powersave

(The first one might not be necessary.)

Jun Ang said...

I'm trying to pinpoint the issue. I cannot confirm if it started with making audio on HDMI work. But now (Ubuntu 14.04 LTS) audio seems to fail/stop if I use the 'Videos' application to play MP4 files, (FLV files works fine). But with VLC, all seems working.

Just now (2015-09-11) Some 'video player (flash)' plugin on firefox breaks my audio.

To fix the issue, i do 'sudo alsa force-reload', i can then play the 'file' that broke the audio with VLC. But force-reloading sometimes break the audio that rebooting can fix. Reboot by the way can fix the issue.

Could it be that some 'codec' breaks the audio?