2 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009 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.mpdutils import mconnect
36 mfade [time [min|max]]
39 * min|max in percentag
42 - not palying: fade in from 0% to max over time
43 default 10 minutes / 50%
44 - playing: fade out from current volume to min over time
45 default 10 minutes / 1/10 of current vol
47 Manual or any external volume change will abort the script.
64 def _consume_sopt(self):
68 if len(sys.argv) > 1 and sys.argv[1] in ['-h', '--help']:
69 sys.stderr.write(USAGE)
72 self.tempo = int(options.pop(1))
73 self.volum = int(options.pop(1))
76 except ValueError, err:
77 sys.stderr.write('Error: wrong option passed: %s' % err)
78 sys.stdout.write(USAGE)
83 self.mpd_state = str(self.cli.status().get('state'))
84 self.mpd_vol = int(self.cli.status().get('volume'))
85 if self.mpd_state == 'play':
87 self.volum = self.mpd_vol / 10
88 if self.volum > self.mpd_vol:
89 sys.stderr.write('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol))
91 sys.stdout.write('fading down from %d%% to %d%% over %smin' % (self.mpd_vol,
92 self.volum, self.tempo))
95 if self.mpd_state in ['stop', 'pause']:
98 sys.stdout.write('fading up from 0%% to %d%% over %smin' % (self.volum,
108 # TODO: handle possible lost connections
109 span = float(self.volum - self.mpd_vol)
110 step = span / float(60 * self.tempo)
113 if int(vol) != int(self.cli.status().get('volume')):
114 sys.stdout.write('Warning: external volume change, aborting!')
117 self.cli.setvol(int(vol))
118 if abs(vol - self.volum) < 1:
119 self.cli.setvol(self.volum)
125 if __name__ == '__main__':
128 except KeyboardInterrupt:
129 sys.stdout.write('exit')
132 # vim: ai ts=4 sw=4 sts=4 expandtab