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

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: refinement Created 5 years, 1 month 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(
13 int min_length_ms, size_t max_frames,
14 const std::vector<int>& look_back_times,
15 const RepetitionCallback& repetition_callback)
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 repetition_callback_(repetition_callback) {
23 DCHECK(main_thread_checker_.CalledOnValidThread());
24 processing_thread_checker_.DetachFromThread();
25 for (auto look_back : look_back_times) {
26 RegisterLookbackTime(look_back);
27 }
28 }
29
30 AudioRepetitionDetector::~AudioRepetitionDetector() {
31 DCHECK(main_thread_checker_.CalledOnValidThread());
32 }
33
34 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames,
35 size_t num_channels, int sample_rate) {
36 DCHECK(processing_thread_checker_.CalledOnValidThread());
37 DCHECK(!states_.empty());
38
39 if (num_channels != num_channels_ || sample_rate != sample_rate_)
40 Reset(num_channels, sample_rate);
41
42 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|.
43 // Therefore, input data with larger frames needs be divided into chunks.
44 const size_t chunk_size = max_frames_ * num_channels;
45 while (num_frames > max_frames_) {
46 Detect(data, max_frames_, num_channels, sample_rate);
47 data += chunk_size;
48 num_frames -= max_frames_;
49 }
50
51 if (num_frames == 0)
52 return;
53
54 AddFramesToBuffer(data, num_frames);
55
56 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) {
57 for (State* state : states_) {
58 // Look back position depends on the sample rate. It is rounded down to
59 // the closest integer.
60 const size_t look_back_frames =
61 state->look_back_ms() * sample_rate_ / 1000;
62 // Equal(data, offset) checks if |data| equals the audio frame located
63 // |offset| frames from the end of buffer. Now a full frame has been
64 // inserted to the buffer, and thus |offset| should compensate for it.
65 if (Equal(data, look_back_frames + idx)) {
66 if (!state->reported()) {
67 state->Increment(IsZero(data, num_channels));
68 if (HasValidReport(state)) {
69 repetition_callback_.Run(state->look_back_ms());
70 state->set_reported(true);
71 }
72 }
73 } else {
74 state->Reset();
75 }
76 }
77 }
78 }
79
80 AudioRepetitionDetector::State::State(int look_back_ms)
81 : look_back_ms_(look_back_ms) {
82 Reset();
83 }
84
85 void AudioRepetitionDetector::State::Increment(bool zero) {
86 if (0 == count_frames_ && zero) {
87 // If a repetition starts with zeros, we enter the all zero mode until
88 // a non zero is found later. The point is that the beginning zeros should
89 // be counted in the length of the repetition as long as the repetition does
90 // not comprise only zeros.
91 all_zero_ = true;
92 }
93 ++count_frames_;
94 if (!zero)
95 all_zero_ = false;
96 }
97
98 void AudioRepetitionDetector::State::Reset() {
99 count_frames_ = 0;
100 all_zero_ = true;
101 reported_ = false;
102 }
103
104 void AudioRepetitionDetector::RegisterLookbackTime(int look_back_ms) {
105 DCHECK(main_thread_checker_.CalledOnValidThread());
106
107 // States are added in the order of their look back times.
108 auto it = states_.begin();
109 for (; it != states_.end(); ++it) {
110 const int it_look_back = (*it)->look_back_ms();
111 if (it_look_back == look_back_ms)
112 return;
113 if (it_look_back < look_back_ms)
114 break;
115 }
116 states_.insert(it, new State(look_back_ms));
117 if (look_back_ms > max_look_back_ms_) {
118 max_look_back_ms_ = look_back_ms;
119 }
120 }
121
122 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) {
123 DCHECK(processing_thread_checker_.CalledOnValidThread());
124 num_channels_ = num_channels;
125 sample_rate_ = sample_rate;
126
127 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|.
128 buffer_size_frames_ =
129 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_;
130
131 audio_buffer_.resize(buffer_size_frames_ * num_channels_);
132 for (State* state : states_)
133 state->Reset();
134 }
135
136 void AudioRepetitionDetector::AddFramesToBuffer(const float* data,
137 size_t num_frames) {
138 DCHECK(processing_thread_checker_.CalledOnValidThread());
139 DCHECK_LE(num_frames, buffer_size_frames_);
140 const size_t margin = buffer_size_frames_ - buffer_end_index_;
141 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_;
142 if (num_frames <= margin) {
143 std::copy(data, data + num_frames * num_channels_, it);
144 buffer_end_index_ += num_frames;
145 } else {
146 std::copy(data, data + margin * num_channels_, it);
147 std::copy(data + margin * num_channels_, data + num_frames * num_channels_,
148 audio_buffer_.begin());
149 buffer_end_index_ = num_frames - margin;
150 }
151 }
152
153 bool AudioRepetitionDetector::Equal(const float* frame,
154 int look_back_frames) const {
155 DCHECK(processing_thread_checker_.CalledOnValidThread());
156 const size_t look_back_index =
157 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) %
158 buffer_size_frames_ ;
159 auto it = audio_buffer_.begin() + look_back_index * num_channels_;
160 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) {
161 if (*frame != *it)
162 return false;
163 }
164 return true;
165 }
166
167 bool AudioRepetitionDetector::IsZero(const float* frame,
168 size_t num_channels) const {
169 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) {
170 if (*frame != 0)
171 return false;
172 }
173 return true;
174 }
175
176 bool AudioRepetitionDetector::HasValidReport(const State* state) const {
177 return (!state->all_zero() && state->count_frames() >=
178 static_cast<size_t>(min_length_ms_ * sample_rate_ / 1000));
179 }
180
181 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698