]> kaliko git repositories - mpd-goodies.git/blob - src/nalbum
* nalbum: Check availability of next album before starting fading out
[mpd-goodies.git] / src / nalbum
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 # Copyright (c) 2009, 2010, 2012 Kaliko Jack <kaliko.jack@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 import sys
22
23 from time import sleep
24
25 from lib.goodies import Goodie
26
27
28 NAME = 'nalbum'
29 VERSION = '0.1'
30
31 NALBUM_OPTS = list([
32     {
33         'sw': ['-c', '--crossfade'],
34         'type': 'int',
35         'dest': 'time',
36         'metavar': 't',
37         'help': 'fade out/fade in over t seconds.'},
38     ])
39
40
41 class Nalbum(Goodie):
42     """
43     """
44     script_info = dict({
45         'version': VERSION,
46         'prog_name': NAME,
47         'description': 'Jump to next album in playlist.',
48         })
49
50     def __init__(self):
51         """"""
52         Goodie.__init__(self, self.__class__.script_info,
53                 extra_options=NALBUM_OPTS)
54         self._run()
55
56     def _fade(self, io='out'):
57         """
58         end_volum => End volume value
59         mpd_vol   => Start volume value
60         """
61         mpd_vol = int(self.init_vol)
62         if io == 'out':
63             end_volum = mpd_vol / 10
64             mpd_vol = self.init_vol
65         if io == 'in':
66             end_volum = int(self.init_vol)
67             mpd_vol = int(self.init_vol) / 10
68         span = float(end_volum - mpd_vol)
69         step = span / float(10*self.cli_options.time)
70         while 42:
71             mpd_vol += step
72             self.client.setvol(int(mpd_vol))
73             if abs(mpd_vol - end_volum) < 1:
74                 self.client.setvol(end_volum)
75                 return True
76             sleep(0.1)
77
78     def _get_next(self):
79         """Retrieve playlist from current song to the end."""
80         if 'song' not in self.client.status():
81             print "No current song set in MPD!"
82             self.client.disconnect()
83             sys.exit(0)
84         current_album = str(self.client.currentsong().get('album', 'TAG MISSING'))
85         current_song_pos = int(self.client.currentsong().get('pos'))
86         print 'Current album: "%s"' % current_album
87         next_album_pos = current_song_pos
88         album = current_album
89         while album == current_album:
90             next_album_pos += 1
91             pl_length = int(self.client.status().get('playlistlength')) - 1
92             if pl_length < next_album_pos:
93                 print 'Next album not found in the playlitst!'
94                 self.client.disconnect()
95                 sys.exit(0)
96             album = self.client.playlistinfo(next_album_pos)[0].get('album', 'TAG MISSING')
97         print 'Next album appears to be: "%s"' % album
98         return next_album_pos
99
100     def _run(self):
101         """"""
102         print 'Connecting %s:%i' % (self.cli_options.host, self.cli_options.port)
103         self.mpdConnect()
104         nalbum = self._get_next()
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(nalbum)
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