ALSA 'amixer' utility howto

How to do things, solutions, recipes, tutorials
Post Reply
Message
Author
Bruce B

ALSA 'amixer' utility howto

#1 Post by Bruce B »

amixer howto

amixer is an ALSA cli utility.

The easiest utility for configuring our sound card volumes is alsamixer, but it doesn't lend itself to scripting the way amixer does.

Recently, I read a post where the person complained that the sound settings were not being saved between reboots. I was not able to do any troubleshooting with the person. Nevertheless, I can conceive of conditions where sound settings could be changed, most likely because of other programs changing them.

The purpose of this how-to is to introduce you to a method whereby you can set and/or reset your sound settings in rc.local without having to do it manually.

For starters, your sound card volume and other settings are kept in this file: /etc/asound.state

You may need to open and read this file in order to get the numid you'll want to use in your command string for /etc/rc.d/rc.local

In this post I'll show you how to set the CD Volume to 80% on each boot. One thing I need is the 'numid' for the CD Volume. So I open /etc/asound.state in my text editor and look for it. Here it is:

Code: Select all

control.21 {
		comment.access 'read write'
		comment.type INTEGER
		comment.count 2
		comment.range '0 - 31'
		iface MIXER
		name 'CD Playback Volume'
		value.0 21
		value.1 21
	}
The numid is 21

Next I enter this command in rc.local

amixer -c 0 cset numid=21 80%

amixer is the alsa utility

-c 0 says this setting is for the first sound card

cset is an amixer agrument to be followed with strings

numid=21 says this is for the CD Volume as shown above in the code snippet. Your sound may not use the same numid for CD Volume.

80% is the volume level for all CD channels.

---------------

I don't think it very necessary to give more examples, because the same proceedure works the same on other controls.

There is however one command, which I think can be very helpful:

amixer info

Outputs this (for me)

Card default 'CK804'/'NVidia CK804 with ALC850 at irq 11'
Mixer name : 'Realtek ALC850 rev 0'
Components : 'AC97a:414c4790'
Controls : 42
Simple ctrls : 27


--------------

For additional information, I've attached amixer's man page in html format.
Attachments
amixer.html.gz
(1.79 KiB) Downloaded 543 times

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#2 Post by disciple »

Isn't it easier to use setvol? Or am I missing something?

Bruce B

#3 Post by Bruce B »

disciple wrote:Isn't it easier to use setvol? Or am I missing something?
Setvol is so simple it doesn't need a howto. Seriously, I didn't know there was a setvol. Thanks.

User avatar
floborg
Posts: 199
Joined: Thu 25 Oct 2007, 12:12
Location: Fort Worth, TX

#4 Post by floborg »

I'm the person Bruce mentioned in the first post. I tried Setvol and it didn't solve the problem. The CD level went right back to 93 after a reboot. So, that's a "no" for Puppy Volume Mixer, alsamixer, and Setvol. I'll try the method posted in the first thread.

Bruce B

#5 Post by Bruce B »

floborg wrote:I'm the person Bruce mentioned in the first post. I tried Setvol and it didn't solve the problem. The CD level went right back to 93 after a reboot. So, that's a "no" for Puppy Volume Mixer, alsamixer, and Setvol. I'll try the method posted in the first thread.
I will admit floborg that I was searching strictly for ALSA utilities. Let me know how it works and we will continue troubleshooting as needed.

Important is: add the entry to /etc/rc.d/rc.local

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#6 Post by disciple »

It sounds like you have just tried using setvol once, then rebooting. I meant running a setvol command in one of the startup scripts (e.g. rc.local)

Or do you have the problem mentioned here where the volume is reset every few minutes?

disciple
Posts: 6984
Joined: Sun 21 May 2006, 01:46
Location: Auckland, New Zealand

#7 Post by disciple »

GuestToo's post here may also be useful: http://www.murga-linux.com/puppy/viewto ... 2576&t=781

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#8 Post by tempestuous »

There are many different volume set/restore utilities.
The one that is officially ALSA-compatible (as GuestToo suggested) is alsactl.
First set your preferred volumes (ideally with alsamixer). Then to store these settings -

Code: Select all

alsactl store
this writes a setup file somewhere, I can't remember where. Then to restore the settings -

Code: Select all

alsactl restore
Add this last command to rc.local and your volumes will come up right at each boot up.

User avatar
floborg
Posts: 199
Joined: Thu 25 Oct 2007, 12:12
Location: Fort Worth, TX

#9 Post by floborg »

Well, a Setvol command in my rc.local file did the trick. It's a satisfactory workaround. The problem is not necessarily fixed.

Funny that Barry mentioned in that post that volume settings are now remembered. That only seems to apply to Master.

tempestuous' idea sounds like a better solution, allowing one to set any control as desired and have it "remembered" at bootup. The question is, "is there a shutdown equivalent to rc.local to stick the alsactl store command?"

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#10 Post by tempestuous »

floborg wrote:is there a shutdown equivalent to rc.local to stick the alsactl store command?
You could add "alsactl store" to the start of /etc/rc.d/rc.shutdown

