picadae calibration programs
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

plot_seq_1.py 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. ##| Copyright: (C) 2019-2020 Kevin Larke <contact AT larke DOT org>
  2. ##| License: GNU GPL version 3.0 or above. See the accompanying LICENSE file.
  3. import os, sys,json
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. from common import parse_yaml_cfg
  7. import rms_analysis
  8. import elbow
  9. def fit_to_reference( pkL, refTakeId ):
  10. us_outL = []
  11. db_outL = []
  12. dur_outL = []
  13. tid_outL = []
  14. dbL,usL,durMsL,takeIdL = tuple(zip(*pkL))
  15. us_refL,db_refL,dur_refL = zip(*[(usL[i],dbL[i],durMsL[i]) for i in range(len(usL)) if takeIdL[i]==refTakeId])
  16. for takeId in set(takeIdL):
  17. us0L,db0L,dur0L = zip(*[(usL[i],dbL[i],durMsL[i]) for i in range(len(usL)) if takeIdL[i]==takeId ])
  18. if takeId == refTakeId:
  19. db_outL += db0L
  20. else:
  21. db1V = elbow.fit_points_to_reference(us0L,db0L,us_refL,db_refL)
  22. if db1V is not None:
  23. db_outL += db1V.tolist()
  24. us_outL += us0L
  25. dur_outL+= dur0L
  26. tid_outL+= [takeId] * len(us0L)
  27. return zip(db_outL,us_outL,dur_outL,tid_outL)
  28. def get_merged_pulse_db_measurements( inDir, midi_pitch, analysisArgsD, takeId=None ):
  29. inDir = os.path.join(inDir,"%i" % (midi_pitch))
  30. takeIdL = []
  31. if takeId is not None:
  32. takeIdL.append(takeId)
  33. else:
  34. takeDirL = os.listdir(inDir)
  35. # for each take in this directory
  36. for take_folder in takeDirL:
  37. takeIdL.append(int(take_folder))
  38. pkL = []
  39. refTakeId = None
  40. usRefL = None
  41. dbRefL = None
  42. # for each take in this directory
  43. for take_number in takeIdL:
  44. if refTakeId is None:
  45. refTakeId = take_number
  46. # analyze this takes audio and locate the note peaks
  47. r = rms_analysis.rms_analysis_main( os.path.join(inDir,str(take_number)), midi_pitch, **analysisArgsD )
  48. # store the peaks in pkL[ (db,us) ]
  49. for db,us,stats in zip(r.pkDbL,r.pkUsL,r.statsL):
  50. pkL.append( (db,us,stats.durMs,take_number) )
  51. pkUsL = []
  52. pkDbL = []
  53. durMsL = []
  54. takeIdL = []
  55. holdDutyPctL = []
  56. if refTakeId is None:
  57. print("No valid data files at %s pitch:%i" % (inDir,midi_pitch))
  58. else:
  59. pkL = fit_to_reference( pkL, refTakeId )
  60. # sort the peaks on increasing attack pulse microseconds
  61. pkL = sorted( pkL, key= lambda x: x[1] )
  62. # merge sample points that separated by less than 'minSampleDistUs' milliseconds
  63. #pkL = merge_close_sample_points( pkL, analysisArgsD['minSampleDistUs'] )
  64. # split pkL
  65. pkDbL,pkUsL,durMsL,takeIdL = tuple(zip(*pkL))
  66. return pkUsL,pkDbL,durMsL,takeIdL,r.holdDutyPctL
  67. def select_resample_reference_indexes( noiseIdxL ):
  68. resampleIdxS = set()
  69. # for each noisy sample index store that index and the index
  70. # before and after it
  71. for i in noiseIdxL:
  72. resampleIdxS.add( i )
  73. if i+1 < len(noiseIdxL):
  74. resampleIdxS.add( i+1 )
  75. if i-1 >= 0:
  76. resampleIdxS.add( i-1 )
  77. resampleIdxL = list(resampleIdxS)
  78. # if a single sample point is left out of a region of
  79. # contiguous sample points then include this as a resample point also
  80. for i in resampleIdxL:
  81. if i + 1 not in resampleIdxL and i + 2 in resampleIdxL: # BUG BUG BUG: Hardcoded constant
  82. if i+1 < len(noiseIdxL):
  83. resampleIdxL.append(i+1)
  84. return resampleIdxL
  85. def locate_resample_regions( usL, dbL, resampleIdxL ):
  86. # locate regions of points to resample
  87. regionL = [] # (bi,ei)
  88. inRegionFl = False
  89. bi = None
  90. for i in range(len(usL)):
  91. if inRegionFl:
  92. if i not in resampleIdxL:
  93. regionL.append((bi,i-1))
  94. inRegionFl = False
  95. bi = None
  96. else:
  97. if i in resampleIdxL:
  98. inRegionFl = True
  99. bi = i
  100. if bi is not None:
  101. regionL.append((bi,len(usL)-1))
  102. # select points around and within the resample regions
  103. # to resample
  104. reUsL = []
  105. reDbL = []
  106. for bi,ei in regionL:
  107. for i in range(bi,ei+2):
  108. if i == 0:
  109. us = usL[i]
  110. db = dbL[i]
  111. elif i >= len(usL):
  112. us = usL[i-1]
  113. db = dbL[i-1]
  114. else:
  115. us = usL[i-1] + (usL[i]-usL[i-1])/2
  116. db = dbL[i-1] + (dbL[i]-dbL[i-1])/2
  117. reUsL.append(us)
  118. reDbL.append(db)
  119. return reUsL,reDbL
  120. def get_dur_skip_indexes( durMsL, dbL, takeIdL, scoreL, minDurMs, minDb, noiseLimitPct ):
  121. firstAudibleIdx = None
  122. firstNonSkipIdx = None
  123. # get the indexes of samples which do not meet the duration, db level, or noise criteria
  124. skipIdxL = [ i for i,(ms,db,score) in enumerate(zip(durMsL,dbL,scoreL)) if ms < minDurMs or db < minDb or score > noiseLimitPct ]
  125. # if a single sample point is left out of a region of
  126. # contiguous skipped points then skip this point also
  127. for i in range(len(durMsL)):
  128. if i not in skipIdxL and i-1 in skipIdxL and i+1 in skipIdxL:
  129. skipIdxL.append(i)
  130. # find the first set of 3 contiguous samples that
  131. # are greater than minDurMs - all samples prior
  132. # to these will be skipped
  133. xL = []
  134. for i in range(len(durMsL)):
  135. if i in skipIdxL:
  136. xL = []
  137. else:
  138. xL.append(i)
  139. if len(xL) == 3: # BUG BUG BUG: Hardcoded constant
  140. firstAudibleIdx = xL[0]
  141. break
  142. # decrease by one decibel to locate the first non-skip
  143. # TODO: what if no note exists that is one decibel less
  144. # The recordings of very quiet notes do not give reliabel decibel measures
  145. # so this may not be the best backup criteria
  146. if firstAudibleIdx is not None:
  147. i = firstAudibleIdx-1
  148. while abs(dbL[i] - dbL[firstAudibleIdx]) < 1.0: # BUG BUG BUG: Hardcoded constant
  149. i -= 1
  150. firstNonSkipIdx = i
  151. return skipIdxL, firstAudibleIdx, firstNonSkipIdx
  152. def get_resample_points( usL, dbL, durMsL, takeIdL, minDurMs, minDb, noiseLimitPct ):
  153. scoreV = np.abs( rms_analysis.samples_to_linear_residual( usL, dbL) * 100.0 / dbL )
  154. skipIdxL, firstAudibleIdx, firstNonSkipIdx = get_dur_skip_indexes( durMsL, dbL, takeIdL, scoreV.tolist(), minDurMs, minDb, noiseLimitPct )
  155. skipL = [ (usL[i],dbL[i]) for i in skipIdxL ]
  156. noiseIdxL = [ i for i in range(scoreV.shape[0]) if scoreV[i] > noiseLimitPct ]
  157. noiseL = [ (usL[i],dbL[i]) for i in noiseIdxL ]
  158. resampleIdxL = select_resample_reference_indexes( noiseIdxL )
  159. if firstNonSkipIdx is not None:
  160. resampleIdxL = [ i for i in resampleIdxL if i >= firstNonSkipIdx ]
  161. resampleL = [ (usL[i],dbL[i]) for i in resampleIdxL ]
  162. reUsL,reDbL = locate_resample_regions( usL, dbL, resampleIdxL )
  163. return reUsL, reDbL, noiseL, resampleL, skipL, firstAudibleIdx, firstNonSkipIdx
  164. def get_resample_points_wrap( inDir, midi_pitch, analysisArgsD ):
  165. usL, dbL, durMsL,_,_ = get_merged_pulse_db_measurements( inDir, midi_pitch, analysisArgsD['rmsAnalysisArgs'] )
  166. reUsL,_,_,_,_,_,_ = get_resample_points( usL, dbL, durMsL, analysisArgsD['resampleMinDurMs'], analysisArgsD['resampleMinDb'], analysisArgsD['resampleNoiseLimitPct'] )
  167. return reUsL
  168. def plot_us_db_curves( ax, inDir, keyMapD, midi_pitch, analysisArgsD, plotResamplePointsFl=False, plotTakesFl=True, usMax=None ):
  169. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, midi_pitch, analysisArgsD['rmsAnalysisArgs'] )
  170. reUsL, reDbL, noiseL, resampleL, skipL, firstAudibleIdx, firstNonSkipIdx = get_resample_points( usL, dbL, durMsL, takeIdL, analysisArgsD['resampleMinDurMs'], analysisArgsD['resampleMinDb'], analysisArgsD['resampleNoiseLimitPct'] )
  171. # plot first audible and non-skip position
  172. if False:
  173. if firstNonSkipIdx is not None:
  174. ax.plot( usL[firstNonSkipIdx], dbL[firstNonSkipIdx], markersize=15, marker='+', linestyle='None', color='red')
  175. if firstAudibleIdx is not None:
  176. ax.plot( usL[firstAudibleIdx], dbL[firstAudibleIdx], markersize=15, marker='*', linestyle='None', color='red')
  177. # plot the resample points
  178. if plotResamplePointsFl:
  179. ax.plot( reUsL, reDbL, markersize=13, marker='x', linestyle='None', color='green')
  180. # plot the noisy sample positions
  181. if noiseL:
  182. nUsL,nDbL = zip(*noiseL)
  183. ax.plot( nUsL, nDbL, marker='o', markersize=9, linestyle='None', color='black')
  184. # plot the noisy sample positions and the neighbors included in the noisy region
  185. if resampleL:
  186. nUsL,nDbL = zip(*resampleL)
  187. ax.plot( nUsL, nDbL, marker='+', markersize=8, linestyle='None', color='red')
  188. # plot actual sample points
  189. elbow_us = None
  190. elbow_db = None
  191. elbow_len = None
  192. usL,dbL,takeIdL = zip(*[(us,dbL[i],takeIdL[i]) for i,us in enumerate(usL) if usMax is None or us <= usMax])
  193. if plotTakesFl:
  194. for takeId in list(set(takeIdL)):
  195. # get the us,db points included in this take
  196. xL,yL = zip(*[(usL[i],dbL[i]) for i in range(len(usL)) if takeIdL[i]==takeId ])
  197. ax.plot(xL,yL, marker='.',label=takeId)
  198. for i,(x,y) in enumerate(zip(xL,yL)):
  199. ax.text(x,y,str(i))
  200. #if elbow_len is None or len(xL) > elbow_len:
  201. if takeId+1 == len(set(takeIdL)):
  202. elbow_us,elbow_db = elbow.find_elbow(xL,yL)
  203. elbow_len = len(xL)
  204. else:
  205. ax.plot(usL, dbL, marker='.')
  206. ax.plot([elbow_us],[elbow_db],marker='*',markersize=12,color='red',linestyle='None')
  207. # plot the skip points in yellow
  208. if False:
  209. if skipL:
  210. nUsL,nDbL = zip(*skipL)
  211. ax.plot( nUsL, nDbL, marker='.', linestyle='None', color='yellow')
  212. # plot the locations where the hold duty cycle changes with vertical black lines
  213. for us_duty in holdDutyPctL:
  214. us,duty = tuple(us_duty)
  215. if us > 0:
  216. ax.axvline(us,color='black')
  217. # plot the 'minDb' reference line
  218. ax.axhline(analysisArgsD['resampleMinDb'] ,color='black')
  219. if os.path.isfile("minInterpDb.json"):
  220. with open("minInterpDb.json","r") as f:
  221. r = json.load(f)
  222. if midi_pitch in r['pitchL']:
  223. ax.axhline( r['minDbL'][ r['pitchL'].index(midi_pitch) ], color='blue' )
  224. ax.axhline( r['maxDbL'][ r['pitchL'].index(midi_pitch) ], color='blue' )
  225. ax.set_ylabel( "%i %s %s" % (midi_pitch, keyMapD[midi_pitch]['type'],keyMapD[midi_pitch]['class']))
  226. def plot_us_db_take_curve( ax, inDir, keyMapD, midi_pitch, takeId, analysisArgsD ):
  227. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, midi_pitch, analysisArgsD['rmsAnalysisArgs'] )
  228. reUsL, reDbL, noiseL, resampleL, skipL, firstAudibleIdx, firstNonSkipIdx = get_resample_points( usL, dbL, durMsL, takeIdL, analysisArgsD['resampleMinDurMs'], analysisArgsD['resampleMinDb'], analysisArgsD['resampleNoiseLimitPct'] )
  229. # plot first audible and non-skip position
  230. if False:
  231. if firstNonSkipIdx is not None:
  232. ax.plot( usL[firstNonSkipIdx], dbL[firstNonSkipIdx], markersize=15, marker='+', linestyle='None', color='red')
  233. if firstAudibleIdx is not None:
  234. ax.plot( usL[firstAudibleIdx], dbL[firstAudibleIdx], markersize=15, marker='*', linestyle='None', color='red')
  235. # plot the resample points
  236. if plotResamplePointsFl:
  237. ax.plot( reUsL, reDbL, markersize=13, marker='x', linestyle='None', color='green')
  238. # plot the noisy sample positions
  239. if noiseL:
  240. nUsL,nDbL = zip(*noiseL)
  241. ax.plot( nUsL, nDbL, marker='o', markersize=9, linestyle='None', color='black')
  242. # plot the noisy sample positions and the neighbors included in the noisy region
  243. if resampleL:
  244. nUsL,nDbL = zip(*resampleL)
  245. ax.plot( nUsL, nDbL, marker='+', markersize=8, linestyle='None', color='red')
  246. # plot actual sample points
  247. elbow_us = None
  248. elbow_db = None
  249. elbow_len = None
  250. usL,dbL,takeIdL = zip(*[(us,dbL[i],takeIdL[i]) for i,us in enumerate(usL) if usMax is None or us <= usMax])
  251. if plotTakesFl:
  252. for takeId in list(set(takeIdL)):
  253. # get the us,db points included in this take
  254. xL,yL = zip(*[(usL[i],dbL[i]) for i in range(len(usL)) if takeIdL[i]==takeId ])
  255. ax.plot(xL,yL, marker='.',label=takeId)
  256. for i,(x,y) in enumerate(zip(xL,yL)):
  257. ax.text(x,y,str(i))
  258. #if elbow_len is None or len(xL) > elbow_len:
  259. if takeId+1 == len(set(takeIdL)):
  260. elbow_us,elbow_db = elbow.find_elbow(xL,yL)
  261. elbow_len = len(xL)
  262. else:
  263. ax.plot(usL, dbL, marker='.')
  264. ax.plot([elbow_us],[elbow_db],marker='*',markersize=12,color='red',linestyle='None')
  265. # plot the skip points in yellow
  266. if False:
  267. if skipL:
  268. nUsL,nDbL = zip(*skipL)
  269. ax.plot( nUsL, nDbL, marker='.', linestyle='None', color='yellow')
  270. # plot the locations where the hold duty cycle changes with vertical black lines
  271. for us_duty in holdDutyPctL:
  272. us,duty = tuple(us_duty)
  273. if us > 0:
  274. ax.axvline(us,color='black')
  275. # plot the 'minDb' reference line
  276. ax.axhline(analysisArgsD['resampleMinDb'] ,color='black')
  277. if os.path.isfile("minInterpDb.json"):
  278. with open("minInterpDb.json","r") as f:
  279. r = json.load(f)
  280. if midi_pitch in r['pitchL']:
  281. ax.axhline( r['minDbL'][ r['pitchL'].index(midi_pitch) ], color='blue' )
  282. ax.axhline( r['maxDbL'][ r['pitchL'].index(midi_pitch) ], color='blue' )
  283. ax.set_ylabel( "%i %s %s" % (midi_pitch, keyMapD[midi_pitch]['type'],keyMapD[midi_pitch]['class']))
  284. def plot_us_db_curves_main( inDir, cfg, pitchL, plotTakesFl=True, usMax=None, printDir="" ):
  285. analysisArgsD = cfg.analysisArgs
  286. keyMapD = { d['midi']:d for d in cfg.key_mapL }
  287. axN = len(pitchL)
  288. fig,axL = plt.subplots(axN,1,sharex=True)
  289. if axN == 1:
  290. axL = [axL]
  291. fig.set_size_inches(18.5, 10.5*axN)
  292. for ax,midi_pitch in zip(axL,pitchL):
  293. plot_us_db_curves( ax,inDir, keyMapD, midi_pitch, analysisArgsD, plotTakesFl=plotTakesFl, usMax=usMax )
  294. if plotTakesFl:
  295. plt.legend()
  296. if printDir:
  297. plt.savefig(os.path.join(printDir,"us_db.png"),format="png")
  298. plt.show()
  299. def _plot_us_db_takes( inDir, cfg, pitchL, takeIdL, printDir="", printFn="" ):
  300. assert( len(pitchL) == len(takeIdL) )
  301. analysisArgsD = cfg.analysisArgs
  302. keyMapD = { d['midi']:d for d in cfg.key_mapL }
  303. fig,ax = plt.subplots(1,1)
  304. fig.set_size_inches(18.5, 10.5)
  305. for midi_pitch,takeId in zip(pitchL,takeIdL):
  306. usL, dbL, durMsL, _, holdDutyPctL = get_merged_pulse_db_measurements( inDir, midi_pitch, analysisArgsD['rmsAnalysisArgs'], takeId=takeId )
  307. ax.plot(usL,dbL, marker='.',label="%i:%i %s %s" % (midi_pitch,takeId,keyMapD[midi_pitch]['class'],keyMapD[midi_pitch]['type']))
  308. # for i,(x,y) in enumerate(zip(usL,dbL)):
  309. # ax.text(x,y,str(i))
  310. if printDir:
  311. plt.savefig(os.path.join(printDir,printFn),format="png")
  312. plt.legend()
  313. plt.show()
  314. def plot_us_db_takes( inDir, cfg, pitchL, printDir=""):
  315. takeIdL = None
  316. takeIdL = [ pitchL[i] for i in range(1,len(pitchL),2) ]
  317. pitchL = [ pitchL[i] for i in range(0,len(pitchL),2) ]
  318. return _plot_us_db_takes( inDir, cfg, pitchL, takeIdL, printDir, "us_db_takes.png")
  319. def plot_us_db_takes_last( inDir, cfg, pitchL, printDir ):
  320. takeIdL = []
  321. for pitch in pitchL:
  322. inDirL = os.listdir( os.path.join(inDir,str(pitch)))
  323. inDirL = sorted(inDirL)
  324. takeIdL.append( int(inDirL[-1]) )
  325. return _plot_us_db_takes( inDir, cfg, pitchL, takeIdL, printDir, "us_db_takes_last.png")
  326. def plot_all_noise_curves( inDir, cfg, pitchL=None ):
  327. pitchFolderL = os.listdir(inDir)
  328. if pitchL is None:
  329. pitchL = [ int( int(pitchFolder) ) for pitchFolder in pitchFolderL ]
  330. fig,ax = plt.subplots()
  331. for midi_pitch in pitchL:
  332. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, midi_pitch, cfg.analysisArgs['rmsAnalysisArgs'] )
  333. scoreV = np.abs( rms_analysis.samples_to_linear_residual( usL, dbL) * 100.0 / dbL )
  334. minDurMs = cfg.analysisArgs['resampleMinDurMs']
  335. minDb = cfg.analysisArgs['resampleMinDb']
  336. noiseLimitPct = cfg.analysisArgs['resampleNoiseLimitPct']
  337. skipIdxL, firstAudibleIdx, firstNonSkipIdx = get_dur_skip_indexes( durMsL, dbL, scoreV.tolist(), takeIdL, minDurMs, minDb, noiseLimitPct )
  338. if False:
  339. ax.plot( usL[firstAudibleIdx], scoreV[firstAudibleIdx], markersize=10, marker='*', linestyle='None', color='red')
  340. ax.plot( usL, scoreV, label="%i"%(midi_pitch) )
  341. ax.set_xlabel('us')
  342. else:
  343. xL = [ (score,db,i) for i,(score,db) in enumerate(zip(scoreV,dbL)) ]
  344. xL = sorted(xL, key=lambda x: x[1] )
  345. scoreV,dbL,idxL = zip(*xL)
  346. ax.plot( dbL[idxL[firstAudibleIdx]], scoreV[idxL[firstAudibleIdx]], markersize=10, marker='*', linestyle='None', color='red')
  347. ax.plot( dbL, scoreV, label="%i"%(midi_pitch) )
  348. ax.set_xlabel('db')
  349. ax.set_ylabel("noise db %")
  350. plt.legend()
  351. plt.show()
  352. def plot_min_max_2_db( inDir, cfg, pitchL=None, takeId=2, printDir=None ):
  353. pitchFolderL = os.listdir(inDir)
  354. print(pitchL)
  355. if pitchL is None:
  356. pitchL = [ int( int(pitchFolder) ) for pitchFolder in pitchFolderL ]
  357. print(pitchL)
  358. okL = []
  359. outPitchL = []
  360. minDbL = []
  361. maxDbL = []
  362. for midi_pitch in pitchL:
  363. print(midi_pitch)
  364. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, midi_pitch, cfg.analysisArgs['rmsAnalysisArgs'] )
  365. okL.append(False)
  366. db_maxL = sorted(dbL)
  367. maxDbL.append( np.mean(db_maxL[-5:]) )
  368. usL,dbL = zip(*[(usL[i],dbL[i]) for i in range(len(usL)) if takeIdL[i]==takeId ])
  369. if len(set(takeIdL)) == 3:
  370. okL[-1] = True
  371. elbow_us,elbow_db = elbow.find_elbow(usL,dbL)
  372. minDbL.append(elbow_db)
  373. outPitchL.append(midi_pitch)
  374. p_dL = sorted( zip(outPitchL,minDbL,maxDbL,okL), key=lambda x: x[0] )
  375. outPitchL,minDbL,maxDbL,okL = zip(*p_dL)
  376. fig,ax = plt.subplots()
  377. ax.plot(outPitchL,minDbL)
  378. ax.plot(outPitchL,maxDbL)
  379. keyMapD = { d['midi']:d for d in cfg.key_mapL }
  380. for pitch,min_db,max_db,okFl in zip(outPitchL,minDbL,maxDbL,okL):
  381. c = 'black' if okFl else 'red'
  382. ax.text( pitch, min_db, "%i %s %s" % (pitch, keyMapD[pitch]['type'],keyMapD[pitch]['class']), color=c)
  383. ax.text( pitch, max_db, "%i %s %s" % (pitch, keyMapD[pitch]['type'],keyMapD[pitch]['class']), color=c)
  384. if printDir:
  385. plt.savefig(os.path.join(printDir,"min_max_db_2.png"),format="png")
  386. plt.show()
  387. def plot_min_db_manual( inDir, cfg, printDir=None ):
  388. pitchL = list(cfg.manualMinD.keys())
  389. outPitchL = []
  390. maxDbL = []
  391. minDbL = []
  392. okL = []
  393. anchorMinDbL = []
  394. anchorMaxDbL = []
  395. for midi_pitch in pitchL:
  396. manual_take_id = cfg.manualMinD[midi_pitch][0]
  397. manual_sample_idx = cfg.manualMinD[midi_pitch][1]
  398. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, midi_pitch, cfg.analysisArgs['rmsAnalysisArgs'] )
  399. okL.append(False)
  400. takeId = len(set(takeIdL))-1
  401. # maxDb is computed on all takes (not just the specified take)
  402. db_maxL = sorted(dbL)
  403. max_db = np.mean(db_maxL[-4:])
  404. maxDbL.append( max_db )
  405. # get the us,db values for the specified take
  406. usL,dbL = zip(*[(usL[i],dbL[i]) for i in range(len(usL)) if takeIdL[i]==manual_take_id ])
  407. # most pitches have 3 sample takes that do not
  408. if len(set(takeIdL)) == 3 and manual_take_id == takeId:
  409. okL[-1] = True
  410. # min db from the sample index manually specified in cfg
  411. manualMinDb = dbL[ manual_sample_idx ]
  412. minDbL.append( manualMinDb )
  413. outPitchL.append(midi_pitch)
  414. if midi_pitch in cfg.manualAnchorPitchMinDbL:
  415. anchorMinDbL.append( manualMinDb )
  416. if midi_pitch in cfg.manualAnchorPitchMaxDbL:
  417. anchorMaxDbL.append( max_db )
  418. # Form the complete set of min/max db levels for each pitch by interpolating the
  419. # db values between the manually selected anchor points.
  420. interpMinDbL = np.interp( pitchL, cfg.manualAnchorPitchMinDbL, anchorMinDbL )
  421. interpMaxDbL = np.interp( pitchL, cfg.manualAnchorPitchMaxDbL, anchorMaxDbL )
  422. fig,ax = plt.subplots()
  423. ax.plot(outPitchL,minDbL) # plot the manually selected minDb values
  424. ax.plot(outPitchL,maxDbL) # plot the max db values
  425. # plot the interpolated minDb/maxDb values
  426. ax.plot(pitchL,interpMinDbL)
  427. ax.plot(pitchL,interpMaxDbL)
  428. keyMapD = { d['midi']:d for d in cfg.key_mapL }
  429. for pitch,min_db,max_db,okFl in zip(outPitchL,minDbL,maxDbL,okL):
  430. c = 'black' if okFl else 'red'
  431. ax.text( pitch, min_db, "%i %s %s" % (pitch, keyMapD[pitch]['type'],keyMapD[pitch]['class']), color=c)
  432. ax.text( pitch, max_db, "%i %s %s" % (pitch, keyMapD[pitch]['type'],keyMapD[pitch]['class']), color=c)
  433. with open("minInterpDb.json",'w') as f:
  434. json.dump( { "pitchL":pitchL, "minDbL":list(interpMinDbL), "maxDbL":list(interpMaxDbL) }, f )
  435. if printDir:
  436. plt.savefig(os.path.join(printDir,"manual_db.png"),format="png")
  437. plt.show()
  438. def plot_min_max_db( inDir, cfg, pitchL=None, printDir=None ):
  439. pitchFolderL = os.listdir(inDir)
  440. if pitchL is None:
  441. pitchL = [ int( int(pitchFolder) ) for pitchFolder in pitchFolderL ]
  442. maxDbL = []
  443. minDbL = []
  444. for midi_pitch in pitchL:
  445. print(midi_pitch)
  446. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, midi_pitch, cfg.analysisArgs['rmsAnalysisArgs'] )
  447. scoreV = np.abs( rms_analysis.samples_to_linear_residual( usL, dbL) * 100.0 / dbL )
  448. minDurMs = cfg.analysisArgs['resampleMinDurMs']
  449. minDb = cfg.analysisArgs['resampleMinDb']
  450. noiseLimitPct = cfg.analysisArgs['resampleNoiseLimitPct']
  451. skipIdxL, firstAudibleIdx, firstNonSkipIdx = get_dur_skip_indexes( durMsL, dbL, takeIdL, scoreV.tolist(), minDurMs, minDb, noiseLimitPct )
  452. minDbL.append( dbL[firstAudibleIdx] )
  453. dbL = sorted(dbL)
  454. x = np.mean(dbL[-3:])
  455. x = np.max(dbL)
  456. maxDbL.append( x )
  457. fig,ax = plt.subplots()
  458. fig.set_size_inches(18.5, 10.5)
  459. p_dL = sorted( zip(pitchL,maxDbL), key=lambda x: x[0] )
  460. pitchL,maxDbL = zip(*p_dL)
  461. ax.plot(pitchL,maxDbL)
  462. ax.plot(pitchL,minDbL)
  463. for pitch,db in zip(pitchL,maxDbL):
  464. keyMapD = { d['midi']:d for d in cfg.key_mapL }
  465. ax.text( pitch, db, "%i %s %s" % (pitch, keyMapD[pitch]['type'],keyMapD[pitch]['class']))
  466. if printDir:
  467. plt.savefig(os.path.join(printDir,"min_max_db.png"),format="png")
  468. plt.show()
  469. def estimate_us_to_db_map( inDir, cfg, minMapDb=16.0, maxMapDb=26.0, incrMapDb=0.5, pitchL=None ):
  470. pitchFolderL = os.listdir(inDir)
  471. if pitchL is None:
  472. pitchL = [ int( int(pitchFolder) ) for pitchFolder in pitchFolderL ]
  473. mapD = {} # pitch:{ loDb: { hiDb, us_avg, us_cls, us_std, us_min, us_max, db_avg, db_std, cnt }}
  474. # where: cnt=count of valid sample points in this db range
  475. # us_cls=us of closest point to center of db range
  476. dbS = set() # { (loDb,hiDb) } track the set of db ranges
  477. for pitch in pitchL:
  478. print(pitch)
  479. # get the sample measurements for pitch
  480. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, pitch, cfg.analysisArgs['rmsAnalysisArgs'] )
  481. # calc the fit to local straight line curve fit at each point
  482. scoreV = np.abs( rms_analysis.samples_to_linear_residual( usL, dbL) * 100.0 / dbL )
  483. minDurMs = cfg.analysisArgs['resampleMinDurMs']
  484. minDb = cfg.analysisArgs['resampleMinDb']
  485. noiseLimitPct = cfg.analysisArgs['resampleNoiseLimitPct']
  486. # get the set of samples that are not valid (too short, too quiet, too noisy)
  487. skipIdxL, firstAudibleIdx, firstNonSkipIdx = get_dur_skip_indexes( durMsL, dbL, takeIdL, scoreV.tolist(), minDurMs, minDb, noiseLimitPct )
  488. mapD[ pitch ] = {}
  489. # get the count of db ranges
  490. N = int(round((maxMapDb - minMapDb) / incrMapDb)) + 1
  491. # for each db range
  492. for i in range(N):
  493. loDb = minMapDb + (i*incrMapDb)
  494. hiDb = loDb + incrMapDb
  495. dbS.add((loDb,hiDb))
  496. # get the valid (pulse,db) pairs for this range
  497. u_dL = [(us,db) for i,(us,db) in enumerate(zip(usL,dbL)) if i not in skipIdxL and loDb<=db and db<hiDb ]
  498. us_avg = 0
  499. us_cls = 0
  500. us_std = 0
  501. us_min = 0
  502. us_max = 0
  503. db_avg = 0
  504. db_std = 0
  505. if len(u_dL) == 0:
  506. print("No valid samples for pitch:",pitch," db range:",loDb,hiDb)
  507. else:
  508. us0L,db0L = zip(*u_dL)
  509. if len(us0L) == 1:
  510. us_avg = us0L[0]
  511. us_cls = us_avg
  512. us_min = us_avg
  513. us_max = us_avg
  514. db_avg = db0L[0]
  515. elif len(us0L) > 1:
  516. us_avg = np.mean(us0L)
  517. us_cls = us0L[ np.argmin(np.abs(np.array(db0L)-(loDb - (hiDb-loDb)/2.0 ))) ]
  518. us_min = np.min(us0L)
  519. us_max = np.max(us0L)
  520. us_std = np.std(us0L)
  521. db_avg = np.mean(db0L)
  522. db_std = np.std(db0L)
  523. us_avg = int(round(us_avg))
  524. mapD[pitch][loDb] = { 'hiDb':hiDb, 'us_avg':us_avg, 'us_cls':us_cls, 'us_std':us_std,'us_min':us_min,'us_max':us_max, 'db_avg':db_avg, 'db_std':db_std, 'cnt':len(u_dL) }
  525. return mapD, list(dbS)
  526. def plot_us_to_db_map( inDir, cfg, minMapDb=16.0, maxMapDb=26.0, incrMapDb=1.0, pitchL=None, printDir=None ):
  527. fig,ax = plt.subplots()
  528. mapD, dbRefL = estimate_us_to_db_map( inDir, cfg, minMapDb, maxMapDb, incrMapDb, pitchL )
  529. # for each pitch
  530. for pitch, dbD in mapD.items():
  531. u_dL = [ (d['us_avg'],d['us_cls'],d['db_avg'],d['us_std'],d['us_min'],d['us_max'],d['db_std']) for loDb, d in dbD.items() if d['us_avg'] != 0 ]
  532. if u_dL:
  533. # get the us/db lists for this pitch
  534. usL,uscL,dbL,ussL,usnL,usxL,dbsL = zip(*u_dL)
  535. # plot central curve and std dev's
  536. p = ax.plot(usL,dbL, marker='.', label=str(pitch))
  537. ax.plot(uscL,dbL, marker='x', label=str(pitch), color=p[0].get_color(), linestyle='None')
  538. ax.plot(usL,np.array(dbL)+dbsL, color=p[0].get_color(), alpha=0.3)
  539. ax.plot(usL,np.array(dbL)-dbsL, color=p[0].get_color(), alpha=0.3)
  540. # plot us error bars
  541. for db,us,uss,us_min,us_max in zip(dbL,usL,ussL,usnL,usxL):
  542. ax.plot([us_min,us_max],[db,db], color=p[0].get_color(), alpha=0.3 )
  543. ax.plot([us-uss,us+uss],[db,db], color=p[0].get_color(), alpha=0.3, marker='.', linestyle='None' )
  544. plt.legend()
  545. if printDir:
  546. plt.savefig(os.path.join(printDir,"us_db_map.png"),format="png")
  547. plt.show()
  548. def report_take_ids( inDir ):
  549. pitchDirL = os.listdir(inDir)
  550. for pitch in pitchDirL:
  551. pitchDir = os.path.join(inDir,pitch)
  552. takeDirL = os.listdir(pitchDir)
  553. if len(takeDirL) == 0:
  554. print(pitch," directory empty")
  555. else:
  556. with open( os.path.join(pitchDir,'0','seq.json'), "rb") as f:
  557. r = json.load(f)
  558. if len(r['eventTimeL']) != 81:
  559. print(pitch," ",len(r['eventTimeL']))
  560. if len(takeDirL) != 3:
  561. print("***",pitch,len(takeDirL))
  562. def cache_us_db( inDir, cfg, outFn ):
  563. pitch_usDbD = {}
  564. pitchDirL = os.listdir(inDir)
  565. for pitch in pitchDirL:
  566. pitch = int(pitch)
  567. print(pitch)
  568. usL, dbL, durMsL, takeIdL, holdDutyPctL = get_merged_pulse_db_measurements( inDir, pitch, cfg.analysisArgs['rmsAnalysisArgs'] )
  569. pitch_usDbD[pitch] = { 'usL':usL, 'dbL':dbL, 'durMsL':durMsL, 'takeIdL':takeIdL, 'holdDutyPctL': holdDutyPctL }
  570. with open(outFn,"w") as f:
  571. json.dump(pitch_usDbD,f)
  572. def gen_vel_map( inDir, cfg, minMaxDbFn, dynLevelN, cacheFn ):
  573. velMapD = {} # { pitch:[ us ] }
  574. pitchDirL = os.listdir(inDir)
  575. with open(cacheFn,"r") as f:
  576. pitchUsDbD = json.load(f)
  577. with open("minInterpDb.json","r") as f:
  578. r = json.load(f)
  579. minMaxDbD = { pitch:(minDb,maxDb) for pitch,minDb,maxDb in zip(r['pitchL'],r['minDbL'],r['maxDbL']) }
  580. pitchL = sorted( [ int(pitch) for pitch in pitchUsDbD.keys()] )
  581. for pitch in pitchL:
  582. d = pitchUsDbD[str(pitch)]
  583. usL = d['usL']
  584. dbL = np.array(d['dbL'])
  585. velMapD[pitch] = []
  586. for i in range(dynLevelN+1):
  587. db = minMaxDbD[pitch][0] + (i * (minMaxDbD[pitch][1] - minMaxDbD[pitch][0])/ dynLevelN)
  588. usIdx = np.argmin( np.abs(dbL - db) )
  589. velMapD[pitch].append( (usL[ usIdx ],db) )
  590. with open("velMapD.json","w") as f:
  591. json.dump(velMapD,f)
  592. mtx = np.zeros((len(velMapD),dynLevelN+1))
  593. print(mtx.shape)
  594. for i,(pitch,usDbL) in enumerate(velMapD.items()):
  595. for j in range(len(usDbL)):
  596. mtx[i,j] = usDbL[j][1]
  597. fig,ax = plt.subplots()
  598. ax.plot(pitchL,mtx)
  599. plt.show()
  600. if __name__ == "__main__":
  601. printDir = None #os.path.expanduser( "~/src/picadae_ac_3/doc")
  602. cfgFn = sys.argv[1]
  603. inDir = sys.argv[2]
  604. mode = sys.argv[3]
  605. if len(sys.argv) <= 4:
  606. pitchL = None
  607. else:
  608. pitchL = [ int(sys.argv[i]) for i in range(4,len(sys.argv)) ]
  609. cfg = parse_yaml_cfg( cfgFn )
  610. if mode == 'us_db':
  611. plot_us_db_curves_main( inDir, cfg, pitchL, plotTakesFl=True,usMax=None, printDir=printDir )
  612. elif mode == 'us_db_pitch_take':
  613. plot_us_db_takes( inDir, cfg, pitchL, printDir=printDir)
  614. elif mode == 'us_db_pitch_last':
  615. plot_us_db_takes_last( inDir, cfg, pitchL, printDir=printDir)
  616. elif mode == 'noise':
  617. plot_all_noise_curves( inDir, cfg, pitchL )
  618. elif mode == 'min_max':
  619. plot_min_max_db( inDir, cfg, pitchL, printDir=printDir )
  620. elif mode == 'min_max_2':
  621. takeId = pitchL[-1]
  622. del pitchL[-1]
  623. plot_min_max_2_db( inDir, cfg, pitchL, takeId=takeId, printDir=printDir )
  624. elif mode == 'us_db_map':
  625. plot_us_to_db_map( inDir, cfg, pitchL=pitchL, printDir=printDir )
  626. elif mode == 'audacity':
  627. rms_analysis.write_audacity_label_files( inDir, cfg.analysisArgs['rmsAnalysisArgs'] )
  628. elif mode == 'rpt_take_ids':
  629. report_take_ids( inDir )
  630. elif mode == 'manual_db':
  631. plot_min_db_manual( inDir, cfg, printDir=printDir )
  632. elif mode == 'gen_vel_map':
  633. gen_vel_map( inDir, cfg, "minInterpDb.json", 9, "cache_us_db.json" )
  634. elif mode == 'cache_us_db':
  635. cache_us_db( inDir, cfg, "cache_us_db.json")
  636. else:
  637. print("Unknown mode:",mode)