2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009, 2010 Efrim <efrim@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/>.
23 heavily borrowed from perl script mpdtoys and converted to python.
24 mpdtoys © 2007 Joey Hess <joey@kitenet.net>
25 http://kitenet.net/~joey/code/mpdtoys
30 from time import sleep
32 from lib.mpdclass import MPDClass
33 from lib.startop import StartOpt
37 USAGE = 'USAGE: %prog [--help] | [ <time> [<final volume level>] ]'
38 DESC = """Fade in/out to <final volume level> over <time>. Defaults are from 0%
39 to 50% when paused or stopped and from current volume to 10th of it if playing,
40 both over 10 minutes."""
43 class Sleep(StartOpt, MPDClass):
52 def __init__(self):#{{{
54 StartOpt.__init__(self, self.__class__.script_info, [])
55 MPDClass.__init__(self)
61 def _consume_args(self):#{{{
63 if (len(self.cli_args) < 1 or
64 len(self.cli_args) > 2):
65 self.parser.error('need at least an argument and no more than two!')
67 self.tempo = int(self.cli_args[0])
68 self.volum = int(self.cli_args[1])
71 except ValueError, err:
72 self.parser.error('wrong option passed (%s)' % err)#}}}
77 self.mpd_state = str(self.client.status().get('state'))
78 self.mpd_vol = int(self.client.status().get('volume'))
79 if self.mpd_state == 'play':
81 self.volum = self.mpd_vol / 10
82 if self.volum > self.mpd_vol:
83 self.parser.error('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol))
84 print >> sys.stdout, 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, self.volum, self.tempo)
87 if self.mpd_state in ['stop', 'pause']:
90 print >> sys.stdout, 'fading up from 0%% to %d%% over %smin' % (self.volum, self.tempo)
99 # TODO: handle possible lost connections
100 span = float(self.volum - self.mpd_vol)
101 step = span / float(60 * self.tempo)
104 if int(vol) != int(self.client.status().get('volume')):
105 sys.stderr.write('Warning: external volume change, aborting!\n')
108 self.client.setvol(int(vol))
109 if abs(vol - self.volum) < 1:
110 self.client.setvol(self.volum)
116 if __name__ == '__main__':
119 except KeyboardInterrupt:
120 sys.stdout.write('exit')
123 # vim: ai ts=4 sw=4 sts=4 expandtab