Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(447)

Unified Diff: webrtc/modules/audio_processing/repetition_detector.cc

Issue 1287663002: Adding audio RepetitionDetector in AudioProcessingModule. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: more comments addressed Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: webrtc/modules/audio_processing/repetition_detector.cc
diff --git a/webrtc/modules/audio_processing/repetition_detector.cc b/webrtc/modules/audio_processing/repetition_detector.cc
new file mode 100644
index 0000000000000000000000000000000000000000..1d70fef633a0466f271ac05bfe0531ccf7619805
--- /dev/null
+++ b/webrtc/modules/audio_processing/repetition_detector.cc
@@ -0,0 +1,174 @@
+/*
+ * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "webrtc/modules/audio_processing/repetition_detector.h"
+
+#include <algorithm>
+
+#include "webrtc/base/checks.h"
+#include "webrtc/base/safe_conversions.h"
+
+namespace webrtc {
+
+namespace {
+static const RepetitionDetector::Pattern kRepetitionPatterns[] = {
+ // {id_, look_back_, length_}
+ {0, 10, 10},
+ {1, 100, 10}
+};
+static const size_t kMaxFrames = 480; // 10ms * 48kHz
+}
+
+RepetitionDetector::State::State(int id, int look_back_ms, int min_length_ms)
+ : id_(id),
+ look_back_ms_(look_back_ms),
+ min_length_ms_(min_length_ms) {
+ Reset();
+}
+
+void RepetitionDetector::State::Increment(bool zero) {
+ if (0 == count_frames_ && zero) {
+ all_zero_ = true;
+ }
+ ++count_frames_;
+ if (!zero) {
+ all_zero_ = false;
+ }
+}
+
+bool RepetitionDetector::State::HasValidReport(int sample_rate_hz) const {
+ return (!all_zero_ && count_frames_ >=
+ rtc::checked_cast<size_t>(min_length_ms_ * sample_rate_hz / 1000));
+}
+
+void RepetitionDetector::State::Reset() {
+ count_frames_ = 0;
+ all_zero_ = true;
+ reported_ = false;
+}
+
+RepetitionDetector::RepetitionDetector()
+ : max_look_back_ms_(0),
+ sample_rate_hz_(0),
+ buffer_size_frames_(0),
+ buffer_end_index_(0),
+ max_frames_(kMaxFrames) {
+ RegisterRepetitionPatterns(kRepetitionPatterns,
+ sizeof(kRepetitionPatterns) / sizeof (Pattern));
+}
+
+RepetitionDetector::~RepetitionDetector() = default;
+
+void RepetitionDetector::RegisterRepetitionPatterns(const Pattern* patterns,
+ size_t num_patterns) {
+ Pattern pattern;
+ for (size_t idx = 0; idx < num_patterns; idx++) {
+ pattern = patterns[idx];
+ states_.push_back(new State(pattern.id_, pattern.look_back_ms_,
+ pattern.min_length_ms_));
+ if (pattern.look_back_ms_ > max_look_back_ms_) {
+ max_look_back_ms_ = pattern.look_back_ms_;
+ }
+ }
+}
+
+void RepetitionDetector::Reset(size_t num_channels, int sample_rate_hz) {
+ num_channels_ = num_channels;
+ sample_rate_hz_ = sample_rate_hz;
+ int sample_1k = max_look_back_ms_ * sample_rate_hz_;
+ // |(sample_1k + 999) / 1000| is an arithmetic way to round up
+ // |sample_1k / 1000|
+ buffer_size_frames_ = (sample_1k + 999) / 1000 + max_frames_;
+ audio_buffer_.resize(buffer_size_frames_ * num_channels_);
+ for (auto state : states_) {
+ state->Reset();
+ }
+}
+
+void RepetitionDetector::AddFramesToBuffer(const float* data,
+ size_t num_frames) {
+ DCHECK_LE(num_frames, buffer_size_frames_);
+ const size_t margin = buffer_size_frames_ - buffer_end_index_;
+ const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_;
+ if (num_frames <= margin) {
+ std::copy(data, data + num_frames * num_channels_, it);
+ buffer_end_index_ += num_frames;
+ } else {
+ std::copy(data, data + margin * num_channels_, it);
+ std::copy(data + margin * num_channels_, data + num_frames * num_channels_,
+ audio_buffer_.begin());
+ buffer_end_index_ = num_frames - margin;
+ }
+}
+
+bool RepetitionDetector::Equal(const float* frame,
+ int look_back_frames) const {
+ const size_t look_back_index =
+ (buffer_end_index_ + buffer_size_frames_ - look_back_frames) %
+ buffer_size_frames_;
+ auto it = audio_buffer_.begin() + look_back_index * num_channels_;
+ for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++frame, ++it) {
+ if (*frame != *it) {
+ return false;
+ }
+ }
+ return true;
+}
+
+bool RepetitionDetector::IsZero(const float* frame) const {
+ for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++frame) {
+ if (*frame != 0) {
+ return false;
+ }
+ }
+ return true;
+}
+
+void RepetitionDetector::Detect(const float* data, size_t num_frames,
+ size_t num_channels, int sample_rate_hz) {
+ DCHECK_GT(states_.size(), 0ul);
+ if (num_channels != num_channels_ || sample_rate_hz != sample_rate_hz_) {
+ Reset(num_channels, sample_rate_hz);
+ }
+
+ while (num_frames > max_frames_) {
+ Detect(data, max_frames_, num_channels, sample_rate_hz);
+ data += max_frames_ * num_channels;
+ num_frames -= max_frames_;
+ }
+
+ if (num_frames == 0)
+ return;
+
+ AddFramesToBuffer(data, num_frames);
+
+ for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) {
+ for (auto state : states_) {
+ const size_t look_back_frames =
+ rtc::CheckedDivExact(state->look_back_ms() * sample_rate_hz_, 1000);
+ // Equal(data, offset) checks if |data| equals the audio frame located
+ // |offset| frames from the end of buffer. Now a full frame has been
+ // inserted to the buffer, and thus |offset| should compensate for it.
+ if (Equal(data, look_back_frames + idx)) {
+ if (!state->reported()) {
+ state->Increment(IsZero(data));
+ if (state->HasValidReport(sample_rate_hz)) {
+ ReportRepetition(state->id());
+ state->set_reported(true);
+ }
+ }
+ } else {
+ state->Reset();
+ }
+ }
+ }
+}
+
+} // namespace webrtc
« no previous file with comments | « webrtc/modules/audio_processing/repetition_detector.h ('k') | webrtc/modules/audio_processing/repetition_detector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698