]> kaliko git repositories - mpd-goodies.git/blob - mfade
* Add wakeup script.
[mpd-goodies.git] / mfade
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009, 2010 Efrim <efrim@azylum.org> {{{
5 #
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.
10 #
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.
15 #
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/>.
18 #
19 #  }}}
20
21 """
22 DOC:
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
26 """
27
28 import sys
29
30 from time import sleep
31
32 from lib.mpdclass import MPDClass
33 from lib.startop import StartOpt
34
35 NAME = 'mfade'
36 VERSION = '0.1'
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."""
41
42
43 class Sleep(StartOpt, MPDClass):
44     """"""
45     script_info = dict({
46         'version': VERSION,
47         'prog_name': NAME,
48         'description': DESC,
49         'usage': USAGE,
50         })
51
52     def __init__(self):#{{{
53         """"""
54         StartOpt.__init__(self, self.__class__.script_info, [])
55         MPDClass.__init__(self)
56         self.tempo = int(10)
57         self.volum = None
58         self._consume_args()
59         self._run()#}}}
60
61     def _consume_args(self):#{{{
62         """"""
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!')
66         try:
67             self.tempo = int(self.cli_args[0])
68             self.volum = int(self.cli_args[1])
69         except IndexError:
70             pass
71         except ValueError, err:
72             self.parser.error('wrong option passed (%s)' % err)#}}}
73
74     def _run(self):#{{{
75         """"""
76         self.mpdConnect()
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':
80             if not self.volum:
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)
85             if self.fade():
86                 self.client.stop()
87         if self.mpd_state in ['stop', 'pause']:
88             if not self.volum:
89                 self.volum = int(50)
90             print >> sys.stdout, 'fading up from 0%% to %d%% over %smin' % (self.volum, self.tempo)
91             self.client.setvol(0)
92             self.mpd_vol = 0
93             self.client.play()
94             self.fade()
95         sleep(1)#}}}
96
97     def fade(self):#{{{
98         """"""
99         # TODO: handle possible lost connections
100         span = float(self.volum - self.mpd_vol)
101         step = span / float(60 * self.tempo)
102         vol = self.mpd_vol
103         while 42:
104             if int(vol) != int(self.client.status().get('volume')):
105                 sys.stderr.write('Warning: external volume change, aborting!\n')
106                 return False
107             vol += step
108             self.client.setvol(int(vol))
109             if abs(vol - self.volum) < 1:
110                 self.client.setvol(self.volum)
111                 return True
112             sleep(1)#}}}
113
114
115 # Script starts here
116 if __name__ == '__main__':
117     try:
118         Sleep()
119     except KeyboardInterrupt:
120         sys.stdout.write('exit')
121
122 # VIM MODLINE
123 # vim: ai ts=4 sw=4 sts=4 expandtab