midi.py 9.9 KB

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