]> kaliko git repositories - mpd-goodies.git/blob - nalbum
* Add wakeup script.
[mpd-goodies.git] / nalbum
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 import sys
23
24 from time import sleep
25
26 from lib.mpdclass import MPDClass
27 from lib.startop import StartOpt
28
29 NAME = 'nalbum'
30 VERSION = '0.1'
31
32 NALBUM_OPTS = list([
33     {
34         'sw': ['-c', '--crossfade'],
35         'type': 'int',
36         'dest': 'time',
37         'metavar': 't',
38         'help': 'fade out/fade in over t seconds.'},
39     ])
40
41
42 class Nalbum(StartOpt, MPDClass):
43     """
44     """
45     script_info = dict({
46         'version': VERSION,
47         'prog_name': NAME,
48         'description': 'Jump to next album in playlist.',
49         })
50
51     def __init__(self):
52         """"""
53         StartOpt.__init__(self, self.__class__.script_info, NALBUM_OPTS)
54         MPDClass.__init__(self)
55         self._run()
56
57     def _fade(self, io='out'):#{{{
58         """
59         end_volum => End volume value
60         mpd_vol   => Start volume value
61         """
62         mpd_vol = int(self.init_vol)
63         if io == 'out':
64             end_volum = mpd_vol / 10
65             mpd_vol = self.init_vol
66         if io == 'in':
67             end_volum = int(self.init_vol)
68             mpd_vol = int(self.init_vol) / 10
69         span = float(end_volum - mpd_vol)
70         step = span / float(10*self.cli_options.time)
71         while 42:
72             mpd_vol += step
73             self.client.setvol(int(mpd_vol))
74             if abs(mpd_vol - end_volum) < 1:
75                 self.client.setvol(end_volum)
76                 return True
77             sleep(0.1)#}}}
78
79     def _get_next(self):#{{{
80         """Retrieve playlist from current song to the end."""
81         if 'song' not in self.client.status():
82             print "No current song set in MPD!"
83             self.client.disconnect()
84             sys.exit(0)
85         current_album = str(self.client.currentsong().get('album', 'TAG MISSING'))
86         current_song_pos = int(self.client.currentsong().get('pos'))
87         print 'Current album: "%s"' % current_album
88         next_album_pos = current_song_pos
89         album = current_album
90         while album == current_album:
91             next_album_pos += 1
92             pl_length = int(self.client.status().get('playlistlength')) - 1
93             if pl_length < next_album_pos:
94                 print 'Next album not found in the playlitst!'
95                 self.client.disconnect()
96                 sys.exit(0)
97             album = self.client.playlistinfo(next_album_pos)[0].get('album', 'TAG MISSING')
98         print 'Next album appears to be: "%s"' % album
99         return next_album_pos#}}}
100
101     def _run(self):
102         """"""
103         print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
104         self.mpdConnect()
105         self.init_vol = int(self.client.status().get('volume'))
106         if self.cli_options.time:
107             print 'Cross fading next album'
108             self._fade()
109         self.client.play(self._get_next())
110         if self.cli_options.time:
111             self._fade(io='in')
112         self.client.disconnect()
113         sys.exit(0)
114
115
116 # Script starts here
117 if __name__ == '__main__':
118     try:
119         Nalbum()
120     except KeyboardInterrupt:
121         sys.stdout.write('exit')
122
123 # VIM MODLINE
124 # vim: ai ts=4 sw=4 sts=4 expandtab