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 #include "content/renderer/media/audio_repetition_detector.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/macros.h" | |
9 #include "base/metrics/histogram_macros.h" | |
10 | |
11 namespace content { | |
12 | |
13 namespace { | |
14 | |
15 const AudioRepetitionDetector::Pattern kRepetitionPatterns[] = { | |
16 {1, 10, 10}, | |
17 {2, 20, 10}, | |
18 {3, 30, 10}, | |
19 {4, 40, 10}, | |
20 {5, 50, 10}, | |
21 {6, 60, 10}, | |
22 {7, 70, 10}, | |
23 {8, 80, 10}, | |
24 {9, 90, 10}, | |
25 {10, 100, 10}, | |
26 {20, 200, 10}, | |
27 }; | |
28 | |
29 // This is used for increasing the efficiency of copying data into the buffer. | |
30 // Input longer than |kMaxFrames| won't be a problem, and will be devided into | |
31 // chunks automatically. | |
32 const size_t kMaxFrames = 480; // 10 ms * 48 kHz | |
33 | |
34 } // namespace | |
35 | |
36 AudioRepetitionDetector::AudioRepetitionDetector() | |
37 : max_look_back_ms_(0), | |
38 sample_rate_(0), | |
39 buffer_size_frames_(0), | |
40 buffer_end_index_(0), | |
41 max_frames_(kMaxFrames) { | |
42 RegisterRepetitionPatterns(kRepetitionPatterns, | |
43 arraysize(kRepetitionPatterns)); | |
44 } | |
45 | |
46 AudioRepetitionDetector::~AudioRepetitionDetector() { | |
47 DCHECK(thread_checker_.CalledOnValidThread()); | |
48 } | |
49 | |
50 | |
51 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames, | |
tommi (sloooow) - chröme
2015/10/01 17:37:46
Has someone reviewed the implementation in this fi
minyue
2015/10/01 21:25:08
Henrik Lundin, Per and Andrew looked at it. Henrik
| |
52 size_t num_channels, int sample_rate) { | |
53 DCHECK(thread_checker_.CalledOnValidThread()); | |
54 DCHECK_GT(states_.size(), 0ul); | |
Henrik Grunell
2015/10/01 16:06:01
Use state_.empty().
minyue
2015/10/01 21:25:08
good point
| |
55 if (num_channels != num_channels_ || sample_rate != sample_rate_) | |
56 Reset(num_channels, sample_rate); | |
57 | |
58 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|. | |
59 // Therefore, input data with larger frames needs be divided into chunks. | |
60 while (num_frames > max_frames_) { | |
61 Detect(data, max_frames_, num_channels, sample_rate); | |
62 data += max_frames_ * num_channels; | |
tommi (sloooow) - chröme
2015/10/01 17:37:47
nit: could calculate max_frames_ * num_channels ou
minyue
2015/10/01 21:25:08
Done.
| |
63 num_frames -= max_frames_; | |
64 } | |
65 | |
66 if (num_frames == 0) | |
67 return; | |
68 | |
69 AddFramesToBuffer(data, num_frames); | |
70 | |
71 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) { | |
72 for (auto state : states_) { | |
tommi (sloooow) - chröme
2015/10/01 17:37:46
can you change this to:
for (const State* state :
minyue
2015/10/01 21:25:08
Done. cannot be const though
| |
73 // Look back position depends on the sample rate. It is rounded down to | |
74 // the closest integer. | |
75 const size_t look_back_frames = | |
76 state->look_back_ms() * sample_rate_ / 1000; | |
77 // Equal(data, offset) checks if |data| equals the audio frame located | |
78 // |offset| frames from the end of buffer. Now a full frame has been | |
79 // inserted to the buffer, and thus |offset| should compensate for it. | |
80 if (Equal(data, look_back_frames + idx)) { | |
81 if (!state->reported()) { | |
82 state->Increment(IsZero(data, num_channels)); | |
83 if (state->HasValidReport(sample_rate)) { | |
84 ReportRepetition(state->id()); | |
85 state->set_reported(true); | |
86 } | |
87 } | |
88 } else { | |
89 state->Reset(); | |
90 } | |
91 } | |
92 } | |
93 } | |
94 | |
95 AudioRepetitionDetector::State::State(const Pattern &pattern) | |
96 : pattern_(pattern) { | |
97 Reset(); | |
98 } | |
99 | |
100 void AudioRepetitionDetector::State::Increment(bool zero) { | |
tommi (sloooow) - chröme
2015/10/01 17:37:46
what about adding a thread checker to these method
minyue
2015/10/01 21:25:08
My idea is that State is an inner private class, a
| |
101 if (0 == count_frames_ && zero) { | |
102 // If a repetition starts with zeros, we enter the all zero mode until | |
103 // a non zero is found later. The point is that the beginning zeros should | |
104 // be counted in the length of the repetition as long as the repetition does | |
105 // not comprise only zeros. | |
106 all_zero_ = true; | |
107 } | |
108 ++count_frames_; | |
109 if (!zero) | |
110 all_zero_ = false; | |
111 } | |
112 | |
113 bool AudioRepetitionDetector::State::HasValidReport(int sample_rate) const { | |
114 return (!all_zero_ && count_frames_ >= | |
115 static_cast<size_t>(pattern_.min_length_ms * sample_rate / 1000)); | |
116 } | |
117 | |
118 void AudioRepetitionDetector::State::Reset() { | |
119 count_frames_ = 0; | |
120 all_zero_ = true; | |
121 reported_ = false; | |
122 } | |
123 | |
124 void AudioRepetitionDetector::RegisterRepetitionPatterns( | |
125 const Pattern* patterns, size_t num_patterns) { | |
126 DCHECK(thread_checker_.CalledOnValidThread()); | |
127 Pattern pattern; | |
128 for (size_t idx = 0; idx < num_patterns; idx++) { | |
129 pattern = patterns[idx]; | |
130 ids_.push_back(pattern.id); | |
131 states_.push_back(new State(pattern)); | |
132 if (pattern.look_back_ms > max_look_back_ms_) | |
133 max_look_back_ms_ = pattern.look_back_ms; | |
134 } | |
135 } | |
136 | |
137 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) { | |
138 DCHECK(thread_checker_.CalledOnValidThread()); | |
139 num_channels_ = num_channels; | |
140 sample_rate_ = sample_rate; | |
141 | |
142 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|. | |
143 buffer_size_frames_ = | |
144 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_; | |
145 | |
146 audio_buffer_.resize(buffer_size_frames_ * num_channels_); | |
147 for (auto state : states_) | |
minyue
2015/10/01 21:25:08
also changed this auto
| |
148 state->Reset(); | |
149 } | |
150 | |
151 void AudioRepetitionDetector::AddFramesToBuffer(const float* data, | |
152 size_t num_frames) { | |
153 DCHECK(thread_checker_.CalledOnValidThread()); | |
154 DCHECK_LE(num_frames, buffer_size_frames_); | |
155 const size_t margin = buffer_size_frames_ - buffer_end_index_; | |
156 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_; | |
157 if (num_frames <= margin) { | |
158 std::copy(data, data + num_frames * num_channels_, it); | |
159 buffer_end_index_ += num_frames; | |
160 } else { | |
161 std::copy(data, data + margin * num_channels_, it); | |
162 std::copy(data + margin * num_channels_, data + num_frames * num_channels_, | |
163 audio_buffer_.begin()); | |
164 buffer_end_index_ = num_frames - margin; | |
165 } | |
166 } | |
167 | |
168 bool AudioRepetitionDetector::Equal(const float* frame, | |
169 int look_back_frames) const { | |
170 DCHECK(thread_checker_.CalledOnValidThread()); | |
171 const size_t look_back_index = | |
172 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) % | |
173 buffer_size_frames_ ; | |
174 auto it = audio_buffer_.begin() + look_back_index * num_channels_; | |
175 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) { | |
176 if (*frame != *it) | |
177 return false; | |
178 } | |
179 return true; | |
180 } | |
181 | |
182 bool AudioRepetitionDetector::IsZero(const float* frame, | |
183 size_t num_channels) const { | |
184 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) { | |
185 if (*frame != 0) | |
186 return false; | |
187 } | |
188 return true; | |
189 } | |
190 | |
191 void AudioRepetitionDetector::ReportRepetition(int id) { | |
192 DCHECK(thread_checker_.CalledOnValidThread()); | |
193 UMA_HISTOGRAM_CUSTOM_ENUMERATION( | |
194 "Media.AudioCapturerRepetition", id, ids_); | |
195 } | |
196 | |
197 } // namespace content | |
OLD | NEW |