-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlpk25.py
296 lines (248 loc) · 7.82 KB
/
lpk25.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#!/usr/bin/python
#
# Script decode/encode configuration files for the Akai LPK25
# (c) Simon Wood, 8 July 2018
#
from optparse import OptionParser
from construct import *
#--------------------------------------------------
# For interactive menus (optional)
# https://github.com/jeffrimko/Qprompt
try:
import qprompt
_hasQPrompt = True
except ImportError:
_hasQPrompt = False
'''
_hasQPrompt = False
'''
#--------------------------------------------------
# Define file format using Construct (v2.9)
# https://github.com/construct/construct
Header = Struct(
Const(b"\xf0"), # SysEx Begin
Const(b"\x47\x7f"), # Mfg ID = Akai
Const(b"\x76"), # Dev ID = LPK25
Const(b"\x61"), # CMD = Dump/Load Preset
Const(b"\x00\x0d"), # Len = 13bytes (7bit stuffed)
"preset" / Byte,
"channel" / Byte,
"octave" / Enum(Byte,
OCT_M4 = 0,
OCT_M3 = 1,
OCT_M2 = 2,
OCT_M1 = 3,
OCT_0 = 4,
OCT_P1 = 5,
OCT_P2 = 6,
OCT_P3 = 7,
OCT_P4 = 8,
),
"transpose" / Enum(Byte,
TRANS_M12 = 0,
TRANS_M11 = 1,
TRANS_M10 = 2,
TRANS_M9 = 3,
TRANS_M8 = 4,
TRANS_M7 = 5,
TRANS_M6 = 6,
TRANS_M5 = 7,
TRANS_M4 = 8,
TRANS_M3 = 9,
TRANS_M2 = 10,
TRANS_M1 = 11,
TRANS_0 = 12,
TRANS_P1 = 13,
TRANS_P2 = 14,
TRANS_P3 = 15,
TRANS_P4 = 16,
TRANS_P5 = 17,
TRANS_P6 = 18,
TRANS_P7 = 19,
TRANS_P8 = 20,
TRANS_P9 = 21,
TRANS_P10 = 22,
TRANS_P11 = 23,
TRANS_P12 = 24,
),
"enable" / Default(Enum(Byte,
OFF = 0,
ON = 1,
), 0),
"mode" / Enum(Byte,
UP = 0,
DOWN = 1,
INCLUSIVE = 2, # note: different order
EXCLUSIVE = 3, # to markings on keyboard
RANDOM = 4,
ORDER = 5,
),
"division" / Enum(Byte,
DIV_1_4 = 0,
DIV_1_4T = 1,
DIV_1_8 = 2,
DIV_1_8T = 3,
DIV_1_16 = 4,
DIV_1_16T = 5,
DIV_1_32 = 6,
DIV_1_32T = 7,
),
"clock" / Enum(Byte,
INTERNAL = 0,
EXTERNAL = 1,
),
"latch" / Enum(Byte,
DISABLE = 0,
ENABLE = 1,
),
"taps" / Byte,
"tempo" / ExprAdapter(Int16ub, # 7bit stuffed - each byte max 0x7F
((obj_ & 0x7f) + ((obj_ & 0x7f00) >> 1)),
((obj_ & 0x7f) + ((obj_ & 0x3f80) << 1)),
),
"octaves" / Enum(Byte,
OCT_1 = 0,
OCT_2 = 1,
OCT_3 = 2,
OCT_4 = 3,
),
)
Footer = Struct(
Const(b"\xf7"), # SysEx End
)
Lpk25 = Sequence(
Header,
Footer,
)
#--------------------------------------------------
config = None
def edit_arpeggio():
global config
menu = qprompt.Menu()
for x,y in Header.enable.subcon.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['enable'] == y:
dft = str(x)
config[0]['enable'] = int(menu.show(msg="Enable", dft=dft))
menu = qprompt.Menu()
for x,y in Header.division.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['division'] == y:
dft = str(x)
config[0]['division'] = int(menu.show(msg="Division", dft=dft))
menu = qprompt.Menu()
for x,y in Header.mode.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['mode'] == y:
dft = str(x)
config[0]['mode'] = int(menu.show(msg="Mode", dft=dft))
menu = qprompt.Menu()
for x,y in Header.latch.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['latch'] == y:
dft = str(x)
config[0]['latch'] = int(menu.show(msg="Latch", dft=dft))
menu = qprompt.Menu()
for x,y in Header.octaves.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['octaves'] == y:
dft = str(x)
config[0]['octaves'] = int(menu.show(msg="Octaves", dft=dft))
config[0]['taps'] = \
qprompt.ask_int("Taps", vld=list(range(2,4)),
dft=config[0]['taps'])
config[0]['tempo'] = \
qprompt.ask_int("Tempo", dft=config[0]['tempo'])
menu = qprompt.Menu()
for x,y in Header.clock.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['clock'] == y:
dft = str(x)
config[0]['clock'] = int(menu.show(msg="Clock", dft=dft))
def edit_general():
global config
config[0]['channel'] = \
qprompt.ask_int("Midi Ch for Keys", dft=config[0]['channel'])
menu = qprompt.Menu()
for x,y in Header.octave.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['octave'] == y:
dft = str(x)
config[0]['octave'] = int(menu.show(msg="Keyboard Octave", dft=dft))
menu = qprompt.Menu()
def edit_transpose():
global config
menu = qprompt.Menu()
for x,y in Header.transpose.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['transpose'] == y:
dft = str(x)
config[0]['transpose'] = int(menu.show(msg="Transpose", dft=dft))
def main():
global config
usage = "usage: %prog [options] FILENAME"
parser = OptionParser(usage)
parser.add_option("-o", "--output", dest="outfile",
help="write data to OUTFILE")
parser.add_option("-v", "--verbose",
action="store_true", dest="verbose")
parser.add_option("-d", "--dump",
help="dump configuration to text",
action="store_true", dest="dump")
parser.add_option("-p", "--preset", dest="preset",
help="change the profile number to PRESET" )
parser.add_option("-t", "--tempo", dest="tempo",
help="change the tempo to TEMPO" )
if _hasQPrompt:
parser.add_option("-A", "--arpeggio",
help="Interactively configure the Arpeggiator",
action="store_true", dest="arpeggio")
parser.add_option("-G", "--general",
help="Interactively configure general settings",
action="store_true", dest="general")
parser.add_option("-T", "--transpose",
help="Interactively configure the Transposing",
action="store_true", dest="transpose")
(options, args) = parser.parse_args()
if len(args) != 1:
parser.error("input FILE not specified")
if options.verbose:
print("Reading %s..." % args[0])
infile = open(args[0], "rb")
if not infile:
print("Unable to open file")
quit(0)
data = infile.read(2000)
try:
config = Lpk25.parse(data)
except:
quit("Unable to read config file")
infile.close()
# Change stuff here...
if options.preset:
config[0]['preset'] = int(options.preset)
if options.tempo:
config[0]['tempo'] = int(options.tempo)
if options.arpeggio:
edit_arpeggio()
if options.general:
edit_general()
if options.transpose:
edit_transpose()
if options.dump:
print(config)
if options.verbose:
if options.outfile:
print("writing %s..." % options.outfile)
else:
print("writing %s..." % args[0])
if options.outfile:
outfile = open(options.outfile, "wb")
else:
outfile = open(args[0], "wb")
if not outfile:
print("Unable to open output file")
else:
outfile.write(Lpk25.build(config))
if __name__ == "__main__":
main()