Skip to content

Instantly share code, notes, and snippets.

@DeusFigendi
Last active April 7, 2021 20:03
Show Gist options
  • Save DeusFigendi/d8cfbb66a881155caf04644573d77039 to your computer and use it in GitHub Desktop.
Save DeusFigendi/d8cfbb66a881155caf04644573d77039 to your computer and use it in GitHub Desktop.
This python3 script randomly chooses an output device and tells pulse audio to bind ALL the outputs from all the programs to that device.
#!/usr/bin/python3
import subprocess
import random
# get a list of all available sinks (output-devices)
get_the_sinks = subprocess.check_output(["pactl", "list", "short", "sinks"]).decode('utf-8').split("\n")
list_of_sinks = []
for outputline in get_the_sinks:
this_sink_listing = outputline.split("\t")
if (this_sink_listing != ['']):
list_of_sinks.append({'id':int(this_sink_listing[0]), 'name':this_sink_listing[1], 'driver':this_sink_listing[2], 'sampling':this_sink_listing[3], 'state':this_sink_listing[4]})
# get a list of all running outputs (like... programs)
get_the_sources = subprocess.check_output(["pactl", "list", "short", "sink-inputs"]).decode('utf-8').split("\n")
list_of_sources = []
for outputline in get_the_sources:
this_source_id = outputline.split("\t")[0]
if (this_source_id != ''):
list_of_sources.append(int(this_source_id))
# choose ONE sink… here it is done randomly but it could also be set by a regexp on the sinks name or hardcoded by its ID or whatever
target_sink = random.choice(list_of_sinks)['id']
for source_id in list_of_sources:
subprocess.run(['pactl', 'move-sink-input', str(source_id), str(target_sink)])
subprocess.run(['pactl', 'set-default-sink', str(target_sink)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment