| 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 |
| 11 #include "webrtc/modules/audio_processing/vad/vad_circular_buffer.h" | 11 #include "webrtc/modules/audio_processing/agc/circular_buffer.h" |
| 12 | 12 |
| 13 #include <assert.h> | 13 #include <assert.h> |
| 14 #include <stdlib.h> | 14 #include <stdlib.h> |
| 15 | 15 |
| 16 namespace webrtc { | 16 namespace webrtc { |
| 17 | 17 |
| 18 VadCircularBuffer::VadCircularBuffer(int buffer_size) | 18 AgcCircularBuffer::AgcCircularBuffer(int buffer_size) |
| 19 : buffer_(new double[buffer_size]), | 19 : buffer_(new double[buffer_size]), |
| 20 is_full_(false), | 20 is_full_(false), |
| 21 index_(0), | 21 index_(0), |
| 22 buffer_size_(buffer_size), | 22 buffer_size_(buffer_size), |
| 23 sum_(0) { | 23 sum_(0) {} |
| 24 } | |
| 25 | 24 |
| 26 VadCircularBuffer::~VadCircularBuffer() { | 25 AgcCircularBuffer::~AgcCircularBuffer() {} |
| 27 } | |
| 28 | 26 |
| 29 void VadCircularBuffer::Reset() { | 27 void AgcCircularBuffer::Reset() { |
| 30 is_full_ = false; | 28 is_full_ = false; |
| 31 index_ = 0; | 29 index_ = 0; |
| 32 sum_ = 0; | 30 sum_ = 0; |
| 33 } | 31 } |
| 34 | 32 |
| 35 VadCircularBuffer* VadCircularBuffer::Create(int buffer_size) { | 33 AgcCircularBuffer* AgcCircularBuffer::Create(int buffer_size) { |
| 36 if (buffer_size <= 0) | 34 if (buffer_size <= 0) |
| 37 return NULL; | 35 return NULL; |
| 38 return new VadCircularBuffer(buffer_size); | 36 return new AgcCircularBuffer(buffer_size); |
| 39 } | 37 } |
| 40 | 38 |
| 41 double VadCircularBuffer::Oldest() const { | 39 double AgcCircularBuffer::Oldest() const { |
| 42 if (!is_full_) | 40 if (!is_full_) |
| 43 return buffer_[0]; | 41 return buffer_[0]; |
| 44 else | 42 else |
| 45 return buffer_[index_]; | 43 return buffer_[index_]; |
| 46 } | 44 } |
| 47 | 45 |
| 48 double VadCircularBuffer::Mean() { | 46 double AgcCircularBuffer::Mean() { |
| 49 double m; | 47 double m; |
| 50 if (is_full_) { | 48 if (is_full_) { |
| 51 m = sum_ / buffer_size_; | 49 m = sum_ / buffer_size_; |
| 52 } else { | 50 } else { |
| 53 if (index_ > 0) | 51 if (index_ > 0) |
| 54 m = sum_ / index_; | 52 m = sum_ / index_; |
| 55 else | 53 else |
| 56 m = 0; | 54 m = 0; |
| 57 } | 55 } |
| 58 return m; | 56 return m; |
| 59 } | 57 } |
| 60 | 58 |
| 61 void VadCircularBuffer::Insert(double value) { | 59 void AgcCircularBuffer::Insert(double value) { |
| 62 if (is_full_) { | 60 if (is_full_) { |
| 63 sum_ -= buffer_[index_]; | 61 sum_ -= buffer_[index_]; |
| 64 } | 62 } |
| 65 sum_ += value; | 63 sum_ += value; |
| 66 buffer_[index_] = value; | 64 buffer_[index_] = value; |
| 67 index_++; | 65 index_++; |
| 68 if (index_ >= buffer_size_) { | 66 if (index_ >= buffer_size_) { |
| 69 is_full_ = true; | 67 is_full_ = true; |
| 70 index_ = 0; | 68 index_ = 0; |
| 71 } | 69 } |
| 72 } | 70 } |
| 73 int VadCircularBuffer::BufferLevel() { | 71 int AgcCircularBuffer::BufferLevel() { |
| 74 if (is_full_) | 72 if (is_full_) |
| 75 return buffer_size_; | 73 return buffer_size_; |
| 76 return index_; | 74 return index_; |
| 77 } | 75 } |
| 78 | 76 |
| 79 int VadCircularBuffer::Get(int index, double* value) const { | 77 int AgcCircularBuffer::Get(int index, double* value) const { |
| 80 int err = ConvertToLinearIndex(&index); | 78 int err = ConvertToLinearIndex(&index); |
| 81 if (err < 0) | 79 if (err < 0) |
| 82 return -1; | 80 return -1; |
| 83 *value = buffer_[index]; | 81 *value = buffer_[index]; |
| 84 return 0; | 82 return 0; |
| 85 } | 83 } |
| 86 | 84 |
| 87 int VadCircularBuffer::Set(int index, double value) { | 85 int AgcCircularBuffer::Set(int index, double value) { |
| 88 int err = ConvertToLinearIndex(&index); | 86 int err = ConvertToLinearIndex(&index); |
| 89 if (err < 0) | 87 if (err < 0) |
| 90 return -1; | 88 return -1; |
| 91 | 89 |
| 92 sum_ -= buffer_[index]; | 90 sum_ -= buffer_[index]; |
| 93 buffer_[index] = value; | 91 buffer_[index] = value; |
| 94 sum_ += value; | 92 sum_ += value; |
| 95 return 0; | 93 return 0; |
| 96 } | 94 } |
| 97 | 95 |
| 98 int VadCircularBuffer::ConvertToLinearIndex(int* index) const { | 96 int AgcCircularBuffer::ConvertToLinearIndex(int* index) const { |
| 99 if (*index < 0 || *index >= buffer_size_) | 97 if (*index < 0 || *index >= buffer_size_) |
| 100 return -1; | 98 return -1; |
| 101 | 99 |
| 102 if (!is_full_ && *index >= index_) | 100 if (!is_full_ && *index >= index_) |
| 103 return -1; | 101 return -1; |
| 104 | 102 |
| 105 *index = index_ - 1 - *index; | 103 *index = index_ - 1 - *index; |
| 106 if (*index < 0) | 104 if (*index < 0) |
| 107 *index += buffer_size_; | 105 *index += buffer_size_; |
| 108 return 0; | 106 return 0; |
| 109 } | 107 } |
| 110 | 108 |
| 111 int VadCircularBuffer::RemoveTransient(int width_threshold, | 109 int AgcCircularBuffer::RemoveTransient(int width_threshold, |
| 112 double val_threshold) { | 110 double val_threshold) { |
| 113 if (!is_full_ && index_ < width_threshold + 2) | 111 if (!is_full_ && index_ < width_threshold + 2) |
| 114 return 0; | 112 return 0; |
| 115 | 113 |
| 116 int index_1 = 0; | 114 int index_1 = 0; |
| 117 int index_2 = width_threshold + 1; | 115 int index_2 = width_threshold + 1; |
| 118 double v = 0; | 116 double v = 0; |
| 119 if (Get(index_1, &v) < 0) | 117 if (Get(index_1, &v) < 0) |
| 120 return -1; | 118 return -1; |
| 121 if (v < val_threshold) { | 119 if (v < val_threshold) { |
| 122 Set(index_1, 0); | 120 Set(index_1, 0); |
| 123 int index; | 121 int index; |
| 124 for (index = index_2; index > index_1; index--) { | 122 for (index = index_2; index > index_1; index--) { |
| 125 if (Get(index, &v) < 0) | 123 if (Get(index, &v) < 0) |
| 126 return -1; | 124 return -1; |
| 127 if (v < val_threshold) | 125 if (v < val_threshold) |
| 128 break; | 126 break; |
| 129 } | 127 } |
| 130 for (; index > index_1; index--) { | 128 for (; index > index_1; index--) { |
| 131 if (Set(index, 0.0) < 0) | 129 if (Set(index, 0.0) < 0) |
| 132 return -1; | 130 return -1; |
| 133 } | 131 } |
| 134 } | 132 } |
| 135 return 0; | 133 return 0; |
| 136 } | 134 } |
| 137 | 135 |
| 138 } // namespace webrtc | 136 } // namespace webrtc |
| OLD | NEW |