2 # -*- coding: utf-8 -*-
3 # SPDX-FileCopyrightText: 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
4 # SPDX-FileCopyrightText: 2007 Joey Hess <joey@kitenet.net>
5 # SPDX-License-Identifier: GPL-3.0-or-later
8 heavily borrowed from perl script mpdtoys and converted to python.
9 mpdtoys © 2007 Joey Hess <joey@kitenet.net>
10 http://kitenet.net/~joey/code/mpdtoys
16 from time import sleep
17 from os.path import basename
23 DESC = """Fade in/out to <final volume level> over <time>.
24 Defaults are from 0% to 50% when paused or stopped
25 and from current volume to 10th of it if playing,
26 both over 10 minutes."""
27 DESC = """Fade in/out to <final volume level> over <time>.
28 Defaults are from 0% to 50% when paused or stopped and from current volume to 10th of it if playing, both over 10 minutes."""
29 EPILOG = """You might need to set your audio_output with "always_on yes" for
30 this command to work properly.
32 Set MPD host/port in env. var"""
35 class Sleep(musicpd.MPDClient):
38 'prog': basename(__file__),
41 'formatter_class': argparse.RawDescriptionHelpFormatter,
46 musicpd.MPDClient.__init__(self)
52 def _test_volume_access(self):
53 status = self.status()
54 if 'volume' not in status:
55 print('Mixer not available!')
56 print('Try to set the always_on audio_output option on.')
61 parser = argparse.ArgumentParser(**self.__class__.script_info)
62 parser.add_argument('--version', action='version',
63 version='v%s' % VERSION)
64 group = parser.add_mutually_exclusive_group()
65 group.add_argument('-m', '--minutes', type=int,
66 help='duration to fade in/out over')
67 group.add_argument('-s', '--seconds', type=int,
68 help='duration to fade in/out over')
69 parser.add_argument('volume', type=int, nargs='?',
70 help='final volume level')
71 args = parser.parse_args()
73 self.tempo = args.minutes*60
75 self.tempo = args.seconds
77 self.volum = args.volume
82 self.mpd_state = str(self.status().get('state'))
83 if self.mpd_state == 'play':
84 self.mpd_vol = int(self.status().get('volume'))
86 self.volum = self.mpd_vol // 10
87 if self.volum >= self.mpd_vol:
88 print('Error: specified min volume (%d%%) >= to current volume (%d%%)' % (
89 self.volum, self.mpd_vol))
91 print('fading down from %d%% to %d%% over %ss' % (self.mpd_vol, self.volum, self.tempo), file=sys.stdout)
92 if self.fade(self.mpd_vol, self.volum):
94 elif self.mpd_state in ['stop', 'pause']:
95 self._test_volume_access()
98 print('fading up from 0%% to %d%% over %ss' % (self.volum, self.tempo), file=sys.stdout)
102 self.fade(self.mpd_vol, self.volum)
104 def fade(self, mpd_vol, target):
106 # TODO: handle possible lost connections
108 span = target - mpd_vol
109 step = span / (resolution*self.tempo)
115 self.setvol(round(vol))
116 # monitor external volume change
117 # print(vol, round(vol), self.status().get('volume'))
118 if abs(vol - target) < 1.1*abs(step):
125 if __name__ == '__main__':
128 except KeyboardInterrupt:
129 sys.stdout.write('exit')
132 # vim: ai ts=4 sw=4 sts=4 expandtab