Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(454)

Side by Side Diff: content/renderer/media/audio_repetition_detector.cc

Issue 1357013006: Add detection for repeated audio in capturing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: decoupling uma reporting from detection Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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
10 namespace content {
11
12 AudioRepetitionDetector::AudioRepetitionDetector(int min_length_ms,
13 size_t max_frames,
14 const int* look_back_times,
15 size_t num_look_back)
16 : max_look_back_ms_(0),
17 min_length_ms_(min_length_ms),
18 sample_rate_(0),
19 buffer_size_frames_(0),
20 buffer_end_index_(0),
21 max_frames_(max_frames) {
22 DCHECK(main_thread_checker_.CalledOnValidThread());
23 processing_thread_checker_.DetachFromThread();
24 for (size_t i = 0; i < num_look_back; ++i) {
25 RegisterLookbackTime(look_back_times[i]);
26 }
27 }
28
29 AudioRepetitionDetector::~AudioRepetitionDetector() {
30 DCHECK(main_thread_checker_.CalledOnValidThread());
31 }
32
33 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames,
34 size_t num_channels, int sample_rate) {
35 DCHECK(processing_thread_checker_.CalledOnValidThread());
36 DCHECK(!states_.empty());
37
38 if (num_channels != num_channels_ || sample_rate != sample_rate_)
39 Reset(num_channels, sample_rate);
40
41 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|.
42 // Therefore, input data with larger frames needs be divided into chunks.
43 const size_t chunk_size = max_frames_ * num_channels;
44 while (num_frames > max_frames_) {
45 Detect(data, max_frames_, num_channels, sample_rate);
46 data += chunk_size;
47 num_frames -= max_frames_;
48 }
49
50 if (num_frames == 0)
51 return;
52
53 AddFramesToBuffer(data, num_frames);
54
55 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) {
56 for (State* state : states_) {
57 // Look back position depends on the sample rate. It is rounded down to
58 // the closest integer.
59 const size_t look_back_frames =
60 state->look_back_ms() * sample_rate_ / 1000;
61 // Equal(data, offset) checks if |data| equals the audio frame located
62 // |offset| frames from the end of buffer. Now a full frame has been
63 // inserted to the buffer, and thus |offset| should compensate for it.
64 if (Equal(data, look_back_frames + idx)) {
65 if (!state->reported()) {
66 state->Increment(IsZero(data, num_channels));
67 if (HasValidReport(state)) {
68 ReportRepetition(state->look_back_ms());
69 state->set_reported(true);
70 }
71 }
72 } else {
73 state->Reset();
74 }
75 }
76 }
77 }
78
79 AudioRepetitionDetector::State::State(int look_back_ms)
80 : look_back_ms_(look_back_ms) {
81 Reset();
82 }
83
84 void AudioRepetitionDetector::State::Increment(bool zero) {
85 if (0 == count_frames_ && zero) {
86 // If a repetition starts with zeros, we enter the all zero mode until
87 // a non zero is found later. The point is that the beginning zeros should
88 // be counted in the length of the repetition as long as the repetition does
89 // not comprise only zeros.
90 all_zero_ = true;
91 }
92 ++count_frames_;
93 if (!zero)
94 all_zero_ = false;
95 }
96
97 void AudioRepetitionDetector::State::Reset() {
98 count_frames_ = 0;
99 all_zero_ = true;
100 reported_ = false;
101 }
102
103 void AudioRepetitionDetector::RegisterLookbackTime(int look_back_ms) {
104 DCHECK(main_thread_checker_.CalledOnValidThread());
Henrik Grunell 2015/10/20 08:50:16 |states_| is accessed to different threads.
minyue 2015/10/23 12:05:23 That is true. But I have no better idea than limit
Henrik Grunell 2015/10/23 13:49:02 You have a few options: call this function on the
minyue 2015/10/26 10:41:46 This is already a private member that is called fr
Henrik Grunell 2015/10/28 14:09:23 Ah, I see. Then it's fine. Actually, since the cto
105
106 // States are added in the order of their look back times.
107 auto it = states_.begin();
108 for (; it != states_.end(); ++it) {
109 const int it_look_back = (*it)->look_back_ms();
110 if (it_look_back == look_back_ms)
111 return;
112 if (it_look_back < look_back_ms)
113 break;
114 }
115 states_.insert(it, new State(look_back_ms));
116 if (look_back_ms > max_look_back_ms_) {
117 max_look_back_ms_ = look_back_ms;
118 }
119 }
120
121 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) {
122 DCHECK(processing_thread_checker_.CalledOnValidThread());
123 num_channels_ = num_channels;
124 sample_rate_ = sample_rate;
125
126 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|.
127 buffer_size_frames_ =
128 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_;
129
130 audio_buffer_.resize(buffer_size_frames_ * num_channels_);
131 for (State* state : states_)
132 state->Reset();
133 }
134
135 void AudioRepetitionDetector::AddFramesToBuffer(const float* data,
136 size_t num_frames) {
137 DCHECK(processing_thread_checker_.CalledOnValidThread());
138 DCHECK_LE(num_frames, buffer_size_frames_);
139 const size_t margin = buffer_size_frames_ - buffer_end_index_;
140 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_;
141 if (num_frames <= margin) {
142 std::copy(data, data + num_frames * num_channels_, it);
143 buffer_end_index_ += num_frames;
144 } else {
145 std::copy(data, data + margin * num_channels_, it);
146 std::copy(data + margin * num_channels_, data + num_frames * num_channels_,
147 audio_buffer_.begin());
148 buffer_end_index_ = num_frames - margin;
149 }
150 }
151
152 bool AudioRepetitionDetector::Equal(const float* frame,
153 int look_back_frames) const {
154 DCHECK(processing_thread_checker_.CalledOnValidThread());
155 const size_t look_back_index =
156 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) %
157 buffer_size_frames_ ;
158 auto it = audio_buffer_.begin() + look_back_index * num_channels_;
159 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) {
160 if (*frame != *it)
161 return false;
162 }
163 return true;
164 }
165
166 bool AudioRepetitionDetector::IsZero(const float* frame,
167 size_t num_channels) const {
168 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) {
169 if (*frame != 0)
170 return false;
171 }
172 return true;
173 }
174
175 bool AudioRepetitionDetector::HasValidReport(const State* state) const {
176 return (!state->all_zero() && state->count_frames() >=
177 static_cast<size_t>(min_length_ms_ * sample_rate_ / 1000));
178 }
179
180 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698