Audio/AudioControl: Lots of fixes
[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 skip(self, distance):
30         self._command('--relative', str(distance))
31
32     def stop(self):
33         self._command('--stop')
34
35     def prev(self):
36         self._command('--prev')
37
38     def next(self):
39         self._command('--next')
40
41     def jump(self, n):
42         self._command('--jump', str(n))
43
44     def clear(self):
45         self._command('--clear')
46
47     def add(self, f):
48         self._command('--enqueue', str(f))
49
50     def speed(self, v):
51         self._command('--speed', str(v))
52
53
54 class Action(Actions.Action):
55
56     def __init__(self, name, action, player, *args):
57         Actions.Action.__init__(self, name)
58         self._action = action
59         self._player = player
60         self._args = args
61
62     def __call__(self, binding):
63         getattr(self._player, self._action)(*self._args);
64
65 class Dispatcher(object):
66
67     def __init__(self, action):
68         self._action = action
69
70     def __call__(self, name, *args):
71         return Action(name, self._action, *args)
72
73 Play   = Dispatcher('play')
74 Pause  = Dispatcher('pause')
75 Skip   = Dispatcher('skip')
76 Stop   = Dispatcher('stop')
77 Toggle = Dispatcher('toggle')
78 Prev   = Dispatcher('prev')
79 Next   = Dispatcher('next')
80 Jump   = Dispatcher('jump')
81 Clear  = Dispatcher('clear')
82 Add    = Dispatcher('add')