Skip to content

Commit 491d0f2

Browse files
committed
small speed optimization: only calculate gradient if necessary
1 parent 7c05599 commit 491d0f2

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

edrumulus.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1500,7 +1500,8 @@ void Edrumulus::Pad::process_control_sample ( const int* input,
15001500
int cur_midi_ctrl_value = ( ( ADC_MAX_RANGE - input[0] - control_threshold ) / control_range * 127 );
15011501
cur_midi_ctrl_value = max ( 0, min ( 127, cur_midi_ctrl_value ) );
15021502

1503-
// detect pedal hit
1503+
1504+
// Detect pedal stomp --------------------------------------------------------
15041505
update_fifo ( cur_midi_ctrl_value, ctrl_history_len, ctrl_hist );
15051506

15061507
float prev_ctrl_average = 0.0f;
@@ -1513,24 +1514,30 @@ void Edrumulus::Pad::process_control_sample ( const int* input,
15131514
prev_ctrl_average /= ctrl_history_len_half;
15141515
cur_ctrl_average /= ctrl_history_len_half;
15151516

1516-
const float ctrl_gradient = ( cur_ctrl_average - prev_ctrl_average ) / ctrl_history_len_half;
1517-
1517+
// check if we just crossed the transition from open to close
15181518
if ( ( prev_ctrl_average < hi_hat_is_open_MIDI_threshold ) &&
1519-
( cur_ctrl_average >= hi_hat_is_open_MIDI_threshold ) &&
1520-
( ctrl_gradient > ctrl_velocity_threshold ) )
1519+
( cur_ctrl_average >= hi_hat_is_open_MIDI_threshold ) )
15211520
{
1522-
// map curve difference (gradient) to velocity
1523-
midi_velocity = min ( 127, max ( 1, static_cast<int> ( ( ctrl_gradient - ctrl_velocity_threshold ) * ctrl_velocity_range_fact ) ) );
1524-
peak_found = true;
1521+
// calculate the gradient which is the measure for the pedal stomp velocity
1522+
const float ctrl_gradient = ( cur_ctrl_average - prev_ctrl_average ) / ctrl_history_len_half;
15251523

1526-
// reset the history after a detection to suppress multiple detections
1527-
for ( int i = 0; i < ctrl_history_len; i++ )
1524+
// only send MIDI note for pedal stomp if we are above the given threshold
1525+
if ( ctrl_gradient > ctrl_velocity_threshold )
15281526
{
1529-
ctrl_hist[i] = hi_hat_is_open_MIDI_threshold;
1527+
// map curve difference (gradient) to velocity
1528+
midi_velocity = min ( 127, max ( 1, static_cast<int> ( ( ctrl_gradient - ctrl_velocity_threshold ) * ctrl_velocity_range_fact ) ) );
1529+
peak_found = true;
1530+
1531+
// reset the history after a detection to suppress multiple detections
1532+
for ( int i = 0; i < ctrl_history_len; i++ )
1533+
{
1534+
ctrl_hist[i] = hi_hat_is_open_MIDI_threshold;
1535+
}
15301536
}
15311537
}
15321538

1533-
// introduce hysteresis to avoid sending too many MIDI control messages
1539+
1540+
// Introduce hysteresis to avoid sending too many MIDI control messages ------
15341541
change_found = false;
15351542

15361543
if ( ( cur_midi_ctrl_value > ( prev_ctrl_value + control_midi_hysteresis ) ) ||

0 commit comments

Comments
 (0)