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

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