Last active
July 15, 2020 23:42
-
-
Save freb/2f15e89e7859f857e97904c901f6bbea to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/env python | |
# | |
# cyclesink sets all audio sources (sink inputs) to the same next | |
# sink in PulseAudio. PulseAudio sets newly created sink inputs to | |
# use the sink that was last assigned to, so all sources should always | |
# be using the same output unless the configuration is changed elsewhere. | |
import os | |
def get_sinks(): | |
stream = os.popen('pactl list short sinks') | |
output = stream.read() | |
sinks = [] | |
for sink in output.splitlines(): | |
sink_no = sink.split()[0] | |
sink_name = sink.split()[1] | |
sinks.append({ | |
'no': sink_no, | |
'name': sink_name, | |
}) | |
return sinks | |
def get_streams(): | |
stream = os.popen('pactl list short sink-inputs') | |
output = stream.read() | |
streams = [] | |
for stream in output.splitlines(): | |
stream_no = stream.split()[0] | |
sink_no = stream.split()[1] | |
streams.append({ | |
'no': stream_no, | |
'sink': sink_no, | |
}) | |
return streams | |
streams = get_streams() | |
sinks = get_sinks() | |
if len(streams) == 0: | |
print('no streams') | |
exit(0) | |
if len(sinks) == 0: | |
print('no sinks') | |
exit(0) | |
# if only one sink, just set all streams to it | |
cur = streams[0]['sink'] | |
idx = 0 | |
for i, sink in enumerate(sinks): | |
if sink['no'] == cur: | |
print('found sink idx:', i) | |
idx = i | |
break | |
nxt = sinks[(idx+1)%len(sinks)] | |
for stream in streams: | |
print('setting {} to {}'.format(stream['no'], nxt['no'])) | |
os.popen('pactl move-sink-input {} {}'.format(stream['no'], nxt['no'])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment