X-Git-Url: https://git.kaliko.me/?a=blobdiff_plain;f=mfade;h=6b8616b56e7f529abef6cd24dd3808c3bac44a77;hb=86013a2622c1f21cc64ef81733d48023caaf75a9;hp=22d2f3a0d9cc810684e9bf1a9d685a390d13c5d6;hpb=0aadd940e70ee66a6b627e672c25ebb5538086bf;p=mpd-goodies.git diff --git a/mfade b/mfade index 22d2f3a..6b8616b 100755 --- a/mfade +++ b/mfade @@ -1,7 +1,7 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -# Copyright (c) 2009 Efrim {{{ +# Copyright (c) 2009, 2010 Efrim {{{ # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -36,11 +36,12 @@ USAGE = """Usage: mfade [time [min|max]] * time in seconds - * min|max in percentag + * min|max in percentage of volume -- not palying: fade in from 0% to max over time +when MPD is: +- not playing: fade in from 0% to max over time default 10 minutes / 50% -- if playing: fade out from current volume to min over time +- playing: fade out from current volume to min over time default 10 minutes / 1/10 of current vol Manual or any external volume change will abort the script. @@ -62,9 +63,10 @@ class Sleep(object): def _consume_sopt(self): """""" + # TODO: use optparse? options = sys.argv - if len(sys.argv) >1 and sys.argv[1] in ['-h', '--help']: - print USAGE + if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help']: + sys.stderr.write(USAGE) sys.exit(1) try: self.tempo = int(options.pop(1)) @@ -72,8 +74,8 @@ class Sleep(object): except IndexError: pass except ValueError, err: - print 'Error: wrong option passed: %s' % err - print USAGE + sys.stderr.write('Error: wrong option passed: %s' % err) + sys.stdout.write(USAGE) sys.exit(1) def _run(self): @@ -84,17 +86,15 @@ class Sleep(object): if not self.volum: self.volum = self.mpd_vol / 10 if self.volum > self.mpd_vol: - print 'Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol) + sys.stderr.write('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol)) sys.exit(1) - print 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, - self.volum, self.tempo) - self.fade() - self.cli.stop() + print >> sys.stdout, 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, self.volum, self.tempo) + if self.fade(): + self.cli.stop() if self.mpd_state in ['stop', 'pause']: if not self.volum: self.volum = int(50) - print 'fading up from 0%% to %d%% over %smin' % (self.volum, - self.tempo) + print >> sys.stdout, 'fading up from 0%% to %d%% over %smin' % (self.volum, self.tempo) self.cli.setvol(0) self.mpd_vol = 0 self.cli.play() @@ -103,30 +103,28 @@ class Sleep(object): def fade(self): """""" + # TODO: handle possible lost connections span = float(self.volum - self.mpd_vol) step = span / float(60 * self.tempo) - print step vol = self.mpd_vol while 42: if int(vol) != int(self.cli.status().get('volume')): - print 'Warning: external volume change, aborting!' - break + sys.stderr.write('Warning: external volume change, aborting!\n') + return False vol += step self.cli.setvol(int(vol)) if abs(vol - self.volum) < 1: self.cli.setvol(self.volum) - break + return True sleep(1) # Script starts here if __name__ == '__main__': - options = [14, 11] - #main(options) try: Sleep() except KeyboardInterrupt: - print 'exit' + sys.stdout.write('exit') # VIM MODLINE # vim: ai ts=4 sw=4 sts=4 expandtab