pad_config.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import json
  2. from padbuilder import NoteOn, NoteOff
  3. REL = "RELATIVE"
  4. FIX = "FIXED"
  5. def Drop(): return { "type": "DROP"}
  6. def Reorder(n=0): return {
  7. "type": "REORDER",
  8. "offset" : n
  9. }
  10. def Translate(ch=0, chm="RELATIVE", k=0, km="RELATIVE", v=0, vm="RELATIVE"):
  11. return {
  12. "type": "TRANSLATE",
  13. "values" : (ch, k, v),
  14. "modes" : (chm, km, vm)
  15. }
  16. def Range(range, key, l=12):
  17. return {
  18. "type": "TRANSLATE",
  19. "range" : range,
  20. "key" : key,
  21. "length" : l
  22. }
  23. class Selection:
  24. def __init__(self, name):
  25. self.name = name
  26. self.inputs = []
  27. self.operations = []
  28. self.locked=[]
  29. self.init=[]
  30. def __iadd__(self, other):
  31. if isinstance(other, list):
  32. for x in other: self.inputs.append(x)
  33. if isinstance(other, int):
  34. self.inputs.append(other)
  35. def add_input(self, other):
  36. if isinstance(other, list):
  37. for x in other: self.inputs.append(x)
  38. if isinstance(other, int):
  39. self.inputs.append(other)
  40. def add_operation(self, op):
  41. self.operations.append(op)
  42. def add_init_operation(self, op):
  43. if isinstance(op, (tuple, list)):
  44. for x in op: self.add_init_operation(x)
  45. else:
  46. self.init.append(op)
  47. def add_locked(self, op):
  48. if isinstance(op, (tuple, list)):
  49. for x in op: self.add_locked(x)
  50. else:
  51. self.locked.append(op)
  52. def json(self):
  53. return {
  54. "operations" : self.operations,
  55. "init" : self.init,
  56. "locked" : self.locked
  57. }
  58. class Configuration:
  59. def __init__(self, name, pad, w, h):
  60. self.w=w
  61. self.h=h
  62. self.description=""
  63. self.name = name
  64. self.pad=pad
  65. self.selection={}
  66. def add_selection(self, s): self.selection[s.name] = s
  67. def json(self):
  68. matrix=[]
  69. for i in range(self.w*self.h):
  70. matrix.append(None)
  71. for s in self.selection:
  72. sel = self.selection[s]
  73. for i in sel.inputs:
  74. matrix[i]=sel.name
  75. sels = {}
  76. for x in self.selection:
  77. sels[self.selection[x].name]=(self.selection[x].json())
  78. return {
  79. "name" : self.name,
  80. "description" : self.description,
  81. "pad" : self.pad,
  82. "selections" : sels,
  83. "matrix" : matrix
  84. }
  85. def write(self, file):
  86. with open(file, "w") as f:
  87. f.write(json.dumps(self.json(), indent=4))
  88. conf = Configuration("APC Mini", "apc.json", 10, 12 )
  89. a = Selection("a")
  90. a.add_input([0,1,2,3,10,11,12,13,20,21,22,23,30,31,32,33])
  91. a.add_operation(Translate(1, FIX, 0, REL))
  92. a.add_init_operation(NoteOn(-1,-1,1))
  93. conf.add_selection(a)
  94. b = Selection("b")
  95. b.add_input([4,5,6,7,14,15,16,17,24,25,26,27,34,35,36,37])
  96. b.add_operation(Reorder())
  97. b.add_operation(Translate(2, FIX, 0, REL))
  98. b.add_init_operation(NoteOn(-1,-1,3))
  99. conf.add_selection(b)
  100. c = Selection("c")
  101. c.add_input([44,45,46,47,54,55,56,57,64,65,66,67,74,75,76,77])
  102. c.add_operation(Translate(3, FIX, 0, REL))
  103. c.add_init_operation(NoteOn(-1,-1,1))
  104. conf.add_selection(c)
  105. d = Selection("d")
  106. d.add_input([40,41,42,43,50,51,52,53,60,61,62,63,70,71,72,73])
  107. d.add_operation(Reorder())
  108. d.add_operation(Translate(4, FIX, 0, REL))
  109. d.add_init_operation(NoteOn(-1,-1,3))
  110. conf.add_selection(d)
  111. e = Selection("e")
  112. e.add_input([110,111,112,113,114,115,116,117,118])
  113. e.add_operation(Reorder())
  114. e.add_operation(Translate(4, FIX, 0, REL))
  115. conf.add_selection(e)
  116. conf.write("../conf.conf")