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

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: restrict to float 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..0d84c9bb88000c0dfc54d74dbf1be93d5a3b1ed7
--- /dev/null
+++ b/webrtc/modules/audio_processing/repetition_detector.cc
@@ -0,0 +1,148 @@
+/*
+ * 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 "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, 100}
+};
+}
+
+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_samples_ && zero) {
+ all_zero_ = true;
+ }
+ ++count_samples_;
+ if (!zero) {
+ all_zero_ = false;
+ }
+}
+
+bool RepetitionDetector::State::HasValidReport(int sample_rate_hz) const {
+ return (!all_zero_ && count_samples_ >=
+ rtc::checked_cast<size_t>(min_length_ms_ * sample_rate_hz / 1000));
+}
+
+void RepetitionDetector::State::Reset() {
+ count_samples_ = 0;
+ all_zero_ = true;
+ reported_ = false;
+}
+
+RepetitionDetector::RepetitionDetector()
+ : max_look_back_ms_(0),
+ sample_rate_hz_(0),
+ buffer_size_samples_(0),
+ buffer_end_index_(0) {
+ RegisterRepetitionPatterns(kRepetitionPatterns,
+ sizeof(kRepetitionPatterns) / sizeof (Pattern));
+}
+
+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) {
Andrew MacDonald 2015/09/07 06:52:18 Do you need this? Why not have users just create a
minyue-webrtc 2015/09/07 07:33:24 This is needed when the num_channels, or sampling
Andrew MacDonald 2015/09/07 22:52:19 The other solution is to have clients recreate it
minyue-webrtc 2015/09/11 14:01:41 I prefer that RepetitionDetector takes care of tha
+ num_channels_ = num_channels;
+ sample_rate_hz_ = sample_rate_hz;
+ int sample_1k = max_look_back_ms_ * sample_rate_hz_;
+ buffer_size_samples_ = sample_1k / 1000 + (sample_1k % 1000 != 0);
+ audio_buffer_.reset(new float[buffer_size_samples_ * num_channels_]());
+ for (auto state : states_) {
+ state->Reset();
+ }
+}
+
+void RepetitionDetector::AddSampleToBuffer(const float* sample) {
+ float* ref = &audio_buffer_[buffer_end_index_ * num_channels_];
+ for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++ref, ++sample) {
Andrew MacDonald 2015/09/07 06:52:18 memcpy? (or even better, std::copy) Might not be
minyue-webrtc 2015/09/07 07:33:24 I also thought about adding data chunks. It is tri
+ *ref = *sample;
+ }
+ buffer_end_index_++;
+ if (buffer_end_index_ == buffer_size_samples_) {
+ buffer_end_index_ = 0;
+ }
+}
+
+bool RepetitionDetector::Equal(const float* sample,
+ int look_back_samples) const {
+ const size_t look_back_index =
+ (buffer_end_index_ + buffer_size_samples_ - look_back_samples) %
+ buffer_size_samples_ ;
+ const float* ref = &audio_buffer_[look_back_index * num_channels_];
+ for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++ref, ++sample) {
+ if (*sample != *ref) {
Andrew MacDonald 2015/09/07 06:52:18 I thought you were worried about exact floating po
minyue-webrtc 2015/09/07 07:33:24 I want to check bit exact equality, and with curre
+ return false;
+ }
+ }
+ return true;
+}
+
+bool RepetitionDetector::IsZero(const float* sample) const {
+ for (size_t cdx = 0; cdx < num_channels_; ++cdx, ++sample) {
+ if (*sample != 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);
+ }
+
+ for (size_t idx = 0; idx < num_frames; ++idx, data += num_channels) {
+ for (auto state : states_) {
+ const size_t look_back_samples =
+ rtc::CheckedDivExact(state->look_back_ms() * sample_rate_hz_, 1000);
+ if (Equal(data, look_back_samples)) {
+ if (!state->reported()) {
+ state->Increment(IsZero(data));
+ if (state->HasValidReport(sample_rate_hz)) {
+ ReportRepetition(state->id());
+ state->set_reported(true);
+ }
+ }
+ } else {
+ state->Reset();
+ }
+ }
+ AddSampleToBuffer(data);
+ }
+}
+
+} // namespace webrtc

Powered by Google App Engine
This is Rietveld 408576698