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.

rt_note_analysis.py 2.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 types
  4. import numpy as np
  5. from rms_analysis import audio_harm_rms
  6. from rms_analysis import audio_rms
  7. from rms_analysis import locate_peak_indexes
  8. from rms_analysis import measure_duration_ms
  9. class RT_Analyzer:
  10. def __init__(self):
  11. self.td_dur_ms = 0
  12. self.td_db = 0
  13. self.hm_dur_ms = 0
  14. self.hm_db = 0
  15. def analyze_note( self, audioDev, midi_pitch, begTimeMs, endTimeMs, anlArgD ):
  16. td_dur_ms = 0
  17. td_db = 0
  18. hm_dur_ms = 0
  19. hm_db = 0
  20. decay_pct = 50.0
  21. result = audioDev.linear_buffer()
  22. if result:
  23. sigV = result.value
  24. # convert the audio signal vector to contain only the first (left) channel
  25. if len(sigV.shape)>1:
  26. sigV = sigV[:,0].squeeze()
  27. anlArgs = types.SimpleNamespace(**anlArgD)
  28. rmsDbV, rms_srate, binHz = audio_harm_rms( audioDev.srate, np.squeeze(sigV), anlArgs.rmsWndMs, anlArgs.rmsHopMs, anlArgs.dbLinRef, midi_pitch, anlArgs.harmCandN, anlArgs.harmN )
  29. pkIdxL = locate_peak_indexes( rmsDbV, rms_srate, [( begTimeMs, endTimeMs)] )
  30. if len(pkIdxL) > 0:
  31. end_idx = int(round(endTimeMs * rms_srate / 1000.0))
  32. if end_idx > pkIdxL[0]:
  33. hm_dur_ms = measure_duration_ms( rmsDbV, rms_srate, pkIdxL[0], end_idx, decay_pct )
  34. hm_db = rmsDbV[ pkIdxL[0] ]
  35. tdRmsDbV, rms0_srate = audio_rms( audioDev.srate, np.squeeze(sigV), anlArgs.rmsWndMs, anlArgs.rmsHopMs, anlArgs.dbLinRef )
  36. tdPkIdxL = locate_peak_indexes( tdRmsDbV, rms0_srate, [( begTimeMs, endTimeMs)] )
  37. if len(tdPkIdxL):
  38. end_idx = int(round(endTimeMs * rms0_srate / 1000.0))
  39. if end_idx > tdPkIdxL[0]:
  40. td_dur_ms = measure_duration_ms( tdRmsDbV, rms0_srate, tdPkIdxL[0], end_idx, decay_pct )
  41. td_db = tdRmsDbV[ tdPkIdxL[0] ]
  42. td_d_ms = td_dur_ms - self.td_dur_ms
  43. td_d_db = td_db - self.td_db
  44. hm_d_ms = hm_dur_ms - self.hm_dur_ms
  45. hm_d_db = hm_db - self.hm_db
  46. print("DUR: %5.2f %5.2f d:%5.2f %5.2f dB | %i %i d:%i %i ms" % (hm_db, td_db, hm_d_db, td_d_db, hm_dur_ms, td_dur_ms, hm_d_ms, td_d_ms) )
  47. self.td_dur_ms = td_dur_ms
  48. self.td_db = td_db
  49. self.hm_dur_ms = hm_dur_ms
  50. self.hm_db = hm_db