aboutsummaryrefslogtreecommitdiffstats
path: root/src/libdtmf/dtmf_encode.c
blob: db1bb6ea9b7e876b675d6f84e829f49c8a5ac848 (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
/* DTMF coder
 *
 * (C) 2016 by Andreas Eversberg <jolly@eversberg.eu>
 * All Rights Reserved
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <stdint.h>
#include <string.h>
#include <math.h>
#include "../libsample/sample.h"
#include "dtmf_encode.h"

#define PI		M_PI

#define PEAK_DTMF_LOW	0.2818	/* -11 dBm, relative to 0 dBm level */ 
#define PEAK_DTMF_HIGH	0.3548	/* -9 dBm, relative to 0 dBm level */ 

void dtmf_encode_init(dtmf_enc_t *dtmf, int samplerate, double dBm_level)
{
	int i;

	memset(dtmf, 0, sizeof(*dtmf));
	dtmf->samplerate = samplerate;

	for (i = 0; i < 65536; i++) {
		dtmf->sine_low[i] = sin((double)i / 65536.0 * 2.0 * PI) * PEAK_DTMF_LOW * dBm_level;
		dtmf->sine_high[i] = sin((double)i / 65536.0 * 2.0 * PI) * PEAK_DTMF_HIGH * dBm_level;
	}
}

/* set dtmf tone */
int dtmf_encode_set_tone(dtmf_enc_t *dtmf, char tone, double on_duration, double off_duration)
{
	double f1, f2;

	switch(tone) {
		case '1': f1 = 697.0; f2 = 1209.0; break;
		case '2': f1 = 697.0; f2 = 1336.0; break;
		case '3': f1 = 697.0; f2 = 1477.0; break;
	case'a':case 'A': f1 = 697.0; f2 = 1633.0; break;
		case '4': f1 = 770.0; f2 = 1209.0; break;
		case '5': f1 = 770.0; f2 = 1336.0; break;
		case '6': f1 = 770.0; f2 = 1477.0; break;
	case'b':case 'B': f1 = 770.0; f2 = 1633.0; break;
		case '7': f1 = 852.0; f2 = 1209.0; break;
		case '8': f1 = 852.0; f2 = 1336.0; break;
		case '9': f1 = 852.0; f2 = 1477.0; break;
	case'c':case 'C': f1 = 852.0; f2 = 1633.0; break;
		case '*': f1 = 941.0; f2 = 1209.0; break;
		case '0': f1 = 941.0; f2 = 1336.0; break;
		case '#': f1 = 941.0; f2 = 1477.0; break;
	case'd':case 'D': f1 = 941.0; f2 = 1633.0; break;
	default:
		dtmf->tone = 0;
		return -1;
	}
	dtmf->tone = tone;
	dtmf->pos = 0;
	dtmf->on = (int)((double)dtmf->samplerate * on_duration);
	dtmf->off = dtmf->on + (int)((double)dtmf->samplerate * off_duration);
	dtmf->phaseshift65536[0] = 65536.0 / ((double)dtmf->samplerate / f1);
	dtmf->phaseshift65536[1] = 65536.0 / ((double)dtmf->samplerate / f2);
	dtmf->phase65536[0] = 0.0;
	dtmf->phase65536[1] = 0.0;

	return 0;
}

/* Generate audio stream from DTMF tone.
 * Keep phase for next call of function.
 * Stop, if tone has finished and return only the samples that were used.
 */
int dtmf_encode(dtmf_enc_t *dtmf, sample_t *samples, int length)
{
        double *phaseshift, *phase;
	sample_t *sine_low, *sine_high;
	int count = 0;
	int i;

	/* if no tone */
	if (!dtmf->tone)
		return 0;

	sine_low = dtmf->sine_low;
	sine_high = dtmf->sine_high;
	phaseshift = dtmf->phaseshift65536;
	phase = dtmf->phase65536;

	for (i = 0; i < length; i++) {
		*samples++ = sine_low[(uint16_t)phase[0]]
			   + sine_high[(uint16_t)phase[1]];
		phase[0] += phaseshift[0];
		if (phase[0] >= 65536.0)
			phase[0] -= 65536.0;
		phase[1] += phaseshift[1];
		if (phase[1] >= 65536.0)
			phase[1] -= 65536.0;

		dtmf->pos++;
		/* tone ends */
		if (dtmf->pos == dtmf->on) {
			phaseshift[0] = 0.0;
			phaseshift[1] = 0.0;
			phase[0] = 0.0;
			phase[1] = 0.0;
		}
		/* pause ends */
		if (dtmf->pos == dtmf->off) {
			dtmf->tone = 0;
			break;
		}
	}
	count += i;

	return count;
}