Ralf Schmitz Bongiolo

April 16, 2021

How to change sound output on Linux using commands

I'm not sure if this is the best or really correct way of doing this, but it does work (at least on my setup).

Usually, I have my shelf speakers on when I'm working or doing anything on my computer. From time to time I might have to join an online meeting and there I want to use my headset + webcam, the problem is that usually the webcam is unplugged and I do plug it on my keyboard for those meetings, but then the system automatically changes the default input source to the webcam mic, which I don't want.

The aliases below allow for easily switching between my speakers or headset, while correctly setting the input source to the headset mic.

alias speakers='pactl set-card-profile HEADSET_CARD_NAME off &&
  pactl set-card-profile HEADSET_CARD_NAME INPUT_PROFILE &&
  pactl set-card-profile SPEAKERS_CARD_NAME OUTPUT_PROFILE &&
  pactl set-default-sink SPEAKERS_SINK_NAME'

alias headset='pactl set-card-profile HEADSET_CARD_NAME off &&
  pactl set-card-profile HEADSET_CARD_NAME OUTPUT_INPUT_PROFILE &&
  pactl set-default-sink HEADSET_SINK_NAME'

# To list available sinks:
pactl list sinks short

# To list available cards and profiles:
pactl list cards

For some reason, unknown to me, it is required to first change a __card profile__ to OFF before changing the input, or else it won't override the webcam mic as the input source.