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