Is there a way to switch sound output between speakers and headphones without physically unplugging any on a Realtek built-in audio?

Hi all!

I know that sound output switching works ok when I physically remove/connect my headphones, but is there a way to achieve the same without unlugging anything?

I have to say that Windows does show speakers and headphones as separate output devices and allows switching between them easily – so I’m quite sure the same can be achieved in Linux.

I think I’ve seen them as separate devices on older (not recent) versions of Fedora as well but I can be mistaken here.

I’ve searched for solution for some time but couldn’t find anything good. I do use a rather crude workaround for this with alsamixer – but it’s rather clumsy and switches between outputting sound to both devices simultaneously or headphones only – not an elegant solution.

Any suggestions are welcome!

P.S. I use desktop computer with Asus motherboard’s integrated audio. Manual says it’s Realtek ® ALC892 8-channel High Definition Audio CODEC.

aplay -l shows it as

card 0: PCH [HDA Intel PCH], device 0: ALC892 Analog [ALC892 Analog]

whereas lspci shows

Audio device: Intel Corporation 7 Series/C216 Chipset Family High Definition Audio Controller (rev 04)
	Subsystem: ASUSTeK Computer Inc. Device 841b
	Kernel driver in use: snd_hda_intel
	Kernel modules: snd_hda_intel

The same behavior on my work computer with Realtek ® ALC887 8-channel high definition audio CODEC*

aplay -

PCH [HDA Intel PCH], device 0: ALC887-VD Analog [ALC887-VD Analog]

lspci:

Audio device: Intel Corporation Cannon Lake PCH cAVS (rev 10)
	DeviceName: Onboard - Sound
	Subsystem: ASUSTeK Computer Inc. Device 86c7
	Kernel driver in use: snd_hda_intel
	Kernel modules: snd_hda_intel, snd_soc_skl, sof_pci_dev

I think it should be possible to separate outputs somehow for alsa but I haven’t found a way to do it.

1 Like

Mmm :thinking: is this too simple?
In Settings → Sound, in the Output Device I can switch between Speakers and Headphones.


I mean: do I understand your question?

4 Likes

Did you search among GNOME Shell extensions?
Keywords: audio, sound, output.

1 Like

@alciregi, that would indeed be too simple. I’m also quite sure that’s how I did it in the older (not recent) versions of Fedora.

But right now in Settings → Sound i see two outputs. One is Digital output S/PDIF and that’s clearly not what we need here.

The other is analog output. It says Speakers when I unplug my headphones, it says “Headphones” when my headphones are plugged in. So where you’re seeing two options side by side I see only one or another – but not both, and I can’t switch between them. I wouldn’t be asking otherwise. So sorry, your answer is not a solution in my particular case.

So it looks like my hardware does present just one (sort of shared between headphones and speakers) analog output to the system. Though I’m quite sure there’s a way to make these outputs separate – how Windows threats them.


I did, tested some of them, but couldn’t find anything that allowed switching between headphones and speakers on my system.


As I said, this is buit-in audio on an Asus motherboard. Manual says it’s Realtek ® ALC892 8-channel High Definition Audio CODEC.

1 Like

Yes, I imagined that.

Just a guess: maybe there is an option to pass to the module (modprope and so on)?

@alciregi, thanks for the suggestion, I’ll look into it.

Have you tried pavucontrol? I use this to switch between the different configurations on my computer (which bluetooth device or headphones, speakers connected to computer and hdmi for the monitor). I use this on Xfce and have a plugin in my panel that brings it up and lets me select.

john

Yes, I did. It essentially shows me just one output too (as Sound Settings does), and this output switches to headphones or speakers dynamically.


Right now (especially after seeing screenshot by @alciregi) I’m quite sure that this behavior is specific to alsa handling of (newer?) Realtek sound chipsets, whereas when I written my question I thought that it could be a part of Gnome’s streamlining of sound settings.

When doing some research on the matter I also saw some mentions about remapping sound card’s outputs by changing alsa config files (~/.alsarc), maybe this can potentially help me solve the issue. But as of now I haven’t found a good guide for how to do so.

