padbuilder.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. import json
  2. class Ports:
  3. def __init__(self, js):
  4. if isinstance(js, object):
  5. self.input = js["input"] if ("input" in js and isinstance(js["input"], str)) else ""
  6. self.output = js["output"] if ("output" in js and isinstance(js["output"], str)) else ""
  7. else:
  8. self.input=""
  9. self.output=""
  10. def json(self): return {"input": self.input, "output" : self.output}
  11. def _Note(t, ch=-1, k=-1, vel=-1):
  12. return [ t, ch if ch!=None else -1, k if k!=None else -1, vel if vel!=None else -1]
  13. def NoteOn(ch=-1, k=-1, vel=-1):
  14. return _Note("noteon", ch, k, vel)
  15. def NoteOff(ch=-1, k=-1, vel=-1):
  16. return _Note("noteoff", ch, k, vel)
  17. class InputDef:
  18. def __init__(self, name, type):
  19. self.name=name
  20. self.type=type
  21. self.actions={}
  22. @staticmethod
  23. def button(name): return InputDef(name, "BUTTON")
  24. @staticmethod
  25. def controller(name): return InputDef(name, "CONTROLLER")
  26. def add_actions(self, name, action):
  27. self.actions[name]=action
  28. def add_noteon(self, name, ch=-1, k=-1, vel=-1):
  29. self.actions[name]=NoteOn(ch, k, vel)
  30. def add_noteoff(self, name, ch=-1, k=-1, vel=-1):
  31. self.actions[name]=NoteOn(ch, k, vel)
  32. def json(self):
  33. return {
  34. "type" : self.type,
  35. "actions" : self.actions
  36. }
  37. def instance(self, ch, key):
  38. return {
  39. "type" : self.name,
  40. "channel" : ch,
  41. "key" : key,
  42. "velocity" : 0,
  43. "locked" : None
  44. }
  45. class Pad:
  46. def __init__(self, name, w, h, ip="", op=""):
  47. self.name = name
  48. self.width = w
  49. self.height = h
  50. self.ports = Ports({"input" : ip, "output" : op})
  51. self.matrix=[]
  52. i=0
  53. l = self.width*self.height
  54. while i<l:
  55. self.matrix.append(None)
  56. i+=1
  57. self.inputdef = {}
  58. def add_inputdef(self, val=None):
  59. self.inputdef[val.name]=val
  60. def set(self, x, y, val):
  61. self.matrix[y*self.width+x]=val
  62. def get(self, x, y):
  63. return self.matrix[y*self.width+x]
  64. def __getitem__(self, item):
  65. if isinstance(item, tuple) and len(item) ==2:
  66. item=item[0]+item[1]*self.width
  67. if isinstance(item, int):
  68. return self.matrix[item]
  69. else:
  70. raise Exception("Pass to Pad[] int or int,int")
  71. def __setitem__(self, item, val):
  72. if isinstance(item, tuple) and len(item) ==2:
  73. item=item[0]+item[1]*self.width
  74. if isinstance(item, int):
  75. self.matrix[item]=val
  76. else:
  77. raise Exception("Pass to Pad[] int or int,int")
  78. def json(self):
  79. inputs = {}
  80. for k in self.inputdef:
  81. inputs[k]=self.inputdef[k].json()
  82. return {
  83. "name" : self.name,
  84. "width" : self.width,
  85. "height" : self.height,
  86. "ports" : self.ports.json(),
  87. "inputs" : inputs,
  88. "matrix" : self.matrix,
  89. "type" : "pad"
  90. }
  91. def write(self, file):
  92. with open(file, "w") as f:
  93. f.write(json.dumps(self.json(), indent=4))