Skip to content

Instantly share code, notes, and snippets.

@ChriRas
Last active November 25, 2023 19:55
Show Gist options
  • Star 75 You must be signed in to star a gist
  • Fork 11 You must be signed in to fork a gist
  • Save ChriRas/b9aef9771a97249cb4620e0d6ef538c4 to your computer and use it in GitHub Desktop.
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.

@cedric80
Copy link

Works perfect! Thanks for nice and easy solution.

@jayyantonucci
Copy link

+1

@seaniedan
Copy link

yay, this worked! Great solution. The general advice was to edit the defaults with
sudo nano /etc/pulse/default.pa
uncommenting the line at the bottom to read something like
set-default-sink output 1 #where 1 is the output you want from 'pactl list short sinks' as above
..however this didn't work for me. Your elegant solution does, thank you!

@blackforestcode
Copy link

Great, thanks! 👍

@Jobin-S
Copy link

Jobin-S commented Nov 5, 2021

thank you, bro. Its worked perfectly

@optimbro
Copy link

Elegant solution, btw little typo in
"Set your internal speaker as default pactl set-default-source e.g. ppactl set-default-source alsa_input.pci-0000_00_1f.3.analog-stereo"

Its pactl not "ppactl" (save some milliseconds for copy pasters 🤣)

anyways, thanks.

@Jobin-S
Copy link

Jobin-S commented Nov 20, 2021

Elegant solution, btw little typo in "Set your internal speaker as default pactl set-default-source e.g. ppactl set-default-source alsa_input.pci-0000_00_1f.3.analog-stereo"

Its pactl not "ppactl" (save some milliseconds for copy pasters rofl)

anyways, thanks.

😆

@ChriRas
Copy link
Author

ChriRas commented Nov 22, 2021

Elegant solution, btw little typo in "Set your internal speaker as default pactl set-default-source e.g. ppactl set-default-source alsa_input.pci-0000_00_1f.3.analog-stereo"
Its pactl not "ppactl" (save some milliseconds for copy pasters rofl)
anyways, thanks.

😆

fixed! Thanks for the hint.

@comxd
Copy link

comxd commented Nov 22, 2021

yay, this worked! Great solution. The general advice was to edit the defaults with sudo nano /etc/pulse/default.pa uncommenting the line at the bottom to read something like set-default-sink output 1 #where 1 is the output you want from 'pactl list short sinks' as above ..however this didn't work for me. Your elegant solution does, thank you!

This is the best way, a native solution without needed to create a startup script. The only thing about that: you need to replace the input and output keywords by your device ID.

Edit /etc/pulse/default.pa:

### Make some devices default
set-default-sink 1
set-default-source 3

Then restart pulseaudio: pulseaudio -k.

To identify your output (speaker) device ID : pactl list short sinks (in my case: 1)
To identity your input (microphone) device ID: pactl list short sources (in my case: 3)

@LucasMatuszewski
Copy link

LucasMatuszewski commented Dec 28, 2021

@seaniedan - setting default output sink in /etc/pulse/default.pa is more elegant, since this file runs on each system boot anyway.
To make it work you have to restart pulseaudio .configs by deleting pulse directory: rm -r ~/.config/pulse
I've found this solution here: https://askubuntu.com/a/1038492

Probably pulseaudio -k command mentioned by @comxd do the same ;)

But it's safer to set sink by name instead of its number (which can change), in my example:
set-default-sink alsa_output.pci-0000_00_1f.3-platform-skl_hda_dsp_generic.HiFi__hw_sofhdadsp__sink

I also had a problem that my Mic's build-in minijack output was set each time I have plugged my Mic into the USB port. To solve it you can also comment this lines in /etc/pulse/default.pa file:

#.ifexists module-switch-on-connect.so
#load-module module-switch-on-connect
#.endif

@GhastlyOne
Copy link

Danke! was just what I needed!

@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