Skip to content

Instantly share code, notes, and snippets.

@freb

freb/cyclesink.py

Last active Jul 15, 2020
Embed
What would you like to do?
#!/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