This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathe2_groove_template.py
209 lines (146 loc) · 4.73 KB
/
e2_groove_template.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
from e2sysex import *
def main():
# Instantiate SysEx communication
e = E2Sysex()
# Create blank groove template
groove = E2Groove.new()
# Set groove name
groove.name = "Test groove 1"
# Set groove loop length (re-triggers on pattern loop)
groove.length = 13
# Move 5th step forward half a step
groove.edit_step(4, 'trigger', 48)
# Move 13th step backward half a step
groove.edit_step(12, 'trigger', -48)
# Decrease 1st step velocity by 32
groove.edit_step(0, 'velocity', -32)
# Set 3rd step velocity to 127
groove.step[2].velocity = 127
# Set 11th step gate time to 96 (tie)
groove.step[10].gate = 96
# Send new groove template to 1st position in list
groove_index = 0
e.set_groove(groove_index, groove.data)
class E2Groove:
# Returns E2Groove object with initialised parameter values
def new():
gv = bytearray(0x140)
# Groove start
gv[:4] = b'GVST'
# Groove name
gv[0x10:0x1f] = b'Init Groove'.ljust(0x0f, b'\x00')[:0x0f]
# Groove length
gv[0x22] = 0x40
gv[0x23] = 0xff
# Trigger, velocity, gate
base = 0x30
leng = 4
for i in range(0, 0x100, 4):
offset = base + int(i/4) * leng
gv[offset] = 0x00
gv[offset+1] = 0x60
gv[offset+2] = 0x60
gv[offset+3] = 0xff
# Groove end
gv[-4:] = b'GVED'
return E2Groove(gv)
# gv_data is bytearray
def __init__(self, gv_data):
self.data = gv_data
self._name = str(self.data[0x10:0x20], 'ascii').strip('\x00')
self._length = self.data[0x22]
self.step = []
for i in range(0x40):
self.step.append(E2GrooveStep(self.data, i))
# Groove name
@property
def name(self):
return self._name
@name.setter
def name(self, strn):
self.data[0x10:0x1f] = strn.encode('ascii').ljust(0x0f, b'\x00')[:0x0f]
self._name = str(self.data[0x10:0x1f], 'ascii').strip('\x00')
# Groove length
@property
def length(self):
return self._length
@length.setter
def length(self, val):
self.data[0x22] = val
self._length = self.data[0x22]
# Edit parameter value at step
# param is target parameter, one of 'trigger', 'velocity', 'gate'
# delta is integer change to existing value
def edit_step(self, step_idx, param, delta):
if param not in ['trigger', 'velocity', 'gate']:
return
if param == 'trigger':
self.step[step_idx].trigger = self.step[step_idx].trigger + delta
elif param == 'velocity':
self.step[step_idx].velocity = self.step[step_idx].velocity + delta
elif param == 'gate':
self.step[step_idx].gate = self.step[step_idx].gate + delta
else:
return
class E2GrooveStep:
def __init__(self, data, idx):
self.data = data
base = 0x30
leng = 4
self.offset = base + idx * leng
self._trigger = self.data[self.offset]
self._velocity = self.data[self.offset+1]
self._gate = self.data[self.offset+2]
self._null = self.data[self.offset+3]
# Trigger time
@property
def trigger(self):
return self._trigger
@trigger.setter
def trigger(self, val):
# Clamp to valid range
if val > 0x30:
val = 0x30
elif val < -0x30:
val = -0x30
# Two's complement
if val < 0:
val = 0x100 + val
self.data[self.offset] = val
self._val = self.data[self.offset]
# Velocity
@property
def velocity(self):
return self._velocity
@velocity.setter
def velocity(self, val):
# Clamp to valid range
if val > 0x7f:
val = 0x7f
elif val < 0:
val = 0
self.data[self.offset+1] = val
self._val = self.data[self.offset+1]
# Gate time
@property
def gate(self):
return self._gate
@gate.setter
def gate(self, val):
# Clamp to valid range
if val > 0x60:
val = 0x60
elif val < 0:
val = 0
self.data[self.offset+2] = val
self._val = self.data[self.offset+2]
# null
@property
def null(self):
return self._null
@null.setter
def null(self, val):
self.data[self.offset+3] = val
self._val = self.data[self.offset+3]
if __name__ == '__main__':
main()