Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #include "base/metrics/histogram_macros.h" | |
| 10 | |
| 11 namespace content { | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 const AudioRepetitionDetector::Pattern kRepetitionPatterns[] = { | |
| 16 {1, 10, 10}, | |
| 17 {2, 20, 10}, | |
| 18 {3, 30, 10}, | |
| 19 {4, 40, 10}, | |
| 20 {5, 50, 10}, | |
| 21 {6, 60, 10}, | |
| 22 {7, 70, 10}, | |
| 23 {8, 80, 10}, | |
| 24 {9, 90, 10}, | |
| 25 {10, 100, 10}, | |
| 26 {20, 200, 10}, | |
| 27 }; | |
| 28 | |
| 29 // This is used for increasing the efficiency of copying data into the buffer. | |
| 30 // Input longer than |kMaxFrames| won't be a problem, and will be devided into | |
| 31 // chunks automatically. | |
| 32 const size_t kMaxFrames = 480; // 10 ms * 48 kHz | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 AudioRepetitionDetector::AudioRepetitionDetector() | |
| 37 : max_look_back_ms_(0), | |
| 38 sample_rate_(0), | |
| 39 buffer_size_frames_(0), | |
| 40 buffer_end_index_(0), | |
| 41 max_frames_(kMaxFrames) { | |
| 42 RegisterRepetitionPatterns(kRepetitionPatterns, | |
| 43 arraysize(kRepetitionPatterns)); | |
| 44 // May be created in the main render thread and used in the audio threads. | |
| 45 thread_checker_.DetachFromThread(); | |
| 46 } | |
| 47 | |
| 48 AudioRepetitionDetector::~AudioRepetitionDetector() { | |
| 49 DCHECK(thread_checker_.CalledOnValidThread()); | |
|
tommi (sloooow) - chröme
2015/10/02 19:45:31
looks like we're hitting this in the tests.
minyue
2015/10/15 18:35:47
Yes, a DetachFromThread() at ctor should also mean
| |
| 50 } | |
| 51 | |
| 52 | |
| 53 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames, | |
| 54 size_t num_channels, int sample_rate) { | |
| 55 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 56 DCHECK(!states_.empty()); | |
| 57 if (num_channels != num_channels_ || sample_rate != sample_rate_) | |
| 58 Reset(num_channels, sample_rate); | |
| 59 | |
| 60 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|. | |
| 61 // Therefore, input data with larger frames needs be divided into chunks. | |
| 62 const size_t chunk_size = max_frames_ * num_channels; | |
| 63 while (num_frames > max_frames_) { | |
| 64 Detect(data, max_frames_, num_channels, sample_rate); | |
| 65 data += chunk_size; | |
| 66 num_frames -= max_frames_; | |
| 67 } | |
| 68 | |
| 69 if (num_frames == 0) | |
| 70 return; | |
| 71 | |
| 72 AddFramesToBuffer(data, num_frames); | |
| 73 | |
| 74 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) { | |
| 75 for (State* state : states_) { | |
| 76 // Look back position depends on the sample rate. It is rounded down to | |
| 77 // the closest integer. | |
| 78 const size_t look_back_frames = | |
| 79 state->look_back_ms() * sample_rate_ / 1000; | |
| 80 // Equal(data, offset) checks if |data| equals the audio frame located | |
| 81 // |offset| frames from the end of buffer. Now a full frame has been | |
| 82 // inserted to the buffer, and thus |offset| should compensate for it. | |
| 83 if (Equal(data, look_back_frames + idx)) { | |
| 84 if (!state->reported()) { | |
| 85 state->Increment(IsZero(data, num_channels)); | |
| 86 if (state->HasValidReport(sample_rate)) { | |
| 87 ReportRepetition(state->id()); | |
| 88 state->set_reported(true); | |
| 89 } | |
| 90 } | |
| 91 } else { | |
| 92 state->Reset(); | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 | |
| 98 AudioRepetitionDetector::State::State(const Pattern &pattern) | |
| 99 : pattern_(pattern) { | |
| 100 Reset(); | |
| 101 } | |
| 102 | |
| 103 void AudioRepetitionDetector::State::Increment(bool zero) { | |
| 104 if (0 == count_frames_ && zero) { | |
| 105 // If a repetition starts with zeros, we enter the all zero mode until | |
| 106 // a non zero is found later. The point is that the beginning zeros should | |
| 107 // be counted in the length of the repetition as long as the repetition does | |
| 108 // not comprise only zeros. | |
| 109 all_zero_ = true; | |
| 110 } | |
| 111 ++count_frames_; | |
| 112 if (!zero) | |
| 113 all_zero_ = false; | |
| 114 } | |
| 115 | |
| 116 bool AudioRepetitionDetector::State::HasValidReport(int sample_rate) const { | |
| 117 return (!all_zero_ && count_frames_ >= | |
| 118 static_cast<size_t>(pattern_.min_length_ms * sample_rate / 1000)); | |
| 119 } | |
| 120 | |
| 121 void AudioRepetitionDetector::State::Reset() { | |
| 122 count_frames_ = 0; | |
| 123 all_zero_ = true; | |
| 124 reported_ = false; | |
| 125 } | |
| 126 | |
| 127 void AudioRepetitionDetector::RegisterRepetitionPatterns( | |
| 128 const Pattern* patterns, size_t num_patterns) { | |
| 129 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 130 Pattern pattern; | |
| 131 for (size_t idx = 0; idx < num_patterns; idx++) { | |
| 132 pattern = patterns[idx]; | |
| 133 ids_.push_back(pattern.id); | |
| 134 states_.push_back(new State(pattern)); | |
| 135 if (pattern.look_back_ms > max_look_back_ms_) | |
| 136 max_look_back_ms_ = pattern.look_back_ms; | |
| 137 } | |
| 138 } | |
| 139 | |
| 140 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) { | |
| 141 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 142 num_channels_ = num_channels; | |
| 143 sample_rate_ = sample_rate; | |
| 144 | |
| 145 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|. | |
| 146 buffer_size_frames_ = | |
| 147 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_; | |
| 148 | |
| 149 audio_buffer_.resize(buffer_size_frames_ * num_channels_); | |
| 150 for (State* state : states_) | |
| 151 state->Reset(); | |
| 152 } | |
| 153 | |
| 154 void AudioRepetitionDetector::AddFramesToBuffer(const float* data, | |
| 155 size_t num_frames) { | |
| 156 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 157 DCHECK_LE(num_frames, buffer_size_frames_); | |
| 158 const size_t margin = buffer_size_frames_ - buffer_end_index_; | |
| 159 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_; | |
| 160 if (num_frames <= margin) { | |
| 161 std::copy(data, data + num_frames * num_channels_, it); | |
| 162 buffer_end_index_ += num_frames; | |
| 163 } else { | |
| 164 std::copy(data, data + margin * num_channels_, it); | |
| 165 std::copy(data + margin * num_channels_, data + num_frames * num_channels_, | |
| 166 audio_buffer_.begin()); | |
| 167 buffer_end_index_ = num_frames - margin; | |
| 168 } | |
| 169 } | |
| 170 | |
| 171 bool AudioRepetitionDetector::Equal(const float* frame, | |
| 172 int look_back_frames) const { | |
| 173 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 174 const size_t look_back_index = | |
| 175 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) % | |
| 176 buffer_size_frames_ ; | |
| 177 auto it = audio_buffer_.begin() + look_back_index * num_channels_; | |
| 178 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) { | |
| 179 if (*frame != *it) | |
| 180 return false; | |
| 181 } | |
| 182 return true; | |
| 183 } | |
| 184 | |
| 185 bool AudioRepetitionDetector::IsZero(const float* frame, | |
| 186 size_t num_channels) const { | |
| 187 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) { | |
| 188 if (*frame != 0) | |
| 189 return false; | |
| 190 } | |
| 191 return true; | |
| 192 } | |
| 193 | |
| 194 void AudioRepetitionDetector::ReportRepetition(int id) { | |
| 195 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 196 UMA_HISTOGRAM_CUSTOM_ENUMERATION( | |
| 197 "Media.AudioCapturerRepetition", id, ids_); | |
|
rkaplow
2015/10/02 14:42:33
what is the output space for the ids? Normally an
minyue
2015/10/02 19:27:51
Our idea is to use the ID (integer) of some patter
| |
| 198 } | |
| 199 | |
| 200 } // namespace content | |
| OLD | NEW |