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

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: use std::sort and unique 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 // Avoid duplications in |look_back_times| if any.
tommi (sloooow) - chröme 2015/10/30 15:33:01 Since we always create a copy here, would it make
minyue 2015/10/30 20:17:05 being sorted or not is not essential (except for t
Henrik Grunell 2015/11/02 09:12:11 I'm in favor of DCHECK-ing that it's sorted and no
27 std::vector<int> temp(look_back_times);
28 std::sort(temp.begin(), temp.end());
29 temp.erase(std::unique(temp.begin(), temp.end()), temp.end());
tommi (sloooow) - chröme 2015/10/30 15:33:01 I guess this is what Henrik commented on (will you
minyue 2015/10/30 20:17:05 You mean the nit Henrik pointed out: i.e., erase v
30
31 max_look_back_ms_ = temp.back();
32 for (auto look_back : temp) {
tommi (sloooow) - chröme 2015/10/30 15:33:01 no {}
tommi (sloooow) - chröme 2015/10/30 15:33:01 instead of auto, just use int here. The way auto
minyue 2015/10/30 20:17:05 Done.
minyue 2015/10/30 20:17:05 Done.
33 states_.push_back(new State(look_back));
34 }
35 }
36
37 AudioRepetitionDetector::~AudioRepetitionDetector() {
38 DCHECK(main_thread_checker_.CalledOnValidThread());
39 }
40
41 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames,
42 size_t num_channels, int sample_rate) {
43 DCHECK(processing_thread_checker_.CalledOnValidThread());
44 DCHECK(!states_.empty());
45
46 if (num_channels != num_channels_ || sample_rate != sample_rate_)
47 Reset(num_channels, sample_rate);
48
49 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|.
50 // Therefore, input data with larger frames needs be divided into chunks.
51 const size_t chunk_size = max_frames_ * num_channels;
52 while (num_frames > max_frames_) {
53 Detect(data, max_frames_, num_channels, sample_rate);
54 data += chunk_size;
55 num_frames -= max_frames_;
56 }
57
58 if (num_frames == 0)
59 return;
60
61 AddFramesToBuffer(data, num_frames);
62
63 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) {
64 for (State* state : states_) {
65 // Look back position depends on the sample rate. It is rounded down to
66 // the closest integer.
67 const size_t look_back_frames =
68 state->look_back_ms() * sample_rate_ / 1000;
69 // Equal(data, offset) checks if |data| equals the audio frame located
70 // |offset| frames from the end of buffer. Now a full frame has been
71 // inserted to the buffer, and thus |offset| should compensate for it.
72 if (Equal(data, look_back_frames + idx)) {
73 if (!state->reported()) {
74 state->Increment(IsZero(data, num_channels));
75 if (HasValidReport(state)) {
76 repetition_callback_.Run(state->look_back_ms());
77 state->set_reported(true);
78 }
79 }
80 } else {
81 state->Reset();
82 }
83 }
84 }
85 }
86
87 AudioRepetitionDetector::State::State(int look_back_ms)
88 : look_back_ms_(look_back_ms) {
89 Reset();
90 }
91
92 void AudioRepetitionDetector::State::Increment(bool zero) {
93 if (0 == count_frames_ && zero) {
tommi (sloooow) - chröme 2015/10/30 15:33:01 nit: convention is how you would read it: |count_f
minyue 2015/10/30 20:17:05 Done.
94 // If a repetition starts with zeros, we enter the all zero mode until
95 // a non zero is found later. The point is that the beginning zeros should
96 // be counted in the length of the repetition as long as the repetition does
97 // not comprise only zeros.
98 all_zero_ = true;
99 }
100 ++count_frames_;
101 if (!zero)
tommi (sloooow) - chröme 2015/10/30 15:33:01 it feels a bit strange to check this flag twice in
minyue 2015/10/30 20:17:05 thanks, it reads much better. if zero is true but
102 all_zero_ = false;
103 }
104
105 void AudioRepetitionDetector::State::Reset() {
106 count_frames_ = 0;
107 all_zero_ = true;
108 reported_ = false;
109 }
110
111 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) {
112 DCHECK(processing_thread_checker_.CalledOnValidThread());
113 num_channels_ = num_channels;
114 sample_rate_ = sample_rate;
115
116 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|.
117 buffer_size_frames_ =
118 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_;
119
120 audio_buffer_.resize(buffer_size_frames_ * num_channels_);
121 for (State* state : states_)
122 state->Reset();
123 }
124
125 void AudioRepetitionDetector::AddFramesToBuffer(const float* data,
126 size_t num_frames) {
127 DCHECK(processing_thread_checker_.CalledOnValidThread());
128 DCHECK_LE(num_frames, buffer_size_frames_);
129 const size_t margin = buffer_size_frames_ - buffer_end_index_;
130 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_;
131 if (num_frames <= margin) {
132 std::copy(data, data + num_frames * num_channels_, it);
133 buffer_end_index_ += num_frames;
134 } else {
135 std::copy(data, data + margin * num_channels_, it);
136 std::copy(data + margin * num_channels_, data + num_frames * num_channels_,
137 audio_buffer_.begin());
138 buffer_end_index_ = num_frames - margin;
139 }
140 }
141
142 bool AudioRepetitionDetector::Equal(const float* frame,
143 int look_back_frames) const {
144 DCHECK(processing_thread_checker_.CalledOnValidThread());
145 const size_t look_back_index =
146 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) %
147 buffer_size_frames_ ;
148 auto it = audio_buffer_.begin() + look_back_index * num_channels_;
149 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) {
150 if (*frame != *it)
151 return false;
152 }
153 return true;
154 }
155
156 bool AudioRepetitionDetector::IsZero(const float* frame,
157 size_t num_channels) const {
158 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) {
159 if (*frame != 0)
160 return false;
161 }
162 return true;
163 }
164
165 bool AudioRepetitionDetector::HasValidReport(const State* state) const {
166 return (!state->all_zero() && state->count_frames() >=
167 static_cast<size_t>(min_length_ms_ * sample_rate_ / 1000));
168 }
169
170 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698