]> kaliko git repositories - mpd-goodies.git/blobdiff - src/mfade
* reorganized source folders
[mpd-goodies.git] / src / mfade
diff --git a/src/mfade b/src/mfade
new file mode 100755 (executable)
index 0000000..0ffd9df
--- /dev/null
+++ b/src/mfade
@@ -0,0 +1,122 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2009, 2010 Efrim <efrim@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/>.
+#
+
+"""
+DOC:
+    heavily borrowed from perl script mpdtoys and converted to python.
+    mpdtoys © 2007 Joey Hess <joey@kitenet.net>
+    http://kitenet.net/~joey/code/mpdtoys
+"""
+
+import sys
+
+from time import sleep
+
+from lib.mpdclass import MPDClass
+from lib.startop import StartOpt
+
+NAME = 'mfade'
+VERSION = '0.1'
+USAGE = 'USAGE:  %prog [--help] | [ <time> [<final volume level>] ]'
+DESC = """Fade in/out to <final volume level> over <time>. Defaults are from 0%
+to 50% when paused or stopped and from current volume to 10th of it if playing,
+both over 10 minutes."""
+
+
+class Sleep(StartOpt, MPDClass):
+    """"""
+    script_info = dict({
+        'version': VERSION,
+        'prog_name': NAME,
+        'description': DESC,
+        'usage': USAGE,
+        })
+
+    def __init__(self):
+        """"""
+        StartOpt.__init__(self, self.__class__.script_info, [])
+        MPDClass.__init__(self)
+        self.tempo = int(10)
+        self.volum = None
+        self._consume_args()
+        self._run()
+
+    def _consume_args(self):
+        """"""
+        if (len(self.cli_args) < 1 or
+                len(self.cli_args) > 2):
+            self.parser.error('need at least an argument and no more than two!')
+        try:
+            self.tempo = int(self.cli_args[0])
+            self.volum = int(self.cli_args[1])
+        except IndexError:
+            pass
+        except ValueError, err:
+            self.parser.error('wrong option passed (%s)' % err)
+
+    def _run(self):
+        """"""
+        self.mpdConnect()
+        self.mpd_state = str(self.client.status().get('state'))
+        self.mpd_vol = int(self.client.status().get('volume'))
+        if self.mpd_state == 'play':
+            if not self.volum:
+                self.volum = self.mpd_vol / 10
+            if self.volum > self.mpd_vol:
+                self.parser.error('Error: specified min volume (%d%%) > to current volume (%d%%)' % (self.volum, self.mpd_vol))
+            print >> sys.stdout, 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol, self.volum, self.tempo)
+            if self.fade():
+                self.client.stop()
+        if self.mpd_state in ['stop', 'pause']:
+            if not self.volum:
+                self.volum = int(50)
+            print >> sys.stdout, 'fading up from 0%% to %d%% over %smin' % (self.volum, self.tempo)
+            self.client.setvol(0)
+            self.mpd_vol = 0
+            self.client.play()
+            self.fade()
+        sleep(1)
+
+    def fade(self):
+        """"""
+        # TODO: handle possible lost connections
+        span = float(self.volum - self.mpd_vol)
+        step = span / float(60 * self.tempo)
+        vol = self.mpd_vol
+        while 42:
+            if int(vol) != int(self.client.status().get('volume')):
+                sys.stderr.write('Warning: external volume change, aborting!\n')
+                return False
+            vol += step
+            self.client.setvol(int(vol))
+            if abs(vol - self.volum) < 1:
+                self.client.setvol(self.volum)
+                return True
+            sleep(1)
+
+
+# Script starts here
+if __name__ == '__main__':
+    try:
+        Sleep()
+    except KeyboardInterrupt:
+        sys.stdout.write('exit')
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab