v2m.py 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # by svsd_val
  4. # jabber : svsd_val@jabber.ru
  5. # mail to: svsdval@gmail.com
  6. import sys
  7. import os
  8. filepath=''
  9. if ( len(sys.argv) < 2 ):
  10. if sys.platform.startswith('win'):
  11. from tkinter import Tk
  12. from tkinter import filedialog as fd
  13. root=Tk()
  14. root.withdraw()
  15. filepath= fd.askopenfilename(filetypes=(("Video Files", ".mpg .mkv .avi .webm .mp4"), ("All Files", "*.*")))
  16. root.destroy()
  17. print("get file [" + filepath +"]")
  18. else:
  19. print("halt, no args")
  20. sys.exit( 0 )
  21. else:
  22. filepath = sys.argv[1]
  23. if not os.path.exists( filepath ):
  24. print("file not exists [" + filepath +"]")
  25. sys.exit( 0 )
  26. import math
  27. import cv2
  28. from midiutil.MidiFile import MIDIFile
  29. import ntpath
  30. import pygame
  31. from pygame.locals import *
  32. from OpenGL.GL import *
  33. from OpenGL.GLU import *
  34. import time
  35. from os.path import expanduser
  36. print("open file [" + filepath + "]")
  37. vidcap = cv2.VideoCapture( filepath )
  38. outputmid= ntpath.basename( filepath ) + "_output.mid"
  39. #
  40. settingsfile= filepath + ".ini"
  41. import video2midi.settings as settings
  42. from video2midi.prefs import prefs
  43. from video2midi.gl import *
  44. from video2midi.midi import *
  45. import datetime
  46. width=640
  47. height=480
  48. mpos = [0,0]
  49. keygrab=0
  50. keygrabid=-1
  51. lastkeygrabid=-1
  52. #
  53. frame= 0
  54. printed_for_frame=0
  55. convertCvtColor=1
  56. # For OpenCV 2.X ..
  57. CAP_PROP_FRAME_COUNT =0
  58. CAP_PROP_POS_FRAMES =0
  59. CAP_PROP_POS_MSEC =0
  60. CAP_PROP_FRAME_WIDTH =0
  61. CAP_PROP_FRAME_HEIGHT=0
  62. CAP_PROP_FPS =0
  63. COLOR_BGR2RGB =0
  64. print("OpenCV version:" + cv2.__version__ )
  65. if cv2.__version__.startswith('2.'):
  66. CAP_PROP_FRAME_COUNT = cv2.cv.CV_CAP_PROP_FRAME_COUNT
  67. CAP_PROP_POS_FRAMES = cv2.cv.CV_CAP_PROP_POS_FRAMES
  68. CAP_PROP_POS_MSEC = cv2.cv.CV_CAP_PROP_POS_MSEC
  69. CAP_PROP_FRAME_WIDTH = cv2.cv.CV_CAP_PROP_FRAME_WIDTH
  70. CAP_PROP_FRAME_HEIGHT = cv2.cv.CV_CAP_PROP_FRAME_HEIGHT
  71. CAP_PROP_FPS = cv2.cv.CV_CAP_PROP_FPS
  72. else:
  73. # 3, 4 , etc ...
  74. CAP_PROP_FRAME_COUNT = cv2.CAP_PROP_FRAME_COUNT
  75. CAP_PROP_POS_FRAMES = cv2.CAP_PROP_POS_FRAMES
  76. CAP_PROP_POS_MSEC = cv2.CAP_PROP_POS_MSEC
  77. CAP_PROP_FRAME_WIDTH = cv2.CAP_PROP_FRAME_WIDTH
  78. CAP_PROP_FRAME_HEIGHT = cv2.CAP_PROP_FRAME_HEIGHT
  79. CAP_PROP_FPS = cv2.CAP_PROP_FPS
  80. COLOR_BGR2RGB = cv2.COLOR_BGR2RGB
  81. #
  82. vidcap.set(CAP_PROP_POS_FRAMES, frame)
  83. vidcap.set(cv2.CAP_PROP_BUFFERSIZE, 2)
  84. success,image = vidcap.read()
  85. debug_keys = 0
  86. length = int(vidcap.get(CAP_PROP_FRAME_COUNT))
  87. video_width = int(vidcap.get(CAP_PROP_FRAME_WIDTH))
  88. video_height = int(vidcap.get(CAP_PROP_FRAME_HEIGHT))
  89. fps = float(vidcap.get(CAP_PROP_FPS))
  90. width = video_width
  91. height = video_height
  92. def fit_to_the_screen():
  93. global width, height
  94. infoObject = pygame.display.Info()
  95. if (width > infoObject.current_w) or ( height > infoObject.current_h):
  96. print("try fit window to the screen")
  97. print("current window size: %sx%s" %(width,height))
  98. print("current screen size: %sx%s" %(infoObject.current_w, infoObject.current_h))
  99. ratio = ( width / infoObject.current_w)
  100. width = int(width / ratio * 0.9 )
  101. height = int(height / ratio *0.9)
  102. print("new window size: %sx%s" %(width,height))
  103. pygame.init()
  104. fit_to_the_screen()
  105. endframe = length
  106. showoutputpath = 0
  107. def resize_window():
  108. global screen, width, height
  109. if prefs.resize:
  110. width = prefs.resize_width
  111. height = prefs.resize_height
  112. else:
  113. width = video_width
  114. height = video_height
  115. fit_to_the_screen()
  116. screen = pygame.display.set_mode((width,height), DOUBLEBUF|OPENGL|pygame.RESIZABLE)
  117. #
  118. doinit()
  119. # set start frame
  120. def getFrame( framenum =-1 ):
  121. global image
  122. global success
  123. global width
  124. global height
  125. global convertCvtColor
  126. global fps
  127. if ( fps == 0 ):
  128. return
  129. if ( framenum != -1 ):
  130. #vidcap.set(CAP_PROP_POS_FRAMES, int(framenum) )
  131. # problems with mpeg formats ...
  132. oldframenum = int(round(vidcap.get(1)))
  133. frametime = framenum * 1000.0 / fps
  134. print("go to frame time :" + str(frametime))
  135. success = vidcap.set(CAP_PROP_POS_MSEC, frametime)
  136. if not success:
  137. print("Cannot set frame position from video file at " + str(framenum))
  138. success = vidcap.set(CAP_PROP_POS_FRAMES, int(oldframenum) )
  139. curframe = vidcap.get(CAP_PROP_POS_FRAMES)
  140. if (curframe != framenum ):
  141. print("OpenCV bug, Requesting frame " + str(framenum) + " but get position on " +str(curframe))
  142. success,image = vidcap.read()
  143. # if ( resize == 1 ):
  144. # image = cv2.resize(image, (resize_width , resize_height))
  145. # print "resize to "+str(resize_width) + "x"+ str(resize_height)
  146. pass
  147. getFrame()
  148. #
  149. print("video " + str(width) + "x" + str(height) +" fps: " + str(fps))
  150. # add some notes
  151. channel = 0
  152. volume = 100
  153. basenote = prefs.octave * 12
  154. notes=[]
  155. notes_db=[]
  156. notes_de=[]
  157. notes_channel=[]
  158. notes_tmp=[]
  159. notes_pressed_color=[]
  160. colorWindow_colorBtns_channel_labels=[]
  161. colorWindow_colorBtns_channel_btns=[]
  162. separate_note_id=-1
  163. screen=0
  164. colorBtns = []
  165. #quantized notes to the grid.
  166. use_snap_notes_to_grid = False
  167. notes_grid_size=32
  168. #
  169. midi_file_format = 0
  170. #
  171. line_height = 20
  172. running = 1
  173. #cfg
  174. home = expanduser("~")
  175. inifile = os.path.join( home, '.v2m.ini')
  176. if os.path.exists( 'v2m.ini' ):
  177. inifile="v2m.ini"
  178. print("local config file exists.")
  179. def update_size():
  180. global width, height
  181. if ( prefs.resize == 1 ):
  182. width = prefs.resize_width
  183. height = prefs.resize_height
  184. else:
  185. fit_to_the_screen()
  186. def loadsettings(cfgfile):
  187. global colorBtns, colorWindow_colorBtns_channel_labels
  188. settings.loadsettings(cfgfile)
  189. settings.compatibleColors(colorBtns)
  190. if len(colorWindow_colorBtns_channel_labels) > 0:
  191. for i in range(len(colorBtns)):
  192. colorWindow_colorBtns_channel_labels[i].text = "Ch:" + str(prefs.keyp_colors_channel[i]+1)
  193. update_size
  194. if 'glwindows' in globals():
  195. glBindTexture(GL_TEXTURE_2D, Gl.bgImgGL)
  196. loadImage(prefs.startframe)
  197. settingsWindow_slider1.setvalue(prefs.keyp_delta)
  198. settingsWindow_slider2.setvalue(prefs.minimal_duration * 100)
  199. settingsWindow_slider3.setvalue(prefs.tempo)
  200. sparks_switch.switch_status = prefs.use_sparks
  201. sparks_slider_delta.value = 0
  202. sparks_slider_delta.id =-1
  203. settingsWindow_rollcheck_button.switch_status = prefs.rollcheck
  204. settingsWindow_rollcheck_priority_button.switch_status = prefs.rollcheck_priority
  205. use_percolor_delta.switch_status = prefs.use_percolor_delta
  206. notes_overlap_btn.switch_status = prefs.notes_overlap
  207. ignore_notes_with_minimal_duration_btn.switch_status = prefs.ignore_minimal_duration
  208. pass
  209. update_size
  210. for i in range(144):
  211. notes.append(0)
  212. notes_db.append(0)
  213. notes_de.append(0)
  214. notes_channel.append(0)
  215. notes_tmp.append(0)
  216. notes_pressed_color.append([0,0,0])
  217. #
  218. prefs.keyp_colors_alternate.append([0,0,0])
  219. prefs.keyp_colors_alternate_sensitivity.append(0)
  220. #
  221. def v_rotate(v, ang):
  222. radAng = ang * math.pi/180
  223. return [ (v[1] * math.cos(radAng)) - (v[0] * math.sin(radAng)), (v[1] * math.sin(radAng)) + (v[0] * math.cos(radAng)) ]
  224. def updatekeys( append=0 ):
  225. xx=0
  226. for i in range(12):
  227. for j in range(12):
  228. if (append == 1) or (i*12+j > len(prefs.keys_pos)-1):
  229. prefs.keys_pos.append( [0,0] )
  230. prefs.keys_pos[i*12+j][0] = int(round( xx ))
  231. prefs.keys_pos[i*12+j][1] = 0
  232. if (j == 1) or ( j ==3 ) or ( j == 6 ) or ( j == 8) or ( j == 10 ):
  233. prefs.keys_pos[i*12+j][1] = prefs.yoffset_blackkeys
  234. xx += -prefs.whitekey_width
  235. # keys_pos[i*12+j][0] = int(round( xx + whitekey_width *0.5 ))
  236. # tune by wuzhuoqing
  237. if (j == 1) or ( j == 6 ):
  238. prefs.keys_pos[i*12+j][0] = int(round( xx + prefs.whitekey_width * prefs.blackkey_relative_position ))
  239. if (j == 8 ):
  240. prefs.keys_pos[i*12+j][0] = int(round( xx + prefs.whitekey_width * 0.5 ))
  241. if ( j ==3 ) or ( j == 10 ):
  242. prefs.keys_pos[i*12+j][0] = int(round( xx + prefs.whitekey_width * (1.0 - prefs.blackkey_relative_position) ))
  243. xx += prefs.whitekey_width
  244. for i in range(len(prefs.keys_pos)):
  245. prefs.keys_pos[i] = v_rotate( prefs.keys_pos[i] , prefs.keys_angle )
  246. prefs.keys_pos[i][0] = - prefs.keys_pos[i][0]
  247. pass
  248. updatekeys( 1 )
  249. loadsettings(inifile)
  250. tStart = t0 = time.time()-1
  251. frames = 0
  252. def snap_to_grid( input_value , input_grid_size ):
  253. quantized = int( (input_value - int(input_value)) * input_grid_size ) / input_grid_size
  254. result = (quantized + int(input_value))
  255. #print ("value before:", input_value , " after :", result)
  256. return result
  257. def framerate():
  258. global t0, frames
  259. t = time.time()
  260. frames += 1
  261. if t - t0 >= 1.0:
  262. seconds = t - t0
  263. if ( seconds != 0) :
  264. fps = frames / seconds
  265. print("%.0f frames in %3.1f seconds = %6.3f FPS" % (frames,seconds,fps))
  266. t0 = t
  267. frames = 0
  268. def loadImage(idframe=130):
  269. global image
  270. global convertCvtColor
  271. if running != 0:
  272. getFrame(idframe)
  273. #image2=cv2.resize(image, (int(video_width/4) , int(video_height/4)))
  274. print("load image from video " + str(width) + "x" + str(height) + " frame: "+ str(idframe))
  275. glPixelStorei(GL_UNPACK_ALIGNMENT,1)
  276. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  277. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST)
  278. glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL)
  279. error_on_load=False
  280. try:
  281. if ( convertCvtColor == 1 ):
  282. #print ("Loading RGB texture")
  283. glTexImage2D(GL_TEXTURE_2D, 0, 3, video_width, video_height, 0, GL_RGB, GL_UNSIGNED_BYTE, cv2.cvtColor(image,COLOR_BGR2RGB) )
  284. else:
  285. #print ("Loading BGR texture")
  286. glTexImage2D(GL_TEXTURE_2D, 0, 3, video_width, video_height, 0, GL_BGR, GL_UNSIGNED_BYTE, image )
  287. return
  288. except Exception as E:
  289. error_on_load=True
  290. print("Can't load image from video to OpenGL: %s" % E);
  291. if error_on_load:
  292. rvideo_width, rvideo_height = 512, 512
  293. print("Trying resize video image to %sx%s" % (rvideo_width, rvideo_height));
  294. try:
  295. rimage = cv2.resize(image , (rvideo_width, rvideo_height))
  296. if ( convertCvtColor == 1 ):
  297. glTexImage2D(GL_TEXTURE_2D, 0, 3, rvideo_width, rvideo_height, 0, GL_RGB, GL_UNSIGNED_BYTE, cv2.cvtColor(rimage,COLOR_BGR2RGB) )
  298. else:
  299. glTexImage2D(GL_TEXTURE_2D, 0, 3, rvideo_width, rvideo_height, 0, GL_BGR, GL_UNSIGNED_BYTE, rimage )
  300. except Exception as E:
  301. print("Can't load image from video to OpenGL: %s" % E);
  302. pass
  303. def update_channels(sender):
  304. print( 'update_channels...' +str(sender.index))
  305. i=abs(sender.index) -1
  306. if (sender.index > 0):
  307. prefs.keyp_colors_channel[i]= prefs.keyp_colors_channel[i] + 1
  308. else:
  309. prefs.keyp_colors_channel[i]= prefs.keyp_colors_channel[i] - 1
  310. if (prefs.keyp_colors_channel[i] > 15):
  311. prefs.keyp_colors_channel[i] = 15
  312. if (prefs.keyp_colors_channel[i] < 0):
  313. prefs.keyp_colors_channel[i] = 0
  314. colorWindow_colorBtns_channel_labels[i].text = "Ch:" + str(prefs.keyp_colors_channel[i]+1)
  315. def disable_color(sender):
  316. print( 'disabled color...' +str(sender.index))
  317. if sender.index < len(prefs.keyp_colors):
  318. prefs.keyp_colors[ sender.index ] = [0,0,0]
  319. # prefs.keyp_colors_channel[i]= prefs.keyp_colors_channel[i] + 1
  320. def readkeycolor(i):
  321. pixx=int(prefs.xoffset_whitekeys + prefs.keys_pos[i][0])
  322. pixy=int(prefs.yoffset_whitekeys + prefs.keys_pos[i][1])
  323. if ( pixx >= width ) or ( pixy >= height ) or ( pixx < 0 ) or ( pixy < 0 ): return
  324. if ( prefs.resize == 1 ):
  325. pixxo=pixx
  326. pixyo=pixy
  327. pixx= int(round( pixx * ( video_width / float(prefs.resize_width) )))
  328. pixy= int(round( pixy * ( video_height / float(prefs.resize_height) )))
  329. if ( pixx > video_width -1 ): pixx = video_width-1
  330. if ( pixy > video_height-1 ): pixy= video_height-1
  331. # print "original x:"+str(pixxo) + "x" +str(pixyo) + " mapped :" +str(pixx) +"x"+str(pixy)
  332. keybgr=image[pixy,pixx]
  333. key=[ keybgr[2], keybgr[1],keybgr[0] ]
  334. prefs.keyp_colors_alternate[i] = key
  335. def readcolors(sender):
  336. for i in range( len(prefs.keys_pos) ):
  337. readkeycolor(i)
  338. def update_alternate_sensitivity(sender,value):
  339. global lastkeygrabid
  340. if ( lastkeygrabid != -1 ):
  341. prefs.keyp_colors_alternate_sensitivity[ lastkeygrabid ] = value
  342. def update_sparks_delta(sender,value):
  343. if (sender.id == -1):
  344. return
  345. if (sender.id < len(prefs.keyp_colors)) :
  346. prefs.keyp_colors_sparks_sensitivity[sender.id] = sender.value
  347. #print("keyp_colors_sparks_sensitivity["+str(sender.id)+"] = "+ str(sender.value) )
  348. def update_blackkey_relative_position(sender,value):
  349. prefs.blackkey_relative_position = value * 0.001
  350. updatekeys()
  351. def update_sync_notes_start_pos_time_delta(sender,value):
  352. prefs.sync_notes_start_pos_time_delta = value *0.001
  353. def change_use_alternate_keys(sender):
  354. global extra_label1
  355. prefs.use_alternate_keys = not prefs.use_alternate_keys
  356. update_alternate_label()
  357. def update_alternate_label():
  358. extra_label1.text = "Use alternate:"+str(prefs.use_alternate_keys)
  359. def change_use_sparks(sender):
  360. prefs.use_sparks = sender.switch_status
  361. # sender.text = "use sparks:"+str(use_sparks)
  362. def change_rollcheck(sender):
  363. prefs.rollcheck = sender.switch_status
  364. def change_rollcheck_priority(sender):
  365. prefs.rollcheck_priority = sender.switch_status
  366. def updatecolor(sender):
  367. if (lastkeygrabid != -1):
  368. readkeycolor(lastkeygrabid)
  369. def update_sparks_y_pos (sender):
  370. if (sender.text == "y+"):
  371. prefs.keyp_spark_y_pos = prefs.keyp_spark_y_pos -1
  372. else:
  373. prefs.keyp_spark_y_pos = prefs.keyp_spark_y_pos +1
  374. pass
  375. def update_line_height(sender,value):
  376. global line_height
  377. line_height = value
  378. def snap_notes_to_the_grid(sender):
  379. global use_snap_notes_to_grid
  380. use_snap_notes_to_grid = sender.switch_status
  381. def raise_octave(*args):
  382. global basenote
  383. prefs.octave += 1
  384. if (prefs.octave > 7): prefs.octave = 7
  385. basenote = prefs.octave * 12
  386. def lower_octave(*args):
  387. global basenote
  388. prefs.octave -= 1
  389. if (prefs.octave < 0): prefs.octave = 0
  390. basenote = prefs.octave * 12
  391. def onPallete_click(sender, index):
  392. selected_color_delta.color = sender.color
  393. if index < len(prefs.percolor_delta):
  394. selected_color_delta.setvalue( prefs.percolor_delta[ index ] )
  395. sparks_slider_delta.id = Gl.keyp_colormap_id
  396. sparks_slider_delta.color = prefs.keyp_colors[Gl.keyp_colormap_id]
  397. sparks_slider_delta.setvalue( prefs.keyp_colors_sparks_sensitivity[Gl.keyp_colormap_id] )
  398. def change_use_percolor_delta(sender):
  399. prefs.use_percolor_delta = sender.switch_status
  400. def update_percolor_delta(sender,value):
  401. if (Gl.keyp_colormap_id == -1):
  402. return
  403. if (Gl.keyp_colormap_id < len(prefs.percolor_delta)):
  404. prefs.percolor_delta[ Gl.keyp_colormap_id ] = sender.value
  405. #print("changed percolor delta for color with id ["+str(sender.id)+"] = "+ str(sender.value) )
  406. def showOrhideallwindows(sender):
  407. if sender == None:
  408. ShowHideButton.switch_status = not ShowHideButton.switch_status
  409. print('switch hidden for all windows')
  410. for i in glwindows:
  411. #print("i.type =%s" % (str(type(i))) )
  412. if isinstance(i, GLWindow ):
  413. i.fullhidden = ShowHideButton.switch_status
  414. def start_recreate_midi(sender):
  415. global running
  416. if prefs.autoclose == 1:
  417. running = 0
  418. else:
  419. reconstruct()
  420. pass
  421. def set_start_frame_to_current_frame(sender):
  422. if sender.index == 0:
  423. prefs.startframe = int(round(vidcap.get(1)))
  424. else:
  425. prefs.startframe = 0
  426. print("set start frame = "+ str(prefs.startframe))
  427. pass
  428. def sef_end_frame_to_current_frame(sender):
  429. global endframe
  430. if sender.index == 0:
  431. endframe = int(round(vidcap.get(1)))
  432. else:
  433. endframe = length
  434. print("set end frame = "+ str(endframe), sender.index)
  435. pass
  436. def switch_notes_overlap(sender):
  437. if sender == None:
  438. prefs.notes_overlap = not prefs.notes_overlap
  439. notes_overlap_btn.switch_status = prefs.notes_overlap
  440. else:
  441. prefs.notes_overlap = notes_overlap_btn.switch_status
  442. pass
  443. def switch_sync_notes_start_pos(sender):
  444. prefs.sync_notes_start_pos = sender.switch_status
  445. pass
  446. def change_save_to_disk_per_channel(sender):
  447. prefs.save_to_disk_per_channel = sender.switch_status
  448. pass
  449. def switch_ignore_notes_with_minimal_duration(sender):
  450. if sender == None:
  451. prefs.ignore_minimal_duration = not prefs.ignore_minimal_duration
  452. ignore_notes_with_minimal_duration_btn.switch_status = prefs.ignore_minimal_duration
  453. else:
  454. prefs.ignore_minimal_duration = ignore_notes_with_minimal_duration_btn.switch_status
  455. pass
  456. def switch_resize_windows(sender):
  457. prefs.resize = not prefs.resize
  458. resize_window()
  459. pass
  460. def scroll_by_steps( steps ):
  461. global frame
  462. frame+=steps
  463. if (frame > length *0.99):
  464. frame=math.trunc(length *0.99)
  465. if (frame < 1):
  466. frame=1
  467. glBindTexture(GL_TEXTURE_2D, Gl.bgImgGL)
  468. loadImage(frame)
  469. pass
  470. def scroll_forward_by_frame(sender):
  471. scroll_by_steps(1)
  472. pass
  473. def scroll_fast_forward(sender):
  474. scroll_by_steps(100)
  475. pass
  476. def scroll_prev_by_frame(sender):
  477. scroll_by_steps(-1)
  478. pass
  479. def scroll_fast_prev(sender):
  480. scroll_by_steps(-100)
  481. pass
  482. def scroll_to_start(sender):
  483. global frame
  484. frame=0
  485. glBindTexture(GL_TEXTURE_2D, Gl.bgImgGL)
  486. loadImage(frame)
  487. pass
  488. def scroll_to_end(sender):
  489. global frame
  490. frame=length-100
  491. glBindTexture(GL_TEXTURE_2D, Gl.bgImgGL)
  492. loadImage(frame)
  493. pass
  494. def btndown_save_settings(sender):
  495. settings.savesettings(settingsfile)
  496. def btndown_load_settings(sender):
  497. old_resize = prefs.resize
  498. loadsettings( settingsfile )
  499. update_alternate_label()
  500. if (prefs.resize != old_resize):
  501. resize_window()
  502. def change_autoclose(sender):
  503. prefs.autoclose = sender.switch_status
  504. def rotate_cw(sender):
  505. prefs.keys_angle -= 5
  506. updatekeys()
  507. def rotate_ccw(sender):
  508. prefs.keys_angle += 5
  509. updatekeys()
  510. #
  511. wh = ( (len(prefs.keyp_colors) // 2)+2 ) * 24 - 24
  512. colorWindow = GLWindow(24, 50, 274, wh, "color map")
  513. settingsWindow = GLWindow(24+275, 80, 550, 340, "Settings")
  514. helpWindow = GLWindow(24+270, 50, 750, 490, "help")
  515. extraWindow = GLWindow(24+270+550+6, 80, 510, 250, "extra/experimental")
  516. sparksWindow = GLWindow(24+270+550+6, 300, 510, 185, "sparks & color settings")
  517. glwindows = []
  518. ShowHideButton = GLButton(0,0 ,13,13, 1, [128,128,128], "" , showOrhideallwindows ,switch=1, switch_status=0 )
  519. ShowHideButton.active = 2
  520. glwindows.append(ShowHideButton)
  521. glwindows.append(colorWindow)
  522. glwindows.append(settingsWindow)
  523. glwindows.append(helpWindow)
  524. glwindows.append(extraWindow)
  525. glwindows.append(sparksWindow)
  526. helpWindow.hidden=1
  527. helpWindow_label1 = GLLabel(0,0, """h - on window title, show/hide the window
  528. q - begin to recreate midi
  529. s - set start frame, (mods : shift, set processing start frame to the beginning)
  530. e - set end frame, (mods : shift, set processing end frame to the ending)
  531. p - if key is set, force separate to 2 channels (on single color video)
  532. o - enable or disable overlap notes
  533. i - enable or disable ignore/lengthening of notes with minimal duration
  534. r - enable or disable resize function
  535. Mouse wheel - keys adjustment
  536. Left mouse button - dragging the selected key / select color from the color map
  537. CTRL + Left mouse button - update selected color in the color map
  538. CTRL + 0 - disable selected color in the color map
  539. Right mouse button - dragging all keys, if the key is selected, the transfer is carried out relative to it.
  540. Arrows - keys adjustment (mods : shift) ( Atl+Arrows UP/Down - sparks position adjustment )
  541. +(PLUS) / - (MINUS) - rotate keys by 5*
  542. PageUp/PageDown - scrolling video (mods : shift)
  543. Home/End - go to the beginning or end of the video
  544. [ / ] - change base octave
  545. F2 / F3 - save / load settings, F4 - move all windows to the mouse point
  546. Escape - quit, TAB - Show/Hide all windows
  547. Space - abort re-creation and save midi file to disk""")
  548. settingsWindow.appendChild( GLButton(260, 20 ,140,20,0 , [128,128,128], "start recreate midi" , start_recreate_midi , hint = "q - hot key") )
  549. settingsWindow.appendChild( GLButton(260, 40 ,140,20,0 , [128,128,128], "set start frame" , set_start_frame_to_current_frame, hint = "s - hot key, (mods : shift + s, set processing start frame to the beginning)" ) )
  550. settingsWindow.appendChild( GLButton(260+141, 40 ,140,20,0, [128,128,128], "set end frame" , sef_end_frame_to_current_frame , hint = "e - hot key, (mods : shift + e, set processing end frame to the ending)" ) )
  551. notes_overlap_btn = GLButton(260, 80 ,140,20,0, [128,128,128], "notes overlap" , switch_notes_overlap , hint = "o - hot key", switch=1, switch_status=0)
  552. ignore_notes_with_minimal_duration_btn = GLButton(260,100 ,272,20,0, [128,128,128], "ignore notes with minimal duration", switch_ignore_notes_with_minimal_duration, hint = "i - hot key", switch=1, switch_status=0)
  553. settingsWindow.appendChild( notes_overlap_btn )
  554. settingsWindow.appendChild( ignore_notes_with_minimal_duration_btn )
  555. settingsWindow.appendChild( GLButton(260+141, 80 ,140,20,0, [128,128,128], "sync notes" , switch_sync_notes_start_pos , hint = "sync notes start pos", switch=1, switch_status=0) )
  556. settingsWindow.appendChild( GLButton(260,120 ,140,20,0, [128,128,128], "resize window" , switch_resize_windows , hint = "r - hot key") )
  557. exit_switch = GLButton(260+141, 120 ,140,20,1, [128,128,128], "auto-close" ,change_autoclose,switch=1, switch_status= prefs.autoclose, hint = "exit after the completion of the midi reconstruction" )
  558. settingsWindow.appendChild( exit_switch )
  559. settingsWindow.appendChild( GLButton(260 , 140 ,140,20,0, [128,128,128], "save settings" , btndown_save_settings , hint = "F2 - hot key, save current settings" ) )
  560. settingsWindow.appendChild( GLButton(260+141, 140 ,140,20,0, [128,128,128], "load settings" , btndown_load_settings , hint = "F3 - hot key, load saved settings" ) )
  561. navbtns_info = [
  562. {'name' : "[<", 'hint' : 'Home - hot key, go to first frame',
  563. 'func' : scroll_to_start },
  564. {'name' : "<<", 'hint' : 'PageDown - hot key, fast scroll backward',
  565. 'func' : scroll_fast_prev },
  566. {'name' : " <", 'hint' : 'Shift+PageDown - shortcut, scroll backward by frame',
  567. 'func' : scroll_prev_by_frame },
  568. {'name' : " >", 'hint' : 'Shift+PageUp - shortcut, scroll forward by frame',
  569. 'func' : scroll_forward_by_frame },
  570. {'name' : ">>", 'hint' : 'PageUp - hot key,fast scroll forward',
  571. 'func' : scroll_fast_forward },
  572. {'name' : " >]", 'hint' : 'End - hot key, go to last frame',
  573. 'func' : scroll_to_end },
  574. {'name' : "R+", 'hint' : 'rotate the keys clockwise, hot key +',
  575. 'func' : rotate_cw },
  576. {'name' : "R-", 'hint' : 'rotate the keys counterclockwise, hot key -',
  577. 'func' : rotate_ccw }
  578. ]
  579. #btnfuncs = [ None, None, None, None, None, None ]
  580. for i in range(len( navbtns_info )):
  581. settingsWindow.appendChild( GLButton(260 + i * 32,230 ,32,20,0, [128,128,128], navbtns_info[i]['name'] , navbtns_info[i]['func'], hint = navbtns_info[i]['hint']) )
  582. helpWindow.appendChild(helpWindow_label1)
  583. settingsWindow_label1 = GLLabel(1,0, "base octave: " + str(prefs.octave))
  584. # + "\nnotes overlap: " + str(prefs.notes_overlap) + "\nignore minimal duration: " + str(prefs.ignore_minimal_duration))
  585. settingsWindow.appendChild(settingsWindow_label1)
  586. settingsWindow.appendChild( GLButton(130,0 ,20,20,1, [128,128,128], "+", raise_octave, hint = "] - hot key, move up base octave (+12 tones)" ) )
  587. settingsWindow.appendChild( GLButton(150,0 ,20,20,1, [128,128,128], " -", lower_octave, hint = "[ - hot key, move down base octave (-12 tones)" ) )
  588. settingsWindow_slider1 = GLSlider(1,40, 240,18, 0,130,prefs.keyp_delta,label="Sensitivity")
  589. settingsWindow_slider1.round=1
  590. settingsWindow.appendChild(settingsWindow_slider1)
  591. settingsWindow_slider2 = GLSlider(1,90, 240,18, 0,200,prefs.minimal_duration*100,label="Minimal note duration (sec)")
  592. settingsWindow_slider2.round=0
  593. settingsWindow.appendChild(settingsWindow_slider2)
  594. settingsWindow_slider3 = GLSlider(1,133, 240,18, 30,240,prefs.tempo,label="Output tempo for midi")
  595. settingsWindow_slider3.round=0
  596. settingsWindow.appendChild(settingsWindow_slider3)
  597. settingsWindow_slider4 = GLSlider(1,175, 240,18, 0,2,midi_file_format,label="Output midi format")
  598. settingsWindow_slider4.round=0
  599. settingsWindow.appendChild(settingsWindow_slider4)
  600. settingsWindow_slider5 = GLSlider(1,215, 240,18, 0,1000,prefs.blackkey_relative_position * 1000, update_blackkey_relative_position, label="black key relative pos")
  601. settingsWindow_slider5.round=0
  602. settingsWindow.appendChild(settingsWindow_slider5)
  603. settingsWindow_slider6 = GLSlider(1,255, 240,18, 0,1000,prefs.sync_notes_start_pos_time_delta, update_sync_notes_start_pos_time_delta, label="sync notes time delta (ms)")
  604. settingsWindow_slider6.round=0
  605. settingsWindow.appendChild(settingsWindow_slider6)
  606. settingsWindow_rollcheck_button = GLButton(260,160 ,140,22,1, [128,128,128], "roll check" ,change_rollcheck,switch=1, switch_status=prefs.rollcheck )
  607. settingsWindow.appendChild(settingsWindow_rollcheck_button)
  608. settingsWindow.appendChild( GLButton(260+141, 160 ,140,20,1, [128,128,128], "per channel save" ,change_save_to_disk_per_channel,switch=1, switch_status= prefs.save_to_disk_per_channel, hint = "split the output midi per channels" ) )
  609. settingsWindow_rollcheck_priority_button = GLButton(260,180 ,222,22,1, [128,128,128], "rollcheck white keys priority" ,change_rollcheck_priority,switch=1, switch_status=prefs.rollcheck_priority )
  610. settingsWindow.appendChild(settingsWindow_rollcheck_priority_button)
  611. # for i in range( len( keyp_colors ) ):
  612. #keyp_colormap_colors_pos.append ([ (i % 2) * 32, ( i // 2 ) * 20 ])
  613. print ('creating new colors '+str(len( prefs.keyp_colors )))
  614. sparks_slider_delta = GLSlider(6,25, 150,18, -50,150,50,update_sparks_delta, label="Sparks delta")
  615. for i in range( len( prefs.keyp_colors ) ):
  616. cx,cy = (i % 2) * 130, ( i // 2 ) * 20
  617. offsetx,offsety=4,4
  618. colorBtns.append( GLColorButton(offsetx+cx,offsety+cy ,20,20,i, prefs.keyp_colors[i], onPallete_click ) )
  619. colorWindow.appendChild(colorBtns[i])
  620. colorWindow_label1 = GLLabel(offsetx+25+cx,offsety+cy , "Ch:" + str(prefs.keyp_colors_channel[i]+1) )
  621. colorWindow_colorBtns_channel_labels.append( colorWindow_label1 )
  622. colorWindow.appendChild(colorWindow_label1)
  623. #
  624. colorWindow_colorBtns_channel_btns.append( GLButton(offsetx+cx+70,offsety+cy ,20,20,(i+1), [128,128,128], "+" ,update_channels) )
  625. colorWindow_colorBtns_channel_btns.append( GLButton(offsetx+cx+70+20,offsety+cy ,20,20,-(i+1), [128,128,128], "-" ,update_channels) )
  626. colorWindow_colorBtns_channel_btns.append( GLButton(offsetx+cx+70+40,offsety+cy ,20,20,i, [128,128,128], "x" ,disable_color, hint="ctrl+0 - shortcut, disable selected color") )
  627. for i in colorWindow_colorBtns_channel_btns:
  628. colorWindow.appendChild( i )
  629. extraWindow.appendChild( GLButton(5, 20 ,128,25,1, [128,128,128], "read colors" ,readcolors) )
  630. extraWindow.appendChild( GLButton(135,20 ,128,25,1, [128,128,128], "update color" ,updatecolor) )
  631. extraWindow.appendChild( GLButton(265,20 ,138,25,1, [128,128,128], "enable/disable" ,change_use_alternate_keys) )
  632. extraWindow.appendChild( GLButton(265,45 ,155,22,1, [96 ,96 ,128], "snap notes to grid" ,snap_notes_to_the_grid,switch=1, switch_status=use_snap_notes_to_grid) )
  633. extra_label1 = GLLabel(6,0, "Use alternate:"+str(prefs.use_alternate_keys) )
  634. extraWindow.appendChild( extra_label1 )
  635. #extra_label2 = GLLabel(0,67, "Selected key sensitivity:"+str(0) )
  636. extra_slider1 = GLSlider(6,65, 240,18, -100,100,0,update_alternate_sensitivity, label="Selected key sensitivity")
  637. #extra_slider1.showvalue=True
  638. #showvaluesinlabel=0
  639. extraWindow.appendChild(extra_slider1)
  640. extra_label3 = GLLabel( 6,90, """to select the key press ctrl + left mouse button on the key rect.
  641. to deselect the key press ctrl + left mouse button on empty space.""" )
  642. extraWindow.appendChild( extra_label3 )
  643. extraWindow_slider2 = GLSlider(5,155, 240,18, 0,2000, line_height, update_line_height, label="length of vertical key lines")
  644. extraWindow_slider2.round=0
  645. extraWindow.appendChild(extraWindow_slider2)
  646. sparks_slider_height = GLSlider(160,25, 150,18, 1,60,1,None, label="Sparks height")
  647. sparks_slider_height.round=0
  648. sparks_switch = GLButton(313,24 ,100,22,1, [128,128,128], "use sparks" ,change_use_sparks,switch=1, switch_status=prefs.use_sparks )
  649. sparksWindow.appendChild( sparks_slider_delta )
  650. sparksWindow.appendChild( sparks_slider_height )
  651. sparksWindow.appendChild( sparks_switch )
  652. #
  653. sparksWindow.appendChild( GLButton(413 ,24 ,32,22,1, [96,96,128], "y+" ,update_sparks_y_pos, hint="move sparks higher") )
  654. sparksWindow.appendChild( GLButton(413+33,24 ,32,22,1, [96,96,128], "y-" ,update_sparks_y_pos, hint="move sparks lower") )
  655. sparksWindow.appendChild( GLLabel( 6,50, "alt + up / down - move sparks label up or down " ))
  656. selected_color_delta = GLSlider(6,100, 200,18, 0,130,50,update_percolor_delta, label="percolor sensitivity")
  657. selected_color_delta.round=1
  658. use_percolor_delta = GLButton(313,100 ,190,22,1, [128,128,128], "use percolor sensitivity" ,change_use_percolor_delta,switch=1, switch_status=prefs.use_sparks )
  659. sparksWindow.appendChild( selected_color_delta )
  660. sparksWindow.appendChild( use_percolor_delta )
  661. #colorSettingsWindow.appendChild( GLButton(413 ,24 ,64,22,1, [96,96,128], "Pallette" , None ) )
  662. #
  663. #extra_slider2.showvalue=True
  664. #extra.appendChild(extra_label2)
  665. #loadsettings( settingsfile )
  666. #frame=801
  667. def getkeyp_pixel_pos( x, y ):
  668. pixx=int(prefs.xoffset_whitekeys + x)
  669. pixy=int(prefs.yoffset_whitekeys + y)
  670. if ( pixx >= width ) or ( pixy >= height ) or ( pixx < 0 ) or ( pixy < 0 ):
  671. return [-1,-1]
  672. #if ( prefs.resize == 1 ):
  673. if 1==1: #disabled
  674. pixx= int(round( pixx * ( video_width / float(width) )))
  675. pixy= int(round( pixy * ( video_height / float(height) )))
  676. if ( pixx > video_width -1 ): pixx = video_width-1
  677. if ( pixy > video_height-1 ): pixy= video_height-1
  678. return [pixx,pixy]
  679. def iswhitekey( key_num ):
  680. j = key_num % 12
  681. if (j == 1) or ( j ==3 ) or ( j == 6 ) or ( j == 8) or ( j == 10 ):
  682. return 1
  683. else:
  684. return 0
  685. def drawframe( lastimage = None):
  686. global pyfont
  687. global helptext
  688. global mousex, mousey
  689. global keyp_colormap_colors_pos
  690. global keyp_colormap_pos
  691. global frame
  692. global printed_for_frame
  693. global notes_tmp
  694. global notes_pressed_color
  695. #global old_spark_color
  696. #global cur_spark_color
  697. print_for_frame_debug = False
  698. if printed_for_frame != frame:
  699. print_for_frame_debug = True
  700. printed_for_frame = frame
  701. scale=1.0
  702. mousex, mousey = pygame.mouse.get_pos()
  703. glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
  704. glViewport (0, 0, width, height)
  705. glMatrixMode (GL_PROJECTION)
  706. glLoadIdentity ()
  707. glOrtho(0, width, height, 0, -1, 100)
  708. glMatrixMode(GL_MODELVIEW)
  709. glLoadIdentity()
  710. glDisable(GL_DEPTH_TEST)
  711. glScale(scale,scale,1)
  712. glColor4f(1.0, 1.0, 1.0, 1.0)
  713. glBindTexture(GL_TEXTURE_2D, Gl.bgImgGL)
  714. glEnable(GL_TEXTURE_2D)
  715. DrawQuad(0,0,width,height)
  716. glEnable(GL_BLEND)
  717. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
  718. glColor4f(1.0, 0.5, 1.0, 0.5)
  719. glPushMatrix()
  720. glTranslatef(prefs.xoffset_whitekeys,prefs.yoffset_whitekeys,0)
  721. glDisable(GL_TEXTURE_2D)
  722. for i in range( len( prefs.keys_pos) ):
  723. pixpos = getkeyp_pixel_pos(prefs.keys_pos[i][0],prefs.keys_pos[i][1])
  724. if (pixpos[0] == -1) and (pixpos[1] == -1):
  725. continue
  726. if lastimage is not None:
  727. keybgr=lastimage[ pixpos[1], pixpos[0] ]
  728. else:
  729. keybgr=image[ pixpos[1], pixpos[0] ]
  730. key= [ keybgr[2], keybgr[1],keybgr[0] ]
  731. keybgr=[0,0,0]
  732. sparkkey=[0,0,0]
  733. if prefs.use_sparks:
  734. sh = int(sparks_slider_height.value)
  735. if sh == 0:
  736. sh = 1
  737. for spark_y_add_pos in range (sh):
  738. sparkpixpos = getkeyp_pixel_pos(prefs.keys_pos[i][0],prefs.keyp_spark_y_pos - spark_y_add_pos )
  739. if not ((sparkpixpos[0] == -1) and (sparkpixpos[1] == -1)):
  740. keybgr = image[ sparkpixpos[1], sparkpixpos[0] ]
  741. sparkkey = [ sparkkey[0] + keybgr[2],
  742. sparkkey[1] + keybgr[1],
  743. sparkkey[2] + keybgr[0] ]
  744. sparkkey = [ sparkkey[0] / sh, sparkkey[1] / sh,sparkkey[2] / sh]
  745. #cur_spark_color[i] = sparkkey
  746. else:
  747. sparkkey = [0,0,0]
  748. note=i
  749. if ( note > 144 ):
  750. print("skip note > 144")
  751. continue
  752. keypressed=0
  753. pressedcolor=[0,0,0]
  754. if prefs.use_alternate_keys:
  755. delta = prefs.keyp_delta + prefs.keyp_colors_alternate_sensitivity[i]
  756. if ( abs( int(key[0]) - prefs.keyp_colors_alternate[i][0] ) > delta ) and ( abs( int(key[1]) - prefs.keyp_colors_alternate[i][1] ) > delta ) and ( abs( int(key[2]) - prefs.keyp_colors_alternate[i][2] ) > delta ):
  757. keypressed=1
  758. pressedcolor=prefs.keyp_colors_alternate[i]
  759. else:
  760. for key_id in range( len(prefs.keyp_colors) ):
  761. keyc = prefs.keyp_colors[key_id]
  762. spark_delta = prefs.keyp_colors_sparks_sensitivity[key_id]
  763. delta = prefs.keyp_delta
  764. if prefs.use_percolor_delta:
  765. if key_id < len( prefs.percolor_delta ):
  766. delta = prefs.percolor_delta[ key_id ]
  767. if (keyc[0] != 0 ) or (keyc[1] != 0 ) or (keyc[2] != 0 ) :
  768. if ( abs( int(key[0]) - keyc[0] ) < delta ) and ( abs( int(key[1]) - keyc[1] ) < delta ) and ( abs( int(key[2]) - keyc[2] ) < delta ):
  769. keypressed=1
  770. pressedcolor = keyc
  771. notes_pressed_color[i] = keyc
  772. if prefs.use_sparks:
  773. #unpressed_by_spark_delta = ( abs( int(sparkkey[0]) - keyc[0] ) < spark_delta ) and ( abs( int(sparkkey[1]) - keyc[1] ) < spark_delta ) and ( abs( int(sparkkey[2]) - keyc[2] ) < spark_delta )
  774. has_spark_delta = ((sparkkey[0] - keyc[0] ) > spark_delta ) or ((sparkkey[1] - keyc[1] ) > spark_delta ) or ((sparkkey[2] - keyc[2] ) > spark_delta )
  775. #unpressed_by_spark_fade = ( cur_spark_color[i][0] < old_spark_color[i][0]) and ( cur_spark_color[i][1] < old_spark_color[i][1]) and ( cur_spark_color[i][2] < old_spark_color[i][2])
  776. #unpressed_by_spark_fade_delta = ( abs( cur_spark_color[i][0] - old_spark_color[i][0]) > 20 )
  777. if print_for_frame_debug:
  778. print("note %d key_id %d spark_delta %d sparkkey vs keyc %d %d, %d %d, %d %d" % (note, key_id, spark_delta, sparkkey[0], keyc[0], sparkkey[1], keyc[1], sparkkey[2], keyc[2]))
  779. if ( not has_spark_delta ):
  780. keypressed=2
  781. notes_tmp[i] = keypressed
  782. if prefs.rollcheck:
  783. for i in range(1, len( prefs.keys_pos) -1 ):
  784. if prefs.rollcheck_priority == 0:
  785. if not iswhitekey(i):
  786. # Priority on Black keys
  787. if notes_tmp[i+1] >0: notes_tmp[i] = 0
  788. if notes_tmp[i-1] >0: notes_tmp[i] = 0
  789. else:
  790. if iswhitekey(i):
  791. # Priority on White keys
  792. if notes_tmp[i+1] >0: notes_tmp[i] = 0
  793. if notes_tmp[i-1] >0: notes_tmp[i] = 0
  794. for i in range( len( prefs.keys_pos) ):
  795. keypressed = notes_tmp[i]
  796. pressedcolor = notes_pressed_color[i]
  797. glPushMatrix()
  798. glTranslatef(prefs.keys_pos[i][0],prefs.keys_pos[i][1],0)
  799. glColor4f(1,1,1,0.5)
  800. if iswhitekey(i):
  801. glColor4f(0.57,0.57,0.57,0.55)
  802. DrawQuad(-0.5,-line_height,0.5, line_height )
  803. if ( keypressed != 0 ):
  804. #glColor4f(1.0, 0.5, 1.0, 0.9)
  805. glColor4f(pressedcolor[0]/255.0,pressedcolor[1]/255.0,pressedcolor[2]/255.0,0.9)
  806. DrawQuad(-6,-7,6,7)
  807. glColor4f(0,0,0,1)
  808. if ( keypressed == 1):
  809. DrawRect(-7,-9,7,9,3)
  810. else:
  811. DrawRect(-5,-7,5,7,3)
  812. else:
  813. glColor4f(0,0,0,1)
  814. DrawRect(-7,-7,7,7,1)
  815. glColor4f(0.5, 1, 1.0, 0.7)
  816. DrawQuad(-5,-5,5,5)
  817. if ( lastkeygrabid == i ):
  818. glColor4f(0.0, 0.5, 1.0, 0.7)
  819. DrawQuad(-4,-4,4,4)
  820. if ( separate_note_id == i ):
  821. glColor4f(0,1,0,1)
  822. DrawRect(-7,-12,7,12,2)
  823. if prefs.octave * 12 == i:
  824. glColor4f(1,0,0,1)
  825. DrawRect(-9,9,9,12,3)
  826. DrawQuad(-1,-1,1,1)
  827. glPopMatrix()
  828. glColor4f(0.0, 1.0, 1.0, 0.7)
  829. # Sparks
  830. if prefs.use_sparks:
  831. glPushMatrix()
  832. glTranslatef(prefs.keys_pos[i][0], prefs.keyp_spark_y_pos ,0)
  833. glColor4f(0.5, 1, 1.0, 0.7)
  834. DrawQuad(-1,-1,1,1)
  835. DrawQuad(-0.5,-sparks_slider_height.value ,0.5,0)
  836. glPopMatrix()
  837. glPopMatrix()
  838. glDisable(GL_BLEND)
  839. glDisable(GL_TEXTURE_2D)
  840. for i in range(len(glwindows)):
  841. glwindows[i].draw()
  842. # drawing hints over all windows
  843. for i in range(len(glwindows)):
  844. glwindows[i].drawhint()
  845. prefs.keyp_delta = int(settingsWindow_slider1.value)
  846. prefs.minimal_duration = settingsWindow_slider2.value *0.01
  847. prefs.tempo = int(settingsWindow_slider3.value)
  848. settingsWindow_label1.text = "base octave: " + str(prefs.octave)
  849. # + "\nnotes overlap: " + str(prefs.notes_overlap) + "\nignore minimal duration: " + str(prefs.ignore_minimal_duration)
  850. #settingsWindow_label2.text = "Sensitivity:"+str(keyp_delta)+"\n\nMinimal note duration (sec):"+format(minimal_duration,'.2f' ) + "\n\nOutput tempo for midi:" + str(tempo)
  851. for i in range(len(prefs.keyp_colors)):
  852. colorBtns[i].color = prefs.keyp_colors[i]
  853. glPushMatrix()
  854. glTranslatef(mousex,mousey,0)
  855. glColor4f(0.2, 0.5, 1, 0.9)
  856. DrawQuad(-1,-1,1,1)
  857. glPopMatrix()
  858. if showoutputpath > time.time():
  859. drawHint( width *0.5, height -20, prefs.save_to_disk_message, True)
  860. def processmidi():
  861. global frame
  862. global width
  863. global height
  864. global length
  865. global fps
  866. global notes
  867. global notes_db
  868. global notes_de
  869. global notes_channel
  870. global success,image
  871. global separate_note_id
  872. global outputmid
  873. global basenote
  874. print("video " + str(width) + "x" + str(height))
  875. basenote = prefs.octave * 12
  876. mf = midinotes( int(midi_file_format))
  877. track = 0 # the only track
  878. time = 0 # start at the beginning
  879. mf.setup_track(time, prefs.miditrackname, prefs.tempo)
  880. first_note_time=0
  881. channel_has_note = [ 0 for x in range(16) ]
  882. for i in range(len(prefs.keyp_colors_channel)):
  883. mf.addProgramChange(track, prefs.keyp_colors_channel[i], prefs.keyp_colors_channel_prog[i])
  884. print("starting from frame:" + str(prefs.startframe))
  885. getFrame( prefs.startframe )
  886. notecnt=0
  887. lastimage = image
  888. while success:
  889. if (frame % 10 == 0):
  890. glBindTexture(GL_TEXTURE_2D, Gl.bgImgGL)
  891. if (frame % 200 == 0):
  892. loadImage(frame)
  893. lastimage = image
  894. #glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
  895. #glTexImage2D(GL_TEXTURE_2D, 0, 3, video_width, video_height, 0, GL_BGR, GL_UNSIGNED_BYTE, image )
  896. #glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST)
  897. glEnable(GL_TEXTURE_2D)
  898. drawframe( lastimage )
  899. glColor4f(1.0, 0.5, 1.0, 0.5)
  900. glDisable(GL_TEXTURE_2D)
  901. p= frame / float( length )
  902. DrawQuad(0,height *0.5 -10, p * width ,height *0.5 +10)
  903. # glPopMatrix();:
  904. pygame.display.flip()
  905. # if (frame % 100 == 0):
  906. print("processing frame: " + str(frame) + " / " + str(length) + " % " + str( math.trunc(p * 100)))
  907. # if ( resize == 1 ):
  908. # image=cv2.resize(image, (width , height))
  909. # processing white keys
  910. for i in range( len(prefs.keys_pos) ):
  911. pixpos = getkeyp_pixel_pos(prefs.keys_pos[i][0],prefs.keys_pos[i][1])
  912. if (pixpos[0] == -1) and (pixpos[1] == -1):
  913. continue
  914. keybgr=image[ pixpos[1], pixpos[0] ]
  915. key= [ keybgr[2], keybgr[1],keybgr[0] ]
  916. keybgr=[0,0,0]
  917. sparkkey=[0,0,0]
  918. if prefs.use_sparks:
  919. sh = int(sparks_slider_height.value)
  920. if sh == 0:
  921. sh = 1
  922. for spark_y_add_pos in range (sh):
  923. sparkpixpos = getkeyp_pixel_pos(prefs.keys_pos[i][0],prefs.keyp_spark_y_pos - spark_y_add_pos )
  924. if not ((sparkpixpos[0] == -1) and (sparkpixpos[1] == -1)):
  925. keybgr = image[ sparkpixpos[1], sparkpixpos[0] ]
  926. sparkkey = [ sparkkey[0] + keybgr[2],
  927. sparkkey[1] + keybgr[1],
  928. sparkkey[2] + keybgr[0] ]
  929. sparkkey = [ sparkkey[0] / sh, sparkkey[1] / sh,sparkkey[2] / sh]
  930. else:
  931. sparkkey = [0,0,0]
  932. note=i
  933. if ( note > 144 ):
  934. print("skip note > 144")
  935. continue
  936. keypressed=0
  937. note_channel=0
  938. # deltaclr = abs( int(key[0]) - keyp_colors[0][0] ) + abs( int(key[1]) - keyp_colors[0][1] ) + abs( int(key[2]) - keyp_colors[0][2] )
  939. deltaclr = prefs.keyp_delta*prefs.keyp_delta*prefs.keyp_delta
  940. deltaid = 0
  941. if prefs.use_alternate_keys:
  942. delta = prefs.keyp_delta + prefs.keyp_colors_alternate_sensitivity[i]
  943. if ( abs( int(key[0]) - prefs.keyp_colors_alternate[i][0] ) > delta ) and ( abs( int(key[1]) - prefs.keyp_colors_alternate[i][1] ) > delta ) and ( abs( int(key[2]) - prefs.keyp_colors_alternate[i][2] ) > delta ):
  944. keypressed = 1
  945. pressedcolor = prefs.keyp_colors_alternate[i]
  946. else:
  947. for j in range(len(prefs.keyp_colors)):
  948. delta = prefs.keyp_delta
  949. if prefs.use_percolor_delta:
  950. if j < len( prefs.percolor_delta ):
  951. delta = prefs.percolor_delta[ j ]
  952. deltaclr = delta*delta*delta
  953. if (prefs.keyp_colors[j][0] != 0 ) or ( prefs.keyp_colors[j][1] != 0 ) or ( prefs.keyp_colors[j][2] != 0 ):
  954. if ( abs( int(key[0]) - prefs.keyp_colors[j][0] ) < delta ) and ( abs( int(key[1]) - prefs.keyp_colors[j][1] ) < delta ) and ( abs( int(key[2]) - prefs.keyp_colors[j][2] ) < delta ):
  955. delta = abs( int(key[0]) - prefs.keyp_colors[j][0] ) + abs( int(key[1]) - prefs.keyp_colors[j][1] ) + abs( int(key[2]) - prefs.keyp_colors[j][2] )
  956. if ( delta < deltaclr ):
  957. deltaclr = delta
  958. deltaid = j
  959. keypressed=1
  960. if prefs.use_sparks:
  961. has_spark_delta = ((sparkkey[0] - prefs.keyp_colors[j][0] ) > prefs.keyp_colors_sparks_sensitivity[j] ) or ((sparkkey[1] - prefs.keyp_colors[j][1] ) > prefs.keyp_colors_sparks_sensitivity[j] ) or ((sparkkey[2] - prefs.keyp_colors[j][2] ) > prefs.keyp_colors_sparks_sensitivity[j] )
  962. #if ( abs( int(sparkkey[0]) - keyp_colors[j][0] ) < keyp_colors_sparks_sensitivity[j] ) and ( abs( int(sparkkey[1]) - keyp_colors[j][1] ) < keyp_colors_sparks_sensitivity[j] ) and ( abs( int(sparkkey[2]) - keyp_colors[j][2] ) < keyp_colors_sparks_sensitivity[j] ):
  963. if ( not has_spark_delta ):
  964. keypressed=0
  965. #
  966. if ( keypressed != 0 ):
  967. note_channel=prefs.keyp_colors_channel[ deltaid ]
  968. if ( prefs.debug == 1 ):
  969. if (keypressed == 1 ):
  970. cv2.rectangle(image, (pixx-5,pixy-5), (pixx+5,pixy+5), (128,128,255), -1 )
  971. cv2.putText(image, str(note_channel), (pixx-5,pixy-10), 0, 0.3, (64,128,255))
  972. # cv2.rectangle(image, (pixx-5,pixy-5), (pixx+5,pixy+5), (255,0,255))
  973. cv2.rectangle(image, (pixx-1,pixy-1), (pixx+1,pixy+1), (255,0,255))
  974. # cv2.putText(image, str(note), (pixx-5,pixy+20), 0, 0.5, (255,0,255))
  975. # reg pressed key; when keypressed==2 and previous keypressed state is 0 or 2 we should also goes here
  976. if keypressed==1 or (keypressed==2 and notes[note] != 1):
  977. # if key is not pressed
  978. if ( notes[note] == 0 ):
  979. if ( debug_keys == 1 ):
  980. print("note pressed on :" + str( note ))
  981. notes_db[ note ] = frame
  982. if (first_note_time == 0):
  983. first_note_time = frame / fps
  984. notes_channel[ note ] = note_channel
  985. if ( separate_note_id != -1 ):
  986. if ( separate_note_id < note ):
  987. notes_channel[ note ] = 0
  988. else:
  989. notes_channel[ note ] = 1
  990. # always update to last press state
  991. notes[ note ] = keypressed
  992. notes_tmp[ note] = keypressed
  993. # save fall notes and then we can check for a near keys with priority...
  994. if prefs.rollcheck:
  995. for i in range(1, len( prefs.keys_pos)-1 ):
  996. if notes[ i ] != 0:
  997. if prefs.rollcheck_priority == 0:
  998. if not iswhitekey(i):
  999. # Priority on Black keys
  1000. if notes[i+1] >0 and notes_tmp[i] >0: notes[i] = 0
  1001. if notes[i-1] >0 and notes_tmp[i] >0: notes[i] = 0
  1002. else:
  1003. if iswhitekey(i):
  1004. # Priority on White keys
  1005. if notes[i+1] >0 and notes_tmp[i] >0: notes[i] = 0
  1006. if notes[i-1] >0 and notes_tmp[i] >0: notes[i] = 0
  1007. #
  1008. for i in range( len( prefs.keys_pos) ):
  1009. note=i
  1010. keypressed = notes[ note ]
  1011. if notes_tmp[ i ] != 0:
  1012. if ( notes[note] != 0 ) and ( notes_channel[ note ] != note_channel ) and ( prefs.notes_overlap == 1 ):
  1013. # case if one key over other
  1014. time = notes_db[note] / fps
  1015. duration = ( frame - notes_db[note] ) / fps
  1016. if (use_snap_notes_to_grid == 1):
  1017. #print ("1 time:", time , "first_note_time:",first_note_time)
  1018. time = snap_to_grid( time - first_note_time , notes_grid_size ) + 1
  1019. duration = snap_to_grid( duration , notes_grid_size )
  1020. #print ("1 time after:", time , "after before:",duration)
  1021. ignore = 0
  1022. if ( duration < prefs.minimal_duration ):
  1023. if ( debug_keys == 1 ):
  1024. print(" duration:" + str(duration) + " < minimal_duration:" + str(prefs.minimal_duration))
  1025. duration = prefs.minimal_duration
  1026. if ( prefs.ignore_minimal_duration == 1 ):
  1027. ignore=1
  1028. if ( debug_keys == 1 ):
  1029. print("keys (one over other), note released :" + str(note) + " de = " + str(notes_de[note]) + "- db =" + str(notes_db[note]))
  1030. print("midi add white keys, note : " +str(note) + " time:" +str(time) + " duration:" + str(duration))
  1031. if ( not ignore ):
  1032. mf.addNote(track, notes_channel[note] , basenote + note, time * prefs.tempo / 60.0 , duration * prefs.tempo / 60.0 , volume )
  1033. channel_has_note[ note_channel ] = 1
  1034. notecnt+=1
  1035. notes_db[ note ] = frame
  1036. notes_channel[ note ] = note_channel
  1037. else:
  1038. # if key been presed and released: two cases goes here keypressed==0 or (keypressed==2 and previous state is keypressed==1)
  1039. if ( notes[note] != 0):
  1040. notes[ note ] = 0
  1041. notes_de[ note ] = frame
  1042. time = notes_db[note] / fps
  1043. duration = ( notes_de[note] - notes_db[note] ) / fps
  1044. if (use_snap_notes_to_grid):
  1045. if (first_note_time == 0):
  1046. first_note_time = time
  1047. #print ("2 time:", time , "first_note_time:",first_note_time)
  1048. time = snap_to_grid( time - first_note_time , notes_grid_size ) + 1
  1049. duration = snap_to_grid( duration , notes_grid_size )
  1050. ignore=0
  1051. if ( duration < prefs.minimal_duration ):
  1052. if ( debug_keys == 1 ):
  1053. print(" duration:" + str(duration) + " < minimal_duration:" + str(prefs.minimal_duration))
  1054. duration = prefs.minimal_duration
  1055. if ( prefs.ignore_minimal_duration == 1 ):
  1056. ignore=1
  1057. if ( debug_keys == 1 ):
  1058. print("keys, note released :" + str(note ) + " de = " + str(notes_de[note]) + "- db =" + str(notes_db[note]))
  1059. print("midi add white keys, note : " +str(note) + " time:" +str(time) + " duration:" + str(duration))
  1060. if ( not ignore ):
  1061. mf.addNote(track, notes_channel[note] , basenote+ note, time * prefs.tempo / 60.0 , duration * prefs.tempo / 60.0 , volume )
  1062. channel_has_note[ note_channel ] = 1
  1063. notecnt+=1
  1064. # coming here when use sparks is true and previous state is keypressed==1. We consider the key is released and then pressed again
  1065. if (keypressed==2):
  1066. notes[ note ] = keypressed
  1067. notes_db[ note ] = frame
  1068. notes_channel[ note ] = note_channel
  1069. xapp=0
  1070. if ( prefs.debug == 1 ):
  1071. cv2.imwrite("/tmp/frame%d.jpg" % frame, image) # save frame as JPEG file
  1072. # success,image = vidcap.read()
  1073. getFrame()
  1074. frame += 1
  1075. framerate()
  1076. if ( frame > endframe ):
  1077. success = False
  1078. for event in pygame.event.get():
  1079. if event.type == pygame.QUIT:
  1080. success = False
  1081. pygame.quit()
  1082. quit()
  1083. elif event.type == pygame.KEYDOWN:
  1084. if event.key == pygame.K_SPACE:
  1085. success = False
  1086. if event.key == pygame.K_ESCAPE:
  1087. running = 0
  1088. pygame.quit()
  1089. quit()
  1090. print("saved notes: " + str(notecnt))
  1091. #search free id for name ...
  1092. fileid=0
  1093. while os.path.exists( outputmid ):
  1094. outputmid = ntpath.basename( filepath ) + "_"+str(fileid)+ "_output.mid"
  1095. fileid+=1
  1096. if ( fileid > 999 ): break
  1097. if prefs.sync_notes_start_pos:
  1098. mf.sync_start_pos(prefs.sync_notes_start_pos_time_delta, False)
  1099. if prefs.save_to_disk_per_channel:
  1100. status, prefs.save_to_disk_message = mf.save_to_disk_per_channel(outputmid)
  1101. else:
  1102. status, prefs.save_to_disk_message = mf.save_to_disk(outputmid)
  1103. return status
  1104. def doinit():
  1105. doinitGl()
  1106. loadImage()
  1107. GenFontTexture()
  1108. def reconstruct():
  1109. global frame
  1110. global showoutputpath
  1111. helpWindow.hidden=1
  1112. frame=prefs.startframe
  1113. t1 = datetime.datetime.now()
  1114. processmidi()
  1115. t2 = datetime.datetime.now()
  1116. print(""" processing time: {} / {} = {}; """.format( t1,t2, t2-t1 ))
  1117. frame=prefs.startframe
  1118. getFrame(frame)
  1119. showoutputpath = time.time() + 5
  1120. def main():
  1121. global pyfont
  1122. global mousex, mousey
  1123. global keyp_colormap_colors_pos
  1124. global keyp_colormap_pos
  1125. global success,image
  1126. global endframe
  1127. global basenote
  1128. global glwindows
  1129. global separate_note_id
  1130. global frame
  1131. global width,height
  1132. global screen
  1133. global lastkeygrabid
  1134. global running
  1135. #global old_spark_color, cur_spark_color
  1136. keygrab=0
  1137. keygrabid=-1
  1138. keygrabaddx=0
  1139. lastkeygrabid=-1
  1140. #pyfont = pygame.font.SysFont('Sans', 20)
  1141. #pygame.RESIZABLE
  1142. screen = pygame.display.set_mode( (width,height) , DOUBLEBUF|OPENGL|pygame.RESIZABLE)
  1143. pygame.display.set_caption(filepath)
  1144. doinit()
  1145. clock = pygame.time.Clock()
  1146. #
  1147. # set start frame
  1148. vidcap.set(CAP_PROP_POS_FRAMES, frame)
  1149. while running==1:
  1150. mouseOnWindows = False
  1151. # mousex, mousey = pygame.mouse.get_pos()
  1152. drawframe()
  1153. mods = pygame.key.get_mods()
  1154. for event in pygame.event.get():
  1155. if event.type == pygame.QUIT:
  1156. running = 0
  1157. pygame.quit()
  1158. quit()
  1159. elif event.type == pygame.VIDEORESIZE:
  1160. prefs.resize = 1
  1161. prefs.resize_width = event.w
  1162. prefs.resize_height = event.h
  1163. width = prefs.resize_width
  1164. height = prefs.resize_height
  1165. screen = pygame.display.set_mode( (width,height) , DOUBLEBUF|OPENGL|pygame.RESIZABLE)
  1166. elif event.type == pygame.KEYUP:
  1167. for wnd in glwindows:
  1168. wnd.update_key_up(event.key)
  1169. elif event.type == pygame.KEYDOWN:
  1170. for wnd in glwindows:
  1171. wnd.update_key_down(event.key)
  1172. # print event.key
  1173. if event.key == pygame.K_q:
  1174. if prefs.autoclose == 1:
  1175. running = 0
  1176. else:
  1177. reconstruct()
  1178. if event.key == pygame.K_o:
  1179. #prefs.notes_overlap = not prefs.notes_overlap
  1180. switch_notes_overlap(None)
  1181. if event.key == pygame.K_i:
  1182. #prefs.ignore_minimal_duration = not prefs.ignore_minimal_duration
  1183. switch_ignore_notes_with_minimal_duration(None)
  1184. if event.key == pygame.K_s:
  1185. if mods & pygame.KMOD_SHIFT:
  1186. prefs.startframe = 0
  1187. else:
  1188. prefs.startframe = int(round(vidcap.get(1)))
  1189. print("set start frame = "+ str(prefs.startframe))
  1190. if event.key == pygame.K_e:
  1191. if mods & pygame.KMOD_SHIFT:
  1192. endframe = length
  1193. else:
  1194. endframe = int(round(vidcap.get(1)))
  1195. print("set end frame = "+ str(endframe))
  1196. if event.key == pygame.K_ESCAPE:
  1197. running = 0
  1198. pygame.quit()
  1199. quit()
  1200. if event.key == pygame.K_F2:
  1201. btndown_save_settings(None)
  1202. if event.key == pygame.K_F3:
  1203. btndown_load_settings(None)
  1204. if event.key == pygame.K_F4:
  1205. for i in range(len(glwindows)):
  1206. # if isinstance(glwindows[i],GLButton):
  1207. # continue
  1208. glwindows[i].x = mousex;
  1209. glwindows[i].y = mousey;
  1210. if event.key == pygame.K_r:
  1211. switch_resize_windows(None)
  1212. if event.key == pygame.K_RIGHTBRACKET:
  1213. raise_octave()
  1214. if event.key == pygame.K_LEFTBRACKET:
  1215. lower_octave()
  1216. if event.key == pygame.K_PLUS or event.key == pygame.K_KP_PLUS or event.key == pygame.K_EQUALS:
  1217. prefs.keys_angle -= 5
  1218. updatekeys()
  1219. if event.key == pygame.K_MINUS or event.key == pygame.K_KP_MINUS:
  1220. prefs.keys_angle += 5
  1221. updatekeys()
  1222. if event.key == pygame.K_UP:
  1223. if mods & pygame.KMOD_ALT:
  1224. prefs.keyp_spark_y_pos -= 1
  1225. else:
  1226. if mods & pygame.KMOD_SHIFT:
  1227. prefs.yoffset_blackkeys -= 1
  1228. else:
  1229. prefs.yoffset_blackkeys -= 2
  1230. updatekeys( )
  1231. if event.key == pygame.K_DOWN:
  1232. if mods & pygame.KMOD_ALT:
  1233. prefs.keyp_spark_y_pos += 1
  1234. else:
  1235. if mods & pygame.KMOD_SHIFT:
  1236. prefs.yoffset_blackkeys += 1
  1237. else:
  1238. prefs.yoffset_blackkeys += 2
  1239. updatekeys( )
  1240. if event.key == pygame.K_TAB:
  1241. showOrhideallwindows(None)
  1242. if event.key == pygame.K_LEFT:
  1243. if mods & pygame.KMOD_SHIFT:
  1244. prefs.whitekey_width-=0.1
  1245. else:
  1246. prefs.whitekey_width-=1.0
  1247. updatekeys( )
  1248. if event.key == pygame.K_RIGHT:
  1249. if mods & pygame.KMOD_SHIFT:
  1250. prefs.whitekey_width+=0.1
  1251. else:
  1252. prefs.whitekey_width+=1.0
  1253. updatekeys( )
  1254. if event.key == pygame.K_HOME:
  1255. scroll_to_start(None)
  1256. if event.key == pygame.K_END:
  1257. scroll_to_end(None)
  1258. if event.key == pygame.K_0:
  1259. if mods & pygame.KMOD_CTRL and Gl.keyp_colormap_id != -1:
  1260. prefs.keyp_colors[Gl.keyp_colormap_id][0] = 0
  1261. prefs.keyp_colors[Gl.keyp_colormap_id][1] = 0
  1262. prefs.keyp_colors[Gl.keyp_colormap_id][2] = 0
  1263. if event.key == pygame.K_PAGEUP:
  1264. if mods & pygame.KMOD_SHIFT:
  1265. scroll_forward_by_frame(None)
  1266. else:
  1267. scroll_fast_forward(None)
  1268. if event.key == pygame.K_PAGEDOWN:
  1269. if mods & pygame.KMOD_SHIFT:
  1270. scroll_prev_by_frame(None)
  1271. else:
  1272. scroll_fast_prev(None)
  1273. if event.key == pygame.K_p:
  1274. size=5
  1275. separate_note_id=-1
  1276. for i in range( len( prefs.keys_pos) ):
  1277. if (abs( mousex - (prefs.keys_pos[i][0] + prefs.xoffset_whitekeys) )< size) and (abs( mousey - (prefs.keys_pos[i][1] + prefs.yoffset_whitekeys) )< size):
  1278. separate_note_id=i
  1279. pass
  1280. #
  1281. elif event.type == pygame.MOUSEBUTTONUP:
  1282. for i in range( len(glwindows)-1, -1 , -1):
  1283. #print("process mouse up on windiws id: ", i)
  1284. if glwindows[i].update_mouse_up(mousex,mousey,event.button) == 1:
  1285. mouseOnWindows=True
  1286. resort=True
  1287. break
  1288. #
  1289. if ( event.button == 1 ):
  1290. keygrab = 0
  1291. keygrabid = -1
  1292. if ( event.button == 3 ):
  1293. keygrab = 0
  1294. #
  1295. elif event.type == pygame.MOUSEBUTTONDOWN:
  1296. resort=False
  1297. for i in range( len(glwindows)-1, -1 , -1):
  1298. #print("process mouse down on windiws id: ", i)
  1299. if glwindows[i].update_mouse_down(mousex,mousey,event.button) == 1:
  1300. mouseOnWindows=True
  1301. resort=True
  1302. break
  1303. if resort:
  1304. glwindows.sort(key=lambda x: x.active, reverse=False)
  1305. # print event.button
  1306. if ( event.button == 4 ):
  1307. prefs.whitekey_width+=0.05
  1308. # print "whitekey_width="+str(whitekey_width)
  1309. updatekeys( )
  1310. # scale+=0.1
  1311. if ( event.button == 5 ):
  1312. prefs.whitekey_width-=0.05
  1313. # print "whitekey_width="+str(whitekey_width)
  1314. updatekeys( )
  1315. #
  1316. if ( event.button == 1 ):
  1317. if mods & pygame.KMOD_CTRL and Gl.keyp_colormap_id != -1:
  1318. pixx = int(mousex)
  1319. pixy = int(mousey)
  1320. if not (( pixx >= width ) or ( pixy >= height ) or ( pixx < 0 ) or ( pixy < 0 )):
  1321. if ( prefs.resize == 1 ):
  1322. pixx= int(round( pixx * ( video_width / float(prefs.resize_width) )))
  1323. pixy= int(round( pixy * ( video_height / float(prefs.resize_height) )))
  1324. if ( pixx > video_width -1 ): pixx = video_width-1
  1325. if ( pixy > video_height-1 ): pixy = video_height-1
  1326. print("original mouse x:"+str(mousex) + "x" +str(mousey) + " mapped :" +str(pixx) +"x"+str(pixy))
  1327. keybgr=image[pixy,pixx]
  1328. prefs.keyp_colors[Gl.keyp_colormap_id][0] = keybgr[2]
  1329. prefs.keyp_colors[Gl.keyp_colormap_id][1] = keybgr[1]
  1330. prefs.keyp_colors[Gl.keyp_colormap_id][2] = keybgr[0]
  1331. else:
  1332. # if not (mods & pygame.KMOD_CTRL):
  1333. if not colorWindow.active and not mouseOnWindows:
  1334. Gl.keyp_colormap_id = -1
  1335. #keyp_colormap_id = -1
  1336. pass
  1337. #
  1338. size=5
  1339. if (mods & pygame.KMOD_CTRL):
  1340. lastkeygrabid=-1
  1341. for i in range( len( prefs.keys_pos) ):
  1342. if (abs( mousex - (prefs.keys_pos[i][0] + prefs.xoffset_whitekeys) )< size) and (abs( mousey - (prefs.keys_pos[i][1] + prefs.yoffset_whitekeys) )< size):
  1343. keygrab=1
  1344. if not ( mods & pygame.KMOD_CTRL ):
  1345. keygrabid=i
  1346. lastkeygrabid=i
  1347. extra_slider1.setvalue( prefs.keyp_colors_alternate_sensitivity[i] )
  1348. print("ok click found on : "+str(keygrabid))
  1349. break
  1350. pass
  1351. # if ( event.button == 2 ):
  1352. # lastkeygrabid=-1
  1353. if ( event.button == 3 ):
  1354. keygrab = 2
  1355. size=5
  1356. print("x offset " + str(prefs.xoffset_whitekeys) + " y offset: " +str(prefs.yoffset_whitekeys))
  1357. keygrabaddx=0
  1358. for i in range( len( prefs.keys_pos) ):
  1359. if (abs( mousex - (prefs.keys_pos[i][0] + prefs.xoffset_whitekeys) )< size) and (abs( mousey - (prefs.keys_pos[i][1] + prefs.yoffset_whitekeys) )< size):
  1360. keygrab=2
  1361. keygrabaddx=prefs.keys_pos[i][0]
  1362. print("ok click found on : "+str(keygrabid))
  1363. break
  1364. if ( keygrab == 1) and ( keygrabid >-1 ):
  1365. # print "moving keyid = " + str(keygrabid)
  1366. prefs.keys_pos[ keygrabid ][0] = mousex - prefs.xoffset_whitekeys
  1367. prefs.keys_pos[ keygrabid ][1] = mousey - prefs.yoffset_whitekeys
  1368. if ( keygrab == 2):
  1369. # print "moving offsets : "+ str(mousex) + " x " + str(mousey)
  1370. prefs.xoffset_whitekeys = mousex - keygrabaddx
  1371. prefs.yoffset_whitekeys = mousey
  1372. for wnd in glwindows:
  1373. wnd.update_mouse_move(mousex,mousey)
  1374. pygame.display.flip()
  1375. #framerate()
  1376. #limit fps to 60 and get the frame time in milliseconds
  1377. ms = clock.tick(60)
  1378. main()
  1379. if prefs.autoclose == 1:
  1380. reconstruct()
  1381. print ("done...")