2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009, 2010, 2012 Kaliko Jack <kaliko.jack@azylum.org>
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 heavily borrowed from perl script mpdtoys and converted to python.
23 mpdtoys © 2007 Joey Hess <joey@kitenet.net>
24 http://kitenet.net/~joey/code/mpdtoys
29 from time import sleep
31 from lib.goodies import Goodie
36 USAGE = 'USAGE: %prog [--help] | [ <time> [<final volume level>] ]'
37 DESC = """Fade in/out to <final volume level> over <time>. Defaults are from 0%
38 to 50% when paused or stopped and from current volume to 10th of it if playing,
39 both over 10 minutes."""
53 Goodie.__init__(self, self.__class__.script_info)
59 def _consume_args(self):
61 if (len(self.cli_args) < 1 or
62 len(self.cli_args) > 2):
63 self.parser.error('need at least an argument and no more than two!')
65 self.tempo = int(self.cli_args[0])
66 self.volum = int(self.cli_args[1])
69 except ValueError, err:
70 self.parser.error('wrong option passed (%s)' % err)
75 self.mpd_state = str(self.client.status().get('state'))
76 self.mpd_vol = int(self.client.status().get('volume'))
77 if self.mpd_state == 'play':
79 self.volum = self.mpd_vol / 10
80 if self.volum > self.mpd_vol:
81 self.parser.error('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol))
82 print >> sys.stdout, 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, self.volum, self.tempo)
85 if self.mpd_state in ['stop', 'pause']:
88 print >> sys.stdout, 'fading up from 0%% to %d%% over %smin' % (self.volum, self.tempo)
97 # TODO: handle possible lost connections
98 span = float(self.volum - self.mpd_vol)
99 step = span / float(60 * self.tempo)
102 if int(vol) != int(self.client.status().get('volume')):
103 sys.stderr.write('Warning: external volume change, aborting!\n')
106 self.client.setvol(int(vol))
107 if abs(vol - self.volum) < 1:
108 self.client.setvol(self.volum)
114 if __name__ == '__main__':
117 except KeyboardInterrupt:
118 sys.stdout.write('exit')
121 # vim: ai ts=4 sw=4 sts=4 expandtab