aboutsummaryrefslogtreecommitdiff
path: root/webrtc/voice_engine/dtmf_inband.cc
blob: 309bd54609a8cad2175632cf48f611b95d99084a (plain)
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "webrtc/voice_engine/dtmf_inband.h"

#include <assert.h>

#include "webrtc/system_wrappers/include/critical_section_wrapper.h"
#include "webrtc/system_wrappers/include/trace.h"

namespace webrtc {

const int16_t Dtmf_a_times2Tab8Khz[8]=
{
	27978, 26956, 25701, 24219,
	19073, 16325, 13085, 9314
};

const int16_t Dtmf_a_times2Tab16Khz[8]=
{
	31548, 31281, 30951, 30556,
	29144, 28361, 27409, 26258
};

const int16_t Dtmf_a_times2Tab32Khz[8]=
{
	32462,32394, 32311, 32210, 31849, 31647, 31400, 31098
};

// Second table is sin(2*pi*f/fs) in Q14

const int16_t Dtmf_ym2Tab8Khz[8]=
{
	8527, 9315, 10163, 11036,
	13322, 14206, 15021, 15708
};

const int16_t Dtmf_ym2Tab16Khz[8]=
{
	4429, 4879, 5380, 5918,
	7490, 8207, 8979, 9801
};

const int16_t Dtmf_ym2Tab32Khz[8]=
{
	2235, 2468, 2728, 3010, 3853, 4249, 4685, 5164
};

const int16_t Dtmf_dBm0kHz[37]=
{
       16141,      14386,      12821,      11427,      10184,       9077,
        8090,       7210,       6426,       5727,       5104,       4549,
        4054,       3614,       3221,       2870,       2558,       2280,
        2032,       1811,       1614,       1439,       1282,       1143,
        1018,        908,        809,        721,        643,        573,
         510,        455,        405,        361,        322,        287,
		 256
};


DtmfInband::DtmfInband(int32_t id) :
    _critSect(*CriticalSectionWrapper::CreateCriticalSection()),
    _id(id),
    _outputFrequencyHz(8000),
    _frameLengthSamples(0),
    _remainingSamples(0),
    _eventCode(0),
    _attenuationDb(0),
    _lengthMs(0),
    _reinit(true),
    _playing(false),
    _delaySinceLastToneMS(1000)
{
    memset(_oldOutputLow, 0, sizeof(_oldOutputLow));
    memset(_oldOutputHigh, 0, sizeof(_oldOutputHigh));
}

DtmfInband::~DtmfInband()
{
	delete &_critSect;
}

int
DtmfInband::SetSampleRate(uint16_t frequency)
{
    if (frequency != 8000 &&
            frequency != 16000 &&
            frequency != 32000)
    {
        // invalid sample rate
        assert(false);
        return -1;
    }
    _outputFrequencyHz = frequency;
    return 0;
}

int
DtmfInband::GetSampleRate(uint16_t& frequency)
{
    frequency = _outputFrequencyHz;
    return 0;
}

void 
DtmfInband::Init()
{
    _remainingSamples = 0;
    _frameLengthSamples = 0;
    _eventCode = 0;
    _attenuationDb = 0;
    _lengthMs = 0;
    _reinit = true;
    _oldOutputLow[0] = 0;
    _oldOutputLow[1] = 0;
    _oldOutputHigh[0] = 0;
    _oldOutputHigh[1] = 0;
    _delaySinceLastToneMS = 1000;
}

int
DtmfInband::AddTone(uint8_t eventCode,
                    int32_t lengthMs,
                    int32_t attenuationDb)
{
    CriticalSectionScoped lock(&_critSect);

    if (attenuationDb > 36 || eventCode > 15)
    {
        assert(false);
        return -1;
    }

    if (IsAddingTone())
    {
        WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_id,-1),
                   "DtmfInband::AddTone() new tone interrupts ongoing tone");
    }

    ReInit();

    _frameLengthSamples = static_cast<int16_t> (_outputFrequencyHz / 100);
    _eventCode = static_cast<int16_t> (eventCode);
    _attenuationDb = static_cast<int16_t> (attenuationDb);
    _remainingSamples = static_cast<int32_t>
        (lengthMs * (_outputFrequencyHz / 1000));
    _lengthMs = lengthMs;

    return 0;
}

int
DtmfInband::ResetTone()
{
    CriticalSectionScoped lock(&_critSect);

    ReInit();

    _frameLengthSamples = static_cast<int16_t> (_outputFrequencyHz / 100);
    _remainingSamples = static_cast<int32_t>
        (_lengthMs * (_outputFrequencyHz / 1000));

    return 0;
}

int
DtmfInband::StartTone(uint8_t eventCode,
                      int32_t attenuationDb)
{
    CriticalSectionScoped lock(&_critSect);

    if (attenuationDb > 36 || eventCode > 15)
    {
        assert(false);
        return -1;
    }

    if (IsAddingTone())
    {
            return -1;
    }

    ReInit();

    _frameLengthSamples = static_cast<int16_t> (_outputFrequencyHz / 100);
    _eventCode = static_cast<int16_t> (eventCode);
    _attenuationDb = static_cast<int16_t> (attenuationDb);
    _playing = true;

    return 0;
}

int
DtmfInband::StopTone()
{
    CriticalSectionScoped lock(&_critSect);

    if (!_playing)
    {
        return 0;
    }

    _playing = false;

    return 0;
}

// Shall be called between tones
void 
DtmfInband::ReInit()
{
    _reinit = true;
}

bool 
DtmfInband::IsAddingTone()
{
    CriticalSectionScoped lock(&_critSect);
    return (_remainingSamples > 0 || _playing);
}

