forked from geofft/linmodem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdtmf.c
192 lines (164 loc) · 4.85 KB
/
dtmf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
* DTMF demodulator, inspirated from the multimon project of Thomas
* Sailer ([email protected], [email protected]). It is different
* in the sense that it uses a true block based DFT estimator.
*
* Copyright (c) 1999 Fabrice Bellard.
*
* This code is released under the GNU General Public License version
* 2. Please read the file COPYING to know the exact terms of the
* license.
*/
#include "lm.h"
/*
* DTMF frequencies
*
* 1209 1336 1477 1633
* 697 1 2 3 A
* 770 4 5 6 B
* 852 7 8 9 C
* 941 * 0 # D
* */
static const char *dtmf_transl = "123A456B789C*0#D";
static int dtmf_freq[] = {
1209, 1336, 1477, 1633,
697, 770, 852, 941,
};
#define SAMPLE_RATE 8000
/* Each DTMF digit is estimed on N samples by estimating the DFT of
the signal. It is quite reliable, but the frequency resolution is
not accurate enough to meet the very strict ITU requirements. */
/* DTMF modulation */
void DTMF_mod_init(DTMF_mod_state *s)
{
s->t1 = s->t2 = 0;
s->omega1 = 0;
s->samples_left = 0;
}
/* compute parameters for a new digit */
static void compute_params(DTMF_mod_state *s)
{
const char *p;
int v,digit,f1,f2;
/* if we were playing a digit, we make a pause */
if (s->omega1 != 0)
goto nodigit;
digit = s->get_digit(s->opaque);
if (digit == -1)
goto nodigit;
p = dtmf_transl;
while (*p != digit && *p) p++;
if (*p) {
v = p - dtmf_transl;
f1 = dtmf_freq[v & 3];
f2 = dtmf_freq[4 + (v >> 2)];
s->omega1 = (PHASE_BASE * f1) / SAMPLE_RATE;
s->omega2 = (PHASE_BASE * f2) / SAMPLE_RATE;
/* amplitude */
s->amp = (int) (pow(10, s->dtmf_level / 20.0) * 32768.0);
/* number of samples to play */
s->samples_left = (s->digit_length_ms * SAMPLE_RATE) / 1000;
} else {
nodigit:
s->omega1 = 0;
s->samples_left = (s->digit_pause_ms * SAMPLE_RATE) / 1000;
}
}
void DTMF_mod(DTMF_mod_state *s, s16 *samples, unsigned int nb)
{
int len, t1, t2, amp, i;
while (nb > 0) {
if (s->samples_left == 0) {
compute_params(s);
}
len = nb;
if (len > s->samples_left)
len = s->samples_left;
if (s->omega1 != 0) {
t1 = s->t1;
t2 = s->t2;
amp = s->amp;
for(i=0;i<len;i++) {
int v;
v = ((dsp_cos(t1) + dsp_cos(t2)) * amp) >> COS_BITS;
samples[i] = v;
t1 += s->omega1;
t2 += s->omega2;
}
s->t1 = t1;
s->t2 = t2;
} else {
memset(samples, 0, nb * 2); /* silence between digits */
}
nb -= len;
samples += len;
s->samples_left -= len;
}
}
/* DTMF demodulation */
void DTMF_demod_init(DTMF_demod_state *s)
{
int i;
for(i=0;i<DTMF_N;i++) {
s->cos_tab[i] = (int) (cos( 2 * M_PI * i / DTMF_N) * COS_BASE);
s->sin_tab[i] = (int) (sin( 2 * M_PI * i / DTMF_N) * COS_BASE);
}
for(i=0;i<8;i++) {
float v;
v = (float)dtmf_freq[i] / (float)SAMPLE_RATE * (float)DTMF_N;
s->dtmf_coefs[i] = (int)rint(v);
}
s->buf_ptr = 0;
s->last_digit = 0;
}
int find_max(int *val,int n)
{
int i,j,max;
j = 0;
max = val[0];
for(i=1;i<n;i++) {
if (val[i] > max) {
j = i;
max = val[i];
}
}
return j;
}
void DTMF_demod(DTMF_demod_state *s,
const s16 *samples, unsigned int nb)
{
int i, j, power[8], power0, v1, v2, digit, bits, p1, p2, p0;
for(j=0;j<nb;j++) {
s->buf[s->buf_ptr++] = samples[j];
if (s->buf_ptr >= DTMF_N) {
/* decision based on one block */
bits = dsp_max_bits(s->buf, DTMF_N);
if (bits < 8) bits = 8;
dsp_sar_tab(s->buf, DTMF_N, bits - 8);
power0 = dsp_norm2(s->buf, DTMF_N, 0);
for(i=0;i<8;i++) {
power[i] = compute_DFT(s->cos_tab, s->sin_tab, s->buf,
s->dtmf_coefs[i], DTMF_N);
}
v1 = find_max(power, 4);
v2 = find_max(power + 4, 4);
p0 = (int)(power0 * 0.3);
p1 = (2 * power[v1]) / DTMF_N;
p2 = (2 * power[v2 + 4]) / DTMF_N;
#if 0
printf("%d %d %d %f %f\n",p0, v1, v2, (float) p1 / p0,(float) p2 / p0);
#endif
if (p1 > p0 && p2 > p0)
digit = dtmf_transl[v1 | (v2 << 2)];
else
digit = 0;
if (digit != s->last_digit) {
if (digit != 0) {
s->put_digit(s->opaque, digit);
}
s->last_digit = digit;
}
s->buf_ptr = 0;
}
}
}