Skip to content

Commit 7c05599

Browse files
committed
bug fix: use correct gradient calculation for pedal stomp detection (see discussion in Pull Request #110)
1 parent db9d2e0 commit 7c05599

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

edrumulus.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,10 @@ void Edrumulus::Pad::initialize()
514514
rim_max_power_low_limit = ADC_MAX_NOISE_AMPL * ADC_MAX_NOISE_AMPL / 31.0f; // lower limit on detected rim power, 15 dB below max noise amplitude
515515
x_rim_hist_len = x_sq_hist_len + rim_shot_window_len;
516516
cancellation_factor = static_cast<float> ( pad_settings.cancellation ) / 31.0f; // cancellation factor: range of 0.0..1.0
517-
ctrl_history_len = 10; // (MUST BE AN EVEN VALUE) control history length, use a fixed value
518-
ctrl_velocity_range_fact = 4.0f; // use a fixed value (TODO make it adjustable)
519-
ctrl_velocity_threshold = 5.0f; // use a fixed value (TODO make it adjustable)
517+
ctrl_history_len = 10; // (MUST BE AN EVEN VALUE) control history length, use a fixed value
518+
ctrl_history_len_half = ctrl_history_len / 2;
519+
ctrl_velocity_range_fact = 20.0f; // use a fixed value (TODO make it adjustable)
520+
ctrl_velocity_threshold = 1.0f; // use a fixed value (TODO make it adjustable)
520521
max_num_overloads = 3; // maximum allowed number of overloaded samples until the overload special case is activated
521522

522523
// The ESP32 ADC has 12 bits resulting in a range of 20*log10(2048)=66.2 dB.
@@ -1502,22 +1503,24 @@ void Edrumulus::Pad::process_control_sample ( const int* input,
15021503
// detect pedal hit
15031504
update_fifo ( cur_midi_ctrl_value, ctrl_history_len, ctrl_hist );
15041505

1505-
float prev_ctrl_average = 0;
1506-
float cur_ctrl_average = 0;
1507-
for ( int i = 0; i < ctrl_history_len / 2; i++ )
1506+
float prev_ctrl_average = 0.0f;
1507+
float cur_ctrl_average = 0.0f;
1508+
for ( int i = 0; i < ctrl_history_len_half; i++ )
15081509
{
1509-
prev_ctrl_average += ctrl_hist[i]; // use first half for previous value
1510-
cur_ctrl_average += ctrl_hist[i + ctrl_history_len / 2]; // use second half for current value
1510+
prev_ctrl_average += ctrl_hist[i]; // use first half for previous value
1511+
cur_ctrl_average += ctrl_hist[i + ctrl_history_len_half]; // use second half for current value
15111512
}
1512-
prev_ctrl_average /= ctrl_history_len / 2;
1513-
cur_ctrl_average /= ctrl_history_len / 2;
1513+
prev_ctrl_average /= ctrl_history_len_half;
1514+
cur_ctrl_average /= ctrl_history_len_half;
1515+
1516+
const float ctrl_gradient = ( cur_ctrl_average - prev_ctrl_average ) / ctrl_history_len_half;
15141517

15151518
if ( ( prev_ctrl_average < hi_hat_is_open_MIDI_threshold ) &&
15161519
( cur_ctrl_average >= hi_hat_is_open_MIDI_threshold ) &&
1517-
( cur_ctrl_average - prev_ctrl_average > ctrl_velocity_threshold ) )
1520+
( ctrl_gradient > ctrl_velocity_threshold ) )
15181521
{
15191522
// map curve difference (gradient) to velocity
1520-
midi_velocity = min ( 127, static_cast<int> ( ( cur_ctrl_average - prev_ctrl_average - ctrl_velocity_threshold ) * ctrl_velocity_range_fact ) );
1523+
midi_velocity = min ( 127, max ( 1, static_cast<int> ( ( ctrl_gradient - ctrl_velocity_threshold ) * ctrl_velocity_range_fact ) ) );
15211524
peak_found = true;
15221525

15231526
// reset the history after a detection to suppress multiple detections

edrumulus.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ const float ADC_noise_peak_velocity_scaling = 1.0f / 6.0f;
448448
int midi_note_open_rim;
449449
int midi_ctrl_ch;
450450
int ctrl_history_len;
451-
int ctrl_velocity_threshold;
451+
int ctrl_history_len_half;
452+
float ctrl_velocity_threshold;
452453
float ctrl_velocity_range_fact;
453454
int prev_ctrl_value;
454455
float cancellation_factor;

0 commit comments

Comments
 (0)