[Solved] Fix for ALSA Volume reset to 100% at boot/ reboot

Questions about codecs, DVD playback, web plugins...
Forum rules
Before you post please read how to get help
Post Reply
shelleyfrank
Level 1
Level 1
Posts: 9
Joined: Fri Jul 12, 2013 9:25 am

[Solved] Fix for ALSA Volume reset to 100% at boot/ reboot

Post by shelleyfrank » Sat Apr 11, 2015 11:54 am

It's been a long-standing issue ever since PulseAudio got introduced as the audio server in linux, and it must've driven plenty of frustrated users off the proverbial cliffs. The question has always been, why are my volume settings always defaulted to max volume whenever I startup my computer, and is there a fix available? Answers have always been partial, and although the murmuring has quieted down, there are still plenty of users who still bump into this problem, and are unable to find a fix.

Problem:
When you boot up or restart your computer you find that the sound always resets to the maximum volume of a 100%. This is caused when the PulseAudio server is started at desktop login time. You are afraid that it may blow up either your speakers, your ears, or your nerves, or probably all, and finding a solution is imperative.

In this tutorial I present you with a three step solution to have your ALSA volume settings restored to the last values set in your previously logged out session. I am presenting it here with the hope that it may relieve other users with the same issue of the seven year itch and I don't claim it to be the best solution; only that it is the only solution that has worked on my system. Follow the steps carefully and intelligently and you are sure to have success. So, before you remove PulseAudio from your system as a last resort so that you may have the comfort of booting up with your speakers at an agreeable audio volume, first try out my tutorial. Good luck.


1. Create a bootscript that would save your ALSA settings to a file in your home directory at Shutdown:

Your ALSA settings are stored to the file /var/lib/alsa/asound.state by default with $ sudo alsactl store and the values can be reset with $ sudo alsactl restore.

However, alsactl with the option -f can save the settings file to any user-specified location in the home directory. I will save the file asound.state in a local directory using $ alsactl -f store /<location> so that the settings can be restored from there without the use of a root shell.

First I will create a folder where the ALSA settings can be stored. Secondly I will create a shell script file in /etc/init.d called alsa-store that will tell alsactl to store the alsa values at my specified location. You can name the script anything you prefer.

In terminal:

Code: Select all

$ mkdir ~/.config/alsa
$ sudo gedit /etc/init.d/alsa-store

Replace gedit with your favourite text editor. In the text editor enter:

Code: Select all

#!/bin/sh
### BEGIN INIT INFO
# Provides:          alsa-store
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Store Alsa settings
# Description:    The purpose of this script is to stores the last Alsa volume
#settings to a file in the home directory at shutdown time.
### END INIT INFO
alsactl store -f /home/<your_username>/.config/alsa/asound.state


Replace <your_username> with your own username. Save and close, and in terminal do:

Code: Select all

$ sudo update-rc.d alsa-store defaults
This will update the rc.d runlevels folders in /etc/ and place symlinks to your script file in the runlever directories (rc0.d, rc6.d etc).

Now your boot/ shutdown script is set up to save your alsa volume values to ~/.config/alsa/asound.state. You will use this file later to restore your volume settings when you next login. You may want to test it by rebooting, and checking weather the file asound.state has been created in the specified directory.

2. Create a shell script that would restore your settings at your desktop-session login.

Use a convenient location to create a new shell script file. Say, ~/scripts/ and call it something like alsa-restore.sh. In a terminal type:

Code: Select all

gedit ~/scripts/alsa-restore.sh
and in the editor enter:

Code: Select all

#! /bin/sh

#Start PulseAudio daemon:
pulseaudio -D
#Restore ALSA volume settings:
alsactl restore -f /home/<your_username>/.config/alsa/asound.state
Save and close, and in terminal do:

Code: Select all

chmod +x ~/scripts/alsa-restore.sh
..to make the script file executable.

3. Add an autostart entry to your Autostart list that will execute your script at login.

An easy way to make your script execute at login time is to add an autostart entry to the list in Settings → Session and Startup. In the Application Autostart tab click on the add button and enter a new name, say alsa-volume-restore, and in the command field enter:

