-
Notifications
You must be signed in to change notification settings - Fork 10
/
mpd218.py
416 lines (343 loc) · 12.8 KB
/
mpd218.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
#!/usr/bin/python
#
# Script decode/encode configuration files for the Akai MPD218
# (c) Simon Wood, 13 June 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 MP218 features
_PADS = 16
_PBANKS = 3
_PTOTAL = _PADS * _PBANKS
_DIALS = 6
_DBANKS = 3
_DTOTAL = _DIALS * _DBANKS
#--------------------------------------------------
# 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"\x34"), # Dev ID = MPD218
Const(b"\x10"), # CMD = Dump/Load Preset
Const(b"\x04\x1d"), # Len = 541bytes (7bit stuffed)
"preset" / Byte,
"name" / PaddedString(8, "ascii"), # Pad with spaces!
"tempo" / ExprAdapter(Int16ub, # 7bit stuffed - each byte max 0x7F
((obj_ & 0x7f) + ((obj_ & 0x7f00) >> 1)),
((obj_ & 0x7f) + ((obj_ & 0x3f80) << 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,
),
"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,
),
)
Pad = Struct(
"type" / Enum(Byte,
NOTE = 0,
PROG = 1,
BANK = 2,
),
"channel" / Byte,
"note" /Byte, # NOTE only
"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
)
Dial = Struct(
"type" / Enum(Byte,
CC = 0,
AFTERTOUCH = 1,
INC_DEC_1 = 2,
INC_DEC_2 = 3,
),
"channel" / Byte,
"midicc" /Byte, # CC and ID2 only
"min" /Byte, # CC and AT only
"max" /Byte, # CC and AT only
"msb" / Byte, # ID1 only
"lsb" /Byte, # ID1 only
"value" / Byte, # ID1 only
)
Footer = Struct(
Const(b"\xf7"), # SysEx End
)
Mpd218 = Sequence(
Header,
Array(_PBANKS, Array(_PADS, Pad,)),
Array(_DBANKS, Array(_DIALS, Dial,)),
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("-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(2000)
config = Mpd218.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.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(Mpd218.build(config))
if __name__ == "__main__":
main()