midi.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. #!/usr/bin/python3
  2. import time
  3. boot = time.time()
  4. import random
  5. import os
  6. import sys
  7. import json
  8. CAPTION = 'LibreLight SDL-MIDI '
  9. sys.path.insert(0,"/opt/LibreLight/Xdesk/")
  10. import tool.movewin as movewin
  11. import tool.git as git
  12. #CAPTION += ':{}'.format(random.randint(100,999))
  13. CAPTION += git.get_all()
  14. import pathlib
  15. _file_path=pathlib.Path(__file__)
  16. print("file:",_file_path)
  17. movewin.check_is_started(CAPTION,_file_path)
  18. #_id = movewin.winfo(CAPTION)
  19. #c1 = movewin.movewin(_id,200,50)
  20. #os.system(c1)
  21. #c1 = movewin.activate(_id)
  22. #os.system(c1)
  23. # ===== GUI =========
  24. import pygame
  25. import pygame.gfxdraw
  26. import pygame.font
  27. pg = pygame
  28. main_size=(500,100)
  29. window = pygame.display.set_mode(main_size,pg.RESIZABLE,32)
  30. pg = pygame
  31. pygame.init()
  32. pg.display.set_caption(CAPTION)
  33. icon = pygame.image.load('icon/scribble.png')
  34. pygame.display.set_icon(icon)
  35. pygame.mixer.quit()
  36. clock = pygame.time.Clock()
  37. import tool.movewin as movewin
  38. import tool.sdl_elm as sdl_elm
  39. #_id = movewin.winfo(CAPTION)
  40. #c1 = movewin.movewin(_id,200,50)
  41. #os.system(c1)
  42. #c1 = movewin.activate(_id)
  43. #os.system(c1)
  44. import lib.zchat as chat
  45. cmd_client = chat.Client(port=30003)
  46. font0 = pygame.font.SysFont("freesans",10)
  47. font0b = pygame.font.SysFont("freesansbold",10)
  48. font = pygame.font.SysFont("freemonobold",22)
  49. font10 = pygame.font.SysFont("freemonobold",10)
  50. font12 = pygame.font.SysFont("freemonobold",12)
  51. font15 = pygame.font.SysFont("freemonobold",15)
  52. font22 = pygame.font.SysFont("FreeSans",22)
  53. #font = pygame.font.SysFont(None,30)
  54. fr = font.render("hallo" ,1, (200,0,255))
  55. start = time.time()
  56. table = []
  57. r = 80
  58. i = 1
  59. bx = sdl_elm.Button(window,pos=[20,r,80,40])
  60. bx.text = "FIX:{}\n<val>\nx".format(i+1)
  61. bx.bg_on = [255,0,255]
  62. bx.btn1.color_on = [255,0,155]
  63. bx.btn1.type = "flash"
  64. table.append(bx)
  65. r+=bx.get_rect()[3]
  66. i += 1
  67. bx = sdl_elm.Button(window,pos=[20,r,80,40])
  68. bx.text = "FIX:{}\n<val>\nx".format(i+1)
  69. table.append(bx)
  70. r+=bx.get_rect()[3]
  71. i += 1
  72. r+=bx.get_rect()[3]
  73. bx = sdl_elm.Button(window,pos=[20,r,80,40])
  74. bx.text = "FIX:{}\n<val>\nx".format(i+1)
  75. #bx.font0 = pygame.font.SysFont("freesans",20)
  76. bx.btn4.val.set( 100)
  77. bx.fader = 0
  78. table.append(bx)
  79. r+=bx.get_rect()[3]
  80. i += 1
  81. bx = sdl_elm.Button(window,pos=[20,r,80,60])
  82. bx.text = "FIX:{}\n<val>\nx".format(i+1)
  83. #bx.font0 = pygame.font.SysFont("freesans-bold",20)
  84. bx.btn1.type = "flash"
  85. table.append(bx)
  86. r+=bx.get_rect()[3]
  87. i += 1
  88. bx = sdl_elm.Button(window,pos=[20,r,80,20])
  89. bx.text = "FIX:{}\n<val>\nx".format(i+1)
  90. #bx.font0 = pygame.font.SysFont("freesans",12)
  91. table.append(bx)
  92. table = []
  93. mouse_down = 0
  94. mouse_pos1 = [0,0]
  95. mouse_pos2 = [0,0]
  96. mouse_grab = []
  97. print(int((time.time()-boot)*10),"loop...")
  98. fps_t = time.time()
  99. fps = 0
  100. fps_old = 0
  101. import _thread as thread
  102. try:
  103. import remote.apcmini as apcmini
  104. apc_main = apcmini.MAIN()
  105. thread.start_new_thread(apc_main.loop,())
  106. time.sleep(1)
  107. except Exception as e:
  108. print("MIDI INI",e)
  109. #while 1:
  110. # if apc_main.buf:
  111. # buf = apc_main.buf[:]
  112. # apc_main.buf = []
  113. # for m in buf:
  114. # print("-> midi:",m)
  115. def remap_midi_row(m,row_len=8):
  116. btn,val=m
  117. row_def = []
  118. row_def.append([56,63])
  119. row_def.append([48,55])
  120. row_def.append([40,47])
  121. row_def.append([32,39])
  122. row_def.append([24,31])
  123. row_def.append([16,23])
  124. row_def.append([8,15])
  125. row_def.append([0,7])
  126. row_def.append([64,71])
  127. b2=-1
  128. v2=0
  129. for i,b in enumerate(row_def):
  130. #print(i,btn,b,val)
  131. if btn >= b[0] and btn <= b[1]:
  132. b2 = btn-b[0]
  133. btn2 = (i)*row_len + b2+1
  134. #print("btn2",btn2,val)
  135. return [btn2,val]
  136. break
  137. buf = []
  138. buf2 = []
  139. while 1:
  140. fps +=1
  141. t = time.time()
  142. if t-fps_t >= 1:
  143. #print("FPS:",fps)
  144. fps_old = fps
  145. fps=0
  146. fps_t =t
  147. pygame.display.flip()
  148. pos = [160,10,70,60]
  149. rgb = (0xdd,0xdd,0xdd,0)
  150. rgb = (0xaa,0xaa,0xaa,0)
  151. window.fill((5,5,5))
  152. pygame.draw.rect(window,(0,0,0),[0,0,main_size[0],main_size[1]])
  153. fr = font15.render("FPS:"+str(fps_old) ,1, (200,200,200))
  154. window.blit(fr,(10,2 ))
  155. fr = font15.render("SDL-MIDI " ,1, (200,200,200))
  156. window.blit(fr,(100,2 ))
  157. try:
  158. if apc_main.buf:
  159. s = time.time()
  160. _buf = apc_main.buf[:]
  161. for b in _buf:
  162. buf.insert(0,b)
  163. apc_main.buf = []
  164. msgs = []
  165. for m in buf:
  166. if m[0] > 1000:
  167. continue
  168. btn,val = remap_midi_row(m,row_len=10)
  169. btn+=400
  170. msg={"event":"EXEC","EXEC":str(btn),"VAL":str(val)}
  171. msgs.append(msg)
  172. #print("msg: ",msg)
  173. buf2.insert(0,["EXEC",str(btn),val])
  174. if msgs:
  175. msgs = json.dumps(msgs).encode("utf-8")
  176. cmd_client.send(msgs)
  177. e = time.time()
  178. print("TIME:",int((e-s)*10000),int(e*100)/100)
  179. except Exception as e:
  180. print("midi",e)
  181. while 1:
  182. if len(buf2) < 7:
  183. break
  184. buf2.pop(len(buf2)-1)
  185. while 1:
  186. if len(buf) < 7:
  187. break
  188. buf.pop(len(buf)-1)
  189. r = 10
  190. fr = font15.render("MIDI: APCMINI" ,1, (200,100,200))
  191. window.blit(fr,(330,10+r ))
  192. r+=10
  193. for m in buf:
  194. #print("-> midi:",m)
  195. fr = font15.render("MIDI:"+str(m) ,1, (200,200,0))
  196. window.blit(fr,(330,10+r ))
  197. r+=10
  198. r = 2
  199. try:
  200. rgb = [10,10,10]
  201. if apc_main.blink:
  202. rgb = [110,110,110]
  203. pygame.draw.rect(window,rgb,[200,r,60,25])
  204. fr = font15.render("BLINK:"+str(apc_main.blink) ,1, (200,200,200))
  205. window.blit(fr,(200,r ))
  206. r+=10
  207. fr = font15.render("open:"+str(apc_main.is_open) ,1, (200,200,200))
  208. window.blit(fr,(200,r ))
  209. except Exception as e:print(e)
  210. r = 10
  211. fr = font15.render("EXEC:" ,1, (200,100,200))
  212. window.blit(fr,(10,10+r ))
  213. r+=10
  214. for m in buf2:
  215. #print("-> midi:",m)
  216. fr = font15.render("SEND:"+str(m) ,1, (200,200,0))
  217. window.blit(fr,(10,10+r ))
  218. r+=10
  219. if 0: #timer balken
  220. pos = [160,110,70+80,20]
  221. pygame.draw.rect(window,rgb,pos)
  222. t=(time.time()-start)
  223. if t > 15:
  224. start = time.time()
  225. b= 80-int(t*10)
  226. pos = [160,110,70+(b),20]
  227. rgb = (0x00,0xff,0xff,0)
  228. pygame.draw.rect(window,rgb,pos)
  229. rgb = (0x00,0x00,0x00,0)
  230. fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
  231. window.blit(fr,pos[:2])
  232. pos = [160,200,80,20]
  233. #fd = sdl_elm.Fader(window,pos)
  234. #fd.draw()
  235. pos = [160,90,70+80,20]
  236. pygame.draw.rect(window,rgb,pos)
  237. b= int(t*10)
  238. pos = [160,90,0+(b),20]
  239. rgb = (0x00,0xff,0xff,0)
  240. pygame.draw.rect(window,rgb,pos)
  241. rgb = (0x00,0x00,0x00,0)
  242. fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
  243. window.blit(fr,pos[:2])
  244. rgb = (0xaa,0xaa,0xaa,0)
  245. fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
  246. window.blit(fr,(500,500))
  247. for t in table:
  248. t.draw()
  249. resize_changed = 0
  250. for event in pygame.event.get():
  251. if "scancode" in event.dict:
  252. if event.scancode == 9:
  253. for t in table:
  254. t.btn2.clean()
  255. print("event",event)
  256. if event.type == pygame.QUIT:
  257. pygame.quit()
  258. #time.sleep(1)
  259. sys.exit(0)
  260. elif event.type == pygame.VIDEORESIZE:
  261. scrsize = event.size
  262. width = event.w
  263. hight = event.h
  264. resize_changed = True
  265. for t in table:
  266. t.event(event)
  267. if "pos" in event.dict:
  268. if "button" in event.dict:
  269. if event.type == 5:#press
  270. mouse_down = 1
  271. mouse_pos1 = [event.pos[0],event.pos[1]]
  272. if event.type == 6:#release
  273. mouse_down = 0
  274. for btn in mouse_grab:
  275. btn.btn2.val.set(1)
  276. mouse_grab = []
  277. mouse_pos2 = [event.pos[0],event.pos[1]]
  278. if mouse_down:
  279. d1 = mouse_pos1[0]-mouse_pos2[0]
  280. d2 = mouse_pos1[1]-mouse_pos2[1]
  281. pix = 23
  282. #print(d1,d2)
  283. if ( d1 > pix or d1 < -pix) or ( d2 >pix or d2 < -pix):
  284. sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
  285. for t in table:
  286. pos = t.get_rect()
  287. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  288. if sdl_elm.check_area2(pos,mpos):
  289. t._set_mouse_focus(1)
  290. mouse_grab.append(t)
  291. else:
  292. t._set_mouse_focus(0)
  293. if resize_changed:# = True
  294. screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
  295. try:
  296. clock.tick(10)
  297. except KeyboardInterrupt as e:
  298. pygame.quit()
  299. raise e