Code: Select all

sh /home/<user_name>/scripts/alsa-restore.sh
This will create a desktop entry file in ~/.config/autostart as alsa-volume-restore.desktop containing the following:

Code: Select all

[Desktop Entry]
Encoding=UTF-8
Version=0.9.4
Type=Application
Name=alsa-volume-restore
Comment=alsa volume restore
Exec=sh /home/michael/scripts/asoundrestore.sh
OnlyShowIn=XFCE;
StartupNotify=false
Terminal=false
Hidden=false
(You can also add this desktop entry manuallly to the ~/.config/autostart folder with your favourite text editor).

Click OK and check the checkbox for your new entry. In the same list make sure to uncheck the entry for PulseAudio Sound System (Start the PulseAudio Sound System).

Now you are set up. Make sure to set your audio volume to anything under 100%, using the Audio Mixer panel Plugin, or the ALSA audio mixer in the Multimedia menu (or terminal: $ alsamixer), and reboot your system. Your audio volume should now be restored to the previous values.

Have fun.
Last edited by shelleyfrank on Wed Jun 10, 2015 6:52 am, edited 8 times in total.

Joris Craessaerts
Level 1
Level 1
Posts: 2
Joined: Sat May 30, 2015 7:02 am

Re: [Solved] Fix for ALSA Volume reset to 100% at boot/ rebo

Post by Joris Craessaerts » Sat May 30, 2015 7:12 am

Hello,

I recently started using Linux mint 17.1. I thought the sound wasn't good so I installed pulsesound for the equaliser. Now I have the same problem that when I restart my system, my sound volume is reset to max. I tried the steps in your tutorial but when I shutdown my system the file asound.state isn't made and because of that I can't edit the file alsa-restore.sh.
Do you know any solutions to this or do you know another way to fix the sound problem?

thanks in advance,

Joris

User avatar
Flemur
Level 16
Level 16
Posts: 6196
Joined: Mon Aug 20, 2012 9:41 pm
Location: Potemkin Village

Re: [Solved] Fix for ALSA Volume reset to 100% at boot/ rebo

Post by Flemur » Sat May 30, 2015 10:26 am

Do you know any solutions to this or do you know another way to fix the sound problem?
I use the script below, run at login. Note that I don't have pulseaudio, so pulse might do other things ...

Code: Select all

#
# Turn on:
#
amixer set 'Master',0       100% unmute
amixer set 'Headphone',0    100% unmute
amixer set 'Capture',0       0dB unmute
amixer set 'Surround',0      50% unmute  # speaker
amixer set 'Side',0          30  unmute  # loop recording
#
# turn on but mute (so no feedback)
#
amixer set 'Line',0           31 mute
#
# Turn on - mute not available
#
amixer set 'PCM',0          0dB
amixer set 'Digital',0      0dB
#
# Turn off:
#
amixer set 'Front',0       0 mute
amixer set 'Front Mic',0   0 mute
amixer set 'Center',0      0 mute
amixer set 'LFE',0         0 mute
amixer set 'Capture',1     0 mute
#
# Turn off - mute not available
#
amixer set 'Rear Mic',0         0
amixer set 'Front Mic Boost',0  0
amixer set 'Line Boost',0       0
amixer set 'Rear Mic Boost',0   0
#
# EOF
~

The percents, db, and numerical values (e.g. '31') are all that way for a reason: generally 0db=100%=31, but not always.
It's setup to record 'what you hear' via a physical cable from 'Side' output to line input.
The script takes "real 0m0.056s" to run.
Please edit your original post title to include [SOLVED] if/when it is solved!
Your data and OS are backed up....right?
Mint 18.3 Xfce/fluxbox/pulse-less
Xubuntu 17.10/fluxbox/pulse-less

shelleyfrank
Level 1
Level 1
Posts: 9
Joined: Fri Jul 12, 2013 9:25 am

Re: [Solved] Fix for ALSA Volume reset to 100% at boot/ rebo

