]> kaliko git repositories - mpd-goodies.git/blobdiff - mfade
* rename fadio and remove useless dev/debug log.
[mpd-goodies.git] / mfade
diff --git a/mfade b/mfade
new file mode 100755 (executable)
index 0000000..22d2f3a
--- /dev/null
+++ b/mfade
@@ -0,0 +1,132 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+# Copyright (c) 2009 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.mpdutils import mconnect
+
+USAGE = """Usage:
+
+mfade [time [min|max]]
+
+   * time in seconds
+   * min|max in percentag
+
+- not palying: fade in from 0% to max over time
+               default 10 minutes / 50%
+- if playing:  fade out from current volume to min over time
+               default 10 minutes / 1/10 of current vol
+
+Manual or any external volume change will abort the script.
+"""
+
+
+class Sleep(object):
+    """
+    """
+
+    def __init__(self):
+        """"""
+        self.tempo = int(10)
+        self.volum = None
+        self._consume_sopt()
+        self.cli = mconnect()
+        self._run()
+        self.cli.disconnect()
+
+    def _consume_sopt(self):
+        """"""
+        options = sys.argv
+        if len(sys.argv) >1 and sys.argv[1] in ['-h', '--help']:
+            print USAGE
+            sys.exit(1)
+        try:
+            self.tempo = int(options.pop(1))
+            self.volum = int(options.pop(1))
+        except IndexError:
+            pass
+        except ValueError, err:
+            print 'Error: wrong option passed: %s' % err
+            print USAGE
+            sys.exit(1)
+
+    def _run(self):
+        """"""
+        self.mpd_state = str(self.cli.status().get('state'))
+        self.mpd_vol = int(self.cli.status().get('volume'))
+        if self.mpd_state == 'play':
+            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%%)' % (self.volum, self.mpd_vol)
+                sys.exit(1)
+            print 'fading down from %d%% to %d%% over %smin' % (self.mpd_vol,
+                    self.volum, self.tempo)
+            self.fade()
+            self.cli.stop()
+        if self.mpd_state in ['stop', 'pause']:
+            if not self.volum:
+                self.volum = int(50)
+            print 'fading up from 0%% to %d%% over %smin' % (self.volum,
+                    self.tempo)
+            self.cli.setvol(0)
+            self.mpd_vol = 0
+            self.cli.play()
+            self.fade()
+        sleep(1)
+
+    def fade(self):
+        """"""
+        span = float(self.volum - self.mpd_vol)
+        step = span / float(60 * self.tempo)
+        print step
+        vol = self.mpd_vol
+        while 42:
+            if int(vol) != int(self.cli.status().get('volume')):
+                print 'Warning: external volume change, aborting!'
+                break
+            vol += step
+            self.cli.setvol(int(vol))
+            if abs(vol - self.volum) < 1:
+                self.cli.setvol(self.volum)
+                break
+            sleep(1)
+
+
+# Script starts here
+if __name__ == '__main__':
+    options = [14, 11]
+    #main(options)
+    try:
+        Sleep()
+    except KeyboardInterrupt:
+        print 'exit'
+
+# VIM MODLINE
+# vim: ai ts=4 sw=4 sts=4 expandtab