Chromium Code Reviews| Index: content/renderer/media/repetition_detector.h |
| diff --git a/content/renderer/media/repetition_detector.h b/content/renderer/media/repetition_detector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..87856035671e0466d954119f0d1aeeecb0c384c4 |
| --- /dev/null |
| +++ b/content/renderer/media/repetition_detector.h |
| @@ -0,0 +1,139 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#ifndef CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |
|
minyue
2015/09/25 14:40:08
I have not forgotten to change the file name, taki
|
| +#define CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |
| + |
| +#include <vector> |
| + |
| +#include "base/memory/scoped_vector.h" |
| +#include "base/threading/thread_checker.h" |
| +#include "content/common/content_export.h" |
| + |
| +namespace content { |
| + |
| + |
| +// AudioRepetitionDetector detects bit-exact audio repetitions of registered |
| +// patterns. A repetition pattern includes a look back time and a minimum |
| +// duration. The detector buffers the audio signal and checks equality of each |
| +// input sample against the samples at the look back positions of all registered |
| +// patterns, and counts the duration of any consecutive equality. |
| +class CONTENT_EXPORT AudioRepetitionDetector { |
| + public: |
| + AudioRepetitionDetector(); |
| + virtual ~AudioRepetitionDetector(); |
| + |
| + // Pattern is used for registering repetition patterns. |
| + // A pattern includes an |id| and two time-related descriptors |look_back_ms| |
| + // and |min_length_ms|, which are counted in milliseconds. |
| + struct Pattern { |
| + int id; |
| + int look_back_ms; |
| + int min_length_ms; |
| + }; |
| + |
| + // Detect repetition in |audio_bus|. A UMA report is generated upon finding |
| + // a repetition. |
| + void Detect(const float* data, size_t num_frames, size_t num_channels, |
| + int sample_rate_hz); |
| + |
| + private: |
| + friend class AudioRepetitionDetectorForTest; // For testing. |
| + |
| + // A state is used by the detector to keep track of a consecutive repetition, |
| + // whether the samples in a repetition are all zeros, and whether a repetition |
| + // has been reported. |
| + class State { |
| + public: |
| + State(const Pattern& pattern); |
| + |
| + bool reported() const { return reported_; } |
| + void set_reported(bool reported) { reported_ = reported; } |
| + |
| + // Increase the counter by 1, and tell if the counted audio is zero. |
| + void Increment(bool zero); |
| + |
| + // Check whether their is a valid repetition report. |
| + bool HasValidReport(int sample_rate_khz) const; |
| + |
| + void Reset(); |
| + |
| + int id() const { return pattern_.id; } |
| + |
| + int look_back_ms() const { return pattern_.look_back_ms; } |
| + |
| + private: |
| + // repetition pattern this state keeps track of. |
| + const Pattern pattern_; |
|
minyue
2015/09/25 14:40:08
I use a Pattern instead of three separate variable
|
| + |
| + // counter of frames in a consecutive repetition. |
| + size_t count_frames_; |
| + |
| + // whether a repetition contains only zeros. |
| + bool all_zero_; |
| + |
| + // whether a repetition has been reported. This is to make sure that a |
| + // repetition that contains multiple cycles will be reported at as early as |
| + // the first cycle and only once. |
| + bool reported_; |
| + }; |
| + |
| + void RegisterRepetitionPatterns(const Pattern* patterns, |
| + size_t num_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 frames (interleaved if stereo) to |audio_buffer_|. |
| + void AddFramesToBuffer(const float* data, size_t num_frames); |
| + |
| + // Determine if an audio frame (samples interleaved if stereo) is identical to |
| + // |audio_buffer_| at a look back position. |
| + bool Equal(const float* frame, int look_back_samples) const; |
| + |
| + // Determine if an audio frame (samples interleaved if stereo) is zero. |
| + bool IsZero(const float* frame, size_t num_channels) const; |
| + |
| + // Action when found repetition. |
| + virtual void ReportRepetition(int id); |
| + |
| + // Used to DCHECK that we are called on the correct thread. |
| + base::ThreadChecker thread_checker_; |
| + |
| + ScopedVector<State> states_; |
| + |
| + // The Ids of all registered patterns. This defines the range of values for |
| + // the UMA report. |
| + std::vector<int> ids_; |
| + |
| + // Ring buffer to store input audio. |
| + std::vector<float> audio_buffer_; |
| + |
| + // Maximum look back time of all registered patterns. This defines the size of |
| + // |audio_buffer_| |
| + int max_look_back_ms_; |
| + |
| + // Number of audio channels in buffer. |
| + size_t num_channels_; |
| + |
| + // Sample rate in Hz. |
| + int sample_rate_hz_; |
| + |
| + // Number of frames in |audio_buffer|. |
| + size_t buffer_size_frames_; |
| + |
| + // The index of the last frame in |audio_buffer|. |
| + size_t buffer_end_index_; |
| + |
| + // The maximum frames |audio_buffer_| can take in each time. |
| + size_t max_frames_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector); |
| +}; |
| + |
| +} // namespace content |
| + |
| +#endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |