Reputation: 109
If you haven't heard of SoundSwitch, its an app for windows that allows you to switch sound output/input devices with a keyboard shortcut. I've made a similar app for linux, but I cant get it to work properly. The majority of the app is done, and if you want to see the full code, its here: https://github.com/boskobs/sound-Source-Switch-4-Linux Bellow is the part responsible for applying the changes:
os.system("pacmd set-default-sink " + str(nextindex))
output = subprocess.getoutput("pacmd list-sink-inputs")
for item in output.split("\n"):
if "index:" in item:
inputindex = item.strip().replace("index: ","")
os.system("pacmd move-sink-input " + str(inputindex) + " " + str(nextindex))
It changes the default sound output device, and transfers all of the current apps to that device. The problem occurs when I exit an app and switch the output device. Next time I start that app, the device it outputs sound to is the old one that was active before the switch. How can I make the new default output device really work as a default?
Upvotes: 3
Views: 2801
Reputation: 1
[NOTE] Make sure to click on the image links to follow this solution.
Ubuntu 20.04
Run pacmd list-cards
to list the audio devices.
Output devices in settings should match list-cards
To set a device run pacmd set-default-sink bluez_sink.38_18_4C_12_44_0B.a2dp_sink
with your own device name.
Then once this works, you can create some keyboard shortcuts.
I use ctrl + Home as mine. You can do this for all audio devices and switch between them with ease.
Upvotes: 0
Reputation: 109
When the currently chosen device is not the same as the device that one of the apps is streaming to, a fix gets applied instead of a switch.
# Checking for changes
output = subprocess.getoutput("pacmd list-sinks").split("\n")
for item in range(0, len(output)-1):
if "* index: " in output[item]:
currentindexname = output[item+1].replace("name: <", "").strip()[:-1]
break
output = subprocess.getoutput("pacmd list-sink-inputs")
for item in output.split("\n"):
if "sink:" in item:
if currentindexname != item.split("<")[1].split(">")[0]:
for item in output.split("\n"):
if "index:" in item:
inputindex = item.strip().replace("index: ","")
os.system("pacmd move-sink-input " + str(inputindex) + " " + str(currentindex))
os.system('notify-send "Source" "Fixed"')
exit()
Its not ideal, but it gets the job done.
Upvotes: 0
Reputation: 131550
According to the FreeDesktop.org wiki as well as this answer on AskUbuntu and related posts, whenever a new stream (sound-producing program) starts up, PulseAudio will attach it to the same sink (output device) that it attached to last time it disappeared. This sounds like the effect you're seeing. You close a program which was using device A, start your Source Switch app and switch everything to device B, and the open the program again, and PulseAudio sets it to using device A again.
You can disable this behavior of PulseAudio by adding the line
load-module module-stream-restore restore_device=false
to /etc/pulse/default.pa
and restarting PulseAudio. This is probably a reasonable choice for someone who is going to be using your app to manage their sound devices; you could incorporate this into your installation procedure, but the standard advice about being very careful when you mess around with system configuration files applies.
Alternatively, you can delete the stream restore database, which is stored in the files $HOME/.pulse/*stream-volumes*.gdbm
. From that point on, PulseAudio will think every audio stream is brand new and will assign it to the fallback audio device, which is what you set with set-default-sink
. (This also requires restarting PA.)
Upvotes: 1