User avatar
floborg
Posts: 199
Joined: Thu 25 Oct 2007, 12:12
Location: Fort Worth, TX

#11 Post by floborg »

tempestuous wrote:
floborg wrote:is there a shutdown equivalent to rc.local to stick the alsactl store command?
You could add "alsactl store" to the start of /etc/rc.d/rc.shutdown
There's something like that in there already:

Code: Select all

#rc.local0 can read this...
setvol 0 | tr "\n" " " > /etc/.mixervolume
#v2.10 instead, probably do it this way...
/usr/sbin/alsactl store
rc.local0 has this:

Code: Select all

#er, no, rc.alsa reads /etc/asound.state, which has volume, but i'm not
 #sure what writes to asound.state, so better leave this in for now...
 #(alsaconf does)
 #rc.shutdown saved mixer volume...
 [ ! -f /etc/.mixervolume ] && echo -n "50" > /etc/.mixervolume
 setvol 0 `cat /etc/.mixervolume`

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#12 Post by tempestuous »

Code: Select all

#er, no, rc.alsa reads /etc/asound.state, which has volume, but i'm not
#sure what writes to asound.state, so better leave this in for now...
Ah yes, /etc/asound.state is the configuration file created by alsactl.

I suggest you just comment out this section -

Code: Select all

[ ! -f /etc/.mixervolume ] && echo -n "50" > /etc/.mixervolume
setvol 0 `cat /etc/.mixervolume`
that should clear the way for alsactl to do its job properly.
And now that you have mentioned this issue, it's probably necessary to also comment out the "setvol" lines in /etc/rc.d/rc.sysinit

Bruce B

#13 Post by Bruce B »

I'm not sure if there is some confusion or not. But I've found that setvol writes or somehow saves its commands to the key configuration file /etc/asound.state

The downside is I have a card that has more features than setvol, such as surround sound and other things. But this is not a problem for me as there are other ways to deal with these features as I want.

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#14 Post by tempestuous »

I can't comment on whether setvol writes to /etc/asound.state, but I can say for certain that alsactl does write to asound.state
The location of asound.state can vary from distribution to distribution, and may possibly depend on the version of alsa-utils.
Bruce B wrote:The downside is I have a card that has more features than setvol, such as surround sound and other things.
alsactl should be able to save/restore those advanced settings.
Bruce B wrote:I'm not sure if there is some confusion or not
Think about the logic of that statement.

User avatar
floborg
Posts: 199
Joined: Thu 25 Oct 2007, 12:12
Location: Fort Worth, TX

#15 Post by floborg »

tempestuous wrote: I suggest you just comment out this section -

Code: Select all

[ ! -f /etc/.mixervolume ] && echo -n "50" > /etc/.mixervolume
setvol 0 `cat /etc/.mixervolume`
that should clear the way for alsactl to do its job properly.
And now that you have mentioned this issue, it's probably necessary to also comment out the "setvol" lines in /etc/rc.d/rc.sysinit
Didn't find any instances of "setvol" or "alsa" in rc.sysinit.

I'll try editing rc.local0 later on, but I'm already satisfied with the setvol workaround. Maybe Barry K should look into deleting that stuff from rc.local0 in the next release.

Bruce B

#16 Post by Bruce B »

tempestuous wrote:
Bruce B wrote:I'm not sure if there is some confusion or not
Think about the logic of that statement.
Before I do that, may I put the sentence into context after you took it out?

In context:
Bruce wrote:I'm not sure if there is some confusion or not. But I've found that setvol writes or somehow saves its commands to the key configuration file /etc/asound.state
Okay, I thought about the logic of that statement.

I also thought about the logic of taking a sentence out of a paragraph and then telling someone to think about the logic of it.

I also thought about maybe you're picking on me and being pedantic see example below from same post I'm responding to.
tempestuous wrote:The location of asound.state can vary from distribution to distribution, and may possibly depend on the version of alsa-utils.
1) the ALSA documentation says the default location is /etc

2) we are not talking about other distributions, or possible variations between distros, we are talking about Puppy

3) in this very series of threads you have referenced Puppy file locations with specificity. Nobody has countered you with distro to distro variations nonsense and pedantry. Why? For one reason is doesn't apply, were are talking about Puppy.

tempestuous
Posts: 5464
Joined: Fri 10 Jun 2005, 05:12
Location: Australia

#17 Post by tempestuous »

Bruce B,
my comments were meant light-heartedly. There's no need to take offence.
I have serious objections to my work colleagues using the term "I'm not sure of" as a thinly-veiled objection, but I was not applying this situation to you.

Yes, the asound.state file is normally found in /etc
and that's exactly where I have been saving/restoring my ALSA values ever since Puppy v1.04 back in 2005.

But I don't want a Puppy forum member taking me to task if they find this configuration file elsewhere in some other distribution, which is quite feasible.

floborg,
you're right, in Puppy 3 there is no reference to setvol in rc.sysinit,
but it does appear in Puppy 4 at line 130

Code: Select all

setvol 0 `cat /etc/.mixervolume`

Post Reply