Select sound card with console commands

1

Is it possible to use cinnamon-settings app in the command line to change the default sound card? The default sound card does not seem to be set in dconf.
I'm trying to write a script that switches between HDMI and analog sound output.

Thommy

Posted 2017-10-12T16:52:14.867

Reputation: 111

Answers

2

I think that you are looking for

pacmd set-default-sink <sink_name|index>

as outlined in https://askubuntu.com/questions/690711/change-sound-output-from-command-line and https://askubuntu.com/questions/14077/how-can-i-change-the-default-audio-device-from-command-line

You can get the available index with

pacmd list-sinks|grep index|awk '{print $NF}'

If you want to control the volume you can do that on the commandline with alsamixer (ncurses) or amixer.

If you are going to play with pulseaudio settings then

cp -i /etc/pulse/default.pa ~/.config/pulse/

will give you your own copy that is safer to edit

pulseaudio -k  #should restart pulseaudio after you change settings.

For your script you may want to check that the available audio rates are compatible with your sink-device:

grep rates $(grep -ci hdmi /proc/asound/card*/codec#0|grep -v :0|sed 's,:.$,,'|head -n1)

and depending on which profile each card is attached to

pactl --server "unix:/run/user/$(id -u)/pulse/native"  set-card-profile 1 output:analog-stereo

and

pactl --server "unix:/run/user/$(id -u)/pulse/native"  set-card-profile 0 output:hdmi-stereo

may help create useful variables. It looks like someone has already tried to do what you are doing:

But some of their variables are either archlinux specific or out-of-date. (Though their path to triggering your script may be of help.)

Alexx Roche

Posted 2017-10-12T16:52:14.867

Reputation: 760

I tried that but it does not work until i logged of or restarted. – Thommy – 2017-10-12T19:48:47.407

Did you do pulseaudio -k – Alexx Roche – 2017-10-12T19:57:34.373

Yes. I also tried with the profile pacmd set-card-profile 2 output:iec958-stereo – Thommy – 2017-10-12T20:12:36.193

if pacmd list-cards | grep output:hdmi-stere finds your card does pactl set-card-profile 0 output:hdmi-stereo make the change happen? https://askubuntu.com/a/541043/155829

– Alexx Roche – 2017-10-12T20:25:45.857

The settings manager shows the change, but audio still comes on the wrong speakers. When I try pacmd move-output it says index does not exist. – Thommy – 2017-10-13T18:20:26.930

What does pacmd list-cards | grep -A1 index say? – Alexx Roche – 2017-10-13T20:26:39.813

index: 0 name: <alsa_card.pci-0000_01_00.1> – Thommy – 2017-10-15T15:35:55.690

ndex: 1 name: <alsa_card.usb-C-Media_Electronics_Inc._USB_Audio_Device-00> – Thommy – 2017-10-15T15:36:05.220

pacmd set-default-sink alsa_card.usb-C-Media_Electronics_Inc._USB_Audio_Device-00 should switch the output pacmd list-sinks|grep name:|sed -e 's/.<//' -e 's/>$//'* is probably a cleaner way to list the sinks – Alexx Roche – 2017-10-16T10:38:02.917

Just spun up a clean debian8 machine and until I ran pactl set-card-profile 0 output:hdmi-stereo the hdmi wasn't listed in pacmd list-cards - then I checked *cat /sys/class/drm/cardHDMI/status* and found my cable was loose – Alexx Roche – 2017-10-16T11:01:30.350

The move sink just won't work. I found a way using the API of my Desktop Environment. This way it is not compatible to other DEs but at least it works for me now. Thanks for your help. – Thommy – 2017-10-17T08:44:36.643

You could post your solution as an answer. – Alexx Roche – 2017-10-17T09:28:47.360

Will do, but I need to clean up the code first :) – Thommy – 2017-10-18T15:12:19.110