import json from padbuilder import NoteOn, NoteOff REL = "RELATIVE" FIX = "FIXED" def Drop(): return { "type": "DROP"} def Reorder(n=0): return { "type": "REORDER", "offset" : n } def Translate(ch=0, chm="RELATIVE", k=0, km="RELATIVE", v=0, vm="RELATIVE"): return { "type": "TRANSLATE", "values" : (ch, k, v), "modes" : (chm, km, vm) } def Range(range, key, l=12): return { "type": "TRANSLATE", "range" : range, "key" : key, "length" : l } class Selection: def __init__(self, name): self.name = name self.inputs = [] self.operations = [] self.locked=[] self.init=[] def __iadd__(self, other): if isinstance(other, list): for x in other: self.inputs.append(x) if isinstance(other, int): self.inputs.append(other) def add_input(self, other): if isinstance(other, list): for x in other: self.inputs.append(x) if isinstance(other, int): self.inputs.append(other) def add_operation(self, op): self.operations.append(op) def add_init_operation(self, op): if isinstance(op, (tuple, list)): for x in op: self.add_init_operation(x) else: self.init.append(op) def add_locked(self, op): if isinstance(op, (tuple, list)): for x in op: self.add_locked(x) else: self.locked.append(op) def json(self): return { "operations" : self.operations, "init" : self.init, "locked" : self.locked } class Configuration: def __init__(self, name, pad, w, h): self.w=w self.h=h self.description="" self.name = name self.pad=pad self.selection={} def add_selection(self, s): self.selection[s.name] = s def json(self): matrix=[] for i in range(self.w*self.h): matrix.append(None) for s in self.selection: sel = self.selection[s] for i in sel.inputs: matrix[i]=sel.name sels = {} for x in self.selection: sels[self.selection[x].name]=(self.selection[x].json()) return { "name" : self.name, "description" : self.description, "pad" : self.pad, "selections" : sels, "matrix" : matrix } def write(self, file): with open(file, "w") as f: f.write(json.dumps(self.json(), indent=4)) conf = Configuration("APC Mini", "apc.json", 10, 12 ) a = Selection("a") a.add_input([0,1,2,3,10,11,12,13,20,21,22,23,30,31,32,33]) a.add_operation(Translate(1, FIX, 0, REL)) a.add_init_operation(NoteOn(-1,-1,1)) conf.add_selection(a) b = Selection("b") b.add_input([4,5,6,7,14,15,16,17,24,25,26,27,34,35,36,37]) b.add_operation(Reorder()) b.add_operation(Translate(2, FIX, 0, REL)) b.add_init_operation(NoteOn(-1,-1,3)) conf.add_selection(b) c = Selection("c") c.add_input([44,45,46,47,54,55,56,57,64,65,66,67,74,75,76,77]) c.add_operation(Translate(3, FIX, 0, REL)) c.add_init_operation(NoteOn(-1,-1,1)) conf.add_selection(c) d = Selection("d") d.add_input([40,41,42,43,50,51,52,53,60,61,62,63,70,71,72,73]) d.add_operation(Reorder()) d.add_operation(Translate(4, FIX, 0, REL)) d.add_init_operation(NoteOn(-1,-1,3)) conf.add_selection(d) e = Selection("e") e.add_input([110,111,112,113,114,115,116,117,118]) e.add_operation(Reorder()) e.add_operation(Translate(4, FIX, 0, REL)) conf.add_selection(e) conf.write("../conf.conf")