OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright (c) 2016 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 "webrtc/modules/audio_processing/echo_detector/circular_buffer.h" | |
12 | |
13 #include <algorithm> | |
14 | |
15 #include "webrtc/base/checks.h" | |
16 #include "webrtc/base/logging.h" | |
17 | |
18 namespace webrtc { | |
19 | |
20 CircularBuffer::CircularBuffer(size_t size) : buffer_(size) {} | |
21 CircularBuffer::CircularBuffer(CircularBuffer&& other) = default; | |
22 CircularBuffer::~CircularBuffer() = default; | |
23 | |
24 void CircularBuffer::Push(float value) { | |
25 buffer_[next_insertion_index_] = value; | |
26 ++next_insertion_index_; | |
27 next_insertion_index_ %= buffer_.size(); | |
28 RTC_DCHECK_LT(next_insertion_index_, buffer_.size()); | |
29 buffer_size_ = std::min(buffer_size_ + 1, buffer_.size()); | |
30 RTC_DCHECK_LE(buffer_size_, buffer_.size()); | |
31 } | |
32 | |
33 rtc::Optional<float> CircularBuffer::Pop() { | |
34 if (buffer_size_ == 0) { | |
35 LOG(LS_ERROR) << "Attempted to get value from buffer, but it was empty."; | |
hlundin-webrtc
2016/10/18 20:52:22
Now you are logging the same error twice – here an
ivoc
2016/10/19 14:12:04
Good point, I will remove this one.
| |
36 return rtc::Optional<float>(); | |
37 } else { | |
hlundin-webrtc
2016/10/18 20:52:22
You don't need the else here, since the if path re
ivoc
2016/10/19 14:12:04
Done.
| |
38 const size_t index = | |
39 (buffer_.size() + next_insertion_index_ - buffer_size_) % | |
40 buffer_.size(); | |
41 RTC_DCHECK_LT(index, buffer_.size()); | |
42 --buffer_size_; | |
43 return rtc::Optional<float>(buffer_[index]); | |
44 } | |
45 } | |
46 | |
47 void CircularBuffer::Clear() { | |
48 std::fill(buffer_.begin(), buffer_.end(), 0.f); | |
49 next_insertion_index_ = 0; | |
50 buffer_size_ = 0; | |
51 } | |
52 | |
53 } // namespace webrtc | |
OLD | NEW |