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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. anlArgs = types.SimpleNamespace(**anlArgD)
  25. rmsDbV, rms_srate, binHz = audio_harm_rms( audioDev.srate, np.squeeze(sigV), anlArgs.rmsWndMs, anlArgs.rmsHopMs, anlArgs.dbLinRef, midi_pitch, anlArgs.harmCandN, anlArgs.harmN )
  26. pkIdxL = locate_peak_indexes( rmsDbV, rms_srate, [( begTimeMs, endTimeMs)] )
  27. if len(pkIdxL) > 0:
  28. end_idx = int(round(endTimeMs * rms_srate / 1000.0))
  29. if end_idx > pkIdxL[0]:
  30. hm_dur_ms = measure_duration_ms( rmsDbV, rms_srate, pkIdxL[0], end_idx, decay_pct )
  31. hm_db = rmsDbV[ pkIdxL[0] ]
  32. tdRmsDbV, rms0_srate = audio_rms( audioDev.srate, np.squeeze(sigV), anlArgs.rmsWndMs, anlArgs.rmsHopMs, anlArgs.dbLinRef )
  33. tdPkIdxL = locate_peak_indexes( tdRmsDbV, rms0_srate, [( begTimeMs, endTimeMs)] )
  34. if len(tdPkIdxL):
  35. end_idx = int(round(endTimeMs * rms0_srate / 1000.0))
  36. if end_idx > tdPkIdxL[0]:
  37. td_dur_ms = measure_duration_ms( tdRmsDbV, rms0_srate, tdPkIdxL[0], end_idx, decay_pct )
  38. td_db = tdRmsDbV[ tdPkIdxL[0] ]
  39. td_d_ms = td_dur_ms - self.td_dur_ms
  40. td_d_db = td_db - self.td_db
  41. hm_d_ms = hm_dur_ms - self.hm_dur_ms
  42. hm_d_db = hm_db - self.hm_db
  43. 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) )
  44. self.td_dur_ms = td_dur_ms
  45. self.td_db = td_db
  46. self.hm_dur_ms = hm_dur_ms
  47. self.hm_db = hm_db