-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmpd232.py
553 lines (465 loc) · 16.2 KB
/
mpd232.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
#!/usr/bin/python
#
# Script decode/encode configuration files for the Akai MPD232
# (c) Simon Wood, 18 Oct 2023
#
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
'''
#--------------------------------------------------
# For programming scales (optional)
# https://github.com/charlottepierce/music_essentials
try:
from music_essentials import Note,Scale
_hasME = True
except ImportError:
_hasME = False
'''
_hasME = False
'''
#--------------------------------------------------
# Constants defining MP232 features
_PADS = 16
_PBANKS = 4
_PTOTAL = _PADS * _PBANKS
_DIALS = 8
_DBANKS = 3
_DTOTAL = _DIALS * _DBANKS
_FADERS = 8
_FBANKS = 3
_FTOTAL = _FADERS * _FBANKS
_SWITCHES = 8
_SBANKS = 3
_STOTAL = _SWITCHES * _SBANKS
#--------------------------------------------------
# Define file format using Construct (v2.9)
# https://github.com/construct/construct
Header = Struct(
Const(b"\xf0"), # SysEx Begin
Const(b"\x47\x00"), # Mfg ID = Akai
Const(b"\x36"), # Dev ID = MPD232
Const(b"\x10"), # CMD = Dump/Load Preset
Const(b"\x1b\x13"), # Len = 1075 bytes (7bit stuffed)
"preset" / Byte,
"name" / PaddedString(8, "ascii"), # Pad with spaces!
"un1" /Byte,
"tempo" / ExprAdapter(Int16ub, # 7bit stuffed - each byte max 0x7F
((obj_ & 0x7f) + ((obj_ & 0x7f00) >> 1)),
((obj_ & 0x7f) + ((obj_ & 0x3f80) << 1)),
),
"timedivisionswitch" / Enum(Byte,
MOMENTARY = 0,
TOGGLE = 1,
),
"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,
),
"noterepeatswitch" / Enum(Byte,
MOMENTARY = 0,
TOGGLE = 1,
),
"gate" /Byte,
"swing" / Enum(Byte, # This could be byte value
SWING_OFF = 50,
SWING_54 = 54,
SWING_56 = 56,
SWING_58 = 58,
SWING_60 = 60,
SWING_62 = 62,
),
"un5" /Byte,
"un6" /Byte,
"un7" /Byte,
"un8" /Byte,
"un9" /Byte,
"transport" / Enum(Byte,
MCC = 0,
MCCMIDI = 1,
MIDI = 2,
CTRL = 3,
PTEX = 4,
),
)
Pad = Struct(
"type" / Enum(Byte,
NOTE = 0,
PROG = 1,
BANK = 2,
),
"channel" / Byte,
"note" /Byte, # NOTE only
"midi2din" / Enum(Byte,
OFF = 0,
ON = 1,
),
"trigger" / Enum(Byte, # NOTE only
MOMENTARY = 0,
TOGGLE = 1,
),
"aftertouch" / Enum(Byte, # NOTE only
OFF = 0,
CHANNEL = 1,
POLY = 2,
),
"program" / Byte, # PROG only
"msb" / Byte, # BANK only
"lsb" /Byte, # BANK only
"offcolor" / Enum(Byte,
OFF = 0, RED = 1, ORANGE = 2, AMBER = 3, YELLOW = 4,
GREEN = 5, GREENBLUE = 6, AQUA = 7, LTBLUE = 8,
BLUE = 9, PURPLE = 10, PINK = 11, HOTPINK = 12,
LTPURPLE = 13, LTGREEN = 14, LTPINK = 15, GREY = 16
),
"oncolor" / Enum(Byte,
OFF = 0, RED = 1, ORANGE = 2, AMBER = 3, YELLOW = 4,
GREEN = 5, GREENBLUE = 6, AQUA = 7, LTBLUE = 8,
BLUE = 9, PURPLE = 10, PINK = 11, HOTPINK = 12,
LTPURPLE = 13, LTGREEN = 14, LTPINK = 15, GREY = 16
),
)
Dial = Struct(
"type" / Enum(Byte,
CC = 0,
AFTERTOUCH = 1,
INC_DEC_1 = 2,
INC_DEC_2 = 3,
),
"channel" / Byte, # 0 = Common, A1..16, B1..16
"midicc" /Byte, # CC and ID2 only
"min" /Byte, # CC and AT only
"max" /Byte, # CC and AT only
"midi2din" / Enum(Byte,
OFF = 0,
ON = 1,
),
"msb" /Byte, # ID1 only
"lsb" / Byte, # ID1 only
"value" / Byte, # ID1 only
)
Fader = Struct(
"type" / Enum(Byte,
CC = 0,
AFTERTOUCH = 1,
),
"channel" / Byte, # 0 = Common, A1..16, B1..16
"midicc" /Byte, # CC only
"min" /Byte, # CC/AFTERTOUCH
"max" /Byte, # CC/AFTERTOUCH
"midi2din" / Enum(Byte,
OFF = 0,
ON = 1,
),
)
Switch = Struct(
"type" / Enum(Byte,
CC = 0,
NOTE = 1,
PROGRAM = 2,
BANK = 3,
KEY = 4,
),
"channel" / Byte, # 0 = Common, A1..16, B1..16
# all but KEYSTROKE
"midicc" /Byte, # CC
"mode" / Enum(Byte, # CC
MOMENTARY = 0,
TOGGLE = 1,
),
"prog" /Byte, # PROGRAM/BANK
"msb" / Byte, # BANK
"lsb" / Byte, # BANK
"midi2din" / Enum(Byte, # all but KEYSTROKE
OFF = 0,
ON = 1,
),
"note" / Byte, # NOTE
"velo" / Byte, # NOTE
"invert" / Enum(Byte, # CC
OFF = 0,
ON = 1,
),
"key1" /Byte, # KEYSTROKE
"key2" / Enum(Byte, # KEYSTROKE
NONE = 0, CTRL = 1, SHIFT = 2, ALT = 3, OPT = 4,
CTRL_SHIFT = 5, CTRL_ALT = 6, CTRL_OPT = 7,
SHIFT_ALT = 8, SHIFT_OPT = 9, ALT_OPT = 10,
CTRL_ALT_OP = 11, CTRL_SH_ALT = 12, CTRL_SH_OPT = 13
),
)
Seq = Struct(
"steps" / Byte, # 0-31
"type" / Enum(Byte,
START_STOP = 0,
EXTERNAL = 1,
SOMETHING = 2,
),
Padding(2),
"velocities" / Array(64, Array(32, Byte,)),
)
Unknown = Struct(
"unknown" /Bytes(24),
)
Footer = Struct(
Const(b"\xf7"), # SysEx End
)
Mpd232 = Sequence(
Header,
Array(_PBANKS, Array(_PADS, Pad,)),
Array(_DBANKS, Array(_DIALS, Dial,)),
Array(_FBANKS, Array(_FADERS, Fader,)),
Array(_SBANKS, Array(_SWITCHES, Switch,)),
Seq,
Unkown,
Footer,
)
#--------------------------------------------------
config = None
def edit_division():
global config
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))
def edit_swing():
global config
menu = qprompt.Menu()
for x,y in Header.swing.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['swing'] == y:
dft = str(x)
config[0]['swing'] = int(menu.show(msg="Swing", dft=dft))
def edit_dial(dial):
global config
bank = int((dial-1) / _DIALS)
subdial = dial-(_DIALS * bank)-1
menu = qprompt.Menu()
for x,y in Dial.type.subcon.ksymapping.items():
menu.add(str(x),y)
if config[2][bank][subdial]['type'] == y:
dft = str(x)
dtype = int(menu.show(hdr="Dial %d (Bank %s-%d):" % (dial, chr(65+bank), subdial+1),
msg="Type", dft=dft))
config[2][bank][subdial]['type'] = dtype
config[2][bank][subdial]['channel'] = \
qprompt.ask_int("Channel", vld=list(range(0,17)),
dft=config[2][bank][subdial]['channel'])
if dtype == 0 or dtype == 3: # CC or ID2
config[2][bank][subdial]['midicc'] = \
qprompt.ask_int("MidiCC", vld=list(range(0,128)),
dft=config[2][bank][subdial]['midicc'])
if dtype == 0 or dtype == 1: # CC or AT
config[2][bank][subdial]['min'] = \
qprompt.ask_int("Min", vld=list(range(0,128)),
dft=config[2][bank][subdial]['min'])
config[2][bank][subdial]['max'] = \
qprompt.ask_int("Max", vld=list(range(0,128)),
dft=config[2][bank][subdial]['max'])
if dtype == 2: # ID1
config[2][bank][subdial]['msb'] = \
qprompt.ask_int("MSB", vld=list(range(0,128)),
dft=config[2][bank][subdial]['msb'])
config[2][bank][subdial]['lsb'] = \
qprompt.ask_int("LSB", vld=list(range(0,128)),
dft=config[2][bank][subdial]['lsb'])
config[2][bank][subdial]['value'] = \
qprompt.ask_int("Value", vld=list(range(0,128)),
dft=config[2][bank][subdial]['value'])
def edit_pad(pad):
global config
bank = int((pad-1) / _PADS)
subpad = pad-(_PADS * bank)-1
menu = qprompt.Menu()
for x,y in Pad.type.subcon.ksymapping.items():
menu.add(str(x),y)
if config[1][bank][subpad]['type'] == y:
dft = str(x)
ptype = menu.show(hdr="Pad %d (Bank %s-%d):" % (pad, chr(65+bank), subpad+1),
msg="Type", dft=dft)
config[1][bank][subpad]['type'] = int(ptype)
config[1][bank][subpad]['channel'] = \
qprompt.ask_int("Channel", vld=list(range(1,17)),
dft=config[1][bank][subpad]['channel'])
if ptype == '0':
config[1][bank][subpad]['note'] = \
qprompt.ask_int("Note", vld=list(range(0,128)),
dft=config[1][bank][subpad]['note'])
menu = qprompt.Menu()
for x,y in Pad.trigger.subcon.ksymapping.items():
menu.add(str(x),y)
if config[1][bank][subpad]['trigger'] == y:
dft = str(x)
config[1][bank][subpad]['trigger'] = int(menu.show(msg="Trigger", dft=dft))
menu = qprompt.Menu()
for x,y in Pad.aftertouch.subcon.ksymapping.items():
menu.add(str(x),y)
if config[1][bank][subpad]['aftertouch'] == y:
dft = str(x)
config[1][bank][subpad]['aftertouch'] = int(menu.show(msg="Aftertouch", dft=dft))
elif ptype == '1':
config[1][bank][subpad]['program'] = \
qprompt.ask_int("Program", vld=list(range(0,128)),
dft=config[1][bank][subpad]['program'])
else:
config[1][bank][subpad]['msb'] = \
qprompt.ask_int("MSB", vld=list(range(0,128)),
dft=config[1][bank][subpad]['msb'])
config[1][bank][subpad]['lsb'] = \
qprompt.ask_int("LSB", vld=list(range(0,128)),
dft=config[1][bank][subpad]['lsb'])
def edit_scale(pad, verbose):
global config
rbank = int((pad-1) / _PADS)
rsubpad = pad-(_PADS * rbank)-1
ptype = config[1][rbank][rsubpad]['type']
if ptype == "BANK":
qprompt.error("Pad %d (Bank %s-%d) is configured as a BANK" % (pad, chr(65+rbank), rsubpad+1))
return
menu = qprompt.Menu()
x = 0
for y in Scale._SCALE_PATTERNS:
menu.add(str(x),y)
x = x + 1
stype = menu.show(hdr="Pad %d (Bank %s-%d):" % (pad, chr(65+rbank), rsubpad+1),
msg="Scale", returns="desc")
root = qprompt.ask_int("Note", vld=list(range(0,128)),
dft=config[1][rbank][rsubpad]['note'])
count = qprompt.ask_int("Count", vld=[0]+list(range(1,_PTOTAL+2-pad)), dft=0)
same = qprompt.ask_yesno(msg="Config all as per Pad %d?" % pad, dft='N')
scale = Scale.build_scale(Note.from_midi_num(root), stype)
scale_lst = []
for note in scale:
scale_lst.append(note.midi_note_number())
if count:
while len(scale_lst) < count:
root = scale_lst.pop()
scale = Scale.build_scale(Note.from_midi_num(root), stype)
for note in scale:
scale_lst.append(note.midi_note_number())
else:
count = len(scale_lst)
if pad + count > _PTOTAL:
count = _PTOTAL + 1 - pad
for note in scale_lst[:count]:
bank = int((pad-1) / _PADS)
subpad = pad-(_PADS * bank)-1
if same and ptype == "NOTE":
config[1][bank][subpad]['type'] = config[1][rbank][rsubpad]['type']
config[1][bank][subpad]['channel'] = config[1][rbank][rsubpad]['channel']
config[1][bank][subpad]['trigger'] = config[1][rbank][rsubpad]['trigger']
config[1][bank][subpad]['aftertouch'] = config[1][rbank][rsubpad]['aftertouch']
if same and ptype == "PROG":
config[1][bank][subpad]['type'] = config[1][rbank][rsubpad]['type']
config[1][bank][subpad]['channel'] = config[1][rbank][rsubpad]['channel']
if ptype == "NOTE":
config[1][bank][subpad]['note'] = note
else:
config[1][bank][subpad]['program'] = note
if verbose:
print("Setting Pad %d (Bank %s-%d) to %d (%s)" %
(pad, chr(65+bank), subpad+1, note, Note.from_midi_num(note)))
pad = pad + 1
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("-s", "--seq",
help="dump seq to hex",
action="store_true", dest="seq")
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" )
parser.add_option("-n", "--name", dest="name",
help="change the profile name to NAME")
if _hasQPrompt:
parser.add_option("-X", "--division",
help="Interactively change the Division",
action="store_true", dest="division")
parser.add_option("-S", "--swing",
help="Interactively change the Swing",
action="store_true", dest="swing")
parser.add_option("-D", "--dial", dest="dial",
help="Interactively configure a Dial")
parser.add_option("-P", "--pad", dest="pad",
help="Interactively configure a Pad" )
if _hasME:
parser.add_option("-M", "--scale", dest="scale",
help="Interactively configure multiple Pads as a scale" )
(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(4000)
config = Mpd232.parse(data)
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.name:
config[0]['name'] = (options.name + (" "*8))[:8]
if _hasQPrompt:
if options.division:
edit_division()
if options.swing:
edit_swing()
if options.dial:
edit_dial(int(options.dial))
if options.pad:
edit_pad(int(options.pad))
if _hasME:
if options.scale:
edit_scale(int(options.scale), options.verbose)
if options.dump:
print(config)
if options.seq:
print(config[5])
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(Mpd232.build(config))
'''
if __name__ == "__main__":
main()