/midilogic.pd_luax

http://github.com/jpburstrom/neu · Unknown · 175 lines · 168 code · 7 blank · 0 comment · 0 complexity · 6c25d424ac27061f6e2a5317546a2575 MD5 · raw file

  1. -- "Logic" for page 2 of controller
  2. -- A complete mess
  3. return function(self, sel, atoms)
  4. function mode(sel)
  5. -- select main mode
  6. self:update_controller(self.v.mode, sel)
  7. self.v.mode = sel
  8. if sel == 3 or sel == 4 then
  9. if self:calc_preset() then
  10. self.v.action = self.v.recstates[sel][self.curpreset]
  11. self:show_rec_play_action()
  12. else
  13. action(0)
  14. end
  15. else
  16. action(1)
  17. self:show_rec_play_action(1)
  18. end
  19. self.v.pendingsave = false
  20. self:do_save_blink()
  21. for i = self.ctl[2], self.ctl[2]+3 do self:ctlout(0, i) end
  22. end
  23. function action(sel)
  24. -- select action: load, save...
  25. prev = self.v.action
  26. self.v.action = sel
  27. if self.v.mode == 3 or self.v.mode == 4 then
  28. if self.v.action == prev then
  29. self:outlet(1, "list", {self.v.mode, "stop", self.curpreset})
  30. self.v.action = 0
  31. elseif self.v.action == 1 then
  32. self:outlet(1, "list", {self.v.mode, "play", self.curpreset})
  33. elseif self.v.action == 2 then
  34. self:outlet(1, "list", {self.v.mode, "rec", self.curpreset})
  35. end
  36. self.v.recstates[self.v.mode][self.curpreset] = self.v.action
  37. self:show_rec_play_action()
  38. else
  39. if self.v.action == 2 then
  40. if not self.v.pendingsave then
  41. self.v.pendingsave = true
  42. self:verbose("Choose bank+slot, then press save again.")
  43. else
  44. self.v.pendingsave = false
  45. self:verbose("Saved as " .. tostring(self.curpreset) ..".")
  46. self:outlet(1, "list", {self.v.mode, "save", self.v.inst[self.v.mode], self.curpreset})
  47. end
  48. self:do_save_blink()
  49. elseif self.v.pendingsave then
  50. self.v.pendingsave = false
  51. self:do_save_blink()
  52. end
  53. end
  54. end
  55. function bank(sel)
  56. -- select preset bank
  57. self:verbose(string.format("Bank %d chosen", sel))
  58. self.v.bank[self.v.mode] = sel
  59. end
  60. function slot(sel)
  61. -- select preset slot
  62. self:verbose(string.format("Slot %d chosen", sel))
  63. self.v.slot[self.v.mode] = sel
  64. self:preset()
  65. end
  66. function instsel(sel)
  67. --select instrument - on which instrument to apply the change
  68. if self.v.inst[self.v.mode] then self:ctlout(0, self.v.inst[self.v.mode]) end
  69. self.v.inst[self.v.mode] = sel
  70. self:ctlout(127, sel)
  71. self:outlet(1, "list", {self.v.mode, "inst", sel})
  72. end
  73. --START defining variables
  74. self.midi = {}
  75. -- mode, action, bank, slot, inst
  76. self.ctl = {65,105,73,77,33}
  77. for i = self.ctl[1], self.ctl[1]+7 do self.midi[i] = {mode, i - self.ctl[1] + 1 } end
  78. for i = self.ctl[2], self.ctl[2]+3 do self.midi[i] = {action, i - self.ctl[2] + 1} end
  79. for i = self.ctl[3], self.ctl[3]+3 do self.midi[i] = {bank, i - self.ctl[3]} end
  80. for i = self.ctl[4], self.ctl[4]+3 do self.midi[i] = {slot, i - self.ctl[4]} end
  81. for i = self.ctl[5], self.ctl[5]+7 do self.midi[i] = {instsel, i - self.ctl[5] + 1 } end
  82. self.v = {}
  83. self.v.mode = 1
  84. self.v.action = 1
  85. self.v.pendingsave = false
  86. self.v.doneinput = false
  87. self.v.recstates = {}
  88. for i = 3,4 do
  89. self.v.recstates[i] = {}
  90. for j = 0,15 do self.v.recstates[i][j] = 0 end
  91. end
  92. self.v.bank = {}
  93. self.v.slot = {}
  94. self.v.inst = {}
  95. for i = 1,8 do self.v.bank[i], self.v.inst[i], self.v.slot[i] = 0, 1, 0 end
  96. self.curpreset = 0
  97. --END defining variables
  98. --Inlets: Midi ctlin
  99. self.inlets = 1
  100. --Outlets: Messages to dsp, messages to ui/controller
  101. self.outlets = 2
  102. function self:in_1_list(sel, atoms)
  103. --Midi ctlin messages (value, control). No error checking!
  104. s = (sel[1] == 0 or sel[1] == 127) and self.midi[sel[2]] or nil
  105. if s then s[1](s[2]) end
  106. if sel[1] == 0 then self:ctlout(126, sel[2]) end
  107. end
  108. function self:calc_preset()
  109. -- Calculate preset number from bank/slot
  110. bank = self.v.bank[self.v.mode]
  111. slot = self.v.slot[self.v.mode]
  112. if bank ~= nil and slot ~= nil then
  113. sel = (bank * 4) + slot
  114. self.curpreset = sel
  115. return true
  116. else
  117. return false
  118. end
  119. end
  120. function self:preset()
  121. --Change preset and send out load message, in case we need to
  122. if self:calc_preset() then
  123. self:verbose(string.format("Preset %d chosen", self.curpreset))
  124. else return
  125. end
  126. inst = self.v.inst[self.v.mode]
  127. if self.v.action == 1 then
  128. self:verbose(string.format(
  129. "Mode %d loading preset %d", self.v.mode, sel))
  130. self:outlet(1, "list", {self.v.mode, "load", inst, sel} )
  131. else self:outlet(1, "list", {self.v.mode, "select", inst, sel} )
  132. end
  133. if self.v.mode == 3 or self.v.mode == 4 then
  134. self.v.action = self.v.recstates[self.v.mode][self.curpreset]
  135. self:show_rec_play_action()
  136. end
  137. end
  138. function self:update_controller(cur, new)
  139. --Update controllers on mode change
  140. --args: current (previous) mode, new mode
  141. self:ctlout(0, self.v.bank[cur] + self.ctl[3])
  142. self:ctlout(127, self.v.bank[new] + self.ctl[3])
  143. self:ctlout(0, self.v.slot[cur] + self.ctl[4])
  144. self:ctlout(127, self.v.slot[new] + self.ctl[4])
  145. self:ctlout(0, self.v.inst[cur] )
  146. self:ctlout(127, self.v.inst[new])
  147. end
  148. function self:ctlout(v, c)
  149. --Send ctlout messages
  150. self:outlet(2, "ctlout", {v, c})
  151. end
  152. function self:verbose(s)
  153. --Print method which might be commented out
  154. pd.post(s)
  155. end
  156. function self:do_save_blink()
  157. --start/stop save blink
  158. if self.v.pendingsave then i = 1 else i = 0 end
  159. self:outlet(2, "blink", {"save", i})
  160. end
  161. function self:show_rec_play_action(a)
  162. --play/rec/stop blink/blink/no-blink
  163. a = a or self.v.action + 1
  164. actions = {"stop", "play", "rec"}
  165. self:outlet (2, "blink", {actions[a]})
  166. end
  167. return true
  168. end