OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2015 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/repetition_detector.h" |
| 12 |
| 13 #include <algorithm> |
| 14 |
| 15 #include "webrtc/base/checks.h" |
| 16 #include "webrtc/base/safe_conversions.h" |
| 17 |
| 18 namespace webrtc { |
| 19 |
| 20 namespace { |
| 21 static const RepetitionDetector::Pattern kRepetitionPatterns[] = { |
| 22 // {id_, look_back_, length_} |
| 23 {0, 10, 10}, |
| 24 {1, 100, 10} |
| 25 }; |
| 26 static const size_t kMaxFrames = 480; // 10ms * 48kHz |
| 27 } |
| 28 |
| 29 RepetitionDetector::State::State(int id, int look_back_ms, int min_length_ms) |
| 30 : id_(id), |
| 31 look_back_ms_(look_back_ms), |
| 32 min_length_ms_(min_length_ms) { |
| 33 Reset(); |
| 34 } |
| 35 |
| 36 void RepetitionDetector::State::Increment(bool zero) { |
| 37 if (0 == count_frames_ && zero) { |
| 38 all_zero_ = true; |
| 39 } |
| 40 ++count_frames_; |
| 41 if (!zero) { |
| 42 all_zero_ = false; |
| 43 } |
| 44 } |
| 45 |
| 46 bool RepetitionDetector::State::HasValidReport(int sample_rate_hz) const { |
| 47 return (!all_zero_ && count_frames_ >= |
| 48 rtc::checked_cast<size_t>(min_length_ms_ * sample_rate_hz / 1000)); |
| 49 } |
| 50 |
| 51 void RepetitionDetector::State::Reset() { |
| 52 count_frames_ = 0; |
| 53 all_zero_ = true; |
| 54 reported_ = false; |
| 55 } |
| 56 |
| 57 RepetitionDetector::RepetitionDetector() |
| 58 : max_look_back_ms_(0), |
| 59 sample_rate_hz_(0), |
| 60 buffer_size_frames_(0), |
| 61 buffer_end_index_(0), |
| 62 max_frames_(kMaxFrames) { |
| 63 RegisterRepetitionPatterns(kRepetitionPatterns, |
| 64 sizeof(kRepetitionPatterns) / sizeof (Pattern)); |
| 65 } |
| 66 |
| 67 RepetitionDetector::~RepetitionDetector() = default; |
| 68 |
| 69 void RepetitionDetector::RegisterRepetitionPatterns(const Pattern* patterns, |
| 70 size_t num_patterns) { |
| 71 Pattern pattern; |
| 72 for (size_t idx = 0; idx < num_patterns; idx++) { |
| 73 pattern = patterns[idx]; |
| 74 states_.push_back(new State(pattern.id_, pattern.look_back_ms_, |
| 75 pattern.min_length_ms_)); |
| 76 if (pattern.look_back_ms_ > max_look_back_ms_) { |
| 77 max_look_back_ms_ = pattern.look_back_ms_; |
| 78 } |
| 79 } |
| 80 } |
| 81 |
| 82 void RepetitionDetector::Reset(size_t num_channels, int sample_rate_hz) { |
| 83 num_channels_ = num_channels; |
| 84 sample_rate_hz_ = sample_rate_hz; |
| 85 int sample_1k = max_look_back_ms_ * sample_rate_hz_; |
| 86 // |(sample_1k + 999) / 1000| is an arithmetic way to round up |
| 87 // |sample_1k / 1000| |
| 88 buffer_size_frames_ = (sample_1k + 999) / 1000 + max_frames_; |
| 89 audio_buffer_.resize(buffer_size_frames_ * num_channels_); |
| 90 for (auto state : states_) { |
| 91 state->Reset(); |
| 92 } |
| 93 } |
| 94 |
| 95 void RepetitionDetector::AddFramesToBuffer(const float* data, |
| 96 size_t num_frames) { |
| 97 DCHECK_LE(num_frames, buffer_size_frames_); |
| 98 const size_t margin = buffer_size_frames_ - buffer_end_index_; |
| 99 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_; |
| 100 if (num_frames <= margin) { |
| 101 std::copy(data, data + num_frames * num_channels_, it); |
| 102 buffer_end_index_ += num_frames; |
| 103 } else { |
| 104 std::copy(data, data + margin * num_channels_, it); |
| 105 std::copy(data + margin * num_channels_, data + num_frames * num_channels_, |
| 106 audio_buffer_.begin()); |
| 107 buffer_end_index_ = num_frames - margin; |
| 108 } |
| 109 } |
| 110 |
| 111 bool RepetitionDetector::Equal(const float* frame, |
| 112 int look_back_frames) const { |
| 113 const size_t look_back_index = |
| 114 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) % |
| 115 buffer_size_frames_; |
| 116 auto it = audio_buffer_.begin() + look_back_index * num_channels_; |
| 117 for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++frame, ++it) { |
| 118 if (*frame != *it) { |
| 119 return false; |
| 120 } |
| 121 } |
| 122 return true; |
| 123 } |
| 124 |
| 125 bool RepetitionDetector::IsZero(const float* frame) const { |
| 126 for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++frame) { |
| 127 if (*frame != 0) { |
| 128 return false; |
| 129 } |
| 130 } |
| 131 return true; |
| 132 } |
| 133 |
| 134 void RepetitionDetector::Detect(const float* data, size_t num_frames, |
| 135 size_t num_channels, int sample_rate_hz) { |
| 136 DCHECK_GT(states_.size(), 0ul); |
| 137 if (num_channels != num_channels_ || sample_rate_hz != sample_rate_hz_) { |
| 138 Reset(num_channels, sample_rate_hz); |
| 139 } |
| 140 |
| 141 while (num_frames > max_frames_) { |
| 142 Detect(data, max_frames_, num_channels, sample_rate_hz); |
| 143 data += max_frames_ * num_channels; |
| 144 num_frames -= max_frames_; |
| 145 } |
| 146 |
| 147 if (num_frames == 0) |
| 148 return; |
| 149 |
| 150 AddFramesToBuffer(data, num_frames); |
| 151 |
| 152 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) { |
| 153 for (auto state : states_) { |
| 154 const size_t look_back_frames = |
| 155 rtc::CheckedDivExact(state->look_back_ms() * sample_rate_hz_, 1000); |
| 156 // Equal(data, offset) checks if |data| equals the audio frame located |
| 157 // |offset| frames from the end of buffer. Now a full frame has been |
| 158 // inserted to the buffer, and thus |offset| should compensate for it. |
| 159 if (Equal(data, look_back_frames + idx)) { |
| 160 if (!state->reported()) { |
| 161 state->Increment(IsZero(data)); |
| 162 if (state->HasValidReport(sample_rate_hz)) { |
| 163 ReportRepetition(state->id()); |
| 164 state->set_reported(true); |
| 165 } |
| 166 } |
| 167 } else { |
| 168 state->Reset(); |
| 169 } |
| 170 } |
| 171 } |
| 172 } |
| 173 |
| 174 } // namespace webrtc |
OLD | NEW |