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/callback.h" |
| 11 #include "base/memory/scoped_vector.h" |
| 12 #include "base/threading/thread_checker.h" |
| 13 #include "content/common/content_export.h" |
| 14 |
| 15 namespace content { |
| 16 |
| 17 // AudioRepetitionDetector detects bit-exact audio repetitions of registered |
| 18 // patterns. A repetition pattern is defined by a look back time. The detector |
| 19 // buffers the audio signal and checks equality of each input sample against the |
| 20 // samples at the look back positions of all registered patterns, and counts the |
| 21 // duration of any consecutive equality. |
| 22 // All methods should be called from the same thread. However, we allow the |
| 23 // construction and destruction be made from a separate thread. |
| 24 |
| 25 class CONTENT_EXPORT AudioRepetitionDetector { |
| 26 public: |
| 27 // Callback that defines the action upon a repetition is detected. One int |
| 28 // parameter to the callback is the look back time (in milliseconds) of the |
| 29 // detected repetition. |
| 30 typedef base::Callback<void(int)> RepetitionCallback; |
| 31 |
| 32 // |min_length_ms| is the minimum duration (in milliseconds) of repetitions |
| 33 // that count. |
| 34 // |max_frames| is the maximum number of audio frames that will be provided to |
| 35 // |Detect()| each time. Input longer than |max_frames| won't cause any |
| 36 // problem, and will only affect computational efficiency. |
| 37 // |look_back_times| is a vector of look back times (in milliseconds) for the |
| 38 // detector to keep track. |
| 39 AudioRepetitionDetector(int min_length_ms, size_t max_frames, |
| 40 const std::vector<int>& look_back_times, |
| 41 const RepetitionCallback& repetition_callback); |
| 42 |
| 43 virtual ~AudioRepetitionDetector(); |
| 44 |
| 45 // Detect repetition in |data|. |sample_rate| is measured in Hz. |
| 46 void Detect(const float* data, size_t num_frames, size_t num_channels, |
| 47 int sample_rate); |
| 48 |
| 49 private: |
| 50 friend class AudioRepetitionDetectorForTest; |
| 51 |
| 52 // A state is used by the detector to keep track of a consecutive repetition, |
| 53 // whether the samples in a repetition are all zeros, and whether a repetition |
| 54 // has been reported. |
| 55 class State { |
| 56 public: |
| 57 explicit State(int look_back_ms); |
| 58 |
| 59 int look_back_ms() const { return look_back_ms_; }; |
| 60 size_t count_frames() const { return count_frames_; } |
| 61 bool all_zero() const { return all_zero_; } |
| 62 bool reported() const { return reported_; } |
| 63 void set_reported(bool reported) { reported_ = reported; } |
| 64 |
| 65 // Increase |count_frames_| by 1, and |zero| indidates whether the added |
| 66 // audio frame is zero. |
| 67 void Increment(bool zero); |
| 68 |
| 69 void Reset(); |
| 70 |
| 71 private: |
| 72 // Look back time of the repetition pattern this state keeps track of. |
| 73 const int look_back_ms_; |
| 74 |
| 75 // Counter of frames in a consecutive repetition. |
| 76 size_t count_frames_; |
| 77 |
| 78 // Whether a repetition contains only zeros. |
| 79 bool all_zero_; |
| 80 |
| 81 // |reported_| tells whether a repetition has been reported. This is to make |
| 82 // sure that a repetition with a long duration will be reported as early as |
| 83 // being detected but no more than one time. |
| 84 bool reported_; |
| 85 |
| 86 DISALLOW_COPY_AND_ASSIGN(State); |
| 87 }; |
| 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 // Check whether the state contains a valid repetition report. |
| 103 bool HasValidReport(const State* state) const; |
| 104 |
| 105 // Used to DCHECK that we are called on the correct thread. Ctor/dtor |
| 106 // should be called on one thread. The rest can be called on another. |
| 107 base::ThreadChecker main_thread_checker_; |
| 108 base::ThreadChecker processing_thread_checker_; |
| 109 |
| 110 ScopedVector<State> states_; |
| 111 |
| 112 // Ring buffer to store input audio. |
| 113 std::vector<float> audio_buffer_; |
| 114 |
| 115 // Maximum look back time of all registered repetitions. This defines the size |
| 116 // of |audio_buffer_| |
| 117 int max_look_back_ms_; |
| 118 |
| 119 // The shortest length for repetitions. |
| 120 const int min_length_ms_; |
| 121 |
| 122 // Number of audio channels in buffer. |
| 123 size_t num_channels_; |
| 124 |
| 125 // Sample rate in Hz. |
| 126 int sample_rate_; |
| 127 |
| 128 // Number of frames in |audio_buffer|. |
| 129 size_t buffer_size_frames_; |
| 130 |
| 131 // The index of the last frame in |audio_buffer|. |
| 132 size_t buffer_end_index_; |
| 133 |
| 134 // The maximum frames |audio_buffer_| can take in each time. |
| 135 const size_t max_frames_; |
| 136 |
| 137 // Action when a repetition is found. |look_back_ms| provides the look back |
| 138 // time of the detected repetition. |
| 139 RepetitionCallback repetition_callback_; |
| 140 |
| 141 DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector); |
| 142 }; |
| 143 |
| 144 } // namespace content |
| 145 |
| 146 #endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ |
OLD | NEW |