script to turn off volume when headphones are disconnected

penguin-159784_640

It has happened to me that while people were sleeping in the early hours of the morning, I listened to heavy music at full volume and by mistake I disconnected the headphones which caused it to be played throughout the house (I think they hated me at that time haha). So program a little script that when disconnected, mutes the volume:

The first thing was to install alsa-utils to be able to manage the volume from commands easily:

sudo pacman -S alsa-utils

The next thing was to discover through a Google search that it alsa saves in plain text files (it can be read with any text editor, in Windows all this would be much more complicated) the status of the sound cards and that it only takes copy the file and then compare the changes.

The file is:
/proc/asound/card0/codec#0

This may vary in some cases, for example if we have more than one sound card. If you want to know more I leave the link of the alsa documentation: http://alsa.opensrc.org/Proc_asound_documentation

I connect the headphones and copy the file:
mkdir $HOME/.audifonos && cp "/proc/asound/card0/codec#0" "$HOME/.audifonos/con.txt"

Then I unplug the headphones and use the diff command which shows the different lines between the two files:
diff "/proc/asound/card0/codec#0" ".audifonos/con.txt"

There is some data that we are not interested in so we have to filter with grep so that it only shows the lines that have the word "Pin-ctls".

diff "/proc/asound/card0/codec#0" ".audifonos/con" | grep Pin-ctls

In case the command does not show anything on the screen, it means that we have headphones on. as it cannot return anything, it returns an error.

To see this error we type:
echo $?

If it shows us a one per screen, it tells us about the error.

comfort

on the other hand, if when we execute this it returns something then we have the headphones disconnected in which case it will not return an error:
echo $?

Hello

With this in mind and knowing bash it is easy to do it:

Headphones.sh

I download it, I give execution permissions to open it for the first time so that it “installs” (it only asks to connect headphones and copy the file that it explains at the beginning)
wget http://paste.desdelinux.net/?dl=5178 && chmod +x audifonos.sh && ./audifonos.sh

To finish I make it start automatically every time I turn on the pc

From Xfce I go to applications> configuration> Session and start> applications autostart> add and place the script


Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Tiles said

    Do you happen to use a WM? It is that at least in Arch with Gnome-Shell 3.16 it preserves the changes in the volume controls, for example, when I connect the HDMI it shows me the maximum volume, when I connect headphones it shows me very low volume and in the built-in speakers almost the most.

    Although in fact I was looking to do that for some time, it was a bitch having to lower the volume to be able to use the speakers so that the sound was not shrill or annoying for others. Thank you very much for the contribution.

    1.    rain said

      for nothing 🙂, I use xfce 😛

      1.    Tiles said

        I just remembered that I was once watching p0rn and had the headphones and the hdp disconnected and heard Shyla's moans: c
        The good thing is that it was only a fragment of at least an eternal second and it sounded like horror movies when a prostitute was killed XD
        Thinking it over I'm going to add it to favorites

  2.   Yoyo Moreno said

    Hahahahahaha the same thing happened to me when I unintentionally disconnected it at midnight and a good Hard Core sounded at full volume XD my family was scared: v

  3.   noxonsoftwares said

    Good information will be useful to me at some point. Thanks a lot

  4.   Andreser said

    You are not fooling me, you were watching for * or gay in Spanish and your headphones were disconnected xD
    Thanks for the contribution, in Linux Mint the volume is preserved, for example if I put mute and then connect the headphones, in the headphones I will have sound and without them not.
    Greetings.

    1.    rain said

      hahaha, no but in those cases it also works (?)

      1.    Andreser said

        Yes, it also works in those cases (͡ ° ͜ʖ ͡ °)

  5.   deni said

    Good information, a suggestion, if the system supports 'devd' how do I proceed to copy or create this file?

    [code] $ PREFIX / share / examples / cups / ulpt-cupsd.conf to $ PREFIX / etc / devd / [/ code]

    1.    rain said

      I could not tell you, is not the cp command enough?

      1.    deni said

        No, it says that directory does not exist and I don't know how to create either.

  6.   fernando said

    Heheheh well if I tell you it has happened to me on more than one occasion and with the same music so I write it down to install it later. Greetings and in the name of Heavy Metal thank you.

  7.   David said

    …… ~$ sudo pacman -S alsa-utils
    [sudo] password for david:
    sudo:pacman:command not found