I highly doubt that ALSA is where you want to fiddle with this. It may be possible to do it that way, but it’s likely that it’ll mess something up for PulseAudio, or that PulseAudio will mess you up at some point in the future. (Though I guess that was more of a danger when it was in active development, now it seems like it’s in a holding pattern.)

@jbs4115 beat me to the pavucontrol suggestion, as that would’ve been my first instinct as well. At the risk of belaboring that point, did you check all of the tabs?

For example, my USB audio device (a literally $10 jobbie I got from Newegg) doesn’t do any kind of fancy auto-switching, but it does have a bunch of different output configurations because it’s one of those units with a combo analog/digital headphone jack, where there’s an LED at the bottom of the port so you can stick a specially-shaped optical cable in there and use SP/DIF. As a result, it has quite a few different configurations.

On the “Output Devices” tab, there’s a Port selector, but just like yours it only ever shows one option:

But that’s because what option is shown there is controlled in the Configuration tab:

So, my first suggestion is definitely to double-check the Configuration tab, and make sure there’s nothing there that will help you.

If there really isn’t, then and only then I’d try going the alsa route:

# Find the card# of the device
$ aplay -l
# Load the mixer controls for the card
$ alsamixer -c $CARD_NUM

Left and right arrow keys move between controls. Up and down to change levels or select between options. Hitting M toggles on/off state. Type Esc to quit — don’t use Q, as that’ll raise the left channel level of the control you’re on by one step. (If you do accidentally change something, B to Balance the levels is good enough, PulseAudio ignores the levels of the controls anyway, it only cares whether they’re muted or not.

The control you’d be looking for is very likely a simple on/off “switch” where white-on-green “OO” means “on” / blue-on-background “MM” means “muted”, or it’s a selector between several fixed options. You could also install the qasmixer package from the repos for an excessively-graphical, but more user-friendly, interface to the same controls.

If all that fails, then looking for an option in the relevant snd_foo kernel module is worth a look, I suppose — but I’m pretty sure anything of that sort that’s remotely relevant got eliminated from the drivers for all current hardware long ago.

2 Likes

I’ll be pretty surprised if there isn’t something in the Configuration tab that’s relevant, because AIUI most of those Realtek chips let you reconfigure the jacks for various types of multi-channel audio — so I’d hope that PulseAudio would be able to manage that.

I don’t use the motherboard audio on this PC as it’s a business unit with no digital or multi-channel audio, but Pulse is even able to detect whether or not the line in has anything plugged into it or not and display the state:

Ooh! Orrrr, from the HD-Audio kernel driver docs, in the notes section:

HD-Audio Reconfiguration

This is an experimental feature to allow you re-configure the HD-audio codec dynamically without reloading the driver. The following sysfs files are available under each codec-hwdep device directory (e.g. /sys/class/sound/hwC0D0):
[…]
hints: Shows / stores hint strings for codec parsers for any use. Its format is key = value . For example, passing jack_detect = no will disable the jack detection of the machine completely.

So that’d be something like,

sudo -s
echo "jack_detect = no" > /sys/class/sound/hwC0D0/hints
echo 1 > /sys/class/sound/hwC0D0/reconfig
exit

(This should be considered an absolute last resort.)

(EDIT: Hah! I just tried that on my motherboard audio — which, it turns out, is a Realtek chip as well — and the reconfig failed because “echo: write error: device or resource busy”. So, if you’re going to try this, you may have to do it from a console VT while logged out. (Or even with the display server shut down entirely, as gdm may also grab the audio out.) So, yeah. Even more last-resort-ey.)

1 Like

I’m absolutely sure I did, it was also my first instinct as well. Still I’ll definitely recheck it again. I did this originally several months ago, maybe even more, on F29 or even earlier. Then I’ve found crude workaround with alsamixer I’ve mentioned, but no clean way to manage this.

Speaking of alsamixer and my crude woraround, there’s a switch control called “Auto-mute”. It was enabled by default, and it this state when I plug headphones, the speakers get muted.

When I set it to false, sound it output to both headphones and speakers.

So right now I set it to false to hear sound in headphones only, and set it to true when I want to hear sound from the speakers. That’s a clutch in a worst sense.

Big thanks for all the suggestions, I’ll try them when I’m at home and report back.

@ferdnyc, I’ve rechecked pavucontrol.

“Curiouser and curiouser!” Cried Alice… (c) Lewis Carroll

It actually shows two outputs in Port dropdown of Output devices but – but this by itself doesn’t help me much. Outputs listed are [Line out (unplugged)] and [Headphones (plugged in)].
At the same time it helps to achieve desirable effect in conjunction with “Auto mute” switch from alsamixer. But I’ll try to list what I observe (hear, rather) in due order.

So I have both speakers and headphones connected an won’t disconnect anything.

  1. With “Auto mute” in alsamixer enabled (it’s the default configuration).

    1.1. When i choose [Headphones (plugged in)] – i hear sound in headphones and nothing from speakers --as it should be.

    1.2. When I choose [Line out (unplugged)] – I hear nothing at all. Both outputs are silent.
    Again, this is expected, “Auto mute” mutes speakers when headphones jack is plugged in.

  2. With “Auto mute” in alsamixer disabled.

    2.1. When i choose [Headphones (plugged in)] – I hear sound both from headphones and from speakers.

    2.2. When I choose [Line out (unplugged)] – I hear sound from speakers only.

I think I can write a simple script with two commands to switch “Auto mute” on or off and to change pulseaudio output to correct port to achieve desirable effect. Still feels like two crutches to me ) but it should work. And it’s definitely better than sound from both outputs as I had it before.

