Monday, April 29, 2024
 Popular · Latest · Hot · Upcoming
11
rated 0 times [  11] [ 0]  / answers: 1 / hits: 9298  / 1 Year ago, wed, may 24, 2023, 1:27:35

Is there a way of muting sound from my computer everytime I unplug my headphones (Like a phone does) to stop sound then playing out of my speakers?


More From » sound

 Answers
5

Howto detect an unplug


Basically what worked for me was:


# When plugged in:
cat /proc/asound/card0/codec#0 > pluggedin.txt

# When not plugged in:
cat /proc/asound/card0/codec#0 > notplugged.txt

# Then compare the differences
diff pluggedin.txt notplugged.txt

For me the difference was in 'Node 0x16' under 'Amp-Out vals':


Node 0x16 [Pin Complex] wcaps 0x40058d: Stereo Amp-Out             Node 0x16 [PinComplex] wcaps 0x40058d: Stereo Amp-Out
Amp-Out caps: ofs=0x00, nsteps=0x00, stepsize=0x00, mute=1 Amp-Out caps:ofs=0x00, nsteps=0x00, stepsize=0x00, mute=1
Amp-Out vals: [0x80 0x80] | Amp-Out vals: [0x00 0x00]

So I based the detection on the difference found.


Howto mute


With this knowledge you can have a script running in the background. If unplugged the scripts mutes your speakers like using amixer sset Master playback 0% (or any other command).


#!/bin/bash
# This scripts detecs unplugging headphones.

oldstatus="unrelated string"
while [ 1 ]; do
# The following line has to be changed depending on the difference (use diff) in '/proc/asound/card0/code#0'
status=$(grep -A 4 'Node 0x16' '/proc/asound/card0/codec#0' | grep 'Amp-Out vals: [0x80 0x80]')
if [ "$status" != "$oldstatus" ]; then
if [ -n "$status" ]; then
echo "Plugged in"
amixer sset Master playback 80% # Set volume to 80%
oldstatus="$status"
else
echo "Unplugged"
amixer sset Master playback 0% # Mute
oldstatus="$status"
fi
fi
done

You can make it executable with chmod +x scriptname.sh and put it in the startup applications. You will have to adjust the unplug detection though by finding your own difference in /proc/asound/card0/codec#0 (maybe even change the numbers here for multiple soundcards.


Related Links:


https://wiki.ubuntu.com/Audio/PreciseJackDetectionTesting


https://unix.stackexchange.com/questions/25776/detecting-headphone-connection-disconnection-in-linux


How to automatically change volume level when un-/plugging headphones?


[#38623] Thursday, May 25, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
etzelmortg

Total Points: 430
Total Questions: 105
Total Answers: 106

Location: Suriname
Member since Sun, Jun 13, 2021
3 Years ago
;