]> kaliko git repositories - mpd-goodies.git/blob - src/mfade
* Add wakeup script.
[mpd-goodies.git] / src / 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 DOC:
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
25 """
26
27 import sys
28
29 from time import sleep
30
31 from lib.mpdclass import MPDClass
32 from lib.startop import StartOpt
33
34 NAME = 'mfade'
35 VERSION = '0.1'
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."""
40
41
42 class Sleep(StartOpt, MPDClass):
43     """"""
44     script_info = dict({
45         'version': VERSION,
46         'prog_name': NAME,
47         'description': DESC,
48         'usage': USAGE,
49         })
50
51     def __init__(self):
52         """"""
53         StartOpt.__init__(self, self.__class__.script_info, [])
54         MPDClass.__init__(self)
55         self.tempo = int(10)
56         self.volum = None
57         self._consume_args()
58         self._run()
59
60     def _consume_args(self):
61         """"""
62         if (len(self.cli_args) < 1 or
63                 len(self.cli_args) > 2):
64             self.parser.error('need at least an argument and no more than two!')
65         try:
66             self.tempo = int(self.cli_args[0])
67             self.volum = int(self.cli_args[1])
68         except IndexError:
69             pass
70         except ValueError, err:
71             self.parser.error('wrong option passed (%s)' % err)
72
73     def _run(self):
74         """"""
75         self.mpdConnect()
76         self.mpd_state = str(self.client.status().get('state'))
77         self.mpd_vol = int(self.client.status().get('volume'))
78         if self.mpd_state == 'play':
79             if not self.volum:
80                 self.volum = self.mpd_vol / 10
81             if self.volum > self.mpd_vol:
82                 self.parser.error('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol))
83             print >> sys.stdout, 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, self.volum, self.tempo)
84             if self.fade():
85                 self.client.stop()
86         if self.mpd_state in ['stop', 'pause']:
87             if not self.volum:
88                 self.volum = int(50)
89             print >> sys.stdout, 'fading up from 0%% to %d%% over %smin' % (self.volum, self.tempo)
90             self.client.setvol(0)
91             self.mpd_vol = 0
92             self.client.play()
93             self.fade()
94         sleep(1)
95
96     def fade(self):
97         """"""
98         # TODO: handle possible lost connections
99         span = float(self.volum - self.mpd_vol)
100         step = span / float(60 * self.tempo)
101         vol = self.mpd_vol
102         while 42:
103             if int(vol) != int(self.client.status().get('volume')):
104                 sys.stderr.write('Warning: external volume change, aborting!\n')
105                 return False
106             vol += step
107             self.client.setvol(int(vol))
108             if abs(vol - self.volum) < 1:
109                 self.client.setvol(self.volum)
110                 return True
111             sleep(1)
112
113
114 # Script starts here
115 if __name__ == '__main__':
116     try:
117         Sleep()
118     except KeyboardInterrupt:
119         sys.stdout.write('exit')
120
121 # VIM MODLINE
122 # vim: ai ts=4 sw=4 sts=4 expandtab