forked from mungewell/mpd-utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mpk_play.py
606 lines (502 loc) · 16.3 KB
/
mpk_play.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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
#!/usr/bin/python
#
# Script decode/encode configuration files for the Akai MPK Play
# (c) Simon Wood, 22 Dec 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
'''
#--------------------------------------------------
# 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 MPK features
_PADS = 8
_PBANKS = 2
_PTOTAL = _PADS * _PBANKS
_DIALS = 4
_DBANKS = 2
_DTOTAL = _DIALS * _DBANKS
#--------------------------------------------------
# Define file format using Construct (v2.9)
# https://github.com/construct/construct
General = Struct(
"preset" / Byte,
"pchannel" / Byte, # Pads
"dchannel" / Byte, # Dials and Keys
"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,
),
)
Arpeggio_enable = Struct(
"enable" / Default(Enum(Byte,
OFF = 0,
ON = 1,
), 0),
)
Arpeggio_mode = Struct(
"mode" / Enum(Byte,
UP = 0,
DOWN = 1,
EXCLUSIVE = 2,
INCLUSIVE = 3,
RANDOM = 4,
ORDER = 5,
),
)
Arpeggio_div = Struct(
"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,
),
)
Arpeggio_clk = Struct(
"clock" / Enum(Byte,
INTERNAL = 0,
EXTERNAL = 1,
),
)
Arpeggio = Struct(
"latch" / Enum(Byte,
DISABLE = 0,
ENABLE = 1,
),
"swing" / Enum(Byte,
SWING_50 = 0,
SWING_55 = 1,
SWING_57 = 2,
SWING_59 = 3,
SWING_61 = 4,
SWING_64 = 5,
),
"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,
),
)
Joy = Struct( # Default values allow Mk1->Mk2 conversion
"axis_x" / Default(Enum(Byte,
PBEND = 0,
CC1 = 1,
CC2 = 2,
), 0),
"x_up" / Default(Byte, 0),
"x_down" / Default(Byte, 1), # CC2 only
"axis_y" / Default(Enum(Byte,
PBEND = 0,
CC1 = 1,
CC2 = 2,
), 1),
"y_up" / Default(Byte, 1),
"y_down" / Default(Byte, 1), # CC2 only
)
Pad = Struct(
"note" /Byte,
)
Dial = Struct(
"midicc" /Byte,
"min" /Byte,
"max" /Byte,
)
Transpose = Struct(
"transpose" / Default(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,
), 12),
)
Sounds = Struct(
"keys" / Byte,
"drums" / Byte,
"filter" / Byte,
"res" / Byte,
"reverb" / Byte,
"chorus" / Byte,
"attack" / Byte,
"release" / Byte,
"eq_low" / Byte,
"eq_high" / Byte,
)
'''
# Lists need to be filled
"keys" / Default(Enum(Byte,
PIANO = 0,
), 0),
"drums" / Default(Enum(Byte,
STANDARD = 0,
), 0),
'''
Footer = Struct(
Const(b"\xf7"), # SysEx End
)
Header = Struct(
Const(b"\xf0"), # SysEx Begin
Const(b"\x47\x00"), # Mfg ID = Akai
Const(b"\x44"), # Dev ID = MPK Mini Play
Const(b"\x64"), # CMD = Dump/Load Preset
Const(b"\x00\x47"), # Len = 71bytes (7bit stuffed)
Embedded(General), # Note: different order to Mk1
Embedded(Arpeggio_enable),
Embedded(Arpeggio_mode),
Embedded(Arpeggio_div),
Embedded(Arpeggio_clk),
Embedded(Arpeggio),
Embedded(Joy),
)
Mpk_Play = Sequence(
Header,
Array(_PBANKS, Array(_PADS, Pad,)),
Array(_DBANKS, Array(_DIALS, Dial,)),
Transpose,
Sounds,
Footer,
)
#--------------------------------------------------
config = None
def edit_arpeggio():
global config
if config[0]['mk2']:
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))
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))
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]['pchannel'] = \
qprompt.ask_int("Midi Ch for Pads", dft=config[0]['pchannel'])
config[0]['dchannel'] = \
qprompt.ask_int("Midi Ch for Dials/Keys", dft=config[0]['dchannel'])
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_joy():
global config
menu = qprompt.Menu()
for x,y in Header.axis_x.subcon.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['axis_x'] == y:
dft = str(x)
config[0]['axis_x'] = int(menu.show(msg="Axis-X", dft=dft))
menu = qprompt.Menu()
config[0]['x_up'] = \
qprompt.ask_int("X-Up", dft=config[0]['x_up'])
config[0]['x_down'] = \
qprompt.ask_int("X-Down", dft=config[0]['x_down'])
for x,y in Header.axis_y.subcon.subcon.ksymapping.items():
menu.add(str(x),y)
if config[0]['axis_y'] == y:
dft = str(x)
config[0]['axis_y'] = int(menu.show(msg="Axis-Y", dft=dft))
config[0]['y_up'] = \
qprompt.ask_int("Y-Up", dft=config[0]['y_up'])
config[0]['y_down'] = \
qprompt.ask_int("Y-Down", dft=config[0]['y_down'])
def edit_transpose():
global config
menu = qprompt.Menu()
for x,y in Transpose.transpose.subcon.subcon.ksymapping.items():
menu.add(str(x),y)
if config[3]['transpose'] == y:
dft = str(x)
transpose = int(menu.show(msg="Transpose", dft=dft))
config[3]['transpose'] = transpose
def edit_dial(dial):
global config
bank = int((dial-1) / _DIALS)
subdial = dial-(_DIALS * bank)-1
config[2][bank][subdial]['midicc'] = \
qprompt.ask_int("MidiCC", vld=list(range(0,128)),
dft=config[2][bank][subdial]['midicc'])
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'])
def edit_pad(pad):
global config
bank = int((pad-1) / _PADS)
subpad = pad-(_PADS * bank)-1
config[1][bank][subpad]['note'] = \
qprompt.ask_int("Note", vld=list(range(0,128)),
dft=config[1][bank][subpad]['note'])
menu = qprompt.Menu()
def edit_sound():
global config
menu = qprompt.Menu()
'''
for x,y in Sounds.keys.subcon.subcon.ksymapping.items():
menu.add(str(x),y)
if config[4]['keys'] == y:
dft = str(x)
config[4]['keys'] = int(menu.show(msg="Key Sound", dft=dft))
'''
config[4]['keys'] = \
qprompt.ask_int("Key Sound", vld=list(range(0,128)),
dft=config[4]['keys'])
'''
for x,y in Sounds.drums.subcon.subcon.ksymapping.items():
menu.add(str(x),y)
if config[4]['drums'] == y:
dft = str(x)
config[4]['drums'] = int(menu.show(msg="Drum Sound", dft=dft))
'''
config[4]['drums'] = \
qprompt.ask_int("Drum Sound", vld=list(range(0,10)),
dft=config[4]['drums'])
config[4]['filter'] = \
qprompt.ask_int("Filter", vld=list(range(0,128)),
dft=config[4]['filter'])
config[4]['res'] = \
qprompt.ask_int("Resonence", vld=list(range(0,128)),
dft=config[4]['res'])
config[4]['reverb'] = \
qprompt.ask_int("Reverb", vld=list(range(0,128)),
dft=config[4]['reverb'])
config[4]['chorus'] = \
qprompt.ask_int("Chorus", vld=list(range(0,128)),
dft=config[4]['chorus'])
config[4]['attack'] = \
qprompt.ask_int("Attack", vld=list(range(0,128)),
dft=config[4]['attack'])
config[4]['release'] = \
qprompt.ask_int("Release", vld=list(range(0,128)),
dft=config[4]['release'])
config[4]['eq_low'] = \
qprompt.ask_int("EQ Low", vld=list(range(0,128)),
dft=config[4]['eq_low'])
config[4]['eq_high'] = \
qprompt.ask_int("EQ High", vld=list(range(0,128)),
dft=config[4]['eq_high'])
def edit_scale(pad, verbose):
global config
rbank = int((pad-1) / _PADS)
rsubpad = pad-(_PADS * rbank)-1
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)
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
config[1][bank][subpad]['note'] = 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("-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("-J", "--joy",
help="Interactively configure the Joystick",
action="store_true", dest="joy")
parser.add_option("-T", "--transpose",
help="Interactively configure the Transposing",
action="store_true", dest="transpose")
parser.add_option("-D", "--dial", dest="dial",
help="Interactively configure a Dial")
parser.add_option("-P", "--pad", dest="pad",
help="Interactively configure a Pad" )
parser.add_option("-S", "--sound",
help="Interactively configure Key and Drum sounds",
action="store_true", dest="sound")
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(2000)
config = Mpk_Play.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.arpeggio:
edit_arpeggio()
if options.general:
edit_general()
if options.joy:
edit_joy()
if options.transpose:
edit_transpose()
if options.dial:
edit_dial(int(options.dial))
if options.pad:
edit_pad(int(options.pad))
if options.sound:
edit_sound()
if options.scale:
edit_scale(int(options.scale), options.verbose)
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(Mpk_Play.build(config))
if __name__ == "__main__":
main()