* Exception:
* The author hereby grants the use of this source code under the
* following license if and only if the source code is distributed
- * as part of the openzap library. Any use or distribution of this
+ * as part of the openzap library. Any use or distribution of this
* source code outside the scope of the openzap library will nullify the
* following license and reinact the MPL 1.1 as stated above.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
static teletone_detection_descriptor_t dtmf_detect_row_2nd[GRID_FACTOR];
static teletone_detection_descriptor_t dtmf_detect_col_2nd[GRID_FACTOR];
-static float dtmf_row[] = {697.0f, 770.0f, 852.0f, 941.0f};
+static float dtmf_row[] = {697.0f, 770.0f, 852.0f, 941.0f};
static float dtmf_col[] = {1209.0f, 1336.0f, 1477.0f, 1633.0f};
static char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
static void goertzel_init(teletone_goertzel_state_t *goertzel_state, teletone_detection_descriptor_t *tdesc) {
- goertzel_state->v2 = goertzel_state->v3 = 0.0;
- goertzel_state->fac = tdesc->fac;
+ goertzel_state->v2 = goertzel_state->v3 = 0.0;
+ goertzel_state->fac = tdesc->fac;
}
void teletone_goertzel_update(teletone_goertzel_state_t *goertzel_state,
int16_t sample_buffer[],
int samples)
{
- int i;
- float v1;
-
- for (i = 0; i < samples; i++) {
- v1 = goertzel_state->v2;
- goertzel_state->v2 = goertzel_state->v3;
- goertzel_state->v3 = goertzel_state->fac*goertzel_state->v2 - v1 + sample_buffer[i];
- }
+ int i;
+ float v1;
+
+ for (i = 0; i < samples; i++) {
+ v1 = goertzel_state->v2;
+ goertzel_state->v2 = goertzel_state->v3;
+ goertzel_state->v3 = goertzel_state->fac*goertzel_state->v2 - v1 + sample_buffer[i];
+ }
}
#define teletone_goertzel_result(gs) ((gs)->v3 * (gs)->v3 + (gs)->v2 * (gs)->v2 - (gs)->v2 * (gs)->v3 * (gs)->fac)
void teletone_dtmf_detect_init (teletone_dtmf_detect_state_t *dtmf_detect_state, int sample_rate)
{
- int i;
- float theta;
+ int i;
+ float theta;
- dtmf_detect_state->hit1 = dtmf_detect_state->hit2 = 0;
+ dtmf_detect_state->hit1 = dtmf_detect_state->hit2 = 0;
- for (i = 0; i < GRID_FACTOR; i++) {
- theta = M_TWO_PI*(dtmf_row[i]/(float)sample_rate);
- dtmf_detect_row[i].fac = 2.0*cos(theta);
+ for (i = 0; i < GRID_FACTOR; i++) {
+ theta = M_TWO_PI*(dtmf_row[i]/(float)sample_rate);
+ dtmf_detect_row[i].fac = 2.0*cos(theta);
- theta = M_TWO_PI*(dtmf_col[i]/(float)sample_rate);
- dtmf_detect_col[i].fac = 2.0*cos(theta);
-
- theta = M_TWO_PI*(dtmf_row[i]*2.0/(float)sample_rate);
- dtmf_detect_row_2nd[i].fac = 2.0*cos(theta);
+ theta = M_TWO_PI*(dtmf_col[i]/(float)sample_rate);
+ dtmf_detect_col[i].fac = 2.0*cos(theta);
+
+ theta = M_TWO_PI*(dtmf_row[i]*2.0/(float)sample_rate);
+ dtmf_detect_row_2nd[i].fac = 2.0*cos(theta);
- theta = M_TWO_PI*(dtmf_col[i]*2.0/(float)sample_rate);
- dtmf_detect_col_2nd[i].fac = 2.0*cos(theta);
-
+ theta = M_TWO_PI*(dtmf_col[i]*2.0/(float)sample_rate);
+ dtmf_detect_col_2nd[i].fac = 2.0*cos(theta);
+
goertzel_init (&dtmf_detect_state->row_out[i], &dtmf_detect_row[i]);
- goertzel_init (&dtmf_detect_state->col_out[i], &dtmf_detect_col[i]);
- goertzel_init (&dtmf_detect_state->row_out2nd[i], &dtmf_detect_row_2nd[i]);
- goertzel_init (&dtmf_detect_state->col_out2nd[i], &dtmf_detect_col_2nd[i]);
+ goertzel_init (&dtmf_detect_state->col_out[i], &dtmf_detect_col[i]);
+ goertzel_init (&dtmf_detect_state->row_out2nd[i], &dtmf_detect_row_2nd[i]);
+ goertzel_init (&dtmf_detect_state->col_out2nd[i], &dtmf_detect_col_2nd[i]);
dtmf_detect_state->energy = 0.0;
- }
- dtmf_detect_state->current_sample = 0;
- dtmf_detect_state->detected_digits = 0;
- dtmf_detect_state->lost_digits = 0;
- dtmf_detect_state->digits[0] = '\0';
- dtmf_detect_state->mhit = 0;
+ }
+ dtmf_detect_state->current_sample = 0;
+ dtmf_detect_state->detected_digits = 0;
+ dtmf_detect_state->lost_digits = 0;
+ dtmf_detect_state->digits[0] = '\0';
+ dtmf_detect_state->mhit = 0;
}
void teletone_multi_tone_init(teletone_multi_tone_t *mt, teletone_tone_map_t *map)
float eng_sum = 0, eng_all[TELETONE_MAX_TONES];
int gtest = 0, see_hit = 0;
- for (sample = 0; sample < samples; sample = limit) {
+ for (sample = 0; sample < samples; sample = limit) {
mt->total_samples++;
- if ((samples - sample) >= (mt->min_samples - mt->current_sample)) {
- limit = sample + (mt->min_samples - mt->current_sample);
+ if ((samples - sample) >= (mt->min_samples - mt->current_sample)) {
+ limit = sample + (mt->min_samples - mt->current_sample);
} else {
- limit = samples;
+ limit = samples;
}
- for (j = sample; j < limit; j++) {
- famp = sample_buffer[j];
+ for (j = sample; j < limit; j++) {
+ famp = sample_buffer[j];
mt->energy += famp*famp;
v1 = mt->gs[x].v2;
mt->gs[x].v2 = mt->gs[x].v3;
mt->gs[x].v3 = mt->gs[x].fac * mt->gs[x].v2 - v1 + famp;
-
+
v1 = mt->gs2[x].v2;
mt->gs2[x].v2 = mt->gs2[x].v3;
mt->gs2[x].v3 = mt->gs2[x].fac*mt->gs2[x].v2 - v1 + famp;
}
}
- mt->current_sample += (limit - sample);
- if (mt->current_sample < mt->min_samples) {
- continue;
+ mt->current_sample += (limit - sample);
+ if (mt->current_sample < mt->min_samples) {
+ continue;
}
eng_sum = 0;
}
}
- /* Reinitialise the detector for the next block */
+ /* Reinitialise the detector for the next block */
for(x = 0; x < mt->tone_count; x++) {
goertzel_init (&mt->gs[x], &mt->tdd[x]);
goertzel_init (&mt->gs2[x], &mt->tdd[x]);
}
mt->energy = 0.0;
- mt->current_sample = 0;
- }
+ mt->current_sample = 0;
+ }
return see_hit;
}
int16_t sample_buffer[],
int samples)
{
- float row_energy[GRID_FACTOR];
- float col_energy[GRID_FACTOR];
- float famp;
- float v1;
- int i;
- int j;
- int sample;
- int best_row;
- int best_col;
- char hit;
- int limit;
-
- hit = 0;
- for (sample = 0; sample < samples; sample = limit) {
- /* BLOCK_LEN is optimised to meet the DTMF specs. */
- if ((samples - sample) >= (BLOCK_LEN - dtmf_detect_state->current_sample)) {
- limit = sample + (BLOCK_LEN - dtmf_detect_state->current_sample);
+ float row_energy[GRID_FACTOR];
+ float col_energy[GRID_FACTOR];
+ float famp;
+ float v1;
+ int i;
+ int j;
+ int sample;
+ int best_row;
+ int best_col;
+ char hit;
+ int limit;
+
+ hit = 0;
+ for (sample = 0; sample < samples; sample = limit) {
+ /* BLOCK_LEN is optimised to meet the DTMF specs. */
+ if ((samples - sample) >= (BLOCK_LEN - dtmf_detect_state->current_sample)) {
+ limit = sample + (BLOCK_LEN - dtmf_detect_state->current_sample);
} else {
- limit = samples;
+ limit = samples;
}
- for (j = sample; j < limit; j++) {
+ for (j = sample; j < limit; j++) {
int x = 0;
- famp = sample_buffer[j];
+ famp = sample_buffer[j];
dtmf_detect_state->energy += famp*famp;
v1 = dtmf_detect_state->row_out[x].v2;
dtmf_detect_state->row_out[x].v2 = dtmf_detect_state->row_out[x].v3;
dtmf_detect_state->row_out[x].v3 = dtmf_detect_state->row_out[x].fac*dtmf_detect_state->row_out[x].v2 - v1 + famp;
-
+
v1 = dtmf_detect_state->col_out[x].v2;
dtmf_detect_state->col_out[x].v2 = dtmf_detect_state->col_out[x].v3;
dtmf_detect_state->col_out[x].v3 = dtmf_detect_state->col_out[x].fac*dtmf_detect_state->col_out[x].v2 - v1 + famp;
v1 = dtmf_detect_state->col_out2nd[x].v2;
dtmf_detect_state->col_out2nd[x].v2 = dtmf_detect_state->col_out2nd[x].v3;
dtmf_detect_state->col_out2nd[x].v3 = dtmf_detect_state->col_out2nd[x].fac*dtmf_detect_state->col_out2nd[x].v2 - v1 + famp;
-
+
v1 = dtmf_detect_state->row_out2nd[x].v2;
dtmf_detect_state->row_out2nd[x].v2 = dtmf_detect_state->row_out2nd[x].v3;
dtmf_detect_state->row_out2nd[x].v3 = dtmf_detect_state->row_out2nd[x].fac*dtmf_detect_state->row_out2nd[x].v2 - v1 + famp;
}
- }
+ }
- dtmf_detect_state->current_sample += (limit - sample);
- if (dtmf_detect_state->current_sample < BLOCK_LEN) {
- continue;
+ dtmf_detect_state->current_sample += (limit - sample);
+ if (dtmf_detect_state->current_sample < BLOCK_LEN) {
+ continue;
}
- /* We are at the end of a DTMF detection block */
- /* Find the peak row and the peak column */
- row_energy[0] = teletone_goertzel_result (&dtmf_detect_state->row_out[0]);
- col_energy[0] = teletone_goertzel_result (&dtmf_detect_state->col_out[0]);
-
- for (best_row = best_col = 0, i = 1; i < GRID_FACTOR; i++) {
- row_energy[i] = teletone_goertzel_result (&dtmf_detect_state->row_out[i]);
- if (row_energy[i] > row_energy[best_row]) {
- best_row = i;
+ /* We are at the end of a DTMF detection block */
+ /* Find the peak row and the peak column */
+ row_energy[0] = teletone_goertzel_result (&dtmf_detect_state->row_out[0]);
+ col_energy[0] = teletone_goertzel_result (&dtmf_detect_state->col_out[0]);
+
+ for (best_row = best_col = 0, i = 1; i < GRID_FACTOR; i++) {
+ row_energy[i] = teletone_goertzel_result (&dtmf_detect_state->row_out[i]);
+ if (row_energy[i] > row_energy[best_row]) {
+ best_row = i;
}
- col_energy[i] = teletone_goertzel_result (&dtmf_detect_state->col_out[i]);
- if (col_energy[i] > col_energy[best_col]) {
- best_col = i;
+ col_energy[i] = teletone_goertzel_result (&dtmf_detect_state->col_out[i]);
+ if (col_energy[i] > col_energy[best_col]) {
+ best_col = i;
}
- }
- hit = 0;
- /* Basic signal level test and the twist test */
- if (row_energy[best_row] >= DTMF_THRESHOLD &&
+ }
+ hit = 0;
+ /* Basic signal level test and the twist test */
+ if (row_energy[best_row] >= DTMF_THRESHOLD &&
col_energy[best_col] >= DTMF_THRESHOLD &&
- col_energy[best_col] < row_energy[best_row]*DTMF_REVERSE_TWIST &&
- col_energy[best_col]*DTMF_NORMAL_TWIST > row_energy[best_row]) {
- /* Relative peak test */
- for (i = 0; i < GRID_FACTOR; i++) {
- if ((i != best_col && col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
- (i != best_row && row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
- break;
- }
- }
- /* ... and second harmonic test */
- if (i >= GRID_FACTOR && (row_energy[best_row] + col_energy[best_col]) > 42.0*dtmf_detect_state->energy &&
- teletone_goertzel_result (&dtmf_detect_state->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col] &&
- teletone_goertzel_result (&dtmf_detect_state->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row]) {
- hit = dtmf_positions[(best_row << 2) + best_col];
- /* Look for two successive similar results */
- /* The logic in the next test is:
- We need two successive identical clean detects, with
+ col_energy[best_col] < row_energy[best_row]*DTMF_REVERSE_TWIST &&
+ col_energy[best_col]*DTMF_NORMAL_TWIST > row_energy[best_row]) {
+ /* Relative peak test */
+ for (i = 0; i < GRID_FACTOR; i++) {
+ if ((i != best_col && col_energy[i]*DTMF_RELATIVE_PEAK_COL > col_energy[best_col]) ||
+ (i != best_row && row_energy[i]*DTMF_RELATIVE_PEAK_ROW > row_energy[best_row])) {
+ break;
+ }
+ }
+ /* ... and second harmonic test */
+ if (i >= GRID_FACTOR && (row_energy[best_row] + col_energy[best_col]) > 42.0*dtmf_detect_state->energy &&
+ teletone_goertzel_result (&dtmf_detect_state->col_out2nd[best_col])*DTMF_2ND_HARMONIC_COL < col_energy[best_col] &&
+ teletone_goertzel_result (&dtmf_detect_state->row_out2nd[best_row])*DTMF_2ND_HARMONIC_ROW < row_energy[best_row]) {
+ hit = dtmf_positions[(best_row << 2) + best_col];
+ /* Look for two successive similar results */
+ /* The logic in the next test is:
+ We need two successive identical clean detects, with
something different preceeding it. This can work with
back to back differing digits. More importantly, it
can work with nasty phones that give a very wobbly start
to a digit. */
- if (hit == dtmf_detect_state->hit3 && dtmf_detect_state->hit3 != dtmf_detect_state->hit2) {
+ if (hit == dtmf_detect_state->hit3 && dtmf_detect_state->hit3 != dtmf_detect_state->hit2) {
dtmf_detect_state->mhit = hit;
dtmf_detect_state->digit_hits[(best_row << 2) + best_col]++;
dtmf_detect_state->detected_digits++;
dtmf_detect_state->lost_digits++;
}
}
- }
- }
- dtmf_detect_state->hit1 = dtmf_detect_state->hit2;
- dtmf_detect_state->hit2 = dtmf_detect_state->hit3;
- dtmf_detect_state->hit3 = hit;
- /* Reinitialise the detector for the next block */
- for (i = 0; i < GRID_FACTOR; i++) {
- goertzel_init (&dtmf_detect_state->row_out[i], &dtmf_detect_row[i]);
- goertzel_init (&dtmf_detect_state->col_out[i], &dtmf_detect_col[i]);
- goertzel_init (&dtmf_detect_state->row_out2nd[i], &dtmf_detect_row_2nd[i]);
- goertzel_init (&dtmf_detect_state->col_out2nd[i], &dtmf_detect_col_2nd[i]);
- }
+ }
+ }
+ dtmf_detect_state->hit1 = dtmf_detect_state->hit2;
+ dtmf_detect_state->hit2 = dtmf_detect_state->hit3;
+ dtmf_detect_state->hit3 = hit;
+ /* Reinitialise the detector for the next block */
+ for (i = 0; i < GRID_FACTOR; i++) {
+ goertzel_init (&dtmf_detect_state->row_out[i], &dtmf_detect_row[i]);
+ goertzel_init (&dtmf_detect_state->col_out[i], &dtmf_detect_col[i]);
+ goertzel_init (&dtmf_detect_state->row_out2nd[i], &dtmf_detect_row_2nd[i]);
+ goertzel_init (&dtmf_detect_state->col_out2nd[i], &dtmf_detect_col_2nd[i]);
+ }
dtmf_detect_state->energy = 0.0;
- dtmf_detect_state->current_sample = 0;
- }
- if ((!dtmf_detect_state->mhit) || (dtmf_detect_state->mhit != hit)) {
+ dtmf_detect_state->current_sample = 0;
+ }
+ if ((!dtmf_detect_state->mhit) || (dtmf_detect_state->mhit != hit)) {
dtmf_detect_state->mhit = 0;
return(0);
- }
- return (hit);
+ }
+ return (hit);
}
char *buf,
int max)
{
- if (max > dtmf_detect_state->current_digits) {
- max = dtmf_detect_state->current_digits;
+ if (max > dtmf_detect_state->current_digits) {
+ max = dtmf_detect_state->current_digits;
+ }
+ if (max > 0) {
+ memcpy (buf, dtmf_detect_state->digits, max);
+ memmove (dtmf_detect_state->digits, dtmf_detect_state->digits + max, dtmf_detect_state->current_digits - max);
+ dtmf_detect_state->current_digits -= max;
}
- if (max > 0) {
- memcpy (buf, dtmf_detect_state->digits, max);
- memmove (dtmf_detect_state->digits, dtmf_detect_state->digits + max, dtmf_detect_state->current_digits - max);
- dtmf_detect_state->current_digits -= max;
- }
- buf[max] = '\0';
- return max;
+ buf[max] = '\0';
+ return max;
}
/* For Emacs: