]> kaliko git repositories - mpd-goodies.git/blob - mfade
* rename fadio and remove useless dev/debug log.
[mpd-goodies.git] / mfade
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009 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.mpdutils import mconnect
33
34 USAGE = """Usage:
35
36 mfade [time [min|max]]
37
38    * time in seconds
39    * min|max in percentag
40
41 - not palying: fade in from 0% to max over time
42                default 10 minutes / 50%
43 - if playing:  fade out from current volume to min over time
44                default 10 minutes / 1/10 of current vol
45
46 Manual or any external volume change will abort the script.
47 """
48
49
50 class Sleep(object):
51     """
52     """
53
54     def __init__(self):
55         """"""
56         self.tempo = int(10)
57         self.volum = None
58         self._consume_sopt()
59         self.cli = mconnect()
60         self._run()
61         self.cli.disconnect()
62
63     def _consume_sopt(self):
64         """"""
65         options = sys.argv
66         if len(sys.argv) >1 and sys.argv[1] in ['-h', '--help']:
67             print USAGE
68             sys.exit(1)
69         try:
70             self.tempo = int(options.pop(1))
71             self.volum = int(options.pop(1))
72         except IndexError:
73             pass
74         except ValueError, err:
75             print 'Error: wrong option passed: %s' % err
76             print USAGE
77             sys.exit(1)
78
79     def _run(self):
80         """"""
81         self.mpd_state = str(self.cli.status().get('state'))
82         self.mpd_vol = int(self.cli.status().get('volume'))
83         if self.mpd_state == 'play':
84             if not self.volum:
85                 self.volum = self.mpd_vol / 10
86             if self.volum > self.mpd_vol:
87                 print 'Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol)
88                 sys.exit(1)
89             print 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol,
90                     self.volum, self.tempo)
91             self.fade()
92             self.cli.stop()
93         if self.mpd_state in ['stop', 'pause']:
94             if not self.volum:
95                 self.volum = int(50)
96             print 'fading up from 0%% to %d%% over %smin' % (self.volum,
97                     self.tempo)
98             self.cli.setvol(0)
99             self.mpd_vol = 0
100             self.cli.play()
101             self.fade()
102         sleep(1)
103
104     def fade(self):
105         """"""
106         span = float(self.volum - self.mpd_vol)
107         step = span / float(60 * self.tempo)
108         print step
109         vol = self.mpd_vol
110         while 42:
111             if int(vol) != int(self.cli.status().get('volume')):
112                 print 'Warning: external volume change, aborting!'
113                 break
114             vol += step
115             self.cli.setvol(int(vol))
116             if abs(vol - self.volum) < 1:
117                 self.cli.setvol(self.volum)
118                 break
119             sleep(1)
120
121
122 # Script starts here
123 if __name__ == '__main__':
124     options = [14, 11]
125     #main(options)
126     try:
127         Sleep()
128     except KeyboardInterrupt:
129         print 'exit'
130
131 # VIM MODLINE
132 # vim: ai ts=4 sw=4 sts=4 expandtab