v2m.py 58 KB

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