midi.py 10 KB

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