2626import logging
2727
2828
29- class AnalogMidiControl (analogcontrol .AnalogControl ):
29+ def as_midi_value (adc_value : int ):
30+ """Convert a 10-bit ADC value (0-1023) to a MIDI value (0-127)."""
31+ return util .renormalize (adc_value , 0 , 1023 , 0 , 127 )
32+
3033
34+ class AnalogMidiControl (analogcontrol .AnalogControl ):
3135 def __init__ (self , spi , adc_channel , tolerance , midi_CC , midi_channel , midiout , type , id = None , cfg = {}):
3236 super (AnalogMidiControl , self ).__init__ (spi , adc_channel , tolerance )
3337 self .midi_CC = midi_CC
@@ -37,7 +41,7 @@ def __init__(self, spi, adc_channel, tolerance, midi_CC, midi_channel, midiout,
3741 # Parent member overrides
3842 self .type = type
3943 self .id = id
40- self .last_read = 0 # this keeps track of the last potentiometer value
44+ self .last_read = 0 # this keeps track of the last potentiometer value
4145 self .value = None
4246 self .cfg = cfg
4347
@@ -47,18 +51,33 @@ def set_midi_channel(self, midi_channel):
4751 def set_value (self , value ):
4852 self .value = value
4953
54+ def send_current_value (self ):
55+ """
56+ Force-send the current analog control value via MIDI.
57+ Used for syncing external devices during pedalboard load.
58+ """
59+ # read the analog pin
60+ value = self .readChannel ()
61+ set_volume = as_midi_value (value )
62+
63+ cc = [self .midi_channel | CONTROL_CHANGE , self .midi_CC , set_volume ]
64+ logging .debug ("AnalogControl force-sending CC event %s" % cc )
65+ self .midiout .send_message (cc )
66+
67+ # save the reading to prevent duplicate sends on next poll
68+ self .last_read = value
69+
5070 # Override of base class method
5171 def refresh (self ):
5272 # read the analog pin
5373 value = self .readChannel ()
5474
5575 # how much has it changed since the last read?
5676 pot_adjust = abs (value - self .last_read )
57- value_changed = ( pot_adjust > self .tolerance )
77+ value_changed = pot_adjust > self .tolerance
5878
5979 if value_changed :
60- # convert 16bit adc0 (0-65535) trim pot read into 0-100 volume level
61- set_volume = util .renormalize (value , 0 , 1023 , 0 , 127 )
80+ set_volume = as_midi_value (value )
6281
6382 cc = [self .midi_channel | CONTROL_CHANGE , self .midi_CC , set_volume ]
6483 logging .debug ("AnalogControl Sending CC event %s" % cc )
0 commit comments