Search code examples
bashshellaudiocommand-line-interfacemute

Script to mute audio in Linux from CLI works, but help needed


I often stream news while I work and wanted to mute sound during commercials, so poked around and found this

Stackoverflow thread

which yielded some good things and resulted in the following script which works like a charm:

#!/bin/bash
#Mute, wait, unmute: attempt 1
for x in `amixer controls  | grep layback` ; do 
    amixer cset "${x}" on ; done
echo Mute for how many seconds?
read v1
sleep $v1
for x in `amixer controls  | grep layback` ; do
    amixer cset "${x}" 700% ; done

Which works but results in a godawful mess on the terminal screen: (just a taste)

<p>161 [~]$ MM<br>
numid=16,iface=MIXER,name='Master Playback Switch'<br>
  ; type=BOOLEAN,access=rw------,values=1<br>
  : values=on<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Switch'<br>
numid=15,iface=MIXER,name='Master Playback Volume'<br>
  ; type=INTEGER,access=rw---R--,values=1,min=0,max=127,step=0<br>
  : values=0<br>
  | dBscale-min=-95.25dB,step=0.75dB,mute=1<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Volume'<br>...etc</p>

Which cleans up a bit with the addition of -q after cset

<p>166 [~]$ MM<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Switch'<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Volume'<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Switch'<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Volume'<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Volume'<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Switch'<br>
Wrong control identifier: Playback<br>
Wrong control identifier: Volume'<br>
amixer: Control default element write error: Operation not
permitted<br>...etc</p>

But why am I getting all of these "Wrong control identifier" messages?

I tried to man grep but then my head fell off and I had to tape it back on and now I have a headache. And I'm hungry.

Cheers Omne


Solution

  • Try this:

    read -p "Mute for how many seconds? " v1 ; \
    amixer controls  | grep layback | grep -v ' Ma' | 
        xargs -I '{}' amixer -q cset '{}' on ; \
    sleep $v1 ; \
    amixer controls  | grep layback | grep -v ' Ma' | 
        xargs -I '{}' amixer -q cset '{}' 700% 
    

    Notes:

    1. The main trouble seems to be mixers like 'Playback Con Mask' and 'Playback Channel Map' were incompatible with the amixer options. Using grep -v to filter those mixers out fixes that.
    2. The read should go first, otherwise the mute lasts for the input seconds plus the time it takes the user to input them.
    3. xargs isn't really necessary here, but it seems simpler than using a loop with variables.
    4. Those ; \s make it easier to copy and paste to the command line in one shot. In a script the ; \ line endings aren't needed.