]> kaliko git repositories - mpd-goodies.git/blobdiff - bin/mfade
Fixed protocol version check for mlast
[mpd-goodies.git] / bin / mfade
index 61a68f305d166944c8cb6e3ece4a1b361db23cb2..1f5c4c0bf2325da61be7068e311bb17d2339d815 100755 (executable)
--- a/bin/mfade
+++ b/bin/mfade
@@ -1,22 +1,8 @@
 #!/usr/bin/env python3
 # -*- coding: utf-8 -*-
-
-# Copyright (c) 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
-#
-#   This program is free software: you can redistribute it and/or modify
-#   it under the terms of the GNU General Public License as published by
-#   the Free Software Foundation, either version 3 of the License, or
-#   (at your option) any later version.
-#
-#   This program is distributed in the hope that it will be useful,
-#   but WITHOUT ANY WARRANTY; without even the implied warranty of
-#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#   GNU General Public License for more details.
-#
-#   You should have received a copy of the GNU General Public License
-#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#
-
+# SPDX-FileCopyrightText: 2009,2010,2012,2019 kaliko <kaliko@azylum.org>
+# SPDX-FileCopyrightText: 2007 Joey Hess <joey@kitenet.net>
+# SPDX-License-Identifier: GPL-3.0-or-later
 """
 DOC:
     heavily borrowed from perl script mpdtoys and converted to python.
@@ -35,7 +21,7 @@ import musicpd
 
 VERSION = '0.2'
 DESC = """Fade in/out to <final volume level> over <time>.
-Defaults are from 0% to 50% when paused or stopped 
+Defaults are from 0% to 50% when paused or stopped
 and from current volume to 10th of it if playing,
 both over 10 minutes."""
 DESC = """Fade in/out to <final volume level> over <time>.
@@ -45,6 +31,7 @@ this command to work properly.
 
 Set MPD host/port in env. var"""
 
+
 class Sleep(musicpd.MPDClient):
     """"""
     script_info = dict({
@@ -97,8 +84,8 @@ class Sleep(musicpd.MPDClient):
             self.mpd_vol = int(self.status().get('volume'))
             if not self.volum:
                 self.volum = self.mpd_vol // 10
-            if self.volum > self.mpd_vol:
-                print('Error: specified min volume (%d%%) > to current volume (%d%%)' % (
+            if self.volum >= self.mpd_vol:
+                print('Error: specified min volume (%d%%) >= to current volume (%d%%)' % (
                     self.volum, self.mpd_vol))
                 sys.exit(1)
             print('fading down from %d%% to %d%% over %ss' % (self.mpd_vol, self.volum, self.tempo), file=sys.stdout)
@@ -117,22 +104,21 @@ class Sleep(musicpd.MPDClient):
     def fade(self, mpd_vol, target):
         """"""
         # TODO: handle possible lost connections
+        resolution = 5
         span = target - mpd_vol
-        step = span / (10*self.tempo)
+        step = span / (resolution*self.tempo)
         vol = mpd_vol
         if step == 0:
             return True
         while True:
-            if int(vol) != int(self.status().get('volume')):
-                sys.stderr.write(
-                    'Warning: external volume change, aborting!\n')
-                return False
             vol += step
-            self.setvol(int(vol))
+            self.setvol(round(vol))
+            # monitor external volume change
+            # print(vol, round(vol), self.status().get('volume'))
             if abs(vol - target) < 1.1*abs(step):
                 self.setvol(target)
                 return True
-            sleep(.1)
+            sleep(1/resolution)
 
 
 # Script starts here