OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |
| 6 #define CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/scoped_vector.h" |
| 11 #include "base/threading/thread_checker.h" |
| 12 #include "content/common/content_export.h" |
| 13 |
| 14 namespace content { |
| 15 |
| 16 |
| 17 // AudioRepetitionDetector detects bit-exact audio repetitions of registered |
| 18 // patterns. A repetition pattern includes a look back time and a minimum |
| 19 // duration. The detector buffers the audio signal and checks equality of each |
| 20 // input sample against the samples at the look back positions of all registered |
| 21 // patterns, and counts the duration of any consecutive equality. |
| 22 class CONTENT_EXPORT AudioRepetitionDetector { |
| 23 public: |
| 24 AudioRepetitionDetector(); |
| 25 virtual ~AudioRepetitionDetector(); |
| 26 |
| 27 // Pattern is used for registering repetition patterns. |
| 28 // A pattern includes an |id| and two time-related descriptors |look_back_ms| |
| 29 // and |min_length_ms|, which are counted in milliseconds. |
| 30 struct Pattern { |
| 31 int id; |
| 32 int look_back_ms; |
| 33 int min_length_ms; |
| 34 }; |
| 35 |
| 36 // Detect repetition in |audio_bus|. A UMA report is generated upon finding |
| 37 // a repetition. |
| 38 void Detect(const float* data, size_t num_frames, size_t num_channels, |
| 39 int sample_rate_hz); |
| 40 |
| 41 private: |
| 42 friend class AudioRepetitionDetectorForTest; // For testing. |
| 43 |
| 44 // A state is used by the detector to keep track of a consecutive repetition, |
| 45 // whether the samples in a repetition are all zeros, and whether a repetition |
| 46 // has been reported. |
| 47 class State { |
| 48 public: |
| 49 State(const Pattern& pattern); |
| 50 |
| 51 bool reported() const { return reported_; } |
| 52 void set_reported(bool reported) { reported_ = reported; } |
| 53 |
| 54 // Increase |count_frames_| by 1, and |zero| indidates whether the added |
| 55 // audio frame is zero. |
| 56 void Increment(bool zero); |
| 57 |
| 58 // Check whether their is a valid repetition report. |
| 59 bool HasValidReport(int sample_rate_hz) const; |
| 60 |
| 61 void Reset(); |
| 62 |
| 63 int id() const { return pattern_.id; } |
| 64 |
| 65 int look_back_ms() const { return pattern_.look_back_ms; } |
| 66 |
| 67 private: |
| 68 // repetition pattern this state keeps track of. |
| 69 const Pattern pattern_; |
| 70 |
| 71 // counter of frames in a consecutive repetition. |
| 72 size_t count_frames_; |
| 73 |
| 74 // whether a repetition contains only zeros. |
| 75 bool all_zero_; |
| 76 |
| 77 // whether a repetition has been reported. This is to make sure that a |
| 78 // repetition that contains multiple cycles will be reported at as early as |
| 79 // the first cycle and only once. |
| 80 bool reported_; |
| 81 }; |
| 82 |
| 83 void RegisterRepetitionPatterns(const Pattern* patterns, |
| 84 size_t num_patterns); |
| 85 |
| 86 void ClearRepetitionPatterns(); |
| 87 |
| 88 // Reset |audio_buffer_| when number of channels or sample rate changes. |
| 89 void Reset(size_t num_channels, int sample_rate_hz); |
| 90 |
| 91 // Add frames (interleaved if stereo) to |audio_buffer_|. |
| 92 void AddFramesToBuffer(const float* data, size_t num_frames); |
| 93 |
| 94 // Determine if an audio frame (samples interleaved if stereo) is identical to |
| 95 // |audio_buffer_| at a look back position. |
| 96 bool Equal(const float* frame, int look_back_samples) const; |
| 97 |
| 98 // Determine if an audio frame (samples interleaved if stereo) is zero. |
| 99 bool IsZero(const float* frame, size_t num_channels) const; |
| 100 |
| 101 // Action when found repetition. |
| 102 virtual void ReportRepetition(int id); |
| 103 |
| 104 // Used to DCHECK that we are called on the correct thread. |
| 105 base::ThreadChecker thread_checker_; |
| 106 |
| 107 ScopedVector<State> states_; |
| 108 |
| 109 // The Ids of all registered patterns. This defines the range of values for |
| 110 // the UMA report. |
| 111 std::vector<int> ids_; |
| 112 |
| 113 // Ring buffer to store input audio. |
| 114 std::vector<float> audio_buffer_; |
| 115 |
| 116 // Maximum look back time of all registered patterns. This defines the size of |
| 117 // |audio_buffer_| |
| 118 int max_look_back_ms_; |
| 119 |
| 120 // Number of audio channels in buffer. |
| 121 size_t num_channels_; |
| 122 |
| 123 // Sample rate in Hz. |
| 124 int sample_rate_hz_; |
| 125 |
| 126 // Number of frames in |audio_buffer|. |
| 127 size_t buffer_size_frames_; |
| 128 |
| 129 // The index of the last frame in |audio_buffer|. |
| 130 size_t buffer_end_index_; |
| 131 |
| 132 // The maximum frames |audio_buffer_| can take in each time. |
| 133 size_t max_frames_; |
| 134 |
| 135 DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector); |
| 136 }; |
| 137 |
| 138 } // namespace content |
| 139 |
| 140 #endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |
OLD | NEW |