Chromium Code Reviews| 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 millisecond) for the | |
|
Henrik Grunell
2015/10/28 14:09:23
Nit: milliseconds
minyue
2015/10/28 15:55:27
Done.
| |
| 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 void RegisterLookbackTime(int look_back_ms); | |
| 90 | |
| 91 // Reset |audio_buffer_| when number of channels or sample rate (Hz) changes. | |
| 92 void Reset(size_t num_channels, int sample_rate); | |
| 93 | |
| 94 // Add frames (interleaved if stereo) to |audio_buffer_|. | |
| 95 void AddFramesToBuffer(const float* data, size_t num_frames); | |
| 96 | |
| 97 // Determine if an audio frame (samples interleaved if stereo) is identical to | |
| 98 // |audio_buffer_| at a look back position. | |
| 99 bool Equal(const float* frame, int look_back_samples) const; | |
| 100 | |
| 101 // Determine if an audio frame (samples interleaved if stereo) is zero. | |
| 102 bool IsZero(const float* frame, size_t num_channels) const; | |
| 103 | |
| 104 // Check whether the state contains a valid repetition report. | |
| 105 bool HasValidReport(const State* state) const; | |
| 106 | |
| 107 // Used to DCHECK that we are called on the correct thread. Ctor/dtor | |
| 108 // should be called on one thread. The rest can be called on another. | |
| 109 base::ThreadChecker main_thread_checker_; | |
| 110 base::ThreadChecker processing_thread_checker_; | |
| 111 | |
| 112 ScopedVector<State> states_; | |
| 113 | |
| 114 // Ring buffer to store input audio. | |
| 115 std::vector<float> audio_buffer_; | |
| 116 | |
| 117 // Maximum look back time of all registered repetitions. This defines the size | |
| 118 // of |audio_buffer_| | |
| 119 int max_look_back_ms_; | |
| 120 | |
| 121 // The shortest length for repetitions. | |
| 122 const int min_length_ms_; | |
| 123 | |
| 124 // Number of audio channels in buffer. | |
| 125 size_t num_channels_; | |
| 126 | |
| 127 // Sample rate in Hz. | |
| 128 int sample_rate_; | |
| 129 | |
| 130 // Number of frames in |audio_buffer|. | |
| 131 size_t buffer_size_frames_; | |
| 132 | |
| 133 // The index of the last frame in |audio_buffer|. | |
| 134 size_t buffer_end_index_; | |
| 135 | |
| 136 // The maximum frames |audio_buffer_| can take in each time. | |
| 137 const size_t max_frames_; | |
| 138 | |
| 139 // Action when a repetition is found. |look_back_ms| provides the look back | |
| 140 // time of the detected repetition. | |
| 141 RepetitionCallback repetition_callback_; | |
| 142 | |
| 143 DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector); | |
| 144 }; | |
| 145 | |
| 146 } // namespace content | |
| 147 | |
| 148 #endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ | |
| OLD | NEW |