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

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
26 // Build |states_| for the all look back times. Automatically avoid
Henrik Grunell 2015/10/28 17:52:01 Is this part only for sorting and avoiding duplica
minyue 2015/10/28 19:45:29 I actually tried to use set at the first place. It
Henrik Grunell 2015/10/30 10:40:16 I mean std::set<int>& look_back_times as argument.
27 // duplications in |look_back_times| if any.
28 for (auto look_back : look_back_times) {
29 // States are added in the order of their look back times.
30 auto it = states_.begin();
31 for (; it != states_.end(); ++it) {
32 const int it_look_back = (*it)->look_back_ms();
33 if (it_look_back == look_back)
34 return;
35 if (it_look_back < look_back)
36 break;
37 }
38 states_.insert(it, new State(look_back));
39 if (look_back > max_look_back_ms_) {
40 max_look_back_ms_ = look_back;
41 }
42 }
43 }
44
45 AudioRepetitionDetector::~AudioRepetitionDetector() {
46 DCHECK(main_thread_checker_.CalledOnValidThread());
47 }
48
49 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames,
50 size_t num_channels, int sample_rate) {
51 DCHECK(processing_thread_checker_.CalledOnValidThread());
52 DCHECK(!states_.empty());
53
54 if (num_channels != num_channels_ || sample_rate != sample_rate_)
55 Reset(num_channels, sample_rate);
56
57 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|.
58 // Therefore, input data with larger frames needs be divided into chunks.
59 const size_t chunk_size = max_frames_ * num_channels;
60 while (num_frames > max_frames_) {
61 Detect(data, max_frames_, num_channels, sample_rate);
62 data += chunk_size;
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 (State* state : states_) {
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 (HasValidReport(state)) {
84 repetition_callback_.Run(state->look_back_ms());
85 state->set_reported(true);
86 }
87 }
88 } else {
89 state->Reset();
90 }
91 }
92 }
93 }
94
95 AudioRepetitionDetector::State::State(int look_back_ms)
96 : look_back_ms_(look_back_ms) {
97 Reset();
98 }
99
100 void AudioRepetitionDetector::State::Increment(bool zero) {
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 void AudioRepetitionDetector::State::Reset() {
114 count_frames_ = 0;
115 all_zero_ = true;
116 reported_ = false;
117 }
118
119 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) {
120 DCHECK(processing_thread_checker_.CalledOnValidThread());
121 num_channels_ = num_channels;
122 sample_rate_ = sample_rate;
123
124 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|.
125 buffer_size_frames_ =
126 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_;
127
128 audio_buffer_.resize(buffer_size_frames_ * num_channels_);
129 for (State* state : states_)
130 state->Reset();
131 }
132
133 void AudioRepetitionDetector::AddFramesToBuffer(const float* data,
134 size_t num_frames) {
135 DCHECK(processing_thread_checker_.CalledOnValidThread());
136 DCHECK_LE(num_frames, buffer_size_frames_);
137 const size_t margin = buffer_size_frames_ - buffer_end_index_;
138 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_;
139 if (num_frames <= margin) {
140 std::copy(data, data + num_frames * num_channels_, it);
141 buffer_end_index_ += num_frames;
142 } else {
143 std::copy(data, data + margin * num_channels_, it);
144 std::copy(data + margin * num_channels_, data + num_frames * num_channels_,
145 audio_buffer_.begin());
146 buffer_end_index_ = num_frames - margin;
147 }
148 }
149
150 bool AudioRepetitionDetector::Equal(const float* frame,
151 int look_back_frames) const {
152 DCHECK(processing_thread_checker_.CalledOnValidThread());
153 const size_t look_back_index =
154 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) %
155 buffer_size_frames_ ;
156 auto it = audio_buffer_.begin() + look_back_index * num_channels_;
157 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) {
158 if (*frame != *it)
159 return false;
160 }
161 return true;
162 }
163
164 bool AudioRepetitionDetector::IsZero(const float* frame,
165 size_t num_channels) const {
166 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) {
167 if (*frame != 0)
168 return false;
169 }
170 return true;
171 }
172
173 bool AudioRepetitionDetector::HasValidReport(const State* state) const {
174 return (!state->all_zero() && state->count_frames() >=
175 static_cast<size_t>(min_length_ms_ * sample_rate_ / 1000));
176 }
177
178 } // namespace content
OLDNEW
« no previous file with comments | « content/renderer/media/audio_repetition_detector.h ('k') | content/renderer/media/audio_repetition_detector_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698