Skip to content

Instantly share code, notes, and snippets.

@Zer0t3ch
Last active August 29, 2015 14:09
Show Gist options
  • Save Zer0t3ch/a738cc9dca5dcae43ded to your computer and use it in GitHub Desktop.
Save Zer0t3ch/a738cc9dca5dcae43ded to your computer and use it in GitHub Desktop.
Audio Device Switcher
#!/usr/bin/python2
import subprocess as sp
import sys
devhdmi = '5'
devheadset = '8'
storedir = "/home/chaos/bin/"
filename = storedir + ".currentdevice"
rawsp = sp.Popen(['pacmd', 'list-sink-inputs'], stdout=sp.PIPE)
rawout = rawsp.stdout
indices = [ ]
command = ['pacmd', 'move-sink-input', '$INDEX', '$DEST']
def write(fil, data):
f = open(fil, mode='w')
f.write(data)
f.close()
return 0
def read(fil):
try:
f = open(fil, mode='r')
except:
write(filename, '0')
return '1'
t = f.read()
f.close()
return t.split()[0]
for line in rawout:
l = str(line)
if "index" in l:
t = l.split()[1]
indices.append(t)
dev = read(filename)
if len(sys.argv) > 1:
if sys.argv[1] == "current":
if dev == "1":
print("Headset")
elif dev == "2":
print("HDMI")
else:
print("Unknown output device ({0})".format(dev))
sys.exit()
# Toggle device file
if dev == '1':
write(filename, '2')
else:
write(filename, '1')
# Set the destination of the command
if dev == '1':
command[3] = devhdmi
else:
command[3] = devheadset
for i in indices:
command[2] = i
sp.call(command)
command[2] = command[3]
command.pop()
command[1] = "set-default-sink"
sp.call(command)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment