5

In Ubuntu and probably other distros there is a graphical thing to switch the audio output device. I have tried to write a command line tool that does the same thing, but it doesn't always work.

What I'm doing to switch to, say, sink 0 is the following:

pacmd set-default-sink 0 

Which I think should make all new inputs go to sink 0. Then, for each existing input, according to pacmd list-sink-inputs I am doing:

pacmd move-sink-input <input_id> 0 

But it often doesn't work. Often I will switch sinks, then play some audio in some app and it will still be attached to the old sink. What am I missing here? How can I do this robustly?

1 Answer 1

5

From this blog post:

PulseAudio itself comes with two basic command line tools, called pacmd and pactl. Former offers a set-default-sink command, but that doesn't do what you expect from it. It turns out that, at least on my system, PulseAudio daemon keeps persistent settings for each application that ever connected to it. This means that the default sink only gets used for applications that the daemon hasn't seen yet.

In other words, this blogger had hit the exact same problem as you did:

  • switching the default sink only affects programs that haven't been used before
  • moving existing input only affects programs actually active with the sound card
  • you'll need a tool to walk through the persistent settings and repoint them to the new card.

And so this blogger had written paswitch, a command line tool to do exactly that. You can find its source code in Git format here.

To install:

  • Make sure you have a Pulseaudio development library package (libpulse-dev or similar) installed. You'll also need the gcc compiler and the make utility.
  • Run git clone https://www.tablix.org/~avian/git/paswitch.git to download the source code.
  • cd paswitch to enter the directory created by the git clone command, then run make.
  • If completed without errors, there should now be a paswitch binary in the directory. Copy it to your /usr/local/bin/ directory.
  • Read the README file in the directory for an example on how to use it.

Another option is to tell PulseAudio daemon to not persist the sink settings for applications, by adding the restore_device=false option to the module-stream-restore line in /etc/pulse/default.pa:

load-module module-stream-restore restore_device=false 

After doing that and restarting PulseAudio, switching the default sink will cause all new sound applications to default to it.

But your mileage may vary, depending on the desktop environment you use: on my Debian 9, disabling the persistence caused other annoying behavior with KDE, and I ended up returning to the default behavior. If I recall correctly, it caused the KDE System Settings -> Multimedia -> Audio and Video -> Device Preference list to gain a new copy of my audio devices at each login.

3
  • Thanks, that really helps. It seems the key is actually the persistent settings which I didn't know about. Disabling those with load-module module-stream-restore device=false in the config file actually does the job and works with standard command line tools. Commented May 24, 2019 at 9:20
  • Maybe you can edit your answer to add this other solution for people who don't want to install a special program to do it. Commented May 24, 2019 at 9:22
  • Added; however, that other solution caused other problems for me (if I recall correctly, KDE's System Settings -> Multimedia -> Audio and Video -> Device Preference ended up gaining a new copy of my audio devices at each login). Commented May 24, 2019 at 16:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.