#! /usr/bin/env python # # mutetoggle.py - mute toggle script for latop's volume mute key # # Written by Harshad Sharma [http://www.harshadsharma.name/] # This program is in the public domain, you are free to use it as you like. # # Thanks: # - Kelly Black for the research. [http://blackk.union.edu/~black/freebsd/] # # This script is used with the following software: # - xbindkeys [http://hocwp.free.fr/xbindkeys/xbindkeys.html] # - xbindkeys_config [http://www.freshports.org/x11/xbindkeys_config/] # # # import commands from time import sleep def add(curVolume, b, endVolume): if curVolume + b > endVolume: return endVolume else: # The else is only for sake of understandability return curVolume + b def sub(curVolume, b, endVolume): if curVolume - b < endVolume: return endVolume else: return curVolume - b def compAdd(curVolume, endVolume): """Returns true if we are done with setting the volume...""" return curVolume >= endVolume def compSub(curVolume, endVolume): """Returns true if we are done with setting the volume...""" return curVolume <= endVolume def softSetVolume(curVolume, endVolume, step = 2, speed = 0.0): """softSetVolume to fade in/out using master volume mixer begin: mixer volume value where to start from end: mixer volume value desired at the end step: how much to add/ subtract at a time. """ if curVolume < endVolume: calculate = add done = compAdd else: calculate = sub done = compSub while(not done(curVolume, endVolume)): curVolume = calculate(curVolume, step, endVolume) commands.getoutput(r"mixer vol %s" % curVolume) sleep(speed) # ---------------------------------------------------------------------- # def main(): volumeStep = 2 # Default: 2 speed = 0.02 # Default: 0.02 currentVolume = int(commands.getoutput(r"mixer vol | sed -e 's/^.*\://'")) if currentVolume == 0: volume = int(commands.getoutput(r"cat ~/.volume")) #commands.getoutput(r"mixer vol %s" % volume) softSetVolume(0, volume, volumeStep, speed) else: commands.getoutput(r"echo %s > ~/.volume" % currentVolume) #commands.getoutput(r"mixer vol 0") softSetVolume(currentVolume, 0 , volumeStep, speed) if __name__ == "__main__": main()