forked from superquadratic/beat-bricks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pattern.py
78 lines (58 loc) · 2.15 KB
/
pattern.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import liblo
import numpy
class Pattern(object):
def __init__(self, tracks=8, steps=16):
self.steps = numpy.zeros((steps, tracks), bool)
self.muted = numpy.zeros(tracks, bool)
@property
def num_tracks(self):
return self.steps.shape[1]
@property
def num_steps(self):
return self.steps.shape[0]
def set_step(self, track, step):
self.steps[step, track] = True
def clear_step(self, track, step):
self.steps[step, track] = False
def mute(self, track):
self.muted[track] = True
def unmute(self, track):
self.muted[track] = False
class SharedPattern(Pattern):
def __init__(self, address=8765):
Pattern.__init__(self)
self.target = liblo.Address(address)
def set_step(self, track, step):
if not self.steps[step, track]:
liblo.send(self.target, '/pattern/set', track, step)
Pattern.set_step(self, track, step)
def clear_step(self, track, step):
if self.steps[step, track]:
liblo.send(self.target, '/pattern/clear', track, step)
Pattern.clear_step(self, track, step)
def mute(self, track):
if not self.muted[track]:
liblo.send(self.target, '/pattern/mute', track)
Pattern.mute(self, track)
def unmute(self, track):
if self.muted[track]:
liblo.send(self.target, '/pattern/unmute', track)
Pattern.unmute(self, track)
class PatternListener(liblo.ServerThread):
def __init__(self, address=8765):
liblo.ServerThread.__init__(self, address)
self.pattern = Pattern()
@liblo.make_method('/pattern/set', 'ii')
def set_callback(self, path, args):
track, step = args
self.pattern.set_step(track, step)
@liblo.make_method('/pattern/clear', 'ii')
def clear_callback(self, path, args):
track, step = args
self.pattern.clear_step(track, step)
@liblo.make_method('/pattern/mute', 'i')
def mute_callback(self, path, track):
self.pattern.mute(track)
@liblo.make_method('/pattern/unmute', 'i')
def unmute_callback(self, path, track):
self.pattern.unmute(track)