int
DtmfInband::Get10msTone(int16_t output[320],
                        uint16_t& outputSizeInSamples)
{
    CriticalSectionScoped lock(&_critSect);
    if (DtmfFix_generate(output,
                         _eventCode,
                         _attenuationDb,
                         _frameLengthSamples,
                         _outputFrequencyHz) == -1)
    {
        return -1;
    }
    _remainingSamples -= _frameLengthSamples;
    outputSizeInSamples = _frameLengthSamples;
    _delaySinceLastToneMS = 0;
    return 0;
}

void
DtmfInband::UpdateDelaySinceLastTone()
{
    _delaySinceLastToneMS += kDtmfFrameSizeMs;
    // avoid wraparound
    if (_delaySinceLastToneMS > (1<<30))
    {
        _delaySinceLastToneMS = 1000;
    }
}

uint32_t
DtmfInband::DelaySinceLastTone() const
{
    return _delaySinceLastToneMS;
}

int16_t
DtmfInband::DtmfFix_generate(int16_t *decoded,
                             int16_t value,
                             int16_t volume,
                             int16_t frameLen,
                             int16_t fs)
{
    const int16_t *a_times2Tbl;
    const int16_t *y2_Table;
    int16_t a1_times2 = 0, a2_times2 = 0;

    if (fs==8000) {
        a_times2Tbl=Dtmf_a_times2Tab8Khz;
        y2_Table=Dtmf_ym2Tab8Khz;
    } else if (fs==16000) {
        a_times2Tbl=Dtmf_a_times2Tab16Khz;
        y2_Table=Dtmf_ym2Tab16Khz;
    } else if (fs==32000) {
        a_times2Tbl=Dtmf_a_times2Tab32Khz;
        y2_Table=Dtmf_ym2Tab32Khz;
    } else {
        return(-1);
    }

    if ((value==1)||(value==2)||(value==3)||(value==12)) {
        a1_times2=a_times2Tbl[0];
        if (_reinit) {
            _oldOutputLow[0]=y2_Table[0];
            _oldOutputLow[1]=0;
        }
    } else if ((value==4)||(value==5)||(value==6)||(value==13)) {
        a1_times2=a_times2Tbl[1];
        if (_reinit) {
            _oldOutputLow[0]=y2_Table[1];
            _oldOutputLow[1]=0;
        }
    } else if ((value==7)||(value==8)||(value==9)||(value==14)) {
        a1_times2=a_times2Tbl[2];
        if (_reinit) {
            _oldOutputLow[0]=y2_Table[2];
            _oldOutputLow[1]=0;
        }
    } else if ((value==10)||(value==0)||(value==11)||(value==15)) {
        a1_times2=a_times2Tbl[3];
        if (_reinit) {
            _oldOutputLow[0]=y2_Table[3];
            _oldOutputLow[1]=0;
        }
    }
    if ((value==1)||(value==4)||(value==7)||(value==10)) {
        a2_times2=a_times2Tbl[4];
        if (_reinit) {
            _oldOutputHigh[0]=y2_Table[4];
            _oldOutputHigh[1]=0;
            _reinit=false;
        }
    } else if ((value==2)||(value==5)||(value==8)||(value==0)) {
        a2_times2=a_times2Tbl[5];
        if (_reinit) {
            _oldOutputHigh[0]=y2_Table[5];
            _oldOutputHigh[1]=0;
            _reinit=false;
        }
    } else if ((value==3)||(value==6)||(value==9)||(value==11)) {
        a2_times2=a_times2Tbl[6];
        if (_reinit) {
            _oldOutputHigh[0]=y2_Table[6];
            _oldOutputHigh[1]=0;
            _reinit=false;
        }
    } else if ((value==12)||(value==13)||(value==14)||(value==15)) {
        a2_times2=a_times2Tbl[7];
        if (_reinit) {
            _oldOutputHigh[0]=y2_Table[7];
            _oldOutputHigh[1]=0;
            _reinit=false;
        }
    }

    return (DtmfFix_generateSignal(a1_times2,
                                   a2_times2,
                                   volume,
                                   decoded,
                                   frameLen));
}

int16_t
DtmfInband::DtmfFix_generateSignal(int16_t a1_times2,
                                   int16_t a2_times2,
                                   int16_t volume,
                                   int16_t *signal,
                                   int16_t length)
{
    int i;

    /* Generate Signal */
    for (i=0;i<length;i++) {
        int32_t tempVal;
        int16_t tempValLow, tempValHigh;

        /* Use recursion formula y[n] = a*2*y[n-1] - y[n-2] */
        tempValLow  = (int16_t)(((( (int32_t)(a1_times2 *
            _oldOutputLow[1])) + 8192) >> 14) - _oldOutputLow[0]);
        tempValHigh = (int16_t)(((( (int32_t)(a2_times2 *
            _oldOutputHigh[1])) + 8192) >> 14) - _oldOutputHigh[0]);

        /* Update memory */
        _oldOutputLow[0]=_oldOutputLow[1];
        _oldOutputLow[1]=tempValLow;
        _oldOutputHigh[0]=_oldOutputHigh[1];
        _oldOutputHigh[1]=tempValHigh;

        tempVal = (int32_t)(kDtmfAmpLow * tempValLow) +
            (int32_t)(kDtmfAmpHigh * tempValHigh);

        /* Norm the signal to Q14 */
        tempVal=(tempVal+16384)>>15;

        /* Scale the signal to correct dbM0 value */
        signal[i]=(int16_t)((tempVal*Dtmf_dBm0kHz[volume]+8192)>>14);
    }

    return(0);
}

}  // namespace webrtc