midi.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. buf_exec =[]
  162. for b in _buf:
  163. #if b[0] > 1000:
  164. # continue
  165. buf.insert(0,b)
  166. buf_exec.append(b)
  167. apc_main.buf = []
  168. msgs = []
  169. for m in buf_exec:
  170. if m[0] > 1000:
  171. continue
  172. btn,val = remap_midi_row(m,row_len=10)
  173. btn+=400
  174. msg={"event":"EXEC","EXEC":str(btn),"VAL":str(val)}
  175. msgs.append(msg)
  176. #print("msg: ",msg)
  177. buf2.append(["EXEC",str(btn),val,m[0],m[1]])
  178. if msgs:
  179. msgs = json.dumps(msgs).encode("utf-8")
  180. print(msgs)
  181. cmd_client.send(msgs)
  182. e = time.time()
  183. print("TIME:",int((e-s)*10000),int(e*100)/100)
  184. msgs=[]
  185. except Exception as e:
  186. print("midi",e)
  187. while 1:
  188. if len(buf2) < 7:
  189. break
  190. buf2.pop(0)#len(buf2)-1)
  191. while 1:
  192. if len(buf) < 7:
  193. break
  194. buf.pop(len(buf)-1)
  195. r = 10
  196. fr = font15.render("MIDI: APCMINI" ,1, (200,100,200))
  197. window.blit(fr,(330,10+r ))
  198. r+=10
  199. for m in buf:
  200. #print("-> midi:",m)
  201. try:
  202. rgb =(200,200,0)
  203. if m[0] >= 1000:
  204. rgb = [100,100,100]
  205. except:pass
  206. fr = font15.render("MIDI:"+str(m) ,1, rgb)
  207. window.blit(fr,(330,10+r ))
  208. r+=10
  209. r = 2
  210. try:
  211. rgb = [10,10,10]
  212. if apc_main.blink:
  213. rgb = [110,110,110]
  214. pygame.draw.rect(window,rgb,[200,r,60,25])
  215. fr = font15.render("BLINK:"+str(apc_main.blink) ,1, (200,200,200))
  216. window.blit(fr,(200,r ))
  217. r+=10
  218. fr = font15.render("open:"+str(apc_main.is_open) ,1, (200,200,200))
  219. window.blit(fr,(200,r ))
  220. except Exception as e:print(e)
  221. r = 10
  222. fr = font15.render("EXEC:" ,1, (200,100,200))
  223. window.blit(fr,(10,10+r ))
  224. r+=10
  225. for m in buf2: #[::-1]:
  226. #print("-> midi:",m)
  227. fr = font15.render("SEND:"+str(m) ,1, (200,200,0))
  228. window.blit(fr,(10,10+r ))
  229. r+=10
  230. if 0: #timer balken
  231. pos = [160,110,70+80,20]
  232. pygame.draw.rect(window,rgb,pos)
  233. t=(time.time()-start)
  234. if t > 15:
  235. start = time.time()
  236. b= 80-int(t*10)
  237. pos = [160,110,70+(b),20]
  238. rgb = (0x00,0xff,0xff,0)
  239. pygame.draw.rect(window,rgb,pos)
  240. rgb = (0x00,0x00,0x00,0)
  241. fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
  242. window.blit(fr,pos[:2])
  243. pos = [160,200,80,20]
  244. #fd = sdl_elm.Fader(window,pos)
  245. #fd.draw()
  246. pos = [160,90,70+80,20]
  247. pygame.draw.rect(window,rgb,pos)
  248. b= int(t*10)
  249. pos = [160,90,0+(b),20]
  250. rgb = (0x00,0xff,0xff,0)
  251. pygame.draw.rect(window,rgb,pos)
  252. rgb = (0x00,0x00,0x00,0)
  253. fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
  254. window.blit(fr,pos[:2])
  255. rgb = (0xaa,0xaa,0xaa,0)
  256. fr = font22.render(str(round(t,1)) ,1, rgb) #(200,200,200))
  257. window.blit(fr,(500,500))
  258. for t in table:
  259. t.draw()
  260. resize_changed = 0
  261. for event in pygame.event.get():
  262. if "scancode" in event.dict:
  263. if event.scancode == 9:
  264. for t in table:
  265. t.btn2.clean()
  266. print("event",event)
  267. if event.type == pygame.QUIT:
  268. pygame.quit()
  269. #time.sleep(1)
  270. sys.exit(0)
  271. elif event.type == pygame.VIDEORESIZE:
  272. scrsize = event.size
  273. width = event.w
  274. hight = event.h
  275. resize_changed = True
  276. for t in table:
  277. t.event(event)
  278. if "pos" in event.dict:
  279. if "button" in event.dict:
  280. if event.type == 5:#press
  281. mouse_down = 1
  282. mouse_pos1 = [event.pos[0],event.pos[1]]
  283. if event.type == 6:#release
  284. mouse_down = 0
  285. for btn in mouse_grab:
  286. btn.btn2.val.set(1)
  287. mouse_grab = []
  288. mouse_pos2 = [event.pos[0],event.pos[1]]
  289. if mouse_down:
  290. d1 = mouse_pos1[0]-mouse_pos2[0]
  291. d2 = mouse_pos1[1]-mouse_pos2[1]
  292. pix = 23
  293. #print(d1,d2)
  294. if ( d1 > pix or d1 < -pix) or ( d2 >pix or d2 < -pix):
  295. sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
  296. for t in table:
  297. pos = t.get_rect()
  298. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  299. if sdl_elm.check_area2(pos,mpos):
  300. t._set_mouse_focus(1)
  301. mouse_grab.append(t)
  302. else:
  303. t._set_mouse_focus(0)
  304. if resize_changed:# = True
  305. screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
  306. try:
  307. clock.tick(10)
  308. except KeyboardInterrupt as e:
  309. pygame.quit()
  310. raise e