44

I have to plugin my USB Audio adapter ( 4300054 Gigawire USB Audio Adapter) for audio input because has a combo-input-output port for voice. After I do this, I have go open Sound Settings and manually select the USB Audio adapter for Input and Output, if I do not, the system default remains selected.

Is there anyway, I can make Ubuntu to automatically select the USB Audio Adapter as the default as soon as I plug-in?

3 Answers3

63

There is pulseaudio module-switch-on-connect and module-switch-on-port-available that enable automatic switching of an audio device on connection. See with the following command if any of these modules is already loaded:

pactl list short modules

If not, then test if you can make automatic switching work by issuing one or both of the following commmands in a terminal:

pactl load-module module-switch-on-connect
pactl load-module module-switch-on-port-available

On success add one or both of the following lines to your /etc/pulse/default.pa

load-module module-switch-on-connect
load-module module-switch-on-port-available

This will then always load these modules on every login, resp. restart of the pulseaudio daemon.

Please note that local user settings in ~/.config/pulse/default.pa will override the system-wide setting. If you have such a local file you will enter the above commands there.

If it still doesn't work, you may have conflicting settings in such a local default.pa. It then may worth to (temporarily) rename this file, followed by a restart of the Pulseaudio server before trying above again:

mv ~/.config/pulse/default.pa ~/.config/pulse/default.pa.old
pulseaudio -k
G.A.
  • 7
Takkat
  • 142,284
  • I just re-found this answer and wish I could upvote it again. This solves the issue on Ubuntu 14.04. – Lars Nyström Oct 08 '15 at 10:02
  • 1
    such a long time it is out there, and still not set to be loaded automatically in 16.04 :| – Ivan Temchenko Jul 16 '16 at 09:54
  • just applied this to 17.04 - perfect solution and really should be default – fishears Aug 04 '17 at 07:06
  • Tested and working on Mint 18 (Sarah) – Lucio Mollinedo Aug 22 '17 at 15:33
  • 6
    What if Module load failed ? – Skippy le Grand Gourou Jul 21 '19 at 18:33
  • Yeah, I'm getting Module load failed too - Mint 19.3. I'll have a dig around. – Scaine Jul 29 '20 at 08:06
  • 1
    @Scaine: hi there :) good to see you again. The module may have already been loaded (see pactl list short modules). PA spits out an error if we try to load a module twice. – Takkat Jul 29 '20 at 19:16
  • Yep, pretty sure that's what's happening here, @Takkat. However, SteamVR isn't triggering the module when it starts up and creates the new input/output devices for some reason. But that's another issue! – Scaine Aug 17 '20 at 07:55
  • @Scaine; did you try with the module switch-on-port-available - this one comes into action after the audio output port was changed. No idea which is involved in your case. – Takkat Aug 17 '20 at 16:14
  • No joy, I'm afraid. I had load-module module-switch-on-port-available already specified in my /etc/pulse/default.pa. I also tried loading your version (which doesn't have the first use of "module" in its name), but SteamVR still creates the new output and I don't see any switching to it. This might be a SteamVR bug however, and I understand that this does work for some. Not to worry, it's not a big deal. – Scaine Sep 07 '20 at 17:25
  • This doesn't work for me. I still need to switch devices. – Heisenberg Dec 28 '20 at 13:11
  • @Heisenberg: you may try with edits I just made to this answer. If that still does not help your audio device may not allow switching. – Takkat Dec 28 '20 at 14:04
  • @Takkat This and the last answer you wrote works only works for wired connections. When I connect my bluetooth device or through HDMI, it doesn't switch. Also, the audio switches automatically in windows so I think my audio device allows switching – Heisenberg Dec 28 '20 at 16:44
  • @Heisenberg: I am really sorry to hear that - for me it works as expected with switching both, Bluetooth, and HDMI connections. – Takkat Dec 28 '20 at 17:52
  • This worked on popos! (ubuntu 22.04) – Neovea Oct 05 '23 at 08:24
27

create ~/.config/pulse/default.pa if it doesn't exist and append

.include /etc/pulse/default.pa
load-module module-switch-on-connect

This is better than editing /etc/pulse/default.pa.

Afterwards you should run pulseaudio -k && pulseaudio --start to have the changes take effect. Thanks for pointing that out lreeder

1

I tested solutions for a long time that I could find in the documentation or on forums and this was the only one that worked.

So here is a script that I created that you can add when starting a session (unfortunately not for the entire computer because PulseAudio is a service that runs independently for each user).

#!/bin/bash
index=$(pacmd list-sources | egrep 'index|ports|analog-input-headset-mic' | egrep '\*\sindex:\s+[0-9]'  | cut -d':' -f2);

acpi_listen | while IFS= read -r line; do if [ "$line" = "jack/headphone HEADPHONE plug" ] then pacmd set-source-port $index analog-input-headset-mic; elif [ "$line" = "jack/headphone HEADPHONE unplug" ] then pacmd set-source-port $index analog-input-internal-mic; fi done

There is a solution which can be found on the first link of my sources, but it does not work for all PCs unfortunately. Here are the links that allowed me to create this script