notes and code for my payphone project

asterisk: halve DTMF_TO_TOTAL_ENERGY when relaxdtmf=yes

This, along with custom DTMF twist values of 20 in dsp.conf, seem to
be required to get coins to be detected through the mouthpiece of my
payphone

+14 -13
+14 -13
asterisk-dsp_recognize_coins.patch
··· 1 1 # initially adapted from https://github.com/saizai/wumpusphone/blob/master/asterisk-1.8.0.patch#L251 2 2 # less invastive version from https://github.com/hharte/1dcoinctrl/blob/master/asterisk/main/dsp.c-patch 3 - # updated for asterisk 13.12.1 3 + # updated for asterisk 18.4.0 4 4 5 - --- main/dsp.c.orig Thu Oct 27 13:48:03 2016 6 - +++ main/dsp.c Sun Oct 30 10:30:35 2016 7 - @@ -272,10 +272,11 @@ typedef struct 5 + --- main/dsp.c.bak.orig Thu May 6 10:34:57 2021 6 + +++ main/dsp.c Wed Jul 28 13:42:09 2021 7 + @@ -278,10 +278,11 @@ typedef struct 8 8 9 9 } tone_detect_state_t; 10 10 ··· 18 18 int hits; /* How many successive hits we have seen already */ 19 19 int misses; /* How many successive misses we have seen already */ 20 20 int lasthit; 21 - @@ -309,15 +310,16 @@ typedef struct 21 + @@ -315,15 +316,16 @@ typedef struct 22 22 } digit_detect_state_t; 23 23 24 24 static const float dtmf_row[] = { ··· 38 38 static const char bell_mf_positions[] = "1247C-358A--69*---0B----#"; 39 39 static int thresholds[THRESHOLD_MAX]; 40 40 static float dtmf_normal_twist; /* AT&T = 8dB */ 41 - @@ -514,7 +516,7 @@ static void ast_dtmf_detect_init(dtmf_detect_state_t * 41 + @@ -522,7 +524,7 @@ static void ast_dtmf_detect_init(dtmf_detect_state_t * 42 42 { 43 43 int i; 44 44 ··· 47 47 goertzel_init(&s->row_out[i], dtmf_row[i], sample_rate); 48 48 goertzel_init(&s->col_out[i], dtmf_col[i], sample_rate); 49 49 } 50 - @@ -674,8 +676,8 @@ static void store_digit(digit_detect_state_t *s, char 50 + @@ -682,8 +684,8 @@ static void store_digit(digit_detect_state_t *s, char 51 51 52 52 static int dtmf_detect(struct ast_dsp *dsp, digit_detect_state_t *s, int16_t amp[], int samples, int squelch, int relax) 53 53 { ··· 58 58 int i; 59 59 int j; 60 60 int sample; 61 - @@ -714,6 +716,8 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 61 + @@ -722,6 +724,8 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 62 62 goertzel_sample(s->td.dtmf.col_out + 2, samp); 63 63 goertzel_sample(s->td.dtmf.row_out + 3, samp); 64 64 goertzel_sample(s->td.dtmf.col_out + 3, samp); ··· 67 67 } 68 68 s->td.dtmf.current_sample += (limit - sample); 69 69 if (s->td.dtmf.current_sample < DTMF_GSIZE) { 70 - @@ -724,7 +728,7 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 70 + @@ -732,7 +736,7 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 71 71 row_energy[0] = goertzel_result(&s->td.dtmf.row_out[0]); 72 72 col_energy[0] = goertzel_result(&s->td.dtmf.col_out[0]); 73 73 ··· 76 76 row_energy[i] = goertzel_result(&s->td.dtmf.row_out[i]); 77 77 if (row_energy[i] > row_energy[best_row]) { 78 78 best_row = i; 79 - @@ -745,7 +749,7 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 79 + @@ -753,7 +757,7 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 80 80 col_energy[best_col] < row_energy[best_row] * (relax ? relax_dtmf_reverse_twist : dtmf_reverse_twist) && 81 81 row_energy[best_row] < col_energy[best_col] * (relax ? relax_dtmf_normal_twist : dtmf_normal_twist)) { 82 82 /* Relative peak test */ ··· 85 85 if ((i != best_col && 86 86 col_energy[i] * DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) || 87 87 (i != best_row 88 - @@ -754,10 +758,10 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 88 + @@ -762,10 +766,10 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 89 89 } 90 90 } 91 91 /* ... and fraction of total energy test */ 92 92 - if (i >= 4 && 93 + - (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY * s->td.dtmf.energy) { 93 94 + if (i >= DTMF_MATRIX_SIZE && 94 - (row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY * s->td.dtmf.energy) { 95 + + (row_energy[best_row] + col_energy[best_col]) > (DTMF_TO_TOTAL_ENERGY / (relax ? 2 : 1)) * s->td.dtmf.energy) { 95 96 /* Got a hit */ 96 97 - hit = dtmf_positions[(best_row << 2) + best_col]; 97 98 + hit = dtmf_positions[(best_row * DTMF_MATRIX_SIZE) + best_col]; 98 99 ast_debug(10, "DTMF hit '%c'\n", hit); 99 100 } 100 101 } 101 - @@ -860,7 +864,7 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 102 + @@ -868,7 +872,7 @@ static int dtmf_detect(struct ast_dsp *dsp, digit_dete 102 103 } 103 104 104 105 /* Reinitialise the detector for the next block */