midi.py 10 KB

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