midi.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. apc_main = None
  103. #while 1:
  104. # if apc_main.buf:
  105. # buf = apc_main.buf[:]
  106. # apc_main.buf = []
  107. # for m in buf:
  108. # print("-> midi:",m)
  109. def remap_midi_row(m,row_len=8):
  110. btn,val=m
  111. row_def = []
  112. row_def.append([56,63])
  113. row_def.append([48,55])
  114. row_def.append([40,47])
  115. row_def.append([32,39])
  116. row_def.append([24,31])
  117. row_def.append([16,23])
  118. row_def.append([8,15])
  119. row_def.append([0,7])
  120. row_def.append([64,71])
  121. b2=-1
  122. v2=0
  123. for i,b in enumerate(row_def):
  124. #print(i,btn,b,val)
  125. if btn >= b[0] and btn <= b[1]:
  126. b2 = btn-b[0]
  127. btn2 = (i)*row_len + b2+1
  128. #print("btn2",btn2,val)
  129. return [btn2,val]
  130. break
  131. buf = []
  132. buf2 = []
  133. msgs = []
  134. buf_exec =[]
  135. while 1:
  136. fps +=1
  137. t = time.time()
  138. if t-fps_t >= 1:
  139. #print("FPS:",fps)
  140. fps_old = fps
  141. fps=0
  142. fps_t =t
  143. pygame.display.flip()
  144. pos = [160,10,70,60]
  145. rgb = (0xdd,0xdd,0xdd,0)
  146. rgb = (0xaa,0xaa,0xaa,0)
  147. window.fill((5,5,5))
  148. pygame.draw.rect(window,(0,0,0),[0,0,main_size[0],main_size[1]])
  149. fr = font15.render("FPS:"+str(fps_old) ,1, (200,200,200))
  150. window.blit(fr,(10,2 ))
  151. fr = font15.render("SDL-MIDI " ,1, (200,200,200))
  152. window.blit(fr,(100,2 ))
  153. if apc_main is None:
  154. try:
  155. print("scan for apcmidi ...")
  156. import remote.apcmini as apcmini
  157. apc_main = apcmini.MAIN()
  158. thread.start_new_thread(apc_main.loop,())
  159. time.sleep(1)
  160. buf = []
  161. buf2 = []
  162. buf_exec =[]
  163. msgs = []
  164. except Exception as e:
  165. time.sleep(1)
  166. pass#print("MIDI INI",e)
  167. try:
  168. if apc_main.buf:
  169. s = time.time()
  170. _buf = apc_main.buf[:]
  171. buf_exec =[]
  172. for b in _buf:
  173. b.append("None")
  174. b.append(time.time())
  175. #if b[0] > 1000:
  176. # continue
  177. buf.insert(0,b)
  178. buf_exec.append(b)
  179. apc_main.buf = []
  180. except Exception as e:
  181. pass #print("midi",e)
  182. msgs = []
  183. try:
  184. for m in buf_exec:
  185. m[2]= "ignore"
  186. if m[0] > 1000:
  187. continue
  188. btn,val = remap_midi_row(m[:2],row_len=10)
  189. btn+=400
  190. msg={"event":"EXEC","EXEC":str(btn),"VAL":str(val)}
  191. msgs.append(msg)
  192. m[2] = "ok"
  193. #print("msg: ",msg)
  194. buf2.append(["EXEC",str(btn),val,m[0],m[1]])
  195. buf_exec = []
  196. if msgs:
  197. msgs = json.dumps(msgs).encode("utf-8")
  198. print(msgs)
  199. cmd_client.send(msgs)
  200. e = time.time()
  201. print("TIME:",int((e-s)*10000),int(e*100)/100)
  202. msgs=[]
  203. except Exception as e:
  204. pass #print("midi",e)
  205. while 1:
  206. if len(buf2) < 7:
  207. break
  208. buf2.pop(0)#len(buf2)-1)
  209. while 1:
  210. if len(buf) < 7:
  211. break
  212. buf.pop(len(buf)-1)
  213. r = 10
  214. fr = font15.render("MIDI: APCMINI" ,1, (200,100,200))
  215. window.blit(fr,(270,10+r ))
  216. r+=10
  217. t2=time.time()
  218. for m in buf:
  219. #print("-> midi:",m)
  220. rgb = [100,100,100]
  221. try:
  222. if m[2] == "ok":#>= 1000:
  223. rgb =(200,200,0)
  224. except:pass
  225. m2 = m[:]
  226. m2[3] -= t2
  227. m2[3] = str(round(m2[3],1))+" sec"
  228. rgb2 = [10,10,10]
  229. if m2[1]:
  230. rgb2 = [0,200,0]
  231. if m2[0] >= 1000:
  232. v=m2[1]
  233. v=v*2
  234. if v > 255:
  235. v = 255
  236. if v < 0:
  237. v=0
  238. rgb2 = [v,v,v]
  239. pygame.draw.rect(window,rgb2,[255,10+r,10,12])
  240. fr = font15.render("MIDI:"+str(m2) ,1, rgb)
  241. window.blit(fr,(270,10+r ))
  242. r+=10
  243. r = 2
  244. _blink =0
  245. _is_open =0
  246. try:
  247. rgb = [10,10,10]
  248. if apc_main.blink:
  249. rgb = [110,110,110]
  250. _blink = apc_main.blink
  251. _is_open = apc_main.is_open
  252. except Exception as e:
  253. pass #print(e)
  254. if _is_open == 0:
  255. buf = []
  256. buf2 = ["connect to apcmini ..."]
  257. buf_exec = []
  258. msgs = []
  259. pygame.draw.rect(window,rgb,[200,r,60,25])
  260. fr = font15.render("BLINK:"+str(_blink) ,1, (200,200,200))
  261. window.blit(fr,(200,r ))
  262. r+=10
  263. fr = font15.render("open:"+str(_is_open) ,1, (200,200,200))
  264. window.blit(fr,(200,r ))
  265. r = 10
  266. fr = font15.render("EXEC:" ,1, (200,100,200))
  267. window.blit(fr,(10,10+r ))
  268. r+=10
  269. for m in buf2[::-1]:
  270. #print("-> midi:",m)
  271. fr = font15.render("SEND:"+str(m) ,1, (200,200,0))
  272. rgb2 = [10,10,10]
  273. if m[2]:
  274. rgb2 = [200,0,200]
  275. pygame.draw.rect(window,rgb2,[160,10+r,10,12])
  276. window.blit(fr,(10,10+r ))
  277. r+=10
  278. for t in table:
  279. t.draw()
  280. resize_changed = 0
  281. for event in pygame.event.get():
  282. if "scancode" in event.dict:
  283. if event.scancode == 9:
  284. for t in table:
  285. t.btn2.clean()
  286. print("event",event)
  287. if event.type == pygame.QUIT:
  288. pygame.quit()
  289. #time.sleep(1)
  290. sys.exit(0)
  291. elif event.type == pygame.VIDEORESIZE:
  292. scrsize = event.size
  293. width = event.w
  294. hight = event.h
  295. resize_changed = True
  296. for t in table:
  297. t.event(event)
  298. if "pos" in event.dict:
  299. if "button" in event.dict:
  300. if event.type == 5:#press
  301. mouse_down = 1
  302. mouse_pos1 = [event.pos[0],event.pos[1]]
  303. if event.type == 6:#release
  304. mouse_down = 0
  305. for btn in mouse_grab:
  306. btn.btn2.val.set(1)
  307. mouse_grab = []
  308. mouse_pos2 = [event.pos[0],event.pos[1]]
  309. if mouse_down:
  310. d1 = mouse_pos1[0]-mouse_pos2[0]
  311. d2 = mouse_pos1[1]-mouse_pos2[1]
  312. pix = 23
  313. #print(d1,d2)
  314. if ( d1 > pix or d1 < -pix) or ( d2 >pix or d2 < -pix):
  315. sdl_elm.draw_mouse_box(window,mouse_pos1,mouse_pos2)
  316. for t in table:
  317. pos = t.get_rect()
  318. mpos = [mouse_pos1[0],mouse_pos1[1],mouse_pos2[0],mouse_pos2[1]]
  319. if sdl_elm.check_area2(pos,mpos):
  320. t._set_mouse_focus(1)
  321. mouse_grab.append(t)
  322. else:
  323. t._set_mouse_focus(0)
  324. if resize_changed:# = True
  325. screen = pygame.display.set_mode(scrsize,pg.RESIZABLE)
  326. try:
  327. clock.tick(10)
  328. except KeyboardInterrupt as e:
  329. pygame.quit()
  330. raise e