]> kaliko git repositories - mpd-goodies.git/blob - bin/nalbum
Swith to SPDX for license headers
[mpd-goodies.git] / bin / nalbum
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 # SPDX-FileCopyrightText: 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
4 # SPDX-License-Identifier: GPL-3.0-or-later
5
6 import sys
7 import argparse
8
9 from time import sleep
10 from os.path import basename
11
12 import musicpd
13
14 VERSION = '0.2'
15
16
17 class Nalbum(musicpd.MPDClient):
18     script_info = dict({
19         'prog': basename(__file__),
20         'description': 'Jump to next album in playlist.',
21         'epilog': 'Set MPD host/port in env. var'
22     })
23
24     def __init__(self):
25         musicpd.MPDClient.__init__(self)
26         self.args = self._get_args()
27         self._run()
28
29     def _get_args(self):
30         parser = argparse.ArgumentParser(**self.__class__.script_info)
31         parser.add_argument('--version', action='version',
32                             version='v%s' % VERSION)
33         parser.add_argument('-c', '--cross-fade', type=int, dest='time',
34                             help='fade out/fade in over t seconds.',)
35         args = parser.parse_args()
36         return args
37
38     def _setvol(self, vol):
39         """
40         Sometimes while fading in on next album, MPD fails.
41         Log shows :
42         "exception: Failed to set mixer for 'My Pulse Output': disconnected"
43         python-musicpd then raises "[52@0] {setvol} problems setting volume"
44         """
45         try:
46             self.setvol(int(vol))
47         except musicpd.CommandError as err:
48             print('MPD raised "%s"' % err)
49             print('Try to set the output with \'always_on    "yes"\'')
50             if 'problems setting volume' in str(err):
51                 sleep(.1)
52                 self._setvol(int(vol))
53
54     def _fade(self, mpd_vol, io='out'):
55         """
56         end_volum => End volume value
57         start_vol   => Start volume value
58         """
59         if io == 'out':
60             end_volum = mpd_vol // 10
61             start_vol = mpd_vol
62         if io == 'in':
63             end_volum = mpd_vol
64             start_vol = mpd_vol // 10
65         # print(start_vol, end_volum)
66         span = float(end_volum - start_vol)
67         step = span / float(10*self.args.time)
68         if step == 0:
69             return True
70         while True:
71             start_vol += step
72             self._setvol(int(start_vol))
73             if abs(start_vol - end_volum) < 1.1*abs(step):
74                 self._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.status():
81             print('No current song set in MPD!')
82             self.disconnect()
83             sys.exit(0)
84         current_album = str(self.currentsong().get('album', 'TAG MISSING'))
85         current_song_pos = int(self.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.status().get('playlistlength')) - 1
92             if pl_length < next_album_pos:
93                 print('Next album not found in the playlitst!')
94                 self.disconnect()
95                 sys.exit(0)
96             album = self.playlistinfo(next_album_pos)[
97                 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         self.connect()
103         nalbum = self._get_next()
104         init_vol = int(self.status().get('volume', 0))
105         if self.args.time:
106             print('Cross fading next album')
107             self._fade(init_vol)
108         self.play(nalbum)
109         if self.args.time:
110             self._fade(init_vol, io='in')
111         self.disconnect()
112         sys.exit(0)
113
114
115 # Script starts here
116 if __name__ == '__main__':
117     try:
118         Nalbum()
119     except KeyboardInterrupt:
120         sys.stdout.write('exit')
121
122 # VIM MODLINE
123 # vim: ai ts=4 sw=4 sts=4 expandtab