niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2011 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 | #include "utility.h" |
kjellander@webrtc.org | 543c3ea | 2011-11-23 12:20:35 +0000 | [diff] [blame] | 12 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | #include <assert.h> |
| 14 | #include <stdio.h> |
| 15 | #include <stdlib.h> |
| 16 | |
tina.legrand@webrtc.org | 73222cf | 2013-03-15 13:29:17 +0000 | [diff] [blame] | 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | #include "webrtc/common_types.h" |
| 19 | #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h" |
| 20 | #include "webrtc/modules/audio_coding/main/source/acm_common_defs.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 21 | |
| 22 | #define NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE 13 |
| 23 | |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 24 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
| 26 | ACMTestTimer::ACMTestTimer() : |
| 27 | _msec(0), |
| 28 | _sec(0), |
| 29 | _min(0), |
| 30 | _hour(0) |
| 31 | { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | ACMTestTimer::~ACMTestTimer() |
| 36 | { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | void ACMTestTimer::Reset() |
| 41 | { |
| 42 | _msec = 0; |
| 43 | _sec = 0; |
| 44 | _min = 0; |
| 45 | _hour = 0; |
| 46 | return; |
| 47 | } |
| 48 | void ACMTestTimer::Tick10ms() |
| 49 | { |
| 50 | _msec += 10; |
| 51 | Adjust(); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | void ACMTestTimer::Tick1ms() |
| 56 | { |
| 57 | _msec++; |
| 58 | Adjust(); |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | void ACMTestTimer::Tick100ms() |
| 63 | { |
| 64 | _msec += 100; |
| 65 | Adjust(); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | void ACMTestTimer::Tick1sec() |
| 70 | { |
| 71 | _sec++; |
| 72 | Adjust(); |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | void ACMTestTimer::CurrentTimeHMS(char* currTime) |
| 77 | { |
| 78 | sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min, (double)_sec + (double)_msec / 1000.); |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | void ACMTestTimer::CurrentTime( |
| 83 | unsigned long& h, |
| 84 | unsigned char& m, |
| 85 | unsigned char& s, |
| 86 | unsigned short& ms) |
| 87 | { |
| 88 | h = _hour; |
| 89 | m = _min; |
| 90 | s = _sec; |
| 91 | ms = _msec; |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | void ACMTestTimer::Adjust() |
| 96 | { |
| 97 | unsigned int n; |
| 98 | if(_msec >= 1000) |
| 99 | { |
| 100 | n = _msec / 1000; |
| 101 | _msec -= (1000 * n); |
| 102 | _sec += n; |
| 103 | } |
| 104 | if(_sec >= 60) |
| 105 | { |
| 106 | n = _sec / 60; |
| 107 | _sec -= (n * 60); |
| 108 | _min += n; |
| 109 | } |
| 110 | if(_min >= 60) |
| 111 | { |
| 112 | n = _min / 60; |
| 113 | _min -= (n * 60); |
| 114 | _hour += n; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 119 | int16_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | ChooseCodec( |
| 121 | CodecInst& codecInst) |
| 122 | { |
| 123 | |
| 124 | PrintCodecs(); |
| 125 | //AudioCodingModule* tmpACM = AudioCodingModule::Create(0); |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 126 | uint8_t noCodec = AudioCodingModule::NumberOfCodecs(); |
| 127 | int8_t codecID; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 128 | bool outOfRange = false; |
| 129 | char myStr[15] = ""; |
| 130 | do |
| 131 | { |
| 132 | printf("\nChoose a codec [0]: "); |
kjellander@webrtc.org | 543c3ea | 2011-11-23 12:20:35 +0000 | [diff] [blame] | 133 | EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 134 | codecID = atoi(myStr); |
| 135 | if((codecID < 0) || (codecID >= noCodec)) |
| 136 | { |
| 137 | printf("\nOut of range.\n"); |
| 138 | outOfRange = true; |
| 139 | } |
| 140 | } while(outOfRange); |
| 141 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 142 | CHECK_ERROR(AudioCodingModule::Codec((uint8_t)codecID, &codecInst)); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | PrintCodecs() |
| 148 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 149 | uint8_t noCodec = AudioCodingModule::NumberOfCodecs(); |
| 150 | |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 151 | CodecInst codecInst; |
| 152 | printf("No Name [Hz] [bps]\n"); |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 153 | for(uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 154 | { |
tina.legrand@webrtc.org | 7a7a008 | 2013-02-21 10:27:48 +0000 | [diff] [blame] | 155 | AudioCodingModule::Codec(codecCntr, &codecInst); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 156 | printf("%2d- %-18s %5d %6d\n", |
| 157 | codecCntr, codecInst.plname, codecInst.plfreq, codecInst.rate); |
| 158 | } |
| 159 | |
| 160 | } |
| 161 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 162 | CircularBuffer::CircularBuffer(uint32_t len): |
tina.legrand@webrtc.org | 2e09692 | 2011-08-18 06:20:30 +0000 | [diff] [blame] | 163 | _buff(NULL), |
| 164 | _idx(0), |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 165 | _buffIsFull(false), |
| 166 | _calcAvg(false), |
| 167 | _calcVar(false), |
| 168 | _sum(0), |
tina.legrand@webrtc.org | 2e09692 | 2011-08-18 06:20:30 +0000 | [diff] [blame] | 169 | _sumSqr(0) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 170 | { |
tina.legrand@webrtc.org | 5efcad1 | 2011-12-20 09:05:55 +0000 | [diff] [blame] | 171 | _buff = new double[len]; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 172 | if(_buff == NULL) |
| 173 | { |
| 174 | _buffLen = 0; |
| 175 | } |
| 176 | else |
| 177 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 178 | for(uint32_t n = 0; n < len; n++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 179 | { |
| 180 | _buff[n] = 0; |
| 181 | } |
| 182 | _buffLen = len; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | CircularBuffer::~CircularBuffer() |
| 187 | { |
| 188 | if(_buff != NULL) |
| 189 | { |
| 190 | delete [] _buff; |
| 191 | _buff = NULL; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | void |
| 196 | CircularBuffer::Update( |
| 197 | const double newVal) |
| 198 | { |
| 199 | assert(_buffLen > 0); |
| 200 | |
| 201 | // store the value that is going to be overwritten |
| 202 | double oldVal = _buff[_idx]; |
| 203 | // record the new value |
| 204 | _buff[_idx] = newVal; |
| 205 | // increment the index, to point to where we would |
| 206 | // write next |
| 207 | _idx++; |
| 208 | // it is a circular buffer, if we are at the end |
| 209 | // we have to cycle to the beginning |
| 210 | if(_idx >= _buffLen) |
| 211 | { |
| 212 | // flag that the buffer is filled up. |
| 213 | _buffIsFull = true; |
| 214 | _idx = 0; |
| 215 | } |
| 216 | |
| 217 | // Update |
| 218 | |
| 219 | if(_calcAvg) |
| 220 | { |
| 221 | // for the average we have to update |
| 222 | // the sum |
| 223 | _sum += (newVal - oldVal); |
| 224 | } |
| 225 | |
| 226 | if(_calcVar) |
| 227 | { |
| 228 | // to calculate variance we have to update |
| 229 | // the sum of squares |
| 230 | _sumSqr += (double)(newVal - oldVal) * (double)(newVal + oldVal); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | void |
| 235 | CircularBuffer::SetArithMean( |
| 236 | bool enable) |
| 237 | { |
| 238 | assert(_buffLen > 0); |
| 239 | |
| 240 | if(enable && !_calcAvg) |
| 241 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 242 | uint32_t lim; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 243 | if(_buffIsFull) |
| 244 | { |
| 245 | lim = _buffLen; |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | lim = _idx; |
| 250 | } |
| 251 | _sum = 0; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 252 | for(uint32_t n = 0; n < lim; n++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 253 | { |
| 254 | _sum += _buff[n]; |
| 255 | } |
| 256 | } |
| 257 | _calcAvg = enable; |
| 258 | } |
| 259 | |
| 260 | void |
| 261 | CircularBuffer::SetVariance( |
| 262 | bool enable) |
| 263 | { |
| 264 | assert(_buffLen > 0); |
| 265 | |
| 266 | if(enable && !_calcVar) |
| 267 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 268 | uint32_t lim; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 269 | if(_buffIsFull) |
| 270 | { |
| 271 | lim = _buffLen; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | lim = _idx; |
| 276 | } |
| 277 | _sumSqr = 0; |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 278 | for(uint32_t n = 0; n < lim; n++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 279 | { |
| 280 | _sumSqr += _buff[n] * _buff[n]; |
| 281 | } |
| 282 | } |
| 283 | _calcAvg = enable; |
| 284 | } |
| 285 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 286 | int16_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 287 | CircularBuffer::ArithMean(double& mean) |
| 288 | { |
| 289 | assert(_buffLen > 0); |
| 290 | |
| 291 | if(_buffIsFull) |
| 292 | { |
| 293 | |
| 294 | mean = _sum / (double)_buffLen; |
| 295 | return 0; |
| 296 | } |
| 297 | else |
| 298 | { |
| 299 | if(_idx > 0) |
| 300 | { |
| 301 | mean = _sum / (double)_idx; |
| 302 | return 0; |
| 303 | } |
| 304 | else |
| 305 | { |
| 306 | return -1; |
| 307 | } |
| 308 | |
| 309 | } |
| 310 | } |
| 311 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 312 | int16_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 313 | CircularBuffer::Variance(double& var) |
| 314 | { |
| 315 | assert(_buffLen > 0); |
| 316 | |
| 317 | if(_buffIsFull) |
| 318 | { |
| 319 | var = _sumSqr / (double)_buffLen; |
| 320 | return 0; |
| 321 | } |
| 322 | else |
| 323 | { |
| 324 | if(_idx > 0) |
| 325 | { |
| 326 | var = _sumSqr / (double)_idx; |
| 327 | return 0; |
| 328 | } |
| 329 | else |
| 330 | { |
| 331 | return -1; |
| 332 | } |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | |
| 337 | |
| 338 | bool |
| 339 | FixedPayloadTypeCodec(const char* payloadName) |
| 340 | { |
| 341 | char fixPayloadTypeCodecs[NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE][32] = { |
| 342 | "PCMU", |
| 343 | "PCMA", |
| 344 | "GSM", |
| 345 | "G723", |
| 346 | "DVI4", |
| 347 | "LPC", |
| 348 | "PCMA", |
| 349 | "G722", |
| 350 | "QCELP", |
| 351 | "CN", |
| 352 | "MPA", |
| 353 | "G728", |
| 354 | "G729" |
| 355 | }; |
| 356 | |
| 357 | for(int n = 0; n < NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE; n++) |
| 358 | { |
| 359 | if(!STR_CASE_CMP(payloadName, fixPayloadTypeCodecs[n])) |
| 360 | { |
| 361 | return true; |
| 362 | } |
| 363 | } |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | DTMFDetector::DTMFDetector() |
| 368 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 369 | for(int16_t n = 0; n < 1000; n++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 370 | { |
| 371 | _toneCntr[n] = 0; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | DTMFDetector::~DTMFDetector() |
| 376 | { |
| 377 | } |
| 378 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 379 | int32_t DTMFDetector::IncomingDtmf(const uint8_t digitDtmf, const bool /* toneEnded */) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 380 | { |
| 381 | fprintf(stdout, "%d-",digitDtmf); |
| 382 | _toneCntr[digitDtmf]++; |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | void DTMFDetector::PrintDetectedDigits() |
| 387 | { |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 388 | for(int16_t n = 0; n < 1000; n++) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 389 | { |
| 390 | if(_toneCntr[n] > 0) |
| 391 | { |
| 392 | fprintf(stdout, "%d %u msec, \n", n, _toneCntr[n]*10); |
| 393 | } |
| 394 | } |
| 395 | fprintf(stdout, "\n"); |
| 396 | return; |
| 397 | } |
| 398 | |
| 399 | void |
| 400 | VADCallback::Reset() |
| 401 | { |
| 402 | for(int n = 0; n < 6; n++) |
| 403 | { |
| 404 | _numFrameTypes[n] = 0; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | VADCallback::VADCallback() |
| 409 | { |
| 410 | for(int n = 0; n < 6; n++) |
| 411 | { |
| 412 | _numFrameTypes[n] = 0; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void |
| 417 | VADCallback::PrintFrameTypes() |
| 418 | { |
| 419 | fprintf(stdout, "No encoding.................. %d\n", _numFrameTypes[0]); |
| 420 | fprintf(stdout, "Active normal encoded........ %d\n", _numFrameTypes[1]); |
| 421 | fprintf(stdout, "Passive normal encoded....... %d\n", _numFrameTypes[2]); |
| 422 | fprintf(stdout, "Passive DTX wideband......... %d\n", _numFrameTypes[3]); |
| 423 | fprintf(stdout, "Passive DTX narrowband....... %d\n", _numFrameTypes[4]); |
| 424 | fprintf(stdout, "Passive DTX super-wideband... %d\n", _numFrameTypes[5]); |
| 425 | } |
| 426 | |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 427 | int32_t |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 428 | VADCallback::InFrameType( |
pbos@webrtc.org | 0946a56 | 2013-04-09 00:28:06 +0000 | [diff] [blame] | 429 | int16_t frameType) |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 430 | { |
| 431 | _numFrameTypes[frameType]++; |
| 432 | return 0; |
| 433 | } |
tina.legrand@webrtc.org | 554ae1a | 2011-12-16 10:09:04 +0000 | [diff] [blame] | 434 | |
| 435 | } // namespace webrtc |