Browse Source

New DTMF code, this one actually works.

Working squelch.
master
Jeroen Vreeken 5 years ago
parent
commit
b70ac0782a
8 changed files with 430 additions and 539 deletions
  1. +1
    -2
      Makefile
  2. +41
    -5
      analog_trx.c
  3. +383
    -0
      dsp.c
  4. +3
    -1
      dtmf.h
  5. +0
    -238
      dtmf_detect.c
  6. +0
    -143
      dtmf_detect.h
  7. +0
    -148
      dtmf_gen.c
  8. +2
    -2
      sound.c

+ 1
- 2
Makefile View File

@ -8,8 +8,7 @@ SRCS = \
eth_ar.c \
interface.c \
sound.c \
dtmf_detect.c \
dtmf_gen.c
dsp.c
OBJS = $(SRCS:.c=.o)


+ 41
- 5
analog_trx.c View File

@ -47,12 +47,19 @@ struct CODEC2 *rx_codec;
int nr_samples;
int16_t *samples_rx;
int nr_rx;
int squelch_level_c = 10000000;
int squelch_level_o = 100000;
int squelch_on_delay = 3;
int squelch_off_delay = 10;
void squelch(int16_t *samples, int nr)
{
double total = 0;
double high = 0;
double high = 0, low = 0;
int i;
static int open_cnt = 0;
static int close_cnt = 0;
static bool state = false;
for (i = 0; i < nr; i++) {
total += samples[i] * samples[i];
@ -60,23 +67,51 @@ void squelch(int16_t *samples, int nr)
high += samples[i];
else
high -= samples[i];
if (i & 2)
low += samples[i];
else
low -= samples[i];
}
high = high * high;
low = low * low;
if ((!state && high < squelch_level_o) ||
(state && high < squelch_level_c)) {
open_cnt++;
close_cnt = 0;
} else {
open_cnt = 0;
close_cnt++;
}
if (!state && open_cnt >= squelch_on_delay) {
printf("Open\t%f\t%f\t%f\n", high, low, high/low);
state = true;
}
if (state && close_cnt >= squelch_off_delay) {
printf("Close\t%f\t%f\t%f\n", high, low, high/low);
state = false;
}
printf("%f\t%f\t%f\n", total, high, total/high);
// static int cnt;
// cnt++;
// if ((cnt & 7) == 0)
// printf("%f\t%f\t%f\n", high, low, high/low);
}
static void cb_control(char *ctrl)
{
printf("DTMF: %s\n", ctrl);
return;
}
static void cb_sound_in(int16_t *samples, int nr)
{
// squelch(samples, nr);
dtmf_decode(samples, nr, cb_control);
squelch(samples, nr);
dtmf_rx(samples, nr, cb_control);
while (nr) {
int copy = nr_samples - nr_rx;
if (copy > nr)
@ -376,6 +411,7 @@ int main(int argc, char **argv)
fd_int = interface_init(netname, mac);
sound_init(sounddev, cb_sound_in, nr_samples);
hl_init();
dtmf_init();
prio();


+ 383
- 0
dsp.c View File

@ -0,0 +1,383 @@
// modified by Luigi Auriemma to work also with damaged audio and allow customized parameters
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 1999 - 2005, Digium, Inc.
*
* Mark Spencer <markster@digium.com>
*
* Goertzel routines are borrowed from Steve Underwood's tremendous work on the
* DTMF detector.
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Convenience Signal Processing routines
*
* \author Mark Spencer <markster@digium.com>
* \author Steve Underwood <steveu@coppice.org>
*/
/* Some routines from tone_detect.c by Steven Underwood as published under the zapata library */
/*
tone_detect.c - General telephony tone detection, and specific
detection of DTMF.
Copyright (C) 2001 Steve Underwood <steveu@coppice.org>
Despite my general liking of the GPL, I place this code in the
public domain for the benefit of all mankind - even the slimy
ones who might try to proprietize my work and use it to my
detriment.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
//#include <malloc.h>
#include "dtmf.h"
#define DSP_DIGITMODE_DTMF 0 /*!< Detect DTMF digits */
#define DSP_DIGITMODE_MF 1 /*!< Detect MF digits */
#define DSP_DIGITMODE_NOQUELCH (1 << 8) /*!< Do not quelch DTMF from in-band */
#define DSP_DIGITMODE_RELAXDTMF (1 << 11) /*!< "Radio" mode (relaxed DTMF) */
#define MAX_DTMF_DIGITS 1024
/* Basic DTMF specs:
*
* Minimum tone on = 40ms
* Minimum tone off = 50ms
* Maximum digit rate = 10 per second
* Normal twist <= 8dB accepted
* Reverse twist <= 4dB accepted
* S/N >= 15dB will detect OK
* Attenuation <= 26dB will detect OK
* Frequency tolerance +- 1.5% will detect, +-3.5% will reject
*/
/* PARAMETERS */
static double DTMF_OPTIMIZED_VALUE = 102;
//#define DTMF_THRESHOLD 8.0e7
static double DTMF_THRESHOLD = 800000000.0; // aluigi work-around
static double DTMF_NORMAL_TWIST = 6.3; /* 8dB */
#if 0
#ifdef RADIO_RELAX
static double DTMF_REVERSE_TWIST1 = 6.5;
static double DTMF_REVERSE_TWIST2 = 2.5;
#else
static double DTMF_REVERSE_TWIST1 = 4.0;
static double DTMF_REVERSE_TWIST2 = 2.5;
#endif
#define DTMF_REVERSE_TWIST ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? DTMF_REVERSE_TWIST1 : DTMF_REVERSE_TWIST2) /* 4dB normal */
static double DTMF_2ND_HARMONIC_ROW1 = 1.7;
static double DTMF_2ND_HARMONIC_ROW2 = 2.5;
#define DTMF_2ND_HARMONIC_ROW ((digitmode & DSP_DIGITMODE_RELAXDTMF) ? DTMF_2ND_HARMONIC_ROW1 : DTMF_2ND_HARMONIC_ROW2) /* 4dB normal */
static double DTMF_2ND_HARMONIC_COL = 63.1; /* 18dB */
static double DTMF_TO_TOTAL_ENERGY = 42.0;
#endif
static double DTMF_RELATIVE_PEAK_ROW = 6.3; /* 8dB */
static double DTMF_RELATIVE_PEAK_COL = 6.3; /* 8dB */
static int SAMPLE_RATE = 8000;
typedef struct {
int v2;
int v3;
int chunky;
int fac;
int samples;
} goertzel_state_t;
typedef struct {
int value;
int power;
} goertzel_result_t;
typedef struct
{
goertzel_state_t row_out[4];
goertzel_state_t col_out[4];
int lasthit;
int current_hit;
double energy;
int current_sample;
} dtmf_detect_state_t;
typedef struct
{
char digits[MAX_DTMF_DIGITS + 1];
int current_digits;
int detected_digits;
int lost_digits;
dtmf_detect_state_t dtmf;
} digit_detect_state_t;
static double dtmf_row[] =
{
697.0, 770.0, 852.0, 941.0
};
static double dtmf_col[] =
{
1209.0, 1336.0, 1477.0, 1633.0
};
static char dtmf_positions[] = "123A" "456B" "789C" "*0#D";
static inline void goertzel_sample(goertzel_state_t *s, short sample)
{
int v1;
v1 = s->v2;
s->v2 = s->v3;
s->v3 = (s->fac * s->v2) >> 15;
s->v3 = s->v3 - v1 + (sample >> s->chunky);
if (abs(s->v3) > 32768) {
s->chunky++;
s->v3 = s->v3 >> 1;
s->v2 = s->v2 >> 1;
v1 = v1 >> 1;
}
}
static inline void goertzel_update(goertzel_state_t *s, short *samps, int count)
{
int i;
for (i=0;i<count;i++)
goertzel_sample(s, samps[i]);
}
static inline double goertzel_result(goertzel_state_t *s)
{
goertzel_result_t r;
r.value = (s->v3 * s->v3) + (s->v2 * s->v2);
r.value -= ((s->v2 * s->v3) >> 15) * s->fac;
r.power = s->chunky * 2;
return (double)r.value * (double)(1 << r.power);
}
static inline void goertzel_init(goertzel_state_t *s, double freq, int samples)
{
s->v2 = s->v3 = s->chunky = 0.0;
s->fac = (int)(32768.0 * 2.0 * cos(2.0 * M_PI * freq / SAMPLE_RATE));
s->samples = samples;
}
static inline void goertzel_reset(goertzel_state_t *s)
{
s->v2 = s->v3 = s->chunky = 0.0;
}
static void ast_dtmf_detect_init (dtmf_detect_state_t *s)
{
int i;
s->lasthit = 0;
s->current_hit = 0;
for (i = 0; i < 4; i++) {
goertzel_init (&s->row_out[i], dtmf_row[i], DTMF_OPTIMIZED_VALUE);
goertzel_init (&s->col_out[i], dtmf_col[i], DTMF_OPTIMIZED_VALUE);
s->energy = 0.0;
}
s->current_sample = 0;
}
static void ast_digit_detect_init(digit_detect_state_t *s)
{
/* s->current_digits = 0;
s->detected_digits = 0;
s->lost_digits = 0;
s->digits[0] = '\0';
*/
ast_dtmf_detect_init(&s->dtmf);
}
/*
static void store_digit(digit_detect_state_t *s, char digit)
{
s->detected_digits++;
if (s->current_digits < MAX_DTMF_DIGITS) {
s->digits[s->current_digits++] = digit;
s->digits[s->current_digits] = '\0';
} else {
//ast_log(LOG_WARNING, "Digit lost due to full buffer\n");
s->lost_digits++;
}
}
*/
static int dtmf_detect(digit_detect_state_t *s, int16_t amp[], int samples,
int digitmode, int *writeback, void (*cb)(char *))
{
double row_energy[4];
double col_energy[4];
double famp;
int i;
int j;
int sample;
int best_row;
int best_col;
int hit;
int limit;
hit = 0;
for (sample = 0; sample < samples; sample = limit) {
/* DTMF_OPTIMIZED_VALUE is optimised to meet the DTMF specs. */
if ((samples - sample) >= (DTMF_OPTIMIZED_VALUE - s->dtmf.current_sample))
limit = sample + (DTMF_OPTIMIZED_VALUE - s->dtmf.current_sample);
else
limit = samples;
/* The following unrolled loop takes only 35% (rough estimate) of the
time of a rolled loop on the machine on which it was developed */
for (j = sample; j < limit; j++) {
famp = amp[j];
s->dtmf.energy += famp*famp;
/* With GCC 2.95, the following unrolled code seems to take about 35%
(rough estimate) as long as a neat little 0-3 loop */
goertzel_sample(s->dtmf.row_out, amp[j]);
goertzel_sample(s->dtmf.col_out, amp[j]);
goertzel_sample(s->dtmf.row_out + 1, amp[j]);
goertzel_sample(s->dtmf.col_out + 1, amp[j]);
goertzel_sample(s->dtmf.row_out + 2, amp[j]);
goertzel_sample(s->dtmf.col_out + 2, amp[j]);
goertzel_sample(s->dtmf.row_out + 3, amp[j]);
goertzel_sample(s->dtmf.col_out + 3, amp[j]);
}
s->dtmf.current_sample += (limit - sample);
if (s->dtmf.current_sample < DTMF_OPTIMIZED_VALUE) {
if (hit && !((digitmode & DSP_DIGITMODE_NOQUELCH))) {
/* If we had a hit last time, go ahead and clear this out since likely it
will be another hit */
for (i=sample;i<limit;i++)
amp[i] = 0;
*writeback = 1;
}
continue;
}
/* We are at the end of a DTMF detection block */
/* Find the peak row and the peak column */
row_energy[0] = goertzel_result (&s->dtmf.row_out[0]);
col_energy[0] = goertzel_result (&s->dtmf.col_out[0]);
for (best_row = best_col = 0, i = 1; i < 4; i++) {
row_energy[i] = goertzel_result (&s->dtmf.row_out[i]);
if (row_energy[i] > row_energy[best_row])
best_row = i;
col_energy[i] = goertzel_result (&s->dtmf.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 &&
col_energy[best_col] >= DTMF_THRESHOLD &&
// col_energy[best_col] < row_energy[best_row] *DTMF_REVERSE_TWIST && // aluigi work-around
col_energy[best_col]*DTMF_NORMAL_TWIST > row_energy[best_row]) {
/* Relative peak test */
for (i = 0; i < 4; 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 fraction of total energy test */
if (i >= 4 /*&&
(row_energy[best_row] + col_energy[best_col]) > DTMF_TO_TOTAL_ENERGY*s->dtmf.energy*/) { // aluigi work-around
/* Got a hit */
hit = dtmf_positions[(best_row << 2) + best_col];
if (!(digitmode & DSP_DIGITMODE_NOQUELCH)) {
/* Zero out frame data if this is part DTMF */
for (i=sample;i<limit;i++)
amp[i] = 0;
*writeback = 1;
}
}
}
/* The logic in the next test is:
For digits 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 != s->dtmf.current_hit) {
if (hit && s->dtmf.lasthit == hit) {
s->dtmf.current_hit = hit;
char cbv[2] = { hit, 0 };
cb(cbv);
} else if (s->dtmf.lasthit != s->dtmf.current_hit) {
s->dtmf.current_hit = 0;
}
}
s->dtmf.lasthit = hit;
/* Reinitialise the detector for the next block */
for (i = 0; i < 4; i++) {
goertzel_reset(&s->dtmf.row_out[i]);
goertzel_reset(&s->dtmf.col_out[i]);
}
s->dtmf.energy = 0.0;
s->dtmf.current_sample = 0;
}
return samples - sample;
}
static digit_detect_state_t dtmf;
int dtmf_rx(short *smp, int nr, void (*cb)(char *))
{
int writeback;
dtmf_detect(&dtmf, smp, nr, DSP_DIGITMODE_NOQUELCH, &writeback, cb);
return 0;
}
int dtmf_init(void)
{
ast_digit_detect_init(&dtmf);
return 0;
}

+ 3
- 1
dtmf.h View File

@ -1,6 +1,8 @@
#ifndef _INCLUDE_DTMF_H_
#define _INCLUDE_DTMF_H_
int dtmf_decode(short *samples, int nr, void (*cb)(char *));
int dtmf_rx(short *smp, int nr, void (*cb)(char *));
int dtmf_init(void);
#endif /* _INCLUDE_DTMF_H_ */

+ 0
- 238
dtmf_detect.c View File

@ -1,238 +0,0 @@
/*
* detect.c
* This program will detect MF tones and normal
* dtmf tones as well as some other common tones such
* as BUSY, DIALTONE and RING.
* The program uses a goertzel algorithm to detect
* the power of various frequency ranges.
*
* input is assumed to be 8 bit samples. The program
* can use either signed or unsigned samples according
* to a compile time option:
*
* cc -DUNSIGNED detect.c -o detect
*
* for unsigned input (soundblaster) and:
*
* cc detect.c -o detect
*
* for signed input (amiga samples)
* if you dont want flushes, -DNOFLUSH
*
* Tim N.
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include "dtmf_detect.h"
#include "dtmf.h"
/* translation of above codes into text */
static char *dtran[] = {
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"*", "#", "A", "B", "C", "D",
"+C11 ", "+C12 ", " KP1+", " KP2+", "+ST ",
" 2400 ", " 2600 ", " 2400+2600 ",
" DIALTONE ", " RING ", " BUSY ","" };
/*
* calculate the power of each tone according
* to a modified goertzel algorithm described in
* _digital signal processing applications using the
* ADSP-2100 family_ by Analog Devices
*
* input is 'data', N sample values
*
* ouput is 'power', NUMTONES values
* corresponding to the power of each tone
*/
static int calc_power(short *data, float *power)
{
float u0[NUMTONES],u1[NUMTONES],t,in;
int i,j;
for(j=0; j<NUMTONES; j++) {
u0[j] = 0.0;
u1[j] = 0.0;
}
for(i=0; i<N; i++) { /* feedback */
in = ((int)data[i] - 32768) / 32768.0;
for(j=0; j<NUMTONES; j++) {
t = u0[j];
u0[j] = in + coef[j] * u0[j] - u1[j];
u1[j] = t;
}
}
for(j=0; j<NUMTONES; j++) /* feedforward */
power[j] = u0[j] * u0[j] + u1[j] * u1[j] - coef[j] * u0[j] * u1[j];
return(0);
}
/*
* detect which signals are present.
*
* return values defined in the include file
* note: DTMF 3 and MF 7 conflict. To resolve
* this the program only reports MF 7 between
* a KP and an ST, otherwise DTMF 3 is returned
*/
static int decode(short *data)
{
float power[NUMTONES],thresh,maxpower;
int on[NUMTONES],on_count;
int bcount, rcount, ccount;
int row, col, b1, b2, i;
int r[4],c[4],b[8];
static int MFmode=0;
calc_power(data,power);
for(i=0, maxpower=0.0; i<NUMTONES;i++)
if(power[i] > maxpower)
maxpower = power[i];
/*
for(i=0;i<NUMTONES;i++)
printf("%f, ",power[i]);
printf("\n");
*/
if(maxpower < THRESH) /* silence? */
return(DSIL);
thresh = RANGE * maxpower; /* allowable range of powers */
for(i=0, on_count=0; i<NUMTONES; i++) {
if(power[i] > thresh) {
on[i] = 1;
on_count ++;
} else
on[i] = 0;
}
/*
printf("%4d: ",on_count);
for(i=0;i<NUMTONES;i++)
putchar('0' + on[i]);
printf("\n");
*/
if(on_count == 1) {
if(on[B7])
return(D24);
if(on[B8])
return(D26);
return(-1);
}
if(on_count == 2) {
if(on[X1] && on[X2])
return(DDT);
if(on[X2] && on[X3])
return(DRING);
if(on[X3] && on[X4])
return(DBUSY);
b[0]= on[B1]; b[1]= on[B2]; b[2]= on[B3]; b[3]= on[B4];
b[4]= on[B5]; b[5]= on[B6]; b[6]= on[B7]; b[7]= on[B8];
c[0]= on[C1]; c[1]= on[C2]; c[2]= on[C3]; c[3]= on[C4];
r[0]= on[R1]; r[1]= on[R2]; r[2]= on[R3]; r[3]= on[R4];
for(i=0, bcount=0; i<8; i++) {
if(b[i]) {
bcount++;
b2 = b1;
b1 = i;
}
}
for(i=0, rcount=0; i<4; i++) {
if(r[i]) {
rcount++;
row = i;
}
}
for(i=0, ccount=0; i<4; i++) {
if(c[i]) {
ccount++;
col = i;
}
}
if(rcount==1 && ccount==1) { /* DTMF */
if(col == 3) /* A,B,C,D */
return(DA + row);
else {
if(row == 3 && col == 0 )
return(DSTAR);
if(row == 3 && col == 2 )
return(DPND);
if(row == 3)
return(D0);
if(row == 0 && col == 2) { /* DTMF 3 conflicts with MF 7 */
if(!MFmode)
return(D3);
} else
return(D1 + col + row*3);
}
}
if(bcount == 2) { /* MF */
/* b1 has upper number, b2 has lower */
switch(b1) {
case 7: return( (b2==6)? D2426: -1);
case 6: return(-1);
case 5: if(b2==2 || b2==3) /* KP */
MFmode=1;
if(b2==4) /* ST */
MFmode=0;
return(DC11 + b2);
/* MF 7 conflicts with DTMF 3, but if we made it
* here then DTMF 3 was already tested for
*/
case 4: return( (b2==3)? D0: D7 + b2);
case 3: return(D4 + b2);
case 2: return(D2 + b2);
case 1: return(D1);
}
}
return(-1);
}
if(on_count == 0)
return(DSIL);
return(-1);
}
static short dtmf_data[N];
static int dtmf_data_len;
static int prev_dec = DSIL;
int dtmf_decode(short *samples, int nr, void (*cb)(char *))
{
while (nr) {
int copy = N - dtmf_data_len;
if (copy > nr)
copy = nr;
memcpy(dtmf_data + dtmf_data_len, samples, copy);
samples += copy;
dtmf_data_len += copy;
nr -= copy;
if (dtmf_data_len == N) {
int dec = decode(dtmf_data);
if (dec >= 0 && dec != DSIL) {
if (prev_dec != dec) {
cb(dtran[dec]);
printf("DTMF: %s\n", dtran[dec]);
}
} else {
// printf("%d\n", dec);
}
prev_dec = dec;
dtmf_data_len = 0;
}
}
return 0;
}

+ 0
- 143
dtmf_detect.h View File

@ -1,143 +0,0 @@
/*
*
* goertzel aglorithm, find the power of different
* frequencies in an N point DFT.
*
* ftone/fsample = k/N
* k and N are integers. fsample is 8000 (8khz)
* this means the *maximum* frequency resolution
* is fsample/N (each step in k corresponds to a
* step of fsample/N hz in ftone)
*
* N was chosen to minimize the sum of the K errors for
* all the tones detected... here are the results :
*
* Best N is 240, with the sum of all errors = 3.030002
* freq freq actual k kactual kerr
* ---- ------------ ------ ------- -----
* 350 (366.66667) 10.500 (11) 0.500
* 440 (433.33333) 13.200 (13) 0.200
* 480 (466.66667) 14.400 (14) 0.400
* 620 (633.33333) 18.600 (19) 0.400
* 697 (700.00000) 20.910 (21) 0.090
* 700 (700.00000) 21.000 (21) 0.000
* 770 (766.66667) 23.100 (23) 0.100
* 852 (866.66667) 25.560 (26) 0.440
* 900 (900.00000) 27.000 (27) 0.000
* 941 (933.33333) 28.230 (28) 0.230
* 1100 (1100.00000) 33.000 (33) 0.000
* 1209 (1200.00000) 36.270 (36) 0.270
* 1300 (1300.00000) 39.000 (39) 0.000
* 1336 (1333.33333) 40.080 (40) 0.080
**** I took out 1477.. too close to 1500
* 1477 (1466.66667) 44.310 (44) 0.310
****
* 1500 (1500.00000) 45.000 (45) 0.000
* 1633 (1633.33333) 48.990 (49) 0.010
* 1700 (1700.00000) 51.000 (51) 0.000
* 2400 (2400.00000) 72.000 (72) 0.000
* 2600 (2600.00000) 78.000 (78) 0.000
*
* notice, 697 and 700hz are indestinguishable (same K)
* all other tones have a seperate k value.
* these two tones must be treated as identical for our
* analysis.
*
* The worst tones to detect are 350 (error = 0.5,
* detet 367 hz) and 852 (error = 0.44, detect 867hz).
* all others are very close.
*
*/
#ifndef _INCLUDE_DTMF_DETECT_H_
#define _INCLUDE_DTMF_DETECT_H_
#define FSAMPLE 8000
#define N 240
int k[] = { 11, 13, 14, 19, 21, 23, 26, 27, 28, 33, 36, 39, 40,
/*44,*/ 45, 49, 51, 72, 78, };
/* coefficients for above k's as:
* 2 * cos( 2*pi* k/N )
*/
float coef[] = {
1.917639, 1.885283, 1.867161, 1.757634,
1.705280, 1.648252, 1.554292, 1.520812, 1.486290,
1.298896, 1.175571, 1.044997, 1.000000, /* 0.813473,*/
0.765367, 0.568031, 0.466891, -0.618034, -0.907981, };
#define X1 0 /* 350 dialtone */
#define X2 1 /* 440 ring, dialtone */
#define X3 2 /* 480 ring, busy */
#define X4 3 /* 620 busy */
#define R1 4 /* 697, dtmf row 1 */
#define R2 5 /* 770, dtmf row 2 */
#define R3 6 /* 852, dtmf row 3 */
#define R4 8 /* 941, dtmf row 4 */
#define C1 10 /* 1209, dtmf col 1 */
#define C2 12 /* 1336, dtmf col 2 */
#define C3 13 /* 1477, dtmf col 3 */
#define C4 14 /* 1633, dtmf col 4 */
#define B1 4 /* 700, blue box 1 */
#define B2 7 /* 900, bb 2 */
#define B3 9 /* 1100, bb 3 */
#define B4 11 /* 1300, bb4 */
#define B5 13 /* 1500, bb5 */
#define B6 15 /* 1700, bb6 */
#define B7 16 /* 2400, bb7 */
#define B8 17 /* 2600, bb8 */
#define NUMTONES 18
/* values returned by detect
* 0-9 DTMF 0 through 9 or MF 0-9
* 10-11 DTMF *, #
* 12-15 DTMF A,B,C,D
* 16-20 MF last column: C11, C12, KP1, KP2, ST
* 21 2400
* 22 2600
* 23 2400 + 2600
* 24 DIALTONE
* 25 RING
* 26 BUSY
* 27 silence
* -1 invalid
*/
#define D0 0
#define D1 1
#define D2 2
#define D3 3
#define D4 4
#define D5 5
#define D6 6
#define D7 7
#define D8 8
#define D9 9
#define DSTAR 10
#define DPND 11
#define DA 12
#define DB 13
#define DC 14
#define DD 15
#define DC11 16
#define DC12 17
#define DKP1 18
#define DKP2 19
#define DST 20
#define D24 21
#define D26 22
#define D2426 23
#define DDT 24
#define DRING 25
#define DBUSY 26
#define DSIL 27
#define RANGE 0.1 /* any thing higher than RANGE*peak is "on" */
#define THRESH 100.0 /* minimum level for the loudest tone */
#define FLUSH_TIME 100 /* 100 frames = 3 seconds */
#endif /* _INCLUDE_DTMF_DETECT_H_ */

+ 0
- 148
dtmf_gen.c View File

@ -1,148 +0,0 @@
/* -------- local defines (if we had more.. seperate file) ----- */
#define FSAMPLE 8000 /* sampling rate, 8KHz */
/*
* FLOAT_TO_SAMPLE converts a float in the range -1.0 to 1.0
* into a format valid to be written out in a sound file
* or to a sound device
*/
#ifdef SIGNED
# define FLOAT_TO_SAMPLE(x) ((char)((x) * 127.0))
#else
# define FLOAT_TO_SAMPLE(x) ((char)((x + 1.0) * 127.0))
#endif
typedef char sample;
/* --------------------------------------------------------------- */
/*
* take the sine of x, where x is 0 to 65535 (for 0 to 360 degrees)
*/
float mysine(in)
short in;
{
static float coef[] = {
3.140625, 0.02026367, -5.325196, 0.5446778, 1.800293 };
float x,y,res;
int sign,i;
if(in < 0) { /* force positive */
sign = -1;
in = -in;
} else
sign = 1;
if(in >= 0x4000) /* 90 degrees */
in = 0x8000 - in; /* 180 degrees - in */
x = in * (1/32768.0);
y = x; /* y holds x^i) */
res = 0;
for(i=0; i<5; i++) {
res += y * coef[i];
y *= x;
}
return(res * sign);
}
/*
* play tone1 and tone2 (in Hz)
* for 'length' milliseconds
* outputs samples to sound_out
*/
void two_tones(sound_out,tone1,tone2,length)
int sound_out;
unsigned int tone1,tone2,length;
{
#define BLEN 128
sample cout[BLEN] __attribute((unused));
float out;
unsigned int ad1,ad2;
short c1,c2;
int i,l,x;
ad1 = (tone1 << 16) / FSAMPLE;
ad2 = (tone2 << 16) / FSAMPLE;
l = (length * FSAMPLE) / 1000;
x = 0;
for( c1=0, c2=0, i=0 ;
i < l;
i++, c1+= ad1, c2+= ad2 ) {
out = (mysine(c1) + mysine(c2)) * 0.5;
cout[x++] = FLOAT_TO_SAMPLE(out);
if (x==BLEN) {
// write(sound_out, cout, x * sizeof(sample));
x=0;
}
}
// write(sound_out, cout, x);
}
/*
* silence on 'sound_out'
* for length milliseconds
*/
void silence(sound_out,length)
int sound_out;
unsigned int length;
{
int l,i,x;
static sample c0 = FLOAT_TO_SAMPLE(0.0);
sample cout[BLEN] __attribute((unused));
x = 0;
l = (length * FSAMPLE) / 1000;
for(i=0; i < l; i++) {
cout[x++] = c0;
if (x==BLEN) {
// write(sound_out, cout, x * sizeof(sample));
x=0;
}
}
// write(sound_out, cout, x);
}
/*
* play a single dtmf tone
* for a length of time,
* input is 0-9 for digit, 10 for * 11 for #
*/
void dtmf(sound_fd, digit, length)
int sound_fd;
int digit, length;
{
/* Freqs for 0-9, *, # */
static int row[] = {
941, 697, 697, 697, 770, 770, 770, 852, 852, 852, 941, 941 };
static int col[] = {
1336, 1209, 1336, 1477, 1209, 1336, 1477, 1209, 1336, 1447,
1209, 1477 };
two_tones(sound_fd, row[digit], col[digit], length);
}
/*
* take a string and output as dtmf
* valid characters, 0-9, *, #
* all others play as 50ms silence
*/
void dial(sound_fd, number)
int sound_fd;
char *number;
{
int i,x;
char c;
for(i=0;number[i];i++) {
c = number[i];
x = -1;
if(c >= '0' && c <= '9')
x = c - '0';
else if(c == '*')
x = 10;
else if(c == '#')
x = 11;
if(x >= 0)
dtmf(sound_fd, x, 50);
silence(sound_fd,50);
}
}

+ 2
- 2
sound.c View File

@ -124,8 +124,8 @@ int sound_param(snd_pcm_t *pcm_handle)
snd_pcm_hw_params_set_rate_near (pcm_handle, hw_params, &rrate, NULL);
snd_pcm_hw_params_set_channels (pcm_handle, hw_params, 1);
snd_pcm_uframes_t buffer_size = nr * 10 * 100;
snd_pcm_uframes_t period_size = nr * 10;
snd_pcm_uframes_t buffer_size = nr * 2 * 10;
snd_pcm_uframes_t period_size = nr * 2;
snd_pcm_hw_params_set_buffer_size_near (pcm_handle, hw_params, &buffer_size);
snd_pcm_hw_params_set_period_size_near (pcm_handle, hw_params, &period_size, NULL);


Loading…
Cancel
Save