1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 // This class provides a generator for DTMF tones. The tone generation is based
12 // on a sinusoid recursion. Each sinusoid is generated using a recursion
13 // formula; x[n] = a * x[n-1] - x[n-2], where the coefficient
14 // a = 2*cos(2*pi*f/fs). The recursion is started with x[-1] = 0 and
15 // x[-2] = sin(2*pi*f/fs). (Note that with this initialization, the resulting
16 // sinusoid gets a "negative" rotation; x[n] = sin(-2*pi*f/fs * n + phi), but
17 // kept this way due to historical reasons.)
18 // TODO(hlundin): Change to positive rotation?
19 //
20 // Each key on the telephone keypad corresponds to an "event", 0-15. Each event
21 // is mapped to a tone pair, with a low and a high frequency. There are four
22 // low and four high frequencies, each corresponding to a row and column,
23 // respectively, on the keypad as illustrated below.
24 //
25 // 1209 Hz 1336 Hz 1477 Hz 1633 Hz
26 // 697 Hz 1 2 3 12
27 // 770 Hz 4 5 6 13
28 // 852 Hz 7 8 9 14
29 // 941 Hz 10 0 11 15
30
31 #include "modules/audio_coding/neteq/dtmf_tone_generator.h"
32
33 #include "modules/audio_coding/neteq/audio_vector.h"
34 #include "rtc_base/arraysize.h"
35 #include "rtc_base/checks.h"
36
37 namespace webrtc {
38
39 // The filter coefficient a = 2*cos(2*pi*f/fs) for the low frequency tone, for
40 // sample rates fs = {8000, 16000, 32000, 48000} Hz, and events 0 through 15.
41 // Values are in Q14.
42 const int DtmfToneGenerator::kCoeff1[4][16] = {
43 {24219, 27980, 27980, 27980, 26956, 26956, 26956, 25701, 25701, 25701,
44 24219, 24219, 27980, 26956, 25701, 24219},
45 {30556, 31548, 31548, 31548, 31281, 31281, 31281, 30951, 30951, 30951,
46 30556, 30556, 31548, 31281, 30951, 30556},
47 {32210, 32462, 32462, 32462, 32394, 32394, 32394, 32311, 32311, 32311,
48 32210, 32210, 32462, 32394, 32311, 32210},
49 {32520, 32632, 32632, 32632, 32602, 32602, 32602, 32564, 32564, 32564,
50 32520, 32520, 32632, 32602, 32564, 32520}};
51
52 // The filter coefficient a = 2*cos(2*pi*f/fs) for the high frequency tone, for
53 // sample rates fs = {8000, 16000, 32000, 48000} Hz, and events 0 through 15.
54 // Values are in Q14.
55 const int DtmfToneGenerator::kCoeff2[4][16] = {
56 {16325, 19073, 16325, 13085, 19073, 16325, 13085, 19073, 16325, 13085,
57 19073, 13085, 9315, 9315, 9315, 9315},
58 {28361, 29144, 28361, 27409, 29144, 28361, 27409, 29144, 28361, 27409,
59 29144, 27409, 26258, 26258, 26258, 26258},
60 {31647, 31849, 31647, 31400, 31849, 31647, 31400, 31849, 31647, 31400,
61 31849, 31400, 31098, 31098, 31098, 31098},
62 {32268, 32359, 32268, 32157, 32359, 32268, 32157, 32359, 32268, 32157,
63 32359, 32157, 32022, 32022, 32022, 32022}};
64
65 // The initialization value x[-2] = sin(2*pi*f/fs) for the low frequency tone,
66 // for sample rates fs = {8000, 16000, 32000, 48000} Hz, and events 0-15.
67 // Values are in Q14.
68 const int DtmfToneGenerator::kInitValue1[4][16] = {
69 {11036, 8528, 8528, 8528, 9315, 9315, 9315, 10163, 10163, 10163, 11036,
70 11036, 8528, 9315, 10163, 11036},
71 {5918, 4429, 4429, 4429, 4879, 4879, 4879, 5380, 5380, 5380, 5918, 5918,
72 4429, 4879, 5380, 5918},
73 {3010, 2235, 2235, 2235, 2468, 2468, 2468, 2728, 2728, 2728, 3010, 3010,
74 2235, 2468, 2728, 3010},
75 {2013, 1493, 1493, 1493, 1649, 1649, 1649, 1823, 1823, 1823, 2013, 2013,
76 1493, 1649, 1823, 2013}};
77
78 // The initialization value x[-2] = sin(2*pi*f/fs) for the high frequency tone,
79 // for sample rates fs = {8000, 16000, 32000, 48000} Hz, and events 0-15.
80 // Values are in Q14.
81 const int DtmfToneGenerator::kInitValue2[4][16] = {
82 {14206, 13323, 14206, 15021, 13323, 14206, 15021, 13323, 14206, 15021,
83 13323, 15021, 15708, 15708, 15708, 15708},
84 {8207, 7490, 8207, 8979, 7490, 8207, 8979, 7490, 8207, 8979, 7490, 8979,
85 9801, 9801, 9801, 9801},
86 {4249, 3853, 4249, 4685, 3853, 4249, 4685, 3853, 4249, 4685, 3853, 4685,
87 5164, 5164, 5164, 5164},
88 {2851, 2582, 2851, 3148, 2582, 2851, 3148, 2582, 2851, 3148, 2582, 3148,
89 3476, 3476, 3476, 3476}};
90
91 // Amplitude multipliers for volume values 0 through 63, corresponding to
92 // 0 dBm0 through -63 dBm0. Values are in Q14.
93 // for a in range(0, 64):
94 // print round(16141.0 * 10**(-float(a)/20))
95 const int DtmfToneGenerator::kAmplitude[64] = {
96 16141, 14386, 12821, 11427, 10184, 9077, 8090, 7210, 6426, 5727, 5104,
97 4549, 4054, 3614, 3221, 2870, 2558, 2280, 2032, 1811, 1614, 1439,
98 1282, 1143, 1018, 908, 809, 721, 643, 573, 510, 455, 405,
99 361, 322, 287, 256, 228, 203, 181, 161, 144, 128, 114,
100 102, 91, 81, 72, 64, 57, 51, 45, 41, 36, 32,
101 29, 26, 23, 20, 18, 16, 14, 13, 11};
102
103 // Constructor.
DtmfToneGenerator()104 DtmfToneGenerator::DtmfToneGenerator()
105 : initialized_(false), coeff1_(0), coeff2_(0), amplitude_(0) {}
106
107 // Initialize the DTMF generator with sample rate fs Hz (8000, 16000, 32000,
108 // 48000), event (0-15) and attenuation (0-36 dB).
109 // Returns 0 on success, otherwise an error code.
Init(int fs,int event,int attenuation)110 int DtmfToneGenerator::Init(int fs, int event, int attenuation) {
111 initialized_ = false;
112 size_t fs_index;
113 if (fs == 8000) {
114 fs_index = 0;
115 } else if (fs == 16000) {
116 fs_index = 1;
117 } else if (fs == 32000) {
118 fs_index = 2;
119 } else if (fs == 48000) {
120 fs_index = 3;
121 } else {
122 RTC_NOTREACHED();
123 fs_index = 1; // Default to 8000 Hz.
124 }
125
126 if (event < 0 || event > 15) {
127 return kParameterError; // Invalid event number.
128 }
129
130 if (attenuation < 0 || attenuation > 63) {
131 return kParameterError; // Invalid attenuation.
132 }
133
134 // Look up oscillator coefficient for low and high frequencies.
135 RTC_DCHECK_LE(0, fs_index);
136 RTC_DCHECK_GT(arraysize(kCoeff1), fs_index);
137 RTC_DCHECK_GT(arraysize(kCoeff2), fs_index);
138 RTC_DCHECK_LE(0, event);
139 RTC_DCHECK_GT(arraysize(kCoeff1[fs_index]), event);
140 RTC_DCHECK_GT(arraysize(kCoeff2[fs_index]), event);
141 coeff1_ = kCoeff1[fs_index][event];
142 coeff2_ = kCoeff2[fs_index][event];
143
144 // Look up amplitude multiplier.
145 RTC_DCHECK_LE(0, attenuation);
146 RTC_DCHECK_GT(arraysize(kAmplitude), attenuation);
147 amplitude_ = kAmplitude[attenuation];
148
149 // Initialize sample history.
150 RTC_DCHECK_LE(0, fs_index);
151 RTC_DCHECK_GT(arraysize(kInitValue1), fs_index);
152 RTC_DCHECK_GT(arraysize(kInitValue2), fs_index);
153 RTC_DCHECK_LE(0, event);
154 RTC_DCHECK_GT(arraysize(kInitValue1[fs_index]), event);
155 RTC_DCHECK_GT(arraysize(kInitValue2[fs_index]), event);
156 sample_history1_[0] = kInitValue1[fs_index][event];
157 sample_history1_[1] = 0;
158 sample_history2_[0] = kInitValue2[fs_index][event];
159 sample_history2_[1] = 0;
160
161 initialized_ = true;
162 return 0;
163 }
164
165 // Reset tone generator to uninitialized state.
Reset()166 void DtmfToneGenerator::Reset() {
167 initialized_ = false;
168 }
169
170 // Generate num_samples of DTMF signal and write to |output|.
Generate(size_t num_samples,AudioMultiVector * output)171 int DtmfToneGenerator::Generate(size_t num_samples, AudioMultiVector* output) {
172 if (!initialized_) {
173 return kNotInitialized;
174 }
175
176 if (!output) {
177 return kParameterError;
178 }
179
180 output->AssertSize(num_samples);
181 for (size_t i = 0; i < num_samples; ++i) {
182 // Use recursion formula y[n] = a * y[n - 1] - y[n - 2].
183 int16_t temp_val_low =
184 ((coeff1_ * sample_history1_[1] + 8192) >> 14) - sample_history1_[0];
185 int16_t temp_val_high =
186 ((coeff2_ * sample_history2_[1] + 8192) >> 14) - sample_history2_[0];
187
188 // Update recursion memory.
189 sample_history1_[0] = sample_history1_[1];
190 sample_history1_[1] = temp_val_low;
191 sample_history2_[0] = sample_history2_[1];
192 sample_history2_[1] = temp_val_high;
193
194 // Attenuate the low frequency tone 3 dB.
195 int32_t temp_val =
196 kAmpMultiplier * temp_val_low + temp_val_high * (1 << 15);
197 // Normalize the signal to Q14 with proper rounding.
198 temp_val = (temp_val + 16384) >> 15;
199 // Scale the signal to correct volume.
200 (*output)[0][i] =
201 static_cast<int16_t>((temp_val * amplitude_ + 8192) >> 14);
202 }
203 // Copy first channel to all other channels.
204 for (size_t channel = 1; channel < output->Channels(); ++channel) {
205 output->CopyChannel(0, channel);
206 }
207
208 return static_cast<int>(num_samples);
209 }
210
initialized() const211 bool DtmfToneGenerator::initialized() const {
212 return initialized_;
213 }
214
215 } // namespace webrtc
216