Skip to content

Instantly share code, notes, and snippets.

@professorjamesmoriarty
Created February 12, 2016 13:37
Show Gist options
  • Save professorjamesmoriarty/4c873b441642cb4f131b to your computer and use it in GitHub Desktop.
Save professorjamesmoriarty/4c873b441642cb4f131b to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# TODO
# add way to toggle boost (-b for example)
# cleanup redunant functions
# alternate osd, maybe built in gtk?
"""Handle vol info from pamixer and pass to aosd_cat."""
import sys
import subprocess
import os
def show_osd(s):
"""Display OSD."""
cmds = ["osd_cat", "--colour=magenta", "--align=right", "--pos=bottom",
"--delay=1", "--age=1", "--font=-*-fixed-medium-r-*-*-32-*-*-*-*-*-*-*"]
p = subprocess.Popen(cmds, stdin=subprocess.PIPE)
p.stdin.write(bytes(s, "UTF-8"))
p.stdin.close()
def cur_volume():
"""Simple return of current volume."""
p = subprocess.Popen(["pamixer", "--get-volume"], stdout=subprocess.PIPE)
s = p.stdout.read()
p.stdout.close()
return int(str(s, 'UTF-8'))
def is_mute():
"""Check if audio is muted."""
p = subprocess.Popen(["pamixer", "--get-mute"], stdout=subprocess.PIPE)
s = p.stdout.read()
s2 = str(s, 'UTF-8').strip()
p.stdout.close()
return s2.startswith("true")
def toggle_mute():
"""Toggle current state of audio."""
os.system("pamixer --toggle-mute")
if is_mute():
show_osd("--- MUTE ---")
else:
show_cur_volume()
def show_cur_volume():
"""Check current volume."""
show_osd("%s%%" % (cur_volume()))
def set_volume(n):
"""Set volume, allow-boost switch to go higher then 100%."""
os.system("pamixer --unmute")
os.system("pamixer --allow-boost --set-volume %s" % (n))
show_cur_volume()
def up_volume():
"""Turn volume up."""
set_volume(cur_volume() + 5)
def down_volume():
"""Turn volume down."""
set_volume(cur_volume() - 5)
def show_state():
"""Check state of audio."""
if is_mute():
show_osd("--- MUTE ---")
else:
show_cur_volume()
if __name__ == '__main__':
if len(sys.argv) > 1:
cmd = sys.argv[1]
if cmd == 'toggle':
toggle_mute()
elif cmd == 'up':
up_volume()
elif cmd == 'down':
down_volume()
elif cmd == 'state':
show_state()
else:
print("unknown command %s" % (cmd))
sys.exit(1)
else:
print("USAGE: %s {state|toggle|up|down}" % (sys.argv[0]))
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment