Audio/AudioController: Added AlsaPlayer and TimeMachine modules
[audiocontrol.git] / AlsaPlayer.py
diff --git a/AlsaPlayer.py b/AlsaPlayer.py
new file mode 100644 (file)
index 0000000..1c6f47a
--- /dev/null
@@ -0,0 +1,78 @@
+import Actions
+import subprocess
+
+class Player(object):
+
+    def __init__(self):
+        pass
+
+    def _command(self,*command):
+        subprocess.call([ 'alsaplayer' ] + list(command))
+
+    def _get_status(self):
+        status = {}
+        for line in subprocess.Popen(['alsaplayer', '--status'], stdout=subprocess.PIPE).stdout:
+            if not ':' in line: continue
+            k, v = line.split(':',1)
+            status[k.strip()] = v.strip()
+        return status
+
+    def play(self):
+        self._command('--start')
+
+    def pause(self):
+        if self._get_status()['speed'] == '0%':
+            self.speed(1.0)
+        else:
+            self.speed(0.0)
+
+    def stop(self):
+        self._command('--stop')
+
+    def prev(self):
+        self._command('--prev')
+
+    def next(self):
+        self._command('--next')
+
+    def jump(self, n):
+        self._command('--jump', str(n))
+
+    def clear(self):
+        self._command('--clear')
+
+    def add(self, f):
+        self._command('--enqueue', str(f))
+
+    def speed(self, v):
+        self._command('--speed', str(v))
+
+
+class Action(Actions.Action):
+
+    def __init__(self, name, action, player, *args):
+        Actions.Action.__init__(self, name)
+        self._action = action
+        self._player = player
+        self._args = args
+
+    def __call__(self, binding):
+        getattr(self._player, self._action)(*self._args);
+
+class Dispatcher(object):
+
+    def __init__(self, action):
+        self._action = action
+
+    def __call__(self, name, *args):
+        return Action(name, self._action, *args)
+
+Play   = Dispatcher('play')
+Pause  = Dispatcher('pause')
+Stop   = Dispatcher('stop')
+Toggle = Dispatcher('toggle')
+Prev   = Dispatcher('prev')
+Next   = Dispatcher('next')
+Jump   = Dispatcher('jump')
+Clear  = Dispatcher('clear')
+Add    = Dispatcher('add')