I still think that a more elegant solution can be achieved – i’m not sure, of course. )


My thinking was actually like this. Pulseaudio by itself doesn’t reconfigure sound outputs, it can only switch outputs presented to it by ALSA. And as we’ve seen right now (and I saw it when I’ve tried it first time) it by itself isn’t able to provide desirable effect.

So I may be able to force ALSA to present two proper outputs to PA – change ALSA config once and don’t touch it every time I need to switch outputs – and then just switch these outputs using PA.


I’ll try "jack_detect = no" sugestion too and report on it.

Interesting! My motherboard’s ALC662 has an Auto Mute with three options:

  • Disabled
  • Speaker Only
  • Line Out + Speaker

Presumably that’s because it actually has separate ports for all three: front headphone jack, speaker out (which I guess is an internal header, maybe? there’s nothing connected and I get no sound if I try to use it, I know that), and line out (rear headphone jack, not connected).

My worry is that you can’t, because Pulse is very literal when it comes to hardware, and I don’t think there are two separate ports. IOW, if I’m right about how your chip is wired, there’s only one audio sink that runs to both the headphone out and the speaker out — it would be literally impossible to have different audio routed to each of them at the same time. Hence the auto-muting.

If I’m right, Pulse might be convinced to treat them as ports that can be enabled and disabled at will, but I don’t believe there’s a chance in hell it’d be willing to treat them as two separate devices. When Pulse sees multiple devices, it wants to treat them as separate sinks, and if two sinks point to the same hardware device then it’d end up in contention with itself at some point and probably end up crashing the whole audio subsystem, or locking itself up at the very least.

I don’t know why you’d really need that, TBH. If you switch auto mute off, Pulse can easily switch between ports, and it should end up muting the one that’s not enabled. You don’t ever want them both playing at once, right? Just to switch between them? I feel like Pulse should be able to do that on its own, modulo hardware limitations.

Like, for S&G just now I grabbed my headphones and plugged them into the front port on my system. The interesting thing is, not only did the device switch to the Headphones port, but the Speakers port was automatically labeled as “(unavailable)”. It stays that way as long as the headphones are attached.

But the really interesting thing is, even though Pulse labels it as “Speakers (unavailable)” while the headphones are plugged in, it still lets me switch to it if I want, and if I have alsamixer open I can even watch it muting/unmuting the various channels and raising/lowering their levels.

As I said, there’s no sound on the Speakers output even when I don’t have headphones connected, so that doesn’t really get me anything. But Pulse seems pretty unperturbed by unavailable devices, it’s happy to try to use them. I suspect, though, that it knows better than I do that its attempts would fail.

