apcmini.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. """
  4. SPDX-License-Identifier: GPL-2.0-only
  5. (c) micha@librelight.de
  6. """
  7. import sys
  8. sys.stdout.write("\x1b]2;APCmini\x07")
  9. import os
  10. import time
  11. import socket, struct
  12. import random
  13. import lib.chat as chat
  14. if 0:
  15. # z.b. Windows
  16. # 10 MB RAM
  17. from interfaces.midi.lib.pygamemidi_wraper import pygamemidi
  18. midi = pygamemidi(out=2,inp=3)
  19. else:
  20. # 4 MB RAM , only tested on linux
  21. import lib.simplemidi_wraper as smidi
  22. device = smidi.list_device(filter="APC_MINI")
  23. _id = device[-1][-1]
  24. midi = smidi.simplemidi("/dev/snd/midiC{}D0".format(_id) ) #,inp="/dev/midi1")
  25. cli = None
  26. class dummyChat():
  27. def __init__(self):
  28. pass
  29. def send(self,*arg,**args):
  30. print("dummyChat",self,arg,args)
  31. #c = chat.tcp_sender()
  32. c = dummyChat()
  33. def send(msg):
  34. print("send",msg)
  35. c.send(msg)
  36. def main():
  37. global send
  38. global node
  39. global toggel,toggel1,toggel2,toggel3
  40. fader = 40
  41. values = []
  42. fader_value= [0]*9
  43. firstfader = 48
  44. fader_move_delay = 0
  45. tog = 0
  46. tog0 = 1
  47. tog1 = 1
  48. cmd = ""
  49. release = 1
  50. while True:
  51. if release:
  52. release = 0
  53. for i in range(4):
  54. midi.write([144,60+i,5])
  55. midi.write([144,52+i,5])
  56. midi.write([144,44+i,5])
  57. midi.write([144,36+i,5])
  58. midi.write([144,24+i,3])
  59. midi.write([144,24+i+4,3])
  60. midi.write([144,56+i,1])
  61. midi.write([144,8+i,1])
  62. midi.write([144,12+i,1])
  63. rows = [56,48,40,32,24,16,8,0,64,48]
  64. midi_function = {
  65. "Off": (128,144),
  66. "On ":(144,160),
  67. "PLY":(160,176),
  68. "CC ":(176,192),
  69. "PC ":(192,208),
  70. "CA ":(208,223),
  71. "QtrFrame":(241,242),
  72. "tclock":(248,249),
  73. }
  74. # cut byte
  75. # x=174;((x)^(x>>4<<4))
  76. # int(bin(151)[-4:],2)+1
  77. #nibl = x=174;((x)^(x>>4<<4))
  78. #nibl = int(bin(151)[-4:],2)+1
  79. ch = -123
  80. if midi.poll():
  81. midi_date = midi.read(1)
  82. #print("MIDI",midi_date)
  83. if 1:# midi_date[0] >= 128 midi_date[0] <= 143:
  84. ch = int(bin(midi_date[0])[-4:],2)+1
  85. for fn,v in midi_function.items():
  86. if midi_date[0] >= v[0] and midi_date[0] < v[1]:
  87. FN = fn
  88. break
  89. r_old = 0
  90. row = 0
  91. for r in rows:
  92. #print(r_old,r)
  93. if r_old and midi_date[1] >= r_old and midi_date[1] < r:
  94. row = midi_data[1] - r+1
  95. print("jo")
  96. r_old=r
  97. nibl = int(bin(midi_date[1])[2:][-4:],2)+1
  98. _bin2 = bin(midi_date[1])
  99. _bin2 = _bin2[2:]
  100. _bin2 = _bin2.rjust(8,"0")
  101. _bin2 = int(_bin2[1:5],2)
  102. _bin = bin(midi_date[1])
  103. _bin = _bin[2:]
  104. _bin = _bin.rjust(8,"0")
  105. _bin = _bin[-3:] # 3 bit
  106. print(row,"in:",FN,"ch",ch,"raw",midi_date,_bin,int(_bin,2),_bin2)
  107. continue
  108. if midi_date[0] == 176:
  109. if midi_date[1] == 55:
  110. fader = midi_date[2]/127.*8
  111. print("LED:",[144,7,fader])
  112. midi.write([144,7,fader])
  113. print("setfadert to", fader)
  114. if midi_date[1] >= firstfader and midi_date[1] <= firstfader+8:
  115. value = int(midi_date[2]*2.008)
  116. fader_id = midi_date[1] - firstfader
  117. fader_value[fader_id] = value
  118. print("FADER:",fader_id+1,value)
  119. if fader_id == 0:
  120. #msg = "group 33 level "+str(int(value))
  121. msg = "d{}:{}:0".format(fader_id+1,value)
  122. send(msg)
  123. elif fader_id == 1:
  124. msg = "group 34 level "+str(int(value))
  125. msg = "d{}:{}:0".format(fader_id+1,value)
  126. send(msg)
  127. elif fader_id == 2:
  128. msg = "group 35 level "+str(int(value))
  129. msg = "d{}:{}:0".format(fader_id+1,value)
  130. send(msg)
  131. elif fader_id == 3:
  132. msg = "group 36 level "+str(int(value))
  133. msg = "d{}:{}:0".format(fader_id+1,value)
  134. send(msg)
  135. elif fader_id == 4:
  136. msg = "group 37 level "+str(int(value))
  137. msg = "d{}:{}:0".format(fader_id+1,value)
  138. send(msg)
  139. elif fader_id == 5:
  140. msg = "group 38 level "+str(int(value))
  141. msg = "d{}:{}:0".format(fader_id+1,value)
  142. send(msg)
  143. elif fader_id == 6:
  144. #value = value*-1+255 #invert
  145. value2 = int(value/255.*100)*2
  146. #msg = "stack 2 indelay="+str(value2)+" ;EOB"
  147. msg = "effect 1 SIZE "+str(value2)+" ;EOB"
  148. send(msg)
  149. elif fader_id == 7:
  150. #value = value*-1+255 #invert
  151. value2 = int(value/255.*100)*2
  152. #msg = "stack 2 indelay="+str(value2)+" ;EOB"
  153. msg = "effect 7 SIZE "+str(value2)+" ;EOB"
  154. send(msg)
  155. elif fader_id == 8:
  156. if value <= 127:
  157. value2 = value / 127.
  158. else:
  159. value2 = (value - 127) / 20. +1.
  160. #value2 = value / 30.5
  161. msg = "preset time "+str(value2)+" ;EOB"
  162. send(msg)
  163. #value2 = value / 25.5
  164. #msg = "preset time_pt "+str(value2)+" ;EOB"
  165. #send(msg)
  166. elif midi_date[0] == 144:
  167. if midi_date[1] == 56:
  168. send("clear")
  169. elif midi_date[1] == 57:
  170. send("store")
  171. elif midi_date[1] == 58:
  172. send("edit")
  173. elif midi_date[1] == 33:
  174. send("preview")
  175. elif midi_date[1] == 34:
  176. send("set")
  177. elif midi_date[1] == 35:
  178. send("next")
  179. elif midi_date[1] >= 8 and midi_date[1] <= 15 :
  180. nr = midi_date[1]-8+25
  181. send("xpreset "+str(int( nr )) )
  182. elif midi_date[1] >= 24 and midi_date[1] <= 31 :
  183. nr = midi_date[1]-24+33
  184. send("xpreset "+str(int( nr )) )
  185. elif midi_date[1] == 20:
  186. pass#send("stack 1 on")
  187. elif midi_date[1] == 12:
  188. pass#send("stack 1 go")
  189. elif midi_date[1] == 4:
  190. pass#send("stack 1 run")
  191. elif midi_date[1] == 68:
  192. pass#send("stack 1 stop")
  193. elif midi_date[1] == 0:
  194. pass#msg = "sel 1"
  195. pass#send(msg)
  196. elif midi_date[1] == 1:
  197. pass#msg = "sel 2"
  198. pass#send(msg)
  199. elif midi_date[1] == 2:
  200. pass#msg = "sel 3"
  201. pass#send(msg)
  202. elif midi_date[1] == 64:
  203. msg = "df{}:255:0".format(midi_date[1]-64+1)
  204. send(msg)
  205. elif midi_date[1] == 65:
  206. msg = "df{}:255:0".format(midi_date[1]-64+1)
  207. send(msg)
  208. pass#msg = "group 25 sel 1 "
  209. pass#send(msg)
  210. elif midi_date[1] == 66:
  211. msg = "df{}:255:0".format(midi_date[1]-64+1)
  212. send(msg)
  213. pass#msg = "group 17 sel 1 "
  214. pass#send(msg)
  215. elif midi_date[1] == 86:
  216. msg = "df{}:255:0".format(midi_date[1]-64+1)
  217. send(msg)
  218. #msg = "group 25 sel 1 "
  219. #send(msg)
  220. #ipc = cli.ipc_write({"SDL-GUI":"VIEW:programmer"})
  221. pass
  222. elif midi_date[1] == 85:
  223. msg = "df{}:255:0".format(midi_date[1]-64)
  224. send(msg)
  225. pass
  226. #msg = "group 25 sel 1 "
  227. #send(msg)
  228. #ipc = cli.ipc_write({"SDL-GUI":"VIEW:groups"})
  229. elif midi_date[1] == 21:
  230. pass
  231. #send("stack 2 on")
  232. elif midi_date[1] == 13:
  233. pass
  234. #send("stack 2 go")
  235. elif midi_date[1] == 5:
  236. pass
  237. #send("stack 2 run")
  238. elif midi_date[1] == 69:
  239. pass
  240. #send("stack 2 stop")
  241. elif midi_date[1] == 22:
  242. pass
  243. #send("stack 2 mode ;")
  244. elif midi_date[1] == 14:
  245. pass
  246. #send("stack 2 dir ;")
  247. elif midi_date[1] == 6:
  248. #send("stack 2 dyn=rider,,1 ;")
  249. send("effect 1 DIR 0")
  250. elif midi_date[1] == 7:
  251. pass
  252. send("effect 7 DIR 0")
  253. #send("stack 2 dyn=rnd,,1 ;")
  254. if midi_date[0] == 128:
  255. midi_date[2] = 0
  256. midi_date[0] = 144
  257. #print("release")
  258. release = 1
  259. if midi_date[1] == 64:
  260. msg = "df{}:off:0".format(midi_date[1]-64+1)
  261. send(msg)
  262. if midi_date[1] == 65:
  263. msg = "df{}:off:0".format(midi_date[1]-64+1)
  264. send(msg)
  265. if midi_date[1] == 66:
  266. msg = "df{}:off:0".format(midi_date[1]-64+1)
  267. send(msg)
  268. if midi_date[1] == 67:
  269. msg = "df{}:off:0".format(midi_date[1]-64+1)
  270. send(msg)
  271. if midi_date[1] == 68:
  272. msg = "df{}:off:0".format(midi_date[1]-64+1)
  273. send(msg)
  274. else:
  275. midi_date[2] = int(fader)
  276. #if midi_date[1] <= 71:
  277. # midi_date[1] = midi_date[1]+8
  278. if midi_date[0] != 176:
  279. print("write",midi_date)
  280. midi.write(midi_date)
  281. if midi_date[0] == 128:
  282. for i in range(4):
  283. midi.write([144,60+i,5])
  284. midi.write([144,52+i,5])
  285. midi.write([144,44+i,5])
  286. midi.write([144,36+i,5])
  287. time.sleep(0.0002)
  288. if (int(time.time())) % 2 == 0:
  289. if tog0:
  290. tog0 = 0
  291. tog1 = 1
  292. midi.write([144,82,0])
  293. #print("o")
  294. else:
  295. if tog1:
  296. tog0 = 1
  297. tog1 = 0
  298. midi.write([144,82,3])
  299. #print("oo")
  300. main()
  301. node = 244
  302. node2 = 0
  303. while 1:
  304. inp = raw_input("end")
  305. try:
  306. inp = inp.split(" ")
  307. midi.write(inp)
  308. midi.write(inp)
  309. except:pass