| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 // respectively, on the keypad as illustrated below. | 23 // respectively, on the keypad as illustrated below. |
| 24 // | 24 // |
| 25 // 1209 Hz 1336 Hz 1477 Hz 1633 Hz | 25 // 1209 Hz 1336 Hz 1477 Hz 1633 Hz |
| 26 // 697 Hz 1 2 3 12 | 26 // 697 Hz 1 2 3 12 |
| 27 // 770 Hz 4 5 6 13 | 27 // 770 Hz 4 5 6 13 |
| 28 // 852 Hz 7 8 9 14 | 28 // 852 Hz 7 8 9 14 |
| 29 // 941 Hz 10 0 11 15 | 29 // 941 Hz 10 0 11 15 |
| 30 | 30 |
| 31 #include "webrtc/modules/audio_coding/neteq/dtmf_tone_generator.h" | 31 #include "webrtc/modules/audio_coding/neteq/dtmf_tone_generator.h" |
| 32 | 32 |
| 33 #include <assert.h> | 33 #include "webrtc/base/arraysize.h" |
| 34 #include "webrtc/base/checks.h" |
| 34 | 35 |
| 35 namespace webrtc { | 36 namespace webrtc { |
| 36 | 37 |
| 37 // The filter coefficient a = 2*cos(2*pi*f/fs) for the low frequency tone, for | 38 // The filter coefficient a = 2*cos(2*pi*f/fs) for the low frequency tone, for |
| 38 // sample rates fs = {8000, 16000, 32000, 48000} Hz, and events 0 through 15. | 39 // sample rates fs = {8000, 16000, 32000, 48000} Hz, and events 0 through 15. |
| 39 // Values are in Q14. | 40 // Values are in Q14. |
| 40 const int DtmfToneGenerator::kCoeff1[4][16] = { | 41 const int DtmfToneGenerator::kCoeff1[4][16] = { |
| 41 { 24219, 27980, 27980, 27980, 26956, 26956, 26956, 25701, 25701, 25701, | 42 { 24219, 27980, 27980, 27980, 26956, 26956, 26956, 25701, 25701, 25701, |
| 42 24219, 24219, 27980, 26956, 25701, 24219 }, | 43 24219, 24219, 27980, 26956, 25701, 24219 }, |
| 43 { 30556, 31548, 31548, 31548, 31281, 31281, 31281, 30951, 30951, 30951, | 44 { 30556, 31548, 31548, 31548, 31281, 31281, 31281, 30951, 30951, 30951, |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 const int DtmfToneGenerator::kInitValue2[4][16] = { | 80 const int DtmfToneGenerator::kInitValue2[4][16] = { |
| 80 { 14206, 13323, 14206, 15021, 13323, 14206, 15021, 13323, 14206, 15021, | 81 { 14206, 13323, 14206, 15021, 13323, 14206, 15021, 13323, 14206, 15021, |
| 81 13323, 15021, 15708, 15708, 15708, 15708}, | 82 13323, 15021, 15708, 15708, 15708, 15708}, |
| 82 { 8207, 7490, 8207, 8979, 7490, 8207, 8979, 7490, 8207, 8979, 7490, 8979, | 83 { 8207, 7490, 8207, 8979, 7490, 8207, 8979, 7490, 8207, 8979, 7490, 8979, |
| 83 9801, 9801, 9801, 9801}, | 84 9801, 9801, 9801, 9801}, |
| 84 { 4249, 3853, 4249, 4685, 3853, 4249, 4685, 3853, 4249, 4685, 3853, 4685, | 85 { 4249, 3853, 4249, 4685, 3853, 4249, 4685, 3853, 4249, 4685, 3853, 4685, |
| 85 5164, 5164, 5164, 5164}, | 86 5164, 5164, 5164, 5164}, |
| 86 { 2851, 2582, 2851, 3148, 2582, 2851, 3148, 2582, 2851, 3148, 2582, 3148, | 87 { 2851, 2582, 2851, 3148, 2582, 2851, 3148, 2582, 2851, 3148, 2582, 3148, |
| 87 3476, 3476, 3476, 3476} }; | 88 3476, 3476, 3476, 3476} }; |
| 88 | 89 |
| 89 // Amplitude multipliers for volume values 0 through 36, corresponding to | 90 // Amplitude multipliers for volume values 0 through 63, corresponding to |
| 90 // 0 dBm0 through -36 dBm0. Values are in Q14. | 91 // 0 dBm0 through -63 dBm0. Values are in Q14. |
| 91 const int DtmfToneGenerator::kAmplitude[37] = { | 92 // for a in range(0, 64): |
| 93 // print round(16141.0 * 10**(-float(a)/20)) |
| 94 const int DtmfToneGenerator::kAmplitude[64] = { |
| 92 16141, 14386, 12821, 11427, 10184, 9077, 8090, 7210, 6426, 5727, 5104, 4549, | 95 16141, 14386, 12821, 11427, 10184, 9077, 8090, 7210, 6426, 5727, 5104, 4549, |
| 93 4054, 3614, 3221, 2870, 2558, 2280, 2032, 1811, 1614, 1439, 1282, 1143, | 96 4054, 3614, 3221, 2870, 2558, 2280, 2032, 1811, 1614, 1439, 1282, 1143, |
| 94 1018, 908, 809, 721, 643, 573, 510, 455, 405, 361, 322, 287, 256 }; | 97 1018, 908, 809, 721, 643, 573, 510, 455, 405, 361, 322, 287, 256, 228, 203, |
| 98 181, 161, 144, 128, 114, 102, 91, 81, 72, 64, 57, 51, 45, 41, 36, 32, 29, |
| 99 26, 23, 20, 18, 16, 14, 13, 11 }; |
| 95 | 100 |
| 96 // Constructor. | 101 // Constructor. |
| 97 DtmfToneGenerator::DtmfToneGenerator() | 102 DtmfToneGenerator::DtmfToneGenerator() |
| 98 : initialized_(false), | 103 : initialized_(false), |
| 99 coeff1_(0), | 104 coeff1_(0), |
| 100 coeff2_(0), | 105 coeff2_(0), |
| 101 amplitude_(0) { | 106 amplitude_(0) { |
| 102 } | 107 } |
| 103 | 108 |
| 104 // Initialize the DTMF generator with sample rate fs Hz (8000, 16000, 32000, | 109 // Initialize the DTMF generator with sample rate fs Hz (8000, 16000, 32000, |
| 105 // 48000), event (0-15) and attenuation (0-36 dB). | 110 // 48000), event (0-15) and attenuation (0-36 dB). |
| 106 // Returns 0 on success, otherwise an error code. | 111 // Returns 0 on success, otherwise an error code. |
| 107 int DtmfToneGenerator::Init(int fs, int event, int attenuation) { | 112 int DtmfToneGenerator::Init(int fs, int event, int attenuation) { |
| 108 initialized_ = false; | 113 initialized_ = false; |
| 109 int fs_index; | 114 size_t fs_index; |
| 110 if (fs == 8000) { | 115 if (fs == 8000) { |
| 111 fs_index = 0; | 116 fs_index = 0; |
| 112 } else if (fs == 16000) { | 117 } else if (fs == 16000) { |
| 113 fs_index = 1; | 118 fs_index = 1; |
| 114 } else if (fs == 32000) { | 119 } else if (fs == 32000) { |
| 115 fs_index = 2; | 120 fs_index = 2; |
| 116 } else if (fs == 48000) { | 121 } else if (fs == 48000) { |
| 117 fs_index = 3; | 122 fs_index = 3; |
| 118 } else { | 123 } else { |
| 119 assert(false); | 124 RTC_NOTREACHED(); |
| 120 fs_index = 1; // Default to 8000 Hz. | 125 fs_index = 1; // Default to 8000 Hz. |
| 121 } | 126 } |
| 122 | 127 |
| 123 if (event < 0 || event > 15) { | 128 if (event < 0 || event > 15) { |
| 124 return kParameterError; // Invalid event number. | 129 return kParameterError; // Invalid event number. |
| 125 } | 130 } |
| 126 | 131 |
| 127 if (attenuation < 0 || attenuation > 36) { | 132 if (attenuation < 0 || attenuation > 63) { |
| 128 return kParameterError; // Invalid attenuation. | 133 return kParameterError; // Invalid attenuation. |
| 129 } | 134 } |
| 130 | 135 |
| 131 // Look up oscillator coefficient for low and high frequencies. | 136 // Look up oscillator coefficient for low and high frequencies. |
| 137 RTC_DCHECK_LE(0u, fs_index); |
| 138 RTC_DCHECK_GT(arraysize(kCoeff1), fs_index); |
| 139 RTC_DCHECK_GT(arraysize(kCoeff2), fs_index); |
| 140 RTC_DCHECK_LE(0, event); |
| 141 RTC_DCHECK_GT(arraysize(kCoeff1[fs_index]), static_cast<size_t>(event)); |
| 142 RTC_DCHECK_GT(arraysize(kCoeff2[fs_index]), static_cast<size_t>(event)); |
| 132 coeff1_ = kCoeff1[fs_index][event]; | 143 coeff1_ = kCoeff1[fs_index][event]; |
| 133 coeff2_ = kCoeff2[fs_index][event]; | 144 coeff2_ = kCoeff2[fs_index][event]; |
| 145 |
| 134 // Look up amplitude multiplier. | 146 // Look up amplitude multiplier. |
| 147 RTC_DCHECK_LE(0, attenuation); |
| 148 RTC_DCHECK_GT(arraysize(kAmplitude), static_cast<size_t>(attenuation)); |
| 135 amplitude_ = kAmplitude[attenuation]; | 149 amplitude_ = kAmplitude[attenuation]; |
| 150 |
| 136 // Initialize sample history. | 151 // Initialize sample history. |
| 152 RTC_DCHECK_LE(0u, fs_index); |
| 153 RTC_DCHECK_GT(arraysize(kInitValue1), fs_index); |
| 154 RTC_DCHECK_GT(arraysize(kInitValue2), fs_index); |
| 155 RTC_DCHECK_LE(0, event); |
| 156 RTC_DCHECK_GT(arraysize(kInitValue1[fs_index]), static_cast<size_t>(event)); |
| 157 RTC_DCHECK_GT(arraysize(kInitValue2[fs_index]), static_cast<size_t>(event)); |
| 137 sample_history1_[0] = kInitValue1[fs_index][event]; | 158 sample_history1_[0] = kInitValue1[fs_index][event]; |
| 138 sample_history1_[1] = 0; | 159 sample_history1_[1] = 0; |
| 139 sample_history2_[0] = kInitValue2[fs_index][event]; | 160 sample_history2_[0] = kInitValue2[fs_index][event]; |
| 140 sample_history2_[1] = 0; | 161 sample_history2_[1] = 0; |
| 141 | 162 |
| 142 initialized_ = true; | 163 initialized_ = true; |
| 143 return 0; | 164 return 0; |
| 144 } | 165 } |
| 145 | 166 |
| 146 // Reset tone generator to uninitialized state. | 167 // Reset tone generator to uninitialized state. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 187 } | 208 } |
| 188 | 209 |
| 189 return static_cast<int>(num_samples); | 210 return static_cast<int>(num_samples); |
| 190 } | 211 } |
| 191 | 212 |
| 192 bool DtmfToneGenerator::initialized() const { | 213 bool DtmfToneGenerator::initialized() const { |
| 193 return initialized_; | 214 return initialized_; |
| 194 } | 215 } |
| 195 | 216 |
| 196 } // namespace webrtc | 217 } // namespace webrtc |
| OLD | NEW |