Restoring/Restarting your sound running alsa sound on Linux (Debian)

15 June 2013, Comments: 0

In general I’m a happy user of Linux systems when it comes to provide (web) services such as Webservers, FTP-Servers, File-servers and even Virtual Boxes. But I figured using Linux, as a desktop system running in the X11/Gnome/KDE on Debian is not yet really stable.
And yes, it’s really going on my nerves, when you start up a browser and you want to see some videos – and suddenly – the sound is frozen. Then the first approach would be restarting your computer or trying everything else.

Besides that, I figured, that there are also other cases, when the sound didn’t work anymore:

  • After the restarting the computer
  • After sending the computer into the suspend mode and waking it up again

But actually, if you are running the alsa sound driver, I implemented a neat solution to restart your sound driver.

Yes, I also read that you can restart your sound somewhere – and actually I used this command since then until now.

The command to restart your alsa sound driver was:

alsa force-reload

After executing this command, yes, you’ll might have your sound driver back and you would be able to hear something…. but always you need to adjust your PCM volume level again.

So, therefore I wrote a small init.d script which is doing the following:

  1. It saves your current PCM volume level
  2. It restarts your alsa sound drivers and waits! until it is fully loaded!
  3. Then after that, it restores your PCM volume level
#! /bin/sh

### BEGIN INIT INFO
# Provides:             sound
# Default-Start:        2 3 4 5
# Default-Stop:
# Short-Description:    Restart the sound
### END INIT INFO

set -e
amixer=/usr/bin/amixer
alsa=/usr/sbin/alsa

# /etc/init.d/sound: restarts the "alsa" daemon

case "$1" in
restart)
rawlevel=$($amixer -c 0 get PCM | tail -2 | awk -F'[' '{print $2}' | sed -e 's/\n//' | sed -e 's/]/,/' | sed -e 's/, //g')
level=$(echo $rawlevel | sed -e 's/ /,/g')
echo Getting current pcm volume level
echo $level
echo Restarting the sound driver
nohup $alsa force-reload > /dev/null ||  wait $!
sleep 3
echo Restoring the volume level
cmd=$(echo $amixer -c 0 sset PCM,0 $level)
echo    $cmd
$cmd
;;

*)
echo "Usage: /etc/init.d/sound {restart}"
exit 1
esac

exit 0

Init.d script to restart the linux alsa sound driver – filename: sound.sh

You can place this script in your /etc/init.d/ folder – (and don’t forget to make it executable). Then you can always call it like the following:

/etc/init.d/sound.sh restart

Additionally you can add the sound.sh init.d script to your sudo commands with:

visudo

And just add this line somewhere
$USER$ ALL=NOPASSWD: /etc/init.d/sound.sh

(Replace $USER$ with your username)

 

Well this script is not build like rocket science – and I’m sure there is a way to optimize it. I just wanted to get it work.

So be happy to use it! But also, I’m happy, if some else out there has a better proposal. I’m listening!

So happy surfing and watch Videos!

Leave a Reply