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 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. | |
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
| |
22 class CONTENT_EXPORT AudioRepetitionDetector { | |
23 public: | |
24 AudioRepetitionDetector(); | |
25 virtual ~AudioRepetitionDetector(); | |
26 | |
27 // Detect repetition in |data|. A UMA report is generated upon finding | |
28 // a repetition. |sample_rate| is measured in Hz. | |
29 void Detect(const float* data, size_t num_frames, size_t num_channels, | |
30 int sample_rate); | |
31 | |
32 private: | |
33 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.
| |
34 | |
35 // A state is used by the detector to keep track of a consecutive repetition, | |
36 // whether the samples in a repetition are all zeros, and whether a repetition | |
37 // has been reported. | |
38 class State { | |
39 public: | |
40 explicit State(int look_back_ms); | |
41 | |
42 int look_back_ms() const { return look_back_ms_; }; | |
43 size_t count_frames() const { return count_frames_; } | |
44 bool all_zero() const { return all_zero_; } | |
45 bool reported() const { return reported_; } | |
46 void set_reported(bool reported) { reported_ = reported; } | |
47 | |
48 // Increase |count_frames_| by 1, and |zero| indidates whether the added | |
49 // audio frame is zero. | |
50 void Increment(bool zero); | |
51 | |
52 void Reset(); | |
53 | |
54 private: | |
55 // Look back time of the repetition pattern this state keeps track of. | |
56 const int look_back_ms_; | |
57 | |
58 // 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.
| |
59 size_t count_frames_; | |
60 | |
61 // whether a repetition contains only zeros. | |
62 bool all_zero_; | |
63 | |
64 // |reported_| tells whether a repetition has been reported. This is to make | |
65 // sure that a repetition with a long duration will be reported as early as | |
66 // being detected but no more than one time. | |
67 bool reported_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(State); | |
70 }; | |
71 | |
72 void RegisterLookbackTime(int look_back_ms); | |
73 | |
74 // Reset |audio_buffer_| when number of channels or sample rate (Hz) changes. | |
75 void Reset(size_t num_channels, int sample_rate); | |
76 | |
77 // Add frames (interleaved if stereo) to |audio_buffer_|. | |
78 void AddFramesToBuffer(const float* data, size_t num_frames); | |
79 | |
80 // Determine if an audio frame (samples interleaved if stereo) is identical to | |
81 // |audio_buffer_| at a look back position. | |
82 bool Equal(const float* frame, int look_back_samples) const; | |
83 | |
84 // Determine if an audio frame (samples interleaved if stereo) is zero. | |
85 bool IsZero(const float* frame, size_t num_channels) const; | |
86 | |
87 // Check whether the state contains a valid repetition report. | |
88 bool HasValidReport(const State* state) const; | |
89 | |
90 // Action when found repetition. | |
91 virtual void ReportRepetition(int look_back_ms); | |
92 | |
93 // Used to DCHECK that we are called on the correct thread. | |
94 base::ThreadChecker thread_checker_; | |
95 | |
96 ScopedVector<State> states_; | |
97 | |
98 // Ring buffer to store input audio. | |
99 std::vector<float> audio_buffer_; | |
100 | |
101 // Maximum look back time of all registered repetitions. This defines the size | |
102 // of |audio_buffer_| | |
103 int max_look_back_ms_; | |
104 | |
105 // The shortest length for repetitions. | |
106 int min_length_ms_; | |
107 | |
108 // Number of audio channels in buffer. | |
109 size_t num_channels_; | |
110 | |
111 // Sample rate in Hz. | |
112 int sample_rate_; | |
113 | |
114 // Number of frames in |audio_buffer|. | |
115 size_t buffer_size_frames_; | |
116 | |
117 // The index of the last frame in |audio_buffer|. | |
118 size_t buffer_end_index_; | |
119 | |
120 // The maximum frames |audio_buffer_| can take in each time. | |
121 size_t max_frames_; | |
122 | |
123 DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector); | |
124 }; | |
125 | |
126 } // namespace content | |
127 | |
128 #endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_ | |
OLD | NEW |