Automute speakers when plugging in headphones

Add your Tips and tricks to configure and tweak your GhostBSD System here.
Post Reply
User avatar
neville
Developer
Posts: 84
Joined: Fri Aug 13, 2021 10:27 am
Location: Japan

Automute speakers when plugging in headphones

Post by neville »

Here's a quick guide on how to automute your speakers when you plug headphones into the headphone jack of your desktop computer.
First you have to find out your audio pins configuration. To do this run this command

Code: Select all

sysctl -a | grep hda
You'll get a lot of output, but this is the part we need:

Code: Select all

dev.hdaa.0.nid38: audio mixer [DISABLED]
dev.hdaa.0.nid37: audio output [DISABLED]
dev.hdaa.0.nid36: vendor widget [DISABLED]
dev.hdaa.0.nid35: audio mixer
dev.hdaa.0.nid34: audio mixer
dev.hdaa.0.nid33: vendor widget [DISABLED]
dev.hdaa.0.nid32: vendor widget [DISABLED]
dev.hdaa.0.nid31_original: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid31_config: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid31: pin: Speaker (None) [DISABLED]
dev.hdaa.0.nid30_original: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid30_config: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid30: pin: Speaker (None) [DISABLED]
dev.hdaa.0.nid29_original: 0x4025c641 as=4 seq=1 device=Headphones conn=None ctype=Optical loc=0x00 color=Res.C misc=6
dev.hdaa.0.nid29_config: 0x4025c641 as=4 seq=1 device=Headphones conn=None ctype=Optical loc=0x00 color=Res.C misc=6
dev.hdaa.0.nid29: beep widget
dev.hdaa.0.nid28_original: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid28_config: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid28: pin: Speaker (None) [DISABLED]
[color=#FF0040]dev.hdaa.0.nid27_original: 0x02214020 as=2 seq=0 device=Headphones conn=Jack ctype=1/8 loc=Front color=Green misc=0
dev.hdaa.0.nid27_config: 0x02214020 as=2 seq=0 device=Headphones conn=Jack ctype=1/8 loc=Front color=Green misc=0
dev.hdaa.0.nid27: pin: Headphones (Green Jack)[/color]
dev.hdaa.0.nid26_original: 0x0181303f as=3 seq=15 device=Line-in conn=Jack ctype=1/8 loc=Rear color=Blue misc=0
dev.hdaa.0.nid26_config: 0x0181303f as=3 seq=15 device=Line-in conn=Jack ctype=1/8 loc=Rear color=Blue misc=0
dev.hdaa.0.nid26: pin: Line-in (Blue Jack)
dev.hdaa.0.nid25_original: 0x02a19040 as=4 seq=0 device=Mic conn=Jack ctype=1/8 loc=Front color=Pink misc=0
dev.hdaa.0.nid25_config: 0x02a19040 as=4 seq=0 device=Mic conn=Jack ctype=1/8 loc=Front color=Pink misc=0
dev.hdaa.0.nid25: pin: Mic (Pink Jack)
dev.hdaa.0.nid24_original: 0x01a19030 as=3 seq=0 device=Mic conn=Jack ctype=1/8 loc=Rear color=Pink misc=0
dev.hdaa.0.nid24_config: 0x01a19030 as=3 seq=0 device=Mic conn=Jack ctype=1/8 loc=Rear color=Pink misc=0
dev.hdaa.0.nid24: pin: Mic (Pink Jack)
dev.hdaa.0.nid23_original: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid23_config: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid23: pin: Speaker (None) [DISABLED]
dev.hdaa.0.nid22_original: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid22_config: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid22: pin: Speaker (None) [DISABLED]
dev.hdaa.0.nid21_original: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid21_config: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid21: pin: Speaker (None) [DISABLED]
[color=#FF0080]dev.hdaa.0.nid20_original: 0x01014010 as=1 seq=0 device=Line-out conn=Jack ctype=1/8 loc=Rear color=Green misc=0
dev.hdaa.0.nid20_config: 0x01014010 as=1 seq=0 device=Line-out conn=Jack ctype=1/8 loc=Rear color=Green misc=0
dev.hdaa.0.nid20: pin: Line-out (Green Jack)[/color]
dev.hdaa.0.nid19: vendor widget [DISABLED]
dev.hdaa.0.nid18_original: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid18_config: 0x411111f0 as=15 seq=0 device=Speaker conn=None ctype=1/8 loc=Rear color=Black misc=1
dev.hdaa.0.nid18: pin: Speaker (None) [DISABLED]
dev.hdaa.0.nid17_original: 0x4037c040 as=4 seq=0 device=CD conn=None ctype=Analog loc=0x00 color=Res.C misc=0
dev.hdaa.0.nid17_config: 0x4037c040 as=4 seq=0 device=CD conn=None ctype=Analog loc=0x00 color=Res.C misc=0
dev.hdaa.0.nid17: pin: CD (None) [DISABLED]
dev.hdaa.0.nid16: audio output [DISABLED]
dev.hdaa.0.nid15: audio mixer [DISABLED]
dev.hdaa.0.nid14: audio mixer [DISABLED]
dev.hdaa.0.nid13: audio mixer
dev.hdaa.0.nid12: audio mixer
dev.hdaa.0.nid11: audio mixer
dev.hdaa.0.nid10: audio input [DISABLED]
dev.hdaa.0.nid9: audio input
dev.hdaa.0.nid8: audio input
dev.hdaa.0.nid7: vendor widget [DISABLED]
dev.hdaa.0.nid6: audio output [DISABLED]
dev.hdaa.0.nid5: audio output [DISABLED]
dev.hdaa.0.nid4: audio output [DISABLED]
dev.hdaa.0.nid3: audio output
dev.hdaa.0.nid2: audio output
This is my cat /dev/sndstat

Code: Select all

installed devices:
pcm0: <Realtek ALC887 (Rear Analog)> (play/rec) default
pcm1: <Realtek ALC887 (Front Analog)> (play/rec)
No devices installed from userspace.

On desktop computers, the audio jack for output to the speakers is a light green colour and this is typically the same colour for the front jack for headphones. So you need to look for this colour in the output above and take note of the nidxx number and the as=xx number for the green colour jack connectors. Also look for the word Headphones. In my case these are the entries:

Headphones (Front panel)

Code: Select all

dev.hdaa.0.[color=#FF0080]nid27[/color]_original: 0x02214020 [color=#FF0080]as=2 seq=0[/color] device=Headphones conn=Jack ctype=1/8 loc=Front color=Green misc=0
dev.hdaa.0.nid27_config: 0x02214020 as=2 seq=0 device=Headphones conn=Jack ctype=1/8 loc=Front color=Green misc=0
dev.hdaa.0.nid27: pin: Headphones (Green Jack)
Rear (Speaker Jack)

Code: Select all

dev.hdaa.0.[color=#FF0080]nid20[/color]_original: 0x01014010 as=1 seq=0 device=Line-out conn=Jack ctype=1/8 loc=Rear color=Green misc=0
dev.hdaa.0.[color=#FF0080]nid20[/color]_config: 0x01014010 [color=#FF0080]as=1 seq=0[/color] device=Line-out conn=Jack ctype=1/8 loc=Rear color=Green misc=0
dev.hdaa.0.nid20: pin: Line-out (Green Jack)
My headphone nid number is 27 (nid27) and my headphone as number is 2 (as=2)
My rear jack nid number is 20 (nid20) and the as number is 1 (as=1)

as=1 means association 1 and as=2 means association 2

Your numbers may be different. So what we have to do is give the headphone pin (nid=27) the same association number as the rear jack pin (nid20), in this case we change the headphone pin from as=2 into as=1. This associates the two pins and directs the audio output to the front panel jack when the headphones are connected. We also have to change the headphone tuneable from seq=0 to seq=15 to make the automute function work.
We have to change this:

Code: Select all

dev.hdaa.0.nid27_config: 0x02214020 as=2 seq=0
to this

Code: Select all

dev.hdaa.0.nid27_config: 0x02214020 as=1 seq=15
To do this, we have to edit the /boot/device.hints file

Login as root and run:

Code: Select all

#ee /boot/device.hints
add this line to the bottom

Code: Select all

hint.hdaa.0.nid27.config="as=1 seq=15"
Change your own numbers if needed. Then reboot for the changes to take effect. That's it. Now when you're using audio, just plug in your headphones and your speakers should automute.

My cat /dev/sndstat is now this:

Code: Select all

Installed devices:
pcm0: <Realtek ALC887 (Analog 2.0+HP/2.0)> (play/rec) default
pcm1: <Realtek ALC887 (Front Analog Mic)> (rec)
No devices installed from userspace.
For laptops, the procedure is the same but there is no green rear audio jack so you have to find the nid=xx and as=xx for connected speakers, not green audio jack. I'll edit this post for laptops in the near future.
hans.wolters
Posts: 6
Joined: Fri Oct 28, 2022 9:41 am

Re: Automute speakers when plugging in headphones

Post by hans.wolters »

Thanks, this fixed one of my smaller problems. I can plugin the headphone again where I want it. Soundsystem with external boxes in the rear and if I plugin my headphone in the frontpanel it switches.
Post Reply