1470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com/*
2470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *
4470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  Use of this source code is governed by a BSD-style license
5470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  that can be found in the LICENSE file in the root of the source
6470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  tree. An additional intellectual property rights grant can be found
7470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  in the file PATENTS.  All contributing project authors may
8470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com *  be found in the AUTHORS file in the root of the source tree.
9470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com */
10470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
11470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include "utility.h"
12543c3eaa46434a7e3974012c4d33ed82fc81fd33kjellander@webrtc.org
13470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include <assert.h>
14470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include <stdio.h>
15470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#include <stdlib.h>
16e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org#include <string.h>
17470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
1873222cff1a64cbc8eade9277cc63d516f7c20947tina.legrand@webrtc.org#include "testing/gtest/include/gtest/gtest.h"
196ea3d1cc9e45ca5661b06ec3390282fa50ded349turaj@webrtc.org#include "webrtc/common.h"
2073222cff1a64cbc8eade9277cc63d516f7c20947tina.legrand@webrtc.org#include "webrtc/common_types.h"
213e6db2321ccdc8738c9cecbe9bdab13d4f0f658dkjellander#include "webrtc/modules/audio_coding/include/audio_coding_module.h"
223e6db2321ccdc8738c9cecbe9bdab13d4f0f658dkjellander#include "webrtc/modules/audio_coding/acm2/acm_common_defs.h"
23470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
24470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com#define NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE 13
25470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
26554ae1ad4ed3e6d1d9d807f15324d2f875888d23tina.legrand@webrtc.orgnamespace webrtc {
27470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
28d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgACMTestTimer::ACMTestTimer()
29d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    : _msec(0),
30d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _sec(0),
31d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _min(0),
32d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _hour(0) {
33d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
34470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
35470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
36d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgACMTestTimer::~ACMTestTimer() {
37d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
38470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
39470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
40d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::Reset() {
41d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _msec = 0;
42d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _sec = 0;
43d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _min = 0;
44d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _hour = 0;
45d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
46470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
47d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::Tick10ms() {
48d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _msec += 10;
49d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  Adjust();
50d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
51470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
52470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
53d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::Tick1ms() {
54d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _msec++;
55d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  Adjust();
56d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
57470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
58470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
59d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::Tick100ms() {
60d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _msec += 100;
61d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  Adjust();
62d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
63470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
64470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
65d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::Tick1sec() {
66d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _sec++;
67d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  Adjust();
68d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
69470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
70470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
71d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::CurrentTimeHMS(char* currTime) {
72d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  sprintf(currTime, "%4lu:%02u:%06.3f", _hour, _min,
73d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org          (double) _sec + (double) _msec / 1000.);
74d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
75470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
76470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
77d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::CurrentTime(unsigned long& h, unsigned char& m,
78d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org                               unsigned char& s, unsigned short& ms) {
79d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  h = _hour;
80d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  m = _min;
81d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  s = _sec;
82d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  ms = _msec;
83d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return;
84470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
85470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
86d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid ACMTestTimer::Adjust() {
87d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  unsigned int n;
88d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_msec >= 1000) {
89d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    n = _msec / 1000;
90d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _msec -= (1000 * n);
91d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _sec += n;
92d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
93d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_sec >= 60) {
94d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    n = _sec / 60;
95d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _sec -= (n * 60);
96d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _min += n;
97d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
98d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_min >= 60) {
99d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    n = _min / 60;
100d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _min -= (n * 60);
101d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _hour += n;
102d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
103470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
104470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
105d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgint16_t ChooseCodec(CodecInst& codecInst) {
106d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
107d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  PrintCodecs();
108d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  //AudioCodingModule* tmpACM = AudioCodingModule::Create(0);
109d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
110d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  int8_t codecID;
111d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  bool outOfRange = false;
112d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  char myStr[15] = "";
113d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  do {
114d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf("\nChoose a codec [0]: ");
115d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    EXPECT_TRUE(fgets(myStr, 10, stdin) != NULL);
116d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    codecID = atoi(myStr);
117d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if ((codecID < 0) || (codecID >= noCodec)) {
118d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      printf("\nOut of range.\n");
119d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      outOfRange = true;
120d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    }
121d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } while (outOfRange);
122470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
123d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  CHECK_ERROR(AudioCodingModule::Codec((uint8_t )codecID, &codecInst));
124d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return 0;
125470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
126470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
127d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid PrintCodecs() {
128d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  uint8_t noCodec = AudioCodingModule::NumberOfCodecs();
129470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
130d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  CodecInst codecInst;
131d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  printf("No  Name                [Hz]    [bps]\n");
132d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (uint8_t codecCntr = 0; codecCntr < noCodec; codecCntr++) {
133d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    AudioCodingModule::Codec(codecCntr, &codecInst);
134d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    printf("%2d- %-18s %5d   %6d\n", codecCntr, codecInst.plname,
135d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org           codecInst.plfreq, codecInst.rate);
136d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
137470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
138470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
139470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
140d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgCircularBuffer::CircularBuffer(uint32_t len)
141d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    : _buff(NULL),
142d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _idx(0),
143d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _buffIsFull(false),
144d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _calcAvg(false),
145d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _calcVar(false),
146d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _sum(0),
147d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _sumSqr(0) {
148d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _buff = new double[len];
149d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_buff == NULL) {
150d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _buffLen = 0;
151d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } else {
152d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (uint32_t n = 0; n < len; n++) {
153d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _buff[n] = 0;
154470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
155d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _buffLen = len;
156d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
157470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
158470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
159d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgCircularBuffer::~CircularBuffer() {
160d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_buff != NULL) {
161d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    delete[] _buff;
162d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _buff = NULL;
163d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
164d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org}
165470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
166d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid CircularBuffer::Update(const double newVal) {
167d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  assert(_buffLen > 0);
168d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
169d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // store the value that is going to be overwritten
170d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  double oldVal = _buff[_idx];
171d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // record the new value
172d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _buff[_idx] = newVal;
173d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // increment the index, to point to where we would
174d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // write next
175d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _idx++;
176d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // it is a circular buffer, if we are at the end
177d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // we have to cycle to the beginning
178d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_idx >= _buffLen) {
179d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // flag that the buffer is filled up.
180d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _buffIsFull = true;
181d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _idx = 0;
182d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
183d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
184d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  // Update
185d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
186d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_calcAvg) {
187d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // for the average we have to update
188d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // the sum
189d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _sum += (newVal - oldVal);
190d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
191d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
192d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_calcVar) {
193d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // to calculate variance we have to update
194d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    // the sum of squares
195d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _sumSqr += (double) (newVal - oldVal) * (double) (newVal + oldVal);
196d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
197470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
198470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
199d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid CircularBuffer::SetArithMean(bool enable) {
200d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  assert(_buffLen > 0);
201d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
202d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (enable && !_calcAvg) {
203d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    uint32_t lim;
204d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_buffIsFull) {
205d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      lim = _buffLen;
206d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    } else {
207d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      lim = _idx;
208470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
209d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _sum = 0;
210d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (uint32_t n = 0; n < lim; n++) {
211d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _sum += _buff[n];
212d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    }
213d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
214d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _calcAvg = enable;
215470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
216470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
217d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid CircularBuffer::SetVariance(bool enable) {
218d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  assert(_buffLen > 0);
219d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org
220d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (enable && !_calcVar) {
221d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    uint32_t lim;
222d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_buffIsFull) {
223d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      lim = _buffLen;
224d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    } else {
225d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      lim = _idx;
226470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
227d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    _sumSqr = 0;
228d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    for (uint32_t n = 0; n < lim; n++) {
229d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      _sumSqr += _buff[n] * _buff[n];
230d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    }
231d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
232d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  _calcAvg = enable;
233470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
234470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
235d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgint16_t CircularBuffer::ArithMean(double& mean) {
236d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  assert(_buffLen > 0);
237470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
238d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_buffIsFull) {
239470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
240d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    mean = _sum / (double) _buffLen;
241d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    return 0;
242d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } else {
243d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_idx > 0) {
244d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      mean = _sum / (double) _idx;
245d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      return 0;
246d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    } else {
247d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      return -1;
248470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
249470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
250d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
251470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
252470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
253d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgint16_t CircularBuffer::Variance(double& var) {
254d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  assert(_buffLen > 0);
255470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
256d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  if (_buffIsFull) {
257d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    var = _sumSqr / (double) _buffLen;
258d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    return 0;
259d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  } else {
260d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (_idx > 0) {
261d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      var = _sumSqr / (double) _idx;
262d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      return 0;
263d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    } else {
264d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      return -1;
265470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
266d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
267470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
268470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
269d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgbool FixedPayloadTypeCodec(const char* payloadName) {
270d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  char fixPayloadTypeCodecs[NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE][32] = { "PCMU",
271d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      "PCMA", "GSM", "G723", "DVI4", "LPC", "PCMA", "G722", "QCELP", "CN",
272d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      "MPA", "G728", "G729" };
273470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
274d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  for (int n = 0; n < NUM_CODECS_WITH_FIXED_PAYLOAD_TYPE; n++) {
275d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org    if (!STR_CASE_CMP(payloadName, fixPayloadTypeCodecs[n])) {
276d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org      return true;
277470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com    }
278d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  }
279d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return false;
280470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
281470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
282d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid VADCallback::Reset() {
283e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org  memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
284470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
285470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
286d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgVADCallback::VADCallback() {
287e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org  memset(_numFrameTypes, 0, sizeof(_numFrameTypes));
288470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
289470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
290d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.orgvoid VADCallback::PrintFrameTypes() {
29122993e1a0c114122fc1b9de0fc74d4096ec868bdpbos  printf("kEmptyFrame......... %d\n", _numFrameTypes[kEmptyFrame]);
292e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org  printf("kAudioFrameSpeech... %d\n", _numFrameTypes[kAudioFrameSpeech]);
293e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org  printf("kAudioFrameCN....... %d\n", _numFrameTypes[kAudioFrameCN]);
294e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org  printf("kVideoFrameKey...... %d\n", _numFrameTypes[kVideoFrameKey]);
295e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org  printf("kVideoFrameDelta.... %d\n", _numFrameTypes[kVideoFrameDelta]);
296470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
297470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com
298e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.orgint32_t VADCallback::InFrameType(FrameType frame_type) {
299e9217b4bdbf9a8fd8b4882b2f995665927f28ad2henrik.lundin@webrtc.org  _numFrameTypes[frame_type]++;
300d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org  return 0;
301470e71d3649f6cac4688e83819640b012b5d38bbniklase@google.com}
302554ae1ad4ed3e6d1d9d807f15324d2f875888d23tina.legrand@webrtc.org
303d5726a1286ce53c47ebd2d21d61b2772fc24aaedtina.legrand@webrtc.org}  // namespace webrtc
304