Linux change audio output

How can I change audio output to HDMI from command line?

Backstory This thread suggests pacmd list-sinks and switching to a different sink with pacmd set-default-sink «SINKNAME» , but my laptop only appears to have 1 sink, so that didn’t help me. This thread suggests pacmd list-cards which shows hdmi-output-0: HDMI / DisplayPort right at the bottom of the output, and that looks right, but it still says Failure: No such entity when I run pactl set-card-profile 0 output:hdmi-output or pactl set-card-profile 0 output:hdmi-output-0

Glad you found a solution and thank you for sharing. You should now write/copy your solution to an own answer and then edit your question to better fit to the Q & A format of this site. Thank you.

Hi James, I’ve added a community wiki answer below containing your solution. If you later decide you want to post your own answer please come and do that and then we can delete mine. Thanks!

2 Answers 2

The OP posted the solution in his question:

While writing this I found the solution:

This command makes it easier to see what my actual output options are:

pacmd list-cards | grep output\: 

That way of looking at the output of pacmd list-cards made me realize that the actual wording for my output is not output:hdmi-output but instead output:hdmi-stereo

This command is what worked for me:

pactl set-card-profile 0 output:hdmi-stereo 

This is how I switched back to my laptop’s internal speakers:

pactl set-card-profile 0 output:analog-stereo 

This is how I changed the volume via command line:

amixer -D pulse sset Master 50% 

This is just a smiley face:

Источник

How can I change the default audio device from command line?

Ubuntu’s desktop gui is great for changing audio settings (System->Preferences->Sound), like the default input/output device and setting the volume. However, I would like to be able to do these things from the command-line. What tool is the gui using behind the scenes?

4 Answers 4

You can control PulseAudio thoroughly through the command line using pacmd and pactl commands. For options see pacmd —help or the wiki at PulseAudio:

pacmd list-sinks for name or index number of possible sinks

pacmd list-sources for name or index number of possible sources

pacmd set-default-sink «SINKNAME» | index to set the default output sink

pacmd set-default-source «SOURCENAME» | index to set the default input

pacmd set-sink-volume index volume

pacmd set-source-volume index volume for volume control ( 65536 = 100 %, 0 = mute; or a bit more intuitive 0x10000 = 100 %, 0x7500 = 75 %, 0x0 = 0 %)

and many many more CLI options.

Читайте также:  Команда линукс создать пользователя

Note: Changing the output sink through the command line interface can only take effect if stream target device reading is disabled. This can be done by editing the corresponding line in /etc/pulse/default.pa to:

load-module module-stream-restore restore_device=false 

Restart PulseAudio for changes to take effect:

For a more elaborate tutorial on how to do this, and for instructions on how to change the sink during playback see this answer.

Источник

How do you set a default audio output device in Ubuntu?

See this screenshot

Every time I boot up, I have to switch my audio output device to my «Line Out» headphones because for some reason my microphone defaults as a speaker. See this screenshot:

At least you got the screenshot done when the dropdown list was shown — that’s more than I can do on Ubuntu 20.04 .

Audio on Ubuntu couldn’t be more broken. Its 2021 and reliably switching between USB / line in / external audio cards / etc etc is still a nightmare.

10 Answers 10

Simple fix in 19.10 that worked for me:

I couldn’t get the solution @singrium proposed to persist on Ubuntu 19.10. It worked with device numbers in /etc/pulse/default.pa but as I connected for example my headset device numbers were changing and things stopped working. It didn’t work with device names in /etc/pulse/default.pa .

The simple workaround I found is adding the pactl set default sink command in startup applications.

  1. Run: pactl list short sinks
  2. Note the device name you want to use as default
  3. Try to run: pactl set-default-sink
    This should work without giving you an error message.
  4. Open the application «Startup Applications» (Should be preinstalled on Ubuntu)
  5. Click on «Add»
  6. Give your startup item a name
  7. Copy your command from above into the command field:
    pactl set-default-sink ‘Your-Device-Name’
  8. Click on «Add».

You are now good to go. Your default audio device will be set on each boot and as such be persistent. If you want to change the default device simply edit the device name in startup applications command.

Marking this answer as the new solution. This method is simpler and doesn’t involve editing or deleting config files and I’ve had some users mention that the earlier marked answer caused issues with their system.

assuming you want your audio-out as hdmi (such as for a TV) and your os keeps picking something else (like a gaming headset), here is a one-liner to put in startup apps (with appropriate delay): /usr/bin/pactl set-default-sink $(/usr/bin/pactl list short sinks|/usr/bin/gawk -F’\\s+’ ‘$2 ~ /^.*[Hh][Dd][Mm][Ii].*$/ < print $2 >‘)

This solution got me part of the way there. I had to run pacmd list-cards to determine the device name and profile, then I created a executable bash script with these commands: pacmd set-card-profile output: then pactl set-default-sink . .

Without using gawk: /usr/bin/pactl set-default-sink $(/usr/bin/pactl list short sinks | grep -i hdmi | cut -f2)

Читайте также:  Reset usb hub linux

EDIT (05/03/2020):
It seems that @phanky5 figured out a simpler solution. Please check it before you try this one.

Here is a well explained tutorial to set a default audio input/output.

First: List the audio output devices using

43 alsa_output.pci-0000_00_1b.0.analog-stereo PipeWire s32le 2ch 48000Hz IDLE 1076 alsa_output.usb-Logitech_Logitech_Wireless_Headset_4473D63ED97A-00.analog-stereo PipeWire s16le 2ch 48000Hz IDLE 

Second: To set a default output device, run the command

Example: pactl set-default-sink ‘alsa_output.pci-0000_00_1f.3.analog-stereo’

If you switch sinks a lot, you can use fzf to select one via the command line and make it the default:

pactl set-default-sink $(pactl list short sinks | cut -f2 | fzf) 

To make the sink permanently the default, follow this:

First, open the file /etc/pulse/default.pa using:

sudo -H gedit /etc/pulse/default.pa 

Then scroll to the bottom of the file, where two lines starting with set- will be commented out.

Now, uncomment these lines and replace the words input and output with the number of the sink (for output) / source (for input) that you want to be the default.

Example (sets both default input and output):

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

PS: As discussed in the comments with Bim, it is also possible (according to him) to put the input/output name in /etc/pulse/default.pa instead of the input/output number.

### Make some devices default set-default-sink alsa_output.pci-0000_00_1f.3.analog-stereo set-default-source alsa_output.pci-0000_00_1f.3.analog-stereo.monitor 

After doing this, save and exit. Then, delete the ~/.config/pulse directory by running rm -r ~/.config/pulse , and then reboot the system. Once the system reboots, the appropriate devices should now be set as the defaults.

EDIT:
As mentioned by ahmorris in his answer, some had to comment this line load-module module-switch-on-connect in the file /etc/pulse/default.pa to be # load-module module-switch-on-connect in order to make the changes persistent.

Источник

How can I switch between different audio output hardware using the shell?

I use my laptop with an external monitor which has speakers. When the monitor is attached through HDMI I can switch (using the GUI: Sound Setting —> Hardware) between the normal laptop audio output and the monitor output. I repeat this procedure a lot of time and I started to wonder if I can automate it or, anyway, execute it in a faster way using the shell. My distro is Ubuntu 12.04 with gnome 3. EDIT: I tried using pacmd, but list-sinks gives me only the device I’m currently using:

pacmd list-sinks | grep name: name:
pacmd list-sinks | grep name: name:
pacmd set-default-sink alsa_output.pci-0000_00_1b.0.hdmi-stereo Welcome to PulseAudio! Use "help" for usage information. Sink alsa_output.pci-0000_00_1b.0.hdmi-stereo does not exist. 

11 Answers 11

In this case the card is always the same. What is changing between a switch and another is the «card-profile».

So the solution which actually worked is:

In my case I found all the card profiles with:

And after I can switch between monitor and laptop speakers with:

pacmd set-card-profile 0 output:hdmi-stereo 
pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo 

Where 0 is the index of the card:

pacmd list-cards Welcome to PulseAudio! Use "help" for usage information. >>> 1 card(s) available. index: 0 name:

And finally, in order to make the switch faster, I set up two alias in my .bashrc file:

alias audio-hdmi='pacmd set-card-profile 0 output:hdmi-stereo+input:analog-stereo' alias audio-laptop='pacmd set-card-profile 0 output:analog-stereo+input:analog-stereo' 

This way I can switch between audio from the monitor or from the laptop (headphones) typing in the shell: audio-hdmi or audio-laptop

Читайте также:  Разархивировать zip архив linux

Mille Grazie

Advice for anyone wanting to figure out what your specific setup needs after “output:”: pacmd list-cards > ~/Downloads/output1.txt , then change to the-other-settings and dump as output2.txt . Then diff things, by diff command or e.g more conveniently in vscode “compare selected”.

I wrote a small indicator applet that lets you switch the sound output. No shell script but maybe helpful to you or other readers.

Screenshot

I created a very small script based on the previous ones, which not only switches the audio but also the video output. It uses the disper to switch between displays.

#!/bin/bash CURRENT_PROFILE=$(pacmd list-cards | grep "active profile" | cut -d ' ' -f 3-) if [ "$CURRENT_PROFILE" = "" ]; then pacmd set-card-profile 0 "output:analog-stereo+input:analog-stereo" disper -s else pacmd set-card-profile 0 "output:hdmi-stereo" disper -S fi 

For me it is especially useful since I don’t like to clone the displays. I either use one or the other. You may need to adapt the audio profiles to your specific system.

You can use pactl , read its man page for more information.

Tested on Ubuntu 10.04 — 13.04 and Arch Linux

Lukas’s python script (https://github.com/lkettenb/sound-output-switcher, posted above) to implement a notifier applet works well. It needs the appindicator package. That can be installed with

sudo apt-get install python-appindicator 

I created the following python script that does the following:

  1. Toggle the default device to the next device on the list (with wrap around) regardless of the id’s
  2. Moves all running applications to this device.
  3. Sends a notifications to the GUI with the device name.
#!/usr/bin/env python3 import subprocess # Toggle default device to the next device (wrap around the list) cards_info = subprocess.run(['pacmd','list-sinks'], stdout=subprocess.PIPE) card_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=cards_info.stdout) indexes_list = card_indexes.stdout.decode().splitlines() card_descriptions = subprocess.run(['grep', 'device.description'], stdout=subprocess.PIPE, input=cards_info.stdout) indices = [i for i, s in enumerate(indexes_list) if '*' in s] if (len(indices) != 1): print("Error finding default device") exit(1) default_index = indices[0] next_default = 0 if (default_index != (len(indexes_list) - 1)): next_default = default_index + 1 next_default_index = (indexes_list[next_default].split("index: ",1)[1]) subprocess.run(['pacmd','set-default-sink %s' %(next_default_index)], stdout=subprocess.PIPE) # Move all existing applications to the new default device inputs_info = subprocess.run(['pacmd','list-sink-inputs'], stdout=subprocess.PIPE) inputs_indexes = subprocess.run(['grep', 'index'], stdout=subprocess.PIPE, input=inputs_info.stdout) inputs_indexes_list = inputs_indexes.stdout.decode().splitlines() for line in inputs_indexes_list: input_index = (line.split("index: ",1)[1]) subprocess.run(['pacmd','move-sink-input %s %s' %(input_index, next_default_index)], stdout=subprocess.PIPE) # Send notification to the GUI descriptions_list = card_descriptions.stdout.decode().splitlines() if (len(descriptions_list) == len(indexes_list)): description = (descriptions_list[next_default].split("= ",1)[1])[1:-1] args = ["notify-send", "Default audio card changed", 'Default audio card was set to %s' %(description)] subprocess.run(args, stdout=subprocess.PIPE) 

Assigned a keyboard shortcut to the script, and my life is happy now

Источник

Оцените статью
Adblock
detector