123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import json
- class Ports:
- def __init__(self, js):
- if isinstance(js, object):
- self.input = js["input"] if ("input" in js and isinstance(js["input"], str)) else ""
- self.output = js["output"] if ("output" in js and isinstance(js["output"], str)) else ""
- else:
- self.input=""
- self.output=""
- def json(self): return {"input": self.input, "output" : self.output}
- def _Note(t, ch=-1, k=-1, vel=-1):
- return [ t, ch if ch!=None else -1, k if k!=None else -1, vel if vel!=None else -1]
- def NoteOn(ch=-1, k=-1, vel=-1):
- return _Note("noteon", ch, k, vel)
- def NoteOff(ch=-1, k=-1, vel=-1):
- return _Note("noteoff", ch, k, vel)
- class InputDef:
- def __init__(self, name, type):
- self.name=name
- self.type=type
- self.actions={}
- @staticmethod
- def button(name): return InputDef(name, "BUTTON")
- @staticmethod
- def controller(name): return InputDef(name, "CONTROLLER")
- def add_actions(self, name, action):
- self.actions[name]=action
- def add_noteon(self, name, ch=-1, k=-1, vel=-1):
- self.actions[name]=NoteOn(ch, k, vel)
- def add_noteoff(self, name, ch=-1, k=-1, vel=-1):
- self.actions[name]=NoteOn(ch, k, vel)
- def json(self):
- return {
- "type" : self.type,
- "actions" : self.actions
- }
- def instance(self, ch, key):
- return {
- "type" : self.name,
- "channel" : ch,
- "key" : key,
- "velocity" : 0,
- "locked" : None
- }
- class Pad:
- def __init__(self, name, w, h, ip="", op=""):
- self.name = name
- self.width = w
- self.height = h
- self.ports = Ports({"input" : ip, "output" : op})
- self.matrix=[]
- i=0
- l = self.width*self.height
- while i<l:
- self.matrix.append(None)
- i+=1
- self.inputdef = {}
- def add_inputdef(self, val=None):
- self.inputdef[val.name]=val
- def set(self, x, y, val):
- self.matrix[y*self.width+x]=val
- def get(self, x, y):
- return self.matrix[y*self.width+x]
- def __getitem__(self, item):
- if isinstance(item, tuple) and len(item) ==2:
- item=item[0]+item[1]*self.width
- if isinstance(item, int):
- return self.matrix[item]
- else:
- raise Exception("Pass to Pad[] int or int,int")
- def __setitem__(self, item, val):
- if isinstance(item, tuple) and len(item) ==2:
- item=item[0]+item[1]*self.width
- if isinstance(item, int):
- self.matrix[item]=val
- else:
- raise Exception("Pass to Pad[] int or int,int")
- def json(self):
- inputs = {}
- for k in self.inputdef:
- inputs[k]=self.inputdef[k].json()
- return {
- "name" : self.name,
- "width" : self.width,
- "height" : self.height,
- "ports" : self.ports.json(),
- "inputs" : inputs,
- "matrix" : self.matrix,
- "type" : "pad"
- }
- def write(self, file):
- with open(file, "w") as f:
- f.write(json.dumps(self.json(), indent=4))
|