Skip to content

Instantly share code, notes, and snippets.

@ChriRas
Last active November 25, 2023 19:55
  • Star 75 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save ChriRas/b9aef9771a97249cb4620e0d6ef538c4 to your computer and use it in GitHub Desktop.
Set up default audio device on Ubuntu 20.04 LTS

Problem

I have a notebook connected to a port replicator. I want to use the build-in speakers and microfone and not the external ones. If I boot my notebook in my port replicator Ubuntu changes the devices to external.

Solution

  1. Find your internal speaker
pactl list short sinks

0	alsa_output.usb-Generic_USB_Audio_201405280001-00.analog-stereo	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
1	alsa_output.pci-0000_00_1f.3.analog-stereo	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
  1. Set your internal speaker as default pactl set-default-sink <DEVICE> e.g. pactl set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo

  2. Add to "Startup Applications" ("Startprogramme" in German)

  • Open the application "Startup Applications" (Should be preinstalled on Ubuntu)
  • Click on "Add"
  • Give your startup item a name
  • Copy your command from above into the command field.
  • Click on "Save".
  1. Find your internal microfone
pactl list short sources

0	alsa_output.usb-Generic_USB_Audio_201405280001-00.analog-stereo.monitormodule-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
1	alsa_input.usb-Generic_USB_Audio_201405280001-00.iec958-stereo	module-alsa-card.c	s16le 2ch 44100Hz	RUNNING
2	alsa_output.pci-0000_00_1f.3.analog-stereo.monitor	module-alsa-card.c	s16le 2ch 48000Hz	SUSPENDED
3	alsa_input.pci-0000_00_1f.3.analog-stereo	module-alsa-card.c	s16le 2ch 44100Hz	SUSPENDED
  1. Set your internal speaker as default pactl set-default-source <DEVICE> e.g. pactl set-default-source alsa_input.pci-0000_00_1f.3.analog-stereo

  2. Repeat step 3.

@Trikenstein
Copy link

Edit etc/pulse/default.pa is the better solution. However, the comments above seem not working in my case Ubuntu 20.04.4 (desktop with sound card + web cam + USB headset) because:

  • The numerical ID of sinks & sources change at each reboot. So hard-coding an ID is inaccurate
  • set-default-... alone is not enough, I end up with two devices enabled. The one pre-selected by PulseAudio (which I don't know based on which criteria) and the one I set by set-default-...

The working solution for me is:

  1. Get the current default devices selected by PulseAudio (those having RUNNING as status)
echo '\nSINKS (output)' && pactl list short sinks && echo '\nSOURCES (input)' && pactl list short sources

SINKS (output)
0       alsa_output.pci-0000_04_00.1.hdmi-stereo        module-alsa-card.c      s16le 2ch 48000Hz       SUSPENDED
1       alsa_output.usb-Logitech_Logitech_B530_USB_Headset-00.analog-stereo     module-alsa-card.c      s16le 2ch 48000Hz       RUNNING
2       alsa_output.pci-0000_00_1b.0.analog-stereo      module-alsa-card.c      s16le 2ch 48000Hz       SUSPENDED

SOURCES (input)
0       alsa_output.pci-0000_04_00.1.hdmi-stereo.monitor        module-alsa-card.c      s16le 2ch 48000Hz       SUSPENDED
1       alsa_output.usb-Logitech_Logitech_B530_USB_Headset-00.analog-stereo.monitor     module-alsa-card.c      s16le 2ch 48000Hz       SUSPENDED
2       alsa_input.usb-Logitech_Logitech_B530_USB_Headset-00.mono-fallback      module-alsa-card.c      s16le 1ch 48000Hz       SUSPENDED
3       alsa_input.usb-HK_5M_EKACOM-K20E_200901010001-02.analog-stereo  module-alsa-card.c      s16le 2ch 48000Hz       RUNNING
4       alsa_output.pci-0000_00_1b.0.analog-stereo.monitor      module-alsa-card.c      s16le 2ch 48000Hz       IDLE
  1. Edit /etc/pulse/default.pa explicitly suspend the devices selected by PulseAudio and set-default the one you want
# CHANGE
#set-default-sink output
#set-default-source input

# TO
suspend-sink alsa_output.usb-Logitech_Logitech_B530_USB_Headset-00.analog-stereo true
set-default-sink alsa_output.pci-0000_00_1b.0.analog-stereo
suspend-source alsa_input.usb-HK_5M_EKACOM-K20E_200901010001-02.analog-stereo true
set-default-source alsa_input.usb-Logitech_Logitech_B530_USB_Headset-00.mono-fallback
  1. Reboot. Maybe pulseaudio --kill && pulseaudio --start would be enough but in my case I tested the real use-case with a reboot.

Reference: Ubuntu manual - pactl

@AndriyKalashnykov
Copy link

AndriyKalashnykov commented Jun 4, 2022

(Ubuntu 22.04 LTS) doing the same when logging into Ubuntu Desktop did it for me:

vi ~/.config/autostart/my_script.desktop

[Desktop Entry]
Type=Application
Name=My Script
Exec=/home/<path-to-your-script>/install-sound.sh
Icon=system-run
X-GNOME-Autostart-enabled=true

vi /home/<path-to-your-script>/install-sound.sh

#!/bin/bash

# get list of available available audio output devices (sinks)
# pactl list short sinks
# set default output device to HDMI/DisplayPort LG monitor

pactl set-default-sink alsa_output.pci-0000_01_00.1.hdmi-stereo

# get list of available available audio input devices (sources):
#pactl list short sources
# set default audio input to USB BRIO web camera

pactl set-default-source alsa_input.usb-046d_Logitech_BRIO_301280B8-02.analog-stereo

@alidplus2
Copy link

+1

@ahmadasmandar
Copy link

thank you very much worked perfectly

@geekafy
Copy link

geekafy commented Aug 18, 2022

Thank you! This fixed my problem!

@oneplusiota
Copy link

oneplusiota commented Aug 18, 2022

How do I find out the location of install-sound.sh file??

@gricar
Copy link

gricar commented Aug 29, 2022

Thank you so much!! 🔊 🎶

@frankhuurman
Copy link

I see no difference in my pactl list short sinks output when plugging out or plugging in my headphones:
image

Just this list of 4 sinks, all in a suspended state.
Is there a way to find the device using the human friendly name that's in settings instead?
image

Gnome really needs something like this:
gnomedefaultaudiobutton

Or just remember the state of the previously selected audio device on reboot.

@Kuchiru
Copy link

Kuchiru commented Dec 5, 2022

How would one handle if there are multiple defaults?

For example when my bt headphones is disconnected i'd like to default to the speakers but when i connect my headphones it should default to the headphones.

Or is this entirely beyond the scope of what is attempted here?

edit: answered my own question with some searching, this is a combination of the above and an udev rule.
see https://askubuntu.com/questions/686144/run-a-shell-command-after-a-bluetooth-input-device-is-detected for the info i used

edit2: you need to add the --server= flag to the pactl command for udev to set your sink or source, you can get the server with pactl info use the server string, you need this because root will run your script which has no known pulseaudio session.
Now my bt headset and default speakers switch seamlessly (both ways) ^^

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment