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

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

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.h
diff --git a/webrtc/modules/audio_processing/repetition_detector.h b/webrtc/modules/audio_processing/repetition_detector.h
new file mode 100644
index 0000000000000000000000000000000000000000..1a8b1535cd9daf77f5539ad12f36003329bf9f16
--- /dev/null
+++ b/webrtc/modules/audio_processing/repetition_detector.h
@@ -0,0 +1,108 @@
+/*
+ * 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.
+ */
+
+#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_REPETITION_DETECTOR_H_
+#define WEBRTC_MODULES_AUDIO_PROCESSING_REPETITION_DETECTOR_H_
+
+#include <vector>
+
+#include "webrtc/base/constructormagic.h"
+#include "webrtc/base/scoped_ptr.h"
+#include "webrtc/system_wrappers/interface/scoped_vector.h"
+#include "webrtc/typedefs.h"
+
+namespace webrtc {
+
+class RepetitionDetectorForTest;
+
+class RepetitionDetector {
+ public:
+ RepetitionDetector();
+ virtual ~RepetitionDetector() {}
+
+ struct Pattern {
+ int id_;
+ // All followings are in milliseconds, since repetition patterns are
+ // supposedly bounded to certain duration in time.
+ int look_back_ms_;
+ int min_length_ms_;
+ };
+
+ // Detect repetition given audio samples. When multichannel, samples should be
+ // interleaved.
+ void Detect(const float* data, size_t num_frames, size_t num_channels,
+ int sample_rate_hz);
+
+ friend class RepetitionDetectorForTest; // For testing.
Andrew MacDonald 2015/09/07 06:52:18 Normally you'd use the FRIEND_TEST macro.
minyue-webrtc 2015/09/07 07:33:24 Acknowledged.
minyue-webrtc 2015/09/11 14:01:41 It turns out a bit complicated here. RepetitionDe
+
+ protected:
+ virtual void ReportRepetition(int id) { }
Andrew MacDonald 2015/09/07 06:52:18 Why protected?
minyue-webrtc 2015/09/07 07:33:24 true, not needed.
+
+ private:
+ class State {
+ public:
+ State(int id, int look_back_ms, int min_length_ms);
+
+ bool reported() const { return reported_; }
+ void set_reported(bool reported) { reported_ = reported; }
+
+ // Increase the counter by 1, and tell if the counted sample is zero.
+ void Increment(bool zero);
+
+ bool HasValidReport(int sample_rate_khz) const;
+
+ void Reset();
+ int id() const { return id_; }
+ int look_back_ms() const { return look_back_ms_; }
+
+ private:
+ const int id_;
+ const int look_back_ms_;
+ const int min_length_ms_;
+ size_t count_samples_;
+ bool all_zero_;
+ bool reported_;
+ };
+
+ // Register repetition patterns.
+ void RegisterRepetitionPatterns(const Pattern* patterns,
+ size_t num_patterns);
+
+ // Clear registered repetition patterns.
+ void ClearRepetitionPatterns();
+
+ // Reset |audio_buffer_| when number of channels or sample rate changes.
+ void Reset(size_t num_channels, int sample_rate_hz);
+
+ // Add sample (interleaved if stereo) to |audio_buffer_|.
+ void AddSampleToBuffer(const float* sample);
+
+ // Determine if a sample (interleaved if stereo) is identical to
+ // |audio_buffer_| at a look back position.
+ bool Equal(const float* sample, int look_back_samples) const;
+
+ // Determine if a sample (interleaved if stereo) is zero.
+ bool IsZero(const float* sample) const;
+
+ ScopedVector<State> states_;
+ int max_look_back_ms_;
+
+ rtc::scoped_ptr<float[]> audio_buffer_; // Ring buffers to store input audio.
+ size_t num_channels_; // Number of audio channels in buffer.
+ int sample_rate_hz_; // Sample rate in kHz.
+ size_t buffer_size_samples_; // Number of samples in |audio_buffer|.
+ size_t buffer_end_index_; // The index of the last sample in |audio_buffer|.
+
+ DISALLOW_COPY_AND_ASSIGN(RepetitionDetector);
+};
+
+} // namespace webrtc
+
+#endif // WEBRTC_MODULES_AUDIO_PROCESSING_REPETITION_DETECTOR_H_

Powered by Google App Engine
This is Rietveld 408576698