midi.py 9.8 KB

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