Last active
January 28, 2018 14:16
-
-
Save indeedwatson/067fbc651b5155fa30db76a91c99d7b2 to your computer and use it in GitHub Desktop.
This file contains 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
#!/usr/bin/python | |
# credit https://gitlab.com/lyze237/dotfiles-public/blob/master/dotfiles/config/awesome/scripts/defaultSink.py | |
# | |
# TODO: if jack is present, ignore Focusrite sink | |
import os | |
import re | |
from subprocess import check_output, call | |
# Mapping of cards and their default volumes | |
volumes = {'alsa_output.usb-Focusrite_Scarlett_2i2_USB-00.analog-stereo': 100, | |
'alsa_output.pci-0000_00_1b.0.analog-stereo': 80, | |
'jack_out': 100} | |
sinks_regex = '(alsa_output\..*?|jack_out)\s' | |
null = open(os.devnull, 'w') | |
# Get available sinks | |
pactl_sinks = check_output( | |
['pactl', 'list', 'sinks', 'short']).decode('utf-8').split('\n') | |
sinks = [] | |
for line in pactl_sinks: | |
match = re.search(sinks_regex, line) | |
if match: | |
sink = re.search(sinks_regex, line).group(1) | |
if 'echo-cancel' not in sink: | |
sinks.append(sink) | |
print('Available sinks:') | |
for sink in sinks: | |
print(' ' + sink) | |
# Get default sink | |
pactl_info = check_output(['pactl', 'info']).decode('utf-8') | |
sink_default = re.search( | |
'Default Sink: ' + sinks_regex, pactl_info).group(1) | |
print('Default sink:') | |
print(' ' + sink_default) | |
# Get next sink | |
index_default = [sink for sink in sinks].index(sink_default) | |
sink_next = sinks[(index_default + 1) % len(sinks)] | |
print('Next sink:') | |
print(' ' + sink_next) | |
# Set new default sink | |
call(['pacmd', 'set-default-sink %s' % sink_next], stdout=null) | |
call(['notify-send', '-i', 'multimedia-volume-control', 'Default Sink', 'Switched to ' + sink_next]) | |
# Mute while moving inputs | |
call(['pactl', 'set-sink-volume', sink_next, '0%'], stdout=null) | |
# Get inputs to move | |
pacmd_inputs = check_output( | |
['pacmd', 'list-sink-inputs']).decode('utf-8') | |
inputs = re.findall('index: (\d+)', pacmd_inputs, re.DOTALL) | |
print('Inputs to move:') | |
print(' ' + ' '.join(inputs)) | |
applications = re.findall('application\.process\.binary = "(.*?)"', | |
pacmd_inputs, re.DOTALL) | |
print('Applications to move:') | |
for application in applications: | |
print(' ' + application) | |
# Move inputs | |
for input_ in inputs: | |
call(['pacmd', 'move-sink-input', input_, sink_next], stdout=null) | |
# Unmute | |
if sink_next in volumes.keys(): | |
call(['pactl', 'set-sink-volume', sink_next, | |
str(volumes[sink_next]) + '%'], stdout=null) | |
else: | |
call(['pactl', 'set-sink-volume', sink_next, '100%'], stdout=null) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment