Audio/AudioController: Added AlsaPlayer and TimeMachine modules
[audiocontrol.git] / AlsaPlayer.py
1 import Actions
2 import subprocess
3
4 class Player(object):
5
6     def __init__(self):
7         pass
8
9     def _command(self,*command):
10         subprocess.call([ 'alsaplayer' ] + list(command))
11
12     def _get_status(self):
13         status = {}
14         for line in subprocess.Popen(['alsaplayer', '--status'], stdout=subprocess.PIPE).stdout:
15             if not ':' in line: continue
16             k, v = line.split(':',1)
17             status[k.strip()] = v.strip()
18         return status
19
20     def play(self):
21         self._command('--start')
22
23     def pause(self):
24         if self._get_status()['speed'] == '0%':
25             self.speed(1.0)
26         else:
27             self.speed(0.0)
28
29     def stop(self):
30         self._command('--stop')
31
32     def prev(self):
33         self._command('--prev')
34
35     def next(self):
36         self._command('--next')
37
38     def jump(self, n):
39         self._command('--jump', str(n))
40
41     def clear(self):
42         self._command('--clear')
43
44     def add(self, f):
45         self._command('--enqueue', str(f))
46
47     def speed(self, v):
48         self._command('--speed', str(v))
49
50
51 class Action(Actions.Action):
52
53     def __init__(self, name, action, player, *args):
54         Actions.Action.__init__(self, name)
55         self._action = action
56         self._player = player
57         self._args = args
58
59     def __call__(self, binding):
60         getattr(self._player, self._action)(*self._args);
61
62 class Dispatcher(object):
63
64     def __init__(self, action):
65         self._action = action
66
67     def __call__(self, name, *args):
68         return Action(name, self._action, *args)
69
70 Play   = Dispatcher('play')
71 Pause  = Dispatcher('pause')
72 Stop   = Dispatcher('stop')
73 Toggle = Dispatcher('toggle')
74 Prev   = Dispatcher('prev')
75 Next   = Dispatcher('next')
76 Jump   = Dispatcher('jump')
77 Clear  = Dispatcher('clear')
78 Add    = Dispatcher('add')