Chromium Code Reviews| Index: content/renderer/media/audio_repetition_detector.h |
| diff --git a/content/renderer/media/audio_repetition_detector.h b/content/renderer/media/audio_repetition_detector.h |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..c233918d7928f8135e42327985a148d16cdecde9 |
| --- /dev/null |
| +++ b/content/renderer/media/audio_repetition_detector.h |
| @@ -0,0 +1,128 @@ |
| +// 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_ |
| +#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 is defined by a look back time. 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. |
|
Henrik Grunell
2015/10/16 07:59:50
Re the dtor check hit, maybe comment on the thread
minyue
2015/10/16 08:34:42
Do you mean to write something like:
An instance
Henrik Grunell
2015/10/16 08:44:11
Yes, something like that. And why destructing it o
minyue
2015/10/16 09:27:28
The failure in an earlier patch that triggered me
Henrik Grunell
2015/10/16 11:08:12
Yes, that's fine. I think it's good to document th
|
| +class CONTENT_EXPORT AudioRepetitionDetector { |
| + public: |
| + AudioRepetitionDetector(); |
| + virtual ~AudioRepetitionDetector(); |
| + |
| + // Detect repetition in |data|. A UMA report is generated upon finding |
| + // a repetition. |sample_rate| is measured in Hz. |
| + void Detect(const float* data, size_t num_frames, size_t num_channels, |
| + int sample_rate); |
| + |
| + private: |
| + friend class AudioRepetitionDetectorForTest; // For testing. |
|
Henrik Grunell
2015/10/16 07:59:50
Nit: I think you can remove the comment, it's clea
minyue
2015/10/16 08:34:42
Acknowledged.
|
| + |
| + // 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: |
| + explicit State(int look_back_ms); |
| + |
| + int look_back_ms() const { return look_back_ms_; }; |
| + size_t count_frames() const { return count_frames_; } |
| + bool all_zero() const { return all_zero_; } |
| + bool reported() const { return reported_; } |
| + void set_reported(bool reported) { reported_ = reported; } |
| + |
| + // Increase |count_frames_| by 1, and |zero| indidates whether the added |
| + // audio frame is zero. |
| + void Increment(bool zero); |
| + |
| + void Reset(); |
| + |
| + private: |
| + // Look back time of the repetition pattern this state keeps track of. |
| + const int look_back_ms_; |
| + |
| + // counter of frames in a consecutive repetition. |
|
Henrik Grunell
2015/10/16 07:59:50
Nit: begin with capital letter. Here and below.
minyue
2015/10/16 08:34:42
Acknowledged.
|
| + size_t count_frames_; |
| + |
| + // whether a repetition contains only zeros. |
| + bool all_zero_; |
| + |
| + // |reported_| tells whether a repetition has been reported. This is to make |
| + // sure that a repetition with a long duration will be reported as early as |
| + // being detected but no more than one time. |
| + bool reported_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(State); |
| + }; |
| + |
| + void RegisterLookbackTime(int look_back_ms); |
| + |
| + // Reset |audio_buffer_| when number of channels or sample rate (Hz) changes. |
| + void Reset(size_t num_channels, int sample_rate); |
| + |
| + // 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; |
| + |
| + // Check whether the state contains a valid repetition report. |
| + bool HasValidReport(const State* state) const; |
| + |
| + // Action when found repetition. |
| + virtual void ReportRepetition(int look_back_ms); |
| + |
| + // Used to DCHECK that we are called on the correct thread. |
| + base::ThreadChecker thread_checker_; |
| + |
| + ScopedVector<State> states_; |
| + |
| + // Ring buffer to store input audio. |
| + std::vector<float> audio_buffer_; |
| + |
| + // Maximum look back time of all registered repetitions. This defines the size |
| + // of |audio_buffer_| |
| + int max_look_back_ms_; |
| + |
| + // The shortest length for repetitions. |
| + int min_length_ms_; |
| + |
| + // Number of audio channels in buffer. |
| + size_t num_channels_; |
| + |
| + // Sample rate in Hz. |
| + int sample_rate_; |
| + |
| + // 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_ |