I’ve learned never to underestimate what PulseAudio can do. While it’s true that it doesn’t interface directly to hardware, it can talk to ALSA. In my experience, anything that we can convince ALSA to do, PulseAudio is perfectly capable of doing for itself — if not more. It may need the right configs, device quirks, or what have you, but things like switching Auto Mute or whatever are perfectly within its power, if it actually needs to.

Ooh. The ALSA page at wiki.archlinux (thank you Arch Wiki documentarians) just reminded me that if you install alsa-tools, one of the tools it comes with is hdajackretask, a routing configuration utility that lets you reconfigure ALSA’s port assignments down to a very fine degree.

(Like, insanely. Never click “Advanced override” unless you want to fear that your sound chip is becoming sentient and plotting to kill you. #ETOOSCARY)

I don’t know that Pulse would necessarily respect those remappings (vs. just rewiring things back the way it expects — like I said, never underestimate what it can do), but it’s at least something to experiment with.

@ferdnyc, first of all, thanks a lot, I take quite a lot from your feedback, some of it I sort of knew or felt it should be this way, some of it is new to me.

That page is really interesting, I haven’t seen in it before.

Now, first things first.

But that’s the crux of my problem! This is exactly what happens and what I’d like to rectify!

Please read along once more:

So I can’t achieve clean switching this way, I switch between two modes: (1) only speakers playing – that’s ok! – and (2) both ports playing at one – that’s meh! It sort of works but bugs me )))) Again, this is exactly how I use it now, and how I used it before opening this thread.

That’s exactly what I want to achieve! One device (and one PA sink) with two separate ports that can be enabled and disabled at will – i.e. easily switched between. Trouble is speakers and headphones are being treated more as ONE port, not two separate. It’s exactly this separation I want to achieve! )


I saw three options too after manually retasking backplate green port to [Internal speaker] using hdajackretask. I.e. this switch depends on state jacks/connectors as seen by alsa or hda sound driver. Without retasking anything I see a two-state switch (just for the speakers).


I always thought that ALSA controlls low-level hardware stuff – like what jacks/outputs does my device have, which controls and switches, etc. Whereas pulseaudio routes audio between various devices my system has (i.e. sinks) and can manipulate controls surfaced to it by ALSA – like volume and mute/unmute. It can also switch between various outputs / ports presented to it by ALSA – like speakers and headphones plugged into one sound card – but again, when they are correctly presented by ALSA as separate independent ports.

My trouble, again, that in my case these ports don’t want to function independently of each other. )))

I’ve read about this tools yesterday on the HD drivers docs page you’ve linked to earlier. I’ve installed and played a bit with it yesterday, I think I’ll try some more.

Things like this utility can do are exactly the level of configuration ALSA can do I’ve meant earlier, pulseaudio doesn’t descent this low / close to the hardware, it relies on ALSA to do so and present correct configurations and controls to it. Or am I wrong?

Well, I don’t underestimate it (I think I don’t) I just thought it has another scope of responsibility so to say. As PA lives above ALSA in the audio stack, it doesn’t need to do things ALSA does, it can (and should?) rely on ALSA correcty doing it’s work and presenting necessary inputs, outputs and controls up to PA. Moreover, if it starts to reconfigure devices by itself – it’ll conflict with ALSA, and surely we don’t want this, PA is designed to live together and rely on ALSA, not make war with it.

But I’m repeating myself, I must be boring you to the death already ))))) Sorry )

So in fact PA respects these remappings and doesn’t drop them to some defaults. It sort of takes them and goes along with them.

This tool looks like it should be capable of properly separating my headphones and speakers. I didn’t get this on a first try though, I’ll play with it more.

Please comment on my thoughts – if I’m wrong in my understanding of ALSA ↔ PulseAudio relations – it may be much more productive to research port configurations / retasking by PulseAudio and not by ALSA.

@m10997, your post is very strongly resembles spam, I’ll mark it as such.

The site you’ve linked to may be a great one and all such, and maybe even has information relevant to my question, but if it’s the case – please, please at least link to relevant article, hot the homepage.

Please correct your post, or it’ll remain marked as spam.