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 27KB

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