Post by shelleyfrank » Wed Jun 10, 2015 6:14 am

Joris Craessaerts wrote:Hello,

I recently started using Linux mint 17.1. I thought the sound wasn't good so I installed pulsesound for the equaliser. Now I have the same problem that when I restart my system, my sound volume is reset to max. I tried the steps in your tutorial but when I shutdown my system the file asound.state isn't made and because of that I can't edit the file alsa-restore.sh.
Do you know any solutions to this or do you know another way to fix the sound problem?

Hi Joris. I am an amateur, and this tutorial is my best attempt to solve the issue. I have no other solutions to offer. However, I will try to polish it a bit. As for now, let me quickly go over the step to create the bootscript in the innit.d folder to see weather you've got it set up correctly.

1. When you type in the terminal

Code: Select all

ls /etc/init.d
do you see the file called "alsa-store" in the list?

If you do see the file there you have created the file correctly. Check the contents of the file. I have edited my post by adding to the init.d script a script header as recommended on the page https://wiki.debian.org/LSBInitScripts. Add it to the script, just to be sure.

2. Have you updated the rc.d directories correctly? When you run the command "sudo update-rc.d alsa-store defaults" The terminal output has to look as follows:
rc.d.png
When you run the command again it will output:

Code: Select all

 System start/stop links for /etc/init.d/alsa-store already exist.
3. Make sure that the directory in your home folder exists i.e. /home/<your user>/.config/alsa/ and that it matches the name of the directory that is pointed to in the "alsa-store" script.

Joris Craessaerts
Level 1
Level 1
Posts: 2
Joined: Sat May 30, 2015 7:02 am

Re: [Solved] Fix for ALSA Volume reset to 100% at boot/ rebo

Post by Joris Craessaerts » Wed Jun 10, 2015 3:00 pm

Hello,

Thanks for replying. I reread the first article and tried everything again. It still didn't work. Then I started searching for Ubuntu tutorials for start-up and log-of scripts and I realised what I did wrong. I forgot to make the shut-down script executable with the chmod +x. There wasn't said in the tutorial you have to make scripts executable before they can be run by Ubuntu or mint and since I'm a complete amateur too, I didn't know that :P. So my problem is solved. Thanks anyway for replying because your solution could maybe help other people.

matheusssilva
Level 1
Level 1
Posts: 3
Joined: Tue Aug 02, 2016 3:52 pm

Re: [Solved] Fix for ALSA Volume reset to 100% at boot/ reboot

Post by matheusssilva » Wed Aug 03, 2016 1:17 pm

It did not work for HDMI output.

warpcafe
Level 1
Level 1
Posts: 4
Joined: Sun Jul 27, 2014 9:50 am
Contact:

Re: [Solved] Fix for ALSA Volume reset to 100% at boot/ reboot

Post by warpcafe » Tue Jun 12, 2018 4:40 am

Hi,

thanks a ton for your post and the work involved in creating that fine tutorial.
I am on a brand-new, fresh install of Debian 9.4.0. and affected by exactly that very same problem.
Your approach is the only reliable workaround I have come across. So again, thanks for saving my day.

However, there is one glitch in your tutorial that I would like to point your attention to, as it may be tricky to overcome for newby users of Debian/Ubuntu/Mint et.al.
In step 3, the contents of your .desktop file mention

Code: Select all

Exec=sh /home/michael/scripts/asoundrestore.sh
...which should read instead:

Code: Select all

Exec=sh /home/<user_name>/scripts/alsa-restore.sh
Other than that, I found that in my case, alsactl isn't executable for the standard user because /usr/sbin isn't part of the path. So I need to either make the directory part of the path or simply use a fully-qualified path to alsactlas in

Code: Select all

/usr/sbin/alsactl
I'm mentioning this just in case someone with a similar setup like me should stumble across the same problem ...and luckily find their way here. ;)

So, the combination of YOUR post along with THIS thread fixed all my problems.
Thanks, cheers,
Thomas

Post Reply

Return to “Sound, Multimedia, & Codecs”