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