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.

MidiFilePlayer.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import json
  2. class MidiFilePlayer:
  3. def __init__( self, cfg, api, midiDev, midiFn, velMapFn="velMapD.json" ):
  4. self.cfg = cfg
  5. self.api = api
  6. self.midiDev = midiDev
  7. self.midiL = [] # [ (us,status,d0,d1) ]
  8. self._parse_midi_file(midiFn)
  9. self.nextIdx = None
  10. self.startMs = 0
  11. self.curDutyPctD = {} # { pitch:duty } track the current hold duty cycle of each note
  12. self.velMapD = {}
  13. self.holdDutyPctD = cfg.calibrateArgs['holdDutyPctD']
  14. with open(velMapFn,'r') as f:
  15. velMapD = json.load(f)
  16. for pitch,usDbL in velMapD.items():
  17. self.velMapD[ int(pitch) ] = usDbL
  18. def start(self, ms):
  19. self.nextIdx = 0
  20. self.startMs = ms
  21. def stop( self, ms):
  22. self.nextIdx = None
  23. for pitch in self.velMapD.keys():
  24. self.api.note_off( int(pitch) )
  25. def tick( self, ms):
  26. if self.nextIdx is None:
  27. return
  28. curOffsMs = ms - self.startMs
  29. while self.nextIdx < len(self.midiL):
  30. if curOffsMs < self.midiL[ self.nextIdx ][0]:
  31. break
  32. cmd = self.midiL[ self.nextIdx ][1]
  33. if cmd == 'non':
  34. self._note_on(self.midiL[ self.nextIdx ][2],self.midiL[ self.nextIdx ][3])
  35. elif cmd == 'nof':
  36. self._note_off(self.midiL[ self.nextIdx ][2])
  37. elif cmd == 'ctl' and self.midiL[ self.nextIdx ][2] == 64:
  38. self.midiDev.send_controller(64,self.midiL[ self.nextIdx ][3])
  39. self.nextIdx += 1
  40. if self.nextIdx >= len(self.midiL):
  41. self.nextIdx = None
  42. def _get_duty_cycle( self, pitch, pulseUsec ):
  43. dutyPct = 50
  44. if pitch in self.holdDutyPctD:
  45. dutyPct = self.holdDutyPctD[pitch][0][1]
  46. for refUsec,refDuty in self.holdDutyPctD[pitch]:
  47. print(pitch,refUsec,refDuty)
  48. if pulseUsec < refUsec:
  49. break
  50. dutyPct = refDuty
  51. return dutyPct
  52. def _set_duty_cycle( self, pitch, pulseUsec ):
  53. dutyPct = self._get_duty_cycle( pitch, pulseUsec )
  54. if pitch not in self.curDutyPctD or self.curDutyPctD[pitch] != dutyPct:
  55. self.curDutyPctD[pitch] = dutyPct
  56. self.api.set_pwm_duty( pitch, dutyPct )
  57. print("Hold Duty Set:",dutyPct)
  58. return dutyPct
  59. def _get_pulse_us( self, pitch, vel ):
  60. usDbL = self.velMapD[pitch]
  61. idx = round(vel * len(usDbL) / 127)
  62. if idx > len(usDbL):
  63. idx = len(usDbL)-1
  64. us = usDbL[ idx ][0]
  65. print('non',pitch,vel,idx,us)
  66. return us
  67. def _note_on( self, pitch, vel ):
  68. if pitch not in self.velMapD:
  69. print("Missing pitch:",pitch)
  70. else:
  71. pulseUs = self._get_pulse_us(pitch,vel)
  72. self._set_duty_cycle( pitch, pulseUs )
  73. self.api.note_on_us( pitch, pulseUs )
  74. def _note_off( self, pitch ):
  75. self.api.note_off( pitch )
  76. def _parse_midi_file( self,fn ):
  77. with open(fn,"r") as f:
  78. for lineNumb,line in enumerate(f):
  79. if lineNumb >= 3:
  80. tokenL = line.split()
  81. if len(tokenL) > 5:
  82. usec = int(tokenL[3])
  83. status = None
  84. d0 = None
  85. d1 = None
  86. if tokenL[5] == 'non' or tokenL[5]=='nof' or tokenL[5]=='ctl':
  87. status = tokenL[5]
  88. d0 = int(tokenL[7])
  89. d1 = int(tokenL[8])
  90. self.midiL.append( (usec/1000,status,d0,d1))
  91. self.midiL = sorted( self.midiL, key=lambda x: x[0] )
  92. if __name__ == "__main__":
  93. midiFn = "/home/kevin/media/audio/midi/txt/988-v25.txt"
  94. mfp = MidiFilePlayer(None,None,midiFn)
  95. print(mfp.midiL[0:10])