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?

4 Answers4

65

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.
  • 27
  • 4
Takkat
  • 144,580
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

0

If you do not want or cannot use acpid, I created script to solve the issues based on @régis-andrē's script and https://bbs.archlinux.org/viewtopic.php?pid=1935889#p1935889:

#!/bin/bash

export LC_ALL=C

pactl subscribe card | while read _ facility _ type _ ; do if [[ $facility = "'change'" && $type = "card" ]] ; then if pacmd list-cards | grep -q "Headset Microphone.available: no" ; then pacmd set-source-port 1 analog-input-internal-mic elif pacmd list-cards | grep -q "Headset Microphone.available: unknown" ; then pacmd set-source-port 1 analog-input-headset-mic fi fi done

Normally, pulse's module switch-on-port-available should do the magic itself, but it's either a system integration bug or a bug in pulse if it does not happen. See also the thread on the ArchLinux forum and https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/issues/1028

sebix
  • 485