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 |data|. A UMA report is generated upon finding |
| 37 // a repetition. |sample_rate| is measured in Hz. |
| 38 void Detect(const float* data, size_t num_frames, size_t num_channels, |
| 39 int sample_rate); |
| 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. |sample_rate| is |
| 59 // measured in Hz. |
| 60 bool HasValidReport(int sample_rate) const; |
| 61 |
| 62 void Reset(); |
| 63 |
| 64 int id() const { return pattern_.id; } |
| 65 |
| 66 int look_back_ms() const { return pattern_.look_back_ms; } |
| 67 |
| 68 private: |
| 69 // The repetition pattern this state keeps track of. |
| 70 const Pattern pattern_; |
| 71 |
| 72 // counter of frames in a consecutive repetition. |
| 73 size_t count_frames_; |
| 74 |
| 75 // whether a repetition contains only zeros. |
| 76 bool all_zero_; |
| 77 |
| 78 // whether a repetition has been reported. This is to make sure that a |
| 79 // repetition that contains multiple cycles will be reported at as early as |
| 80 // the first cycle and only once. |
| 81 bool reported_; |
| 82 |
| 83 DISALLOW_COPY_AND_ASSIGN(State); |
| 84 }; |
| 85 |
| 86 void RegisterRepetitionPatterns(const Pattern* patterns, |
| 87 size_t num_patterns); |
| 88 |
| 89 // Reset |audio_buffer_| when number of channels or sample rate (Hz) changes. |
| 90 void Reset(size_t num_channels, int sample_rate); |
| 91 |
| 92 // Add frames (interleaved if stereo) to |audio_buffer_|. |
| 93 void AddFramesToBuffer(const float* data, size_t num_frames); |
| 94 |
| 95 // Determine if an audio frame (samples interleaved if stereo) is identical to |
| 96 // |audio_buffer_| at a look back position. |
| 97 bool Equal(const float* frame, int look_back_samples) const; |
| 98 |
| 99 // Determine if an audio frame (samples interleaved if stereo) is zero. |
| 100 bool IsZero(const float* frame, size_t num_channels) const; |
| 101 |
| 102 // Action when found repetition. |
| 103 virtual void ReportRepetition(int id); |
| 104 |
| 105 // Used to DCHECK that we are called on the correct thread. |
| 106 base::ThreadChecker thread_checker_; |
| 107 |
| 108 ScopedVector<State> states_; |
| 109 |
| 110 // The Ids of all registered patterns. This defines the range of values for |
| 111 // the UMA report. |
| 112 std::vector<int> ids_; |
| 113 |
| 114 // Ring buffer to store input audio. |
| 115 std::vector<float> audio_buffer_; |
| 116 |
| 117 // Maximum look back time of all registered patterns. This defines the size of |
| 118 // |audio_buffer_| |
| 119 int max_look_back_ms_; |
| 120 |
| 121 // Number of audio channels in buffer. |
| 122 size_t num_channels_; |
| 123 |
| 124 // Sample rate in Hz. |
| 125 int sample_rate_; |
| 126 |
| 127 // Number of frames in |audio_buffer|. |
| 128 size_t buffer_size_frames_; |
| 129 |
| 130 // The index of the last frame in |audio_buffer|. |
| 131 size_t buffer_end_index_; |
| 132 |
| 133 // The maximum frames |audio_buffer_| can take in each time. |
| 134 size_t max_frames_; |
| 135 |
| 136 DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector); |
| 137 }; |
| 138 |
| 139 } // namespace content |
| 140 |
| 141 #endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |
OLD | NEW |