16

When I plug in my headphones the audio output device switches to the headphones from my speakers.

Rather than plugging/unplugging the headphones all the time, which will eventually wear out my cord/jack I would like to just use a software switch.

When I open Sound Settings and switch from "Headphones -> Built-in Audio" to "Line Out -> Built-in Audio" no sound comes out of my speaker or headphones.

I opened alsamixer to see what happens when I plug/unplug the headphones.

Headphones in: Headphones in

Headphones out: Headphones out

With the headphones plugged in I manually changed all the settings to match the settings when the headphones are out and still didn't get any sound through the speaker.

As a side note, changing the audio output device through the "Sound Settings" causes the same changes to alsamixer.

  • 1
    As far as I know, it is a hardware switch inside the Headphone jack that will not allow for switching. Once the switch is engaged, the speakers are disabled. Have you thought about maybe using Bluetooth headphones? – Terrance Dec 22 '15 at 21:59
  • Thanks @Terrance, I had the feeling it might be something like that, I even checked BIOS to see if there was something in there. I'm just a bit surprised that they would have a hardware solution for what should be a software problem. – user276318 Dec 22 '15 at 22:04
  • 1
    Keep in mind the sound card may not be capable of multiple device output - some sound cards are hard coded at the hardware level to turn off speaker output when a headphone connection is made, and that's not able to be overridden... – Thomas Ward Dec 23 '15 at 12:05
  • @Terrance I started a bounty, maybe you want to dig further into it and write an answer? – dessert Feb 13 '18 at 16:08
  • @dessert see GoodGuyNick's answer below -- it may not work on all hardware, and in my experience it doesn't separate the outputs (which would allow you to send programA's audio to speakers and programB's to headphones at the same time), but if simply disabling the auto-switch is needed, that's the first thing to try. – quixotic Feb 13 '18 at 16:25
6

To make it possible to switch between speakers and headphones, follow these steps:

  1. Run alsamixer.
  2. If necessary, select your sound card with F6.
  3. Navigate to “Auto-Mute” with the right arrow .
  4. Disable it with the down arrow .
  5. Press Esc to exit.

Or you can do all these steps with one command:

  1. Open terminal
  2. run amixer -c 1 set 'Auto-Mute Mode' Disabled

Now you can change between speakers and headphones in the PulseAudio Volume Control.

After applying these instructions you can make your life easier by using Sound Switcher Indicator to quickly switch between headphones and speakers.

  • 2
    I Don't think that this will work for all hardware combinations - some systems (like mine) do seem to use a hardware switch of some variety, which is not unknown. For example, if you plug a headphone into a guitar amp, the speakers in that amp will usually be turned off - and there is no computer in those devices.... – Charles Green Feb 13 '18 at 17:03
  • This worked for me on a Realtek ALC887-VD – Paul Praet Mar 10 '18 at 10:59
  • Thank you. It worked for me. Is there any way to accomplish it via bash script? – ka3ak Sep 8 '18 at 6:54
  • 1
    @ka3ak I've updated the answer for you – GoodGuyNick Sep 9 '18 at 17:47
5

Having both speakers and headphones plugged in and switching in software on-the-fly

By design, Pulseaudio automatically turns off Line Out when headphones are plugged in and uses Headphone slider instead. You can observe this behavior in alsamixer. What we want is to have Headphone and Line Out sliders working separately and at the same time. This is extremely useful if you want to remap Realtek's jacks to have, say, Rear Green for headphones and Blue for speakers (with the help of hdajackretask from alsa-tools).

To achieve this, you should directly edit Pulseaudio mixer's configuration.

1. We tell pulseaudio that headphones are always plugged in.

Edit:

/usr/share/pulseaudio/alsa-mixer/paths/analog-output-lineout.conf

Find:

[Jack Headphone]
state.plugged = no
state.unplugged = unknown

Change no to yes.

If you're using the jacks on the front of your computer's case change [Jack Front Headphone] instead.

2. By default, Line Out's volume controlled only by Master, and not by Line Out slider itself.

We want to merge Line Out with Master. Add this snippet to the end of the file:

[Element Line Out]
switch = mute
volume = merge

3. We need to completely cut off Line Out when we use headphones.

Edit:

/usr/share/pulseaudio/alsa-mixer/paths/analog-output-headphones.conf

Add this snippet to the end of the file:

[Element Line Out]
switch = off
volume = off

On some systems you may also need to disable Front by adding this snippet:

[Element Front]
switch = off
volume = off

4. Like Pulseaudio, Alsa itself cuts off speakers when headphones are plugged in.

Open alsamixer: alsamixer -c0 or alsamixer -c1

Use / to Auto-Mute mode. Disable Auto-Mute Mode with . Close alsamixer with Esc.

Save your changes with: alsactl store

5. Restart Pulseaudio

$ pulseaudio -k
$ pulseaudio --start

Now you have two separate ports on the same sink in pulseaudio. They mute each other, so you can switch to headphones and this will mute Line Out, and vice versa. To switch between ports you can use Gnome or Plasma sound mixer, or install appropriate desktop extension.


Source

  • You shouldn't edit files directly in /usr/share. If pulseaudio receives an update (for example to fix bugs), your changes will get wiped out. Usually there's a mechanism to provide override files in /etc or similar, but I don't know about pulseaudio specifically. – Robie Basak Feb 15 '18 at 23:55
  • @RobieBasak The source of this information is Arch Linux website. Unfortunately I'm not an Arch Linux user so I can't repost your comment on their site. Hopefully one of the Arch Linux users floating around here reads your comment and reposts it there. – WinEunuuchs2Unix Feb 16 '18 at 0:01
  • 1
    @RobieBasak, well, one could write a wrapper script that does this and invoke it after login – Robert Riedl Feb 16 '18 at 11:16
  • Unfortunately it didn't work for me, but thank you for your answer! – dessert Feb 20 '18 at 19:57
  • @dessert My pleasure. It was interesting researching this area. I hope you find what you are looking for. – WinEunuuchs2Unix Feb 21 '18 at 0:15
4
+250

hdajackretask is a tool to reassign the function your jacks for different input, output or disabled - e g, turn your Mic jack into an extra Headphone, or why not make them both line outs and connect them to your surround receiver?

Install alsa-tools-gui

sudo apt-get update
sudo apt-get install alsa-tools-gui

Run hdajackretask Then To override one of your jacks:

  1. Select a codec
  2. Click the "Override" checkbox for that pin and select the new desired function
  3. Click "Apply now"

    Note: If the headphone jack has a built in hardware switch that re-routes the audio then you are SOL (Sorry outta luck). However, if the OS detects when a line is plugged in the headphone jack then the audio is most likely not mechanically switched.

I found that overriding both the headphones and speakers at the same time works best.


hdajackretask Screenshot to switch the headphone to speakers:

enter image description here


After you click "Apply now" a shell script is created to make the changes and prompts for super user password to execute. The script is also saved in /tmp which can be copied and later executed with sudo from the command line.

enter image description here

Command line execution

As mentioned previously,hdajackretask creates a script, script.sh in /tmp similar to /tmp/hda-jack-retask-QW1EEZ/ for the jack functions you desire:

#!/bin/sh
echo "0x12 0x90a60140" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x14 0x90170150" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x17 0x40000000" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x18 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x19 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x1a 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x1b 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x1d 0x40700001" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x1e 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo "0x21 0x40f000f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log
echo 1 | tee /sys/class/sound/hwC0D0/reconfig 2>>/tmp/hda-jack-retask-WFEIEZ/errors.log


Copy this script somewhere else on your system (other than /tmp)

cd /tmp/hda-jack-retask-QW1EEZ
cp script.sh ~username/speaker.sh

Then edit speaker.sh

cd ~username
nano speaker.sh

Add the line killall pulseaudio & at the top of the file. This is necessary because the jack function can not be changed while pulseaudio is running. By default pulseaudio will respawn. Also change the path to errors.log file to /tmp or wherever you like:

#!/bin/sh
killall pulseaudio &
echo "0x12 0x90a60140" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x14 0x90170150" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x17 0x40000000" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x18 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x19 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x1a 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x1b 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x1d 0x40700001" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x1e 0x411111f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo "0x21 0x40f000f0" | tee /sys/class/sound/hwC0D0/user_pin_configs 2>>/tmp/errors.log
echo 1 | tee /sys/class/sound/hwC0D0/reconfig 2>>/tmp/errors.log


To run the script:

sudo ./speaker.sh



From the alsa-tools-1.1.5/hdajackretask/README documentation:

Documentation for hdajackretask

Most HDA Intel soundcards are to some degree retaskable, i e can be used for more than one thing. This tool is a GUI to make it easy to retask your jacks - e g, turn your Mic jack into an extra Headphone, or why not make them both line outs and connect them to your surround receiver?

Quickstart

Start the application "hdajackretask" from the command line. Select a codec in the top bar; some people have only one - if you have more than one, one is the "main" one, and the rest are probably HDMI codecs.

All jacks (and other inputs/outputs) are shown under "Pin configuration". To override one of your jacks, click the "Override" checkbox for that pin and select the desired function. Note: If you select a "Line out (back)" override, you should also have a "Line out (front)" override. A "Line out (Center/LFE)" override will only work with the previous two overrides, and so on.

You can try out your new configuration by clicking "Apply now". To make your new configuration the boot up default, click "Install boot override". Doing any of these requires root privileges, so you will likely be asked for your password.

Quickend

Did you screw up? Click the "Remove boot override" button, and once the process has completed, shut down your computer, wait a little while and turn it on again. You should now be back where you started.

Did it work?

If it did, be happy, share your joy, do something nice!

If it didn't, here are some things to try:

  • Go into alsamixer and see if there are new volume controls, and see if they are unmuted and set to a reasonable value.

  • Sometimes the driver does not shut off unconnected pins (if they're unconnected, why worry?). You might be able to fix this by turning your computer completely off for a little while.

  • See the "model=auto" option below

If this does not help, chances are high you're running into either a driver or a hardware limitation. You might run into driver bugs (maybe nobody has ever tested four headphones before!). If you do, feel free to report them on the alsa-devel mailinglist: however, due to lack of manpower, chances are this will be dealt with at a very low priority (having sound working out of the box for a standard configuration, is more important than supporting strange configurations). However, if you are a kernel hacker and come up with a patch that solves your problem without screwing up anything else, chances are pretty high it will be merged.

If you suspect the problem is with this GUI application rather than the kernel driver, feel free to write me an email (and if you like, include a patch!). If so, write to diwic@ubuntu.com.

Options

  • Show unconnected pins Your BIOS is responsible for setting up what pins on the codec that are actually connected to something and which ones are not. Sometimes BIOS is buggy, and will not show all your jacks. If you have a jack your BIOS says you haven't, you can try enabling random pins and see if it works.

  • Set Model=auto Some codecs, especially older ones and on kernels 3.8 and below, are hard-coded to use a specific model, and thus will not care about your overrides. In many cases and with a reasonably new kernel, the auto parser now works well for these codecs as well. You can force the auto parser to be used by checking this box. In some cases, though, the explicit model is there for a reason, if so, you're stuck.

  • Advanced override This is for the experts only. It makes you select each configuration field individually, instead of just a few predefined values that make sense. Note that most combinations here are invalid in one way or the other, so you should probably not mess with this unless you have read and understood the "Configuration Default" section of the HD Audio specification. (Which, at the time of this writing, is available here: http://www.intel.com/content/dam/www/public/us/en/documents/product-specifications/high-definition-audio-specification.pdf )

  • Parser hints This enables you to send special "hints" to the driver that causes parsing to behave differently. Leave them at the "default" setting unless you have read the driver documentation. ( Which, at the time of this writing, is available here: https://www.kernel.org/doc/Documentation/sound/alsa/HD-Audio.txt - see the "Hint strings" section. )

  • 1
    Unfortunately it didn't work for me, but thank you for your answer! As yours comes very close to what I hoped for and seems to provide the best way to achieve the goal (if possible) you're awarded the bounty – congrats! :) – dessert Feb 20 '18 at 20:03
2

In KDE this can be done:

system settings -> multimedia -> audio and video settings -> audio hardware setup tab > select the appropriate sound card > under Connector you shall see headphones and speakers option.

system settings

  • 2
    I'm not using kde but Unity has a similar "Sound Settings". Changing the audio output device from headphones to speakers will only mute the headphones. It appears to be a hardware switch when the headphones are plugged in. – user276318 Dec 23 '15 at 8:09
1

Gnome-Shell Extension Audio-Output-Switcher

the simplest way is just install audio output switcher extension

or you can clone with git directly:

git clone https://github.com/adaxi/audio-output-switcher.git ~/.local/share/gnome-shell/extensions/audio-output-switcher@anduchs

screenshot

This extension adds a little entry to the status-menu that shows the currently selected pulse-audio-output device. Clicking on that will open a submenu with all available output devices and let's you choose which one to use.

  • Unfortunately it didn't work for me, but thank you for your answer! – dessert Feb 20 '18 at 19:57
0

You could use Sound Switcher Indicator, if you don't mind adding a PPA.

This will give you an indicator in the system tray that you can click on to change the input or output audio sources. You can read more at http://yktoo.com/en/software/indicator-sound-switcher.

Terminal Method

To install, using a terminal enter the following.

sudo add-apt-repository ppa:yktooo/ppa
sudo apt-get update
sudo apt-get install indicator-sound-switcher

GUI Method

To install, using the GUI, follow these steps:

  1. Launch the Software & Updates app from the Unity Dash.

  2. Click on the on the "Other Software" tab. Then add the following new source:

    deb http://ppa.launchpad.net/yktooo/ppa/ubuntu vivid main
    

    (Remember to replace "vivid" with your Ubuntu release. Supported releases are listed at http://ppa.launchpad.net/yktooo/ppa/ubuntu/dists/).

  3. Launch Ubuntu Software Center from the Unity Dash.

  4. Search for "Sound input/output selector indicator", and install it.

Note: This solution won't work if your system has a hardware switch inside the Headphone jack, as Terrance suggested above.

  • I actually already had this installed. As in your note, it appears there is a hardware switch. – user276318 Dec 23 '15 at 8:10

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.