Pages

Friday, March 28, 2014

Control volume via command line

I have a neighbour who complains currently about my music which is too loud in
the night. So we agreed that I turn of my music at 11PM. First I tried to look
on my watch and to turn off music at 11PM. Most I failed and my neighbour stands
in my door. The fun partis that I'm listening to music via my Linux based HTPC.
So I deceided to create a cronjob that sets the volume to 0 every day at 11PM.
The first thing I needed was a list of all available alsa controls (where -c 0
is the first sound card):
 
# amixer -c 0 scontrols
Simple mixer control 'Master',0
Simple mixer control 'Headphone',0
...


To turn off just everything I like to use the Master conrol. The current volume
level for the Master control can be easily checked with:

# amixer -c 0 sget "Master"
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 31 [100%] [0.00dB] [on]


The current volume is set to 100%. To set the volume level to 0% I use:

# amixer -c 0 sset "Master" 0%
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 0 [0%] [-46.50dB] [on]


And back to 100%:

# amixer -c 0 sset "Master" 100%
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 31 [100%] [0.00dB] [on]


Setting the volume level via command line is very easy. Another chance is to
just mute the volume. The volume level stays at eg. 100% but it is like
somebody just unplug the speakers:

# amixer -c 0 sset "Master" playback mute
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 31 [100%] [0.00dB] [off]


Note the state in the last line. Off means that the control is muted. Also the
volume level is still at 100%. To unmute use:
 
# amixer -c 0 sset "Master" playback unmute
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 31 [100%] [0.00dB] [on]


It is also possible to toggle between mute and unmute:

# amixer -c 0 sset "Master" playback toggle
Simple mixer control 'Master',0
  Capabilities: pvolume pvolume-joined pswitch pswitch-joined
  Playback channels: Mono
  Limits: Playback 0 - 31
  Mono: Playback 31 [100%] [0.00dB] [off]


The last thing i did was to create a cronjob that turns the volume level to
zero at 11PM every day:

# crontab -e
...
0 23 * * * /usr/bin/amixer -q -c 0 sset "Master" 0
...


I have just added the -q option so that amixer won't create any ouput.