-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathlpd8.py
218 lines (167 loc) · 5.26 KB
/
lpd8.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
#!/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
'''
#--------------------------------------------------
# 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
'''
#--------------------------------------------------
# Define file format using Construct (v2.9)
# https://github.com/construct/construct
_PADS = 8
_DIALS = 8
Header = Struct(
Const(b"\xf0"), # SysEx Begin
Const(b"\x47\x7f"), # Mfg ID = Akai
Const(b"\x75"), # Dev ID = LPD8
Const(b"\x61"), # CMD = Dump/Load Preset
Const(b"\x00\x3a"), # Len = 13bytes (7bit stuffed)
"preset" / Byte,
"channel" / Byte,
)
Pad = Struct(
"note" /Byte,
"program" / Byte,
"midicc" /Byte,
"trigger" / Enum(Byte,
MOMENTARY = 0,
TOGGLE = 1,
),
)
Dial = Struct(
"midicc" /Byte,
"min" /Byte,
"max" /Byte,
)
Footer = Struct(
Const(b"\xf7"), # SysEx End
)
Lpd8 = Sequence(
Header,
Array(_PADS, Pad,),
Array(_DIALS, Dial,),
Footer,
)
#--------------------------------------------------
config = None
def edit_general():
global config
config[0]['channel'] = \
qprompt.ask_int("Midi Ch for Keys", dft=config[0]['channel'])
menu = qprompt.Menu()
def edit_dial(dial):
global config
config[1][dial]['midicc'] = \
qprompt.ask_int("CC", vld=list(range(0,128)),
dft=config[1][dial]['midicc'])
config[1][dial]['max'] = \
qprompt.ask_int("Max", vld=list(range(0,128)),
dft=config[1][dial]['max'])
config[1][dial]['min'] = \
qprompt.ask_int("Min", vld=list(range(0,128)),
dft=config[1][dial]['min'])
def edit_pad(pad):
global config
config[1][pad]['note'] = \
qprompt.ask_int("Note", vld=list(range(0,128)),
dft=config[1][pad]['note'])
menu = qprompt.Menu()
config[1][pad]['program'] = \
qprompt.ask_int("Program", vld=list(range(0,128)),
dft=config[1][pad]['program'])
config[1][pad]['midicc'] = \
qprompt.ask_int("CC", vld=list(range(0,128)),
dft=config[1][pad]['midicc'])
config[1][pad]['trigger'] = \
qprompt.ask_int("Trigger", vld=list(range(0,2)),
dft=config[1][pad]['trigger'])
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("-c", "--channel", dest="channel",
help="change the channel number to CHANNEL" )
if _hasQPrompt:
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)
try:
config = Lpd8.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.channel:
config[0]['channel'] = int(options.channel)
if options.dump:
print(config)
if options.dial:
edit_dial(int(options.dial))
if options.pad:
edit_pad(int(options.pad))
'''
if options.scale:
edit_scale(int(options.scale), options.verbose)
'''
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(Lpd8.build(config))
if __name__ == "__main__":
main()