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 // trunks automatically. | |
| 32 const size_t kMaxFrames = 480; // 10 ms * 48 kHz | |
| 33 | |
| 34 } // namespace | |
| 35 | |
| 36 AudioRepetitionDetector::State::State(const Pattern &pattern) | |
| 37 : pattern_(pattern) { | |
| 38 Reset(); | |
| 39 } | |
| 40 | |
| 41 void AudioRepetitionDetector::State::Increment(bool zero) { | |
| 42 if (0 == count_frames_ && zero) { | |
| 43 // If a repetition starts with zeros, we enter the all zero mode until | |
| 44 // a non zero is found later. The point is that the beginning zeros should | |
| 45 // be counted in the length of the repetition as long as the repetition does | |
| 46 // not comprise only zeros. | |
| 47 all_zero_ = true; | |
| 48 } | |
| 49 ++count_frames_; | |
| 50 if (!zero) | |
| 51 all_zero_ = false; | |
| 52 } | |
| 53 | |
| 54 bool AudioRepetitionDetector::State::HasValidReport(int sample_rate) const { | |
| 55 return (!all_zero_ && count_frames_ >= | |
| 56 static_cast<size_t>(pattern_.min_length_ms * sample_rate / 1000)); | |
| 57 } | |
| 58 | |
| 59 void AudioRepetitionDetector::State::Reset() { | |
| 60 count_frames_ = 0; | |
| 61 all_zero_ = true; | |
| 62 reported_ = false; | |
| 63 } | |
| 64 | |
| 65 AudioRepetitionDetector::AudioRepetitionDetector() | |
| 66 : max_look_back_ms_(0), | |
| 67 sample_rate_(0), | |
| 68 buffer_size_frames_(0), | |
| 69 buffer_end_index_(0), | |
| 70 max_frames_(kMaxFrames) { | |
| 71 RegisterRepetitionPatterns(kRepetitionPatterns, | |
| 72 arraysize(kRepetitionPatterns)); | |
| 73 } | |
| 74 | |
| 75 AudioRepetitionDetector::~AudioRepetitionDetector() { | |
| 76 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 77 } | |
| 78 | |
| 79 void AudioRepetitionDetector::RegisterRepetitionPatterns( | |
| 80 const Pattern* patterns, size_t num_patterns) { | |
| 81 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 82 Pattern pattern; | |
| 83 for (size_t idx = 0; idx < num_patterns; idx++) { | |
| 84 pattern = patterns[idx]; | |
| 85 ids_.push_back(pattern.id); | |
| 86 states_.push_back(new State(pattern)); | |
| 87 if (pattern.look_back_ms > max_look_back_ms_) | |
| 88 max_look_back_ms_ = pattern.look_back_ms; | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 void AudioRepetitionDetector::Reset(size_t num_channels, int sample_rate) { | |
| 93 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 94 num_channels_ = num_channels; | |
| 95 sample_rate_ = sample_rate; | |
| 96 | |
| 97 // |(xxx + 999) / 1000| is an arithmetic way to round up |xxx / 1000|. | |
| 98 buffer_size_frames_ = | |
| 99 (max_look_back_ms_ * sample_rate_ + 999) / 1000 + max_frames_; | |
| 100 | |
| 101 audio_buffer_.resize(buffer_size_frames_ * num_channels_); | |
| 102 for (auto state : states_) | |
| 103 state->Reset(); | |
| 104 } | |
| 105 | |
| 106 void AudioRepetitionDetector::AddFramesToBuffer(const float* data, | |
| 107 size_t num_frames) { | |
| 108 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 109 DCHECK_LE(num_frames, buffer_size_frames_); | |
| 110 const size_t margin = buffer_size_frames_ - buffer_end_index_; | |
| 111 const auto it = audio_buffer_.begin() + buffer_end_index_ * num_channels_; | |
| 112 if (num_frames <= margin) { | |
| 113 std::copy(data, data + num_frames * num_channels_, it); | |
| 114 buffer_end_index_ += num_frames; | |
| 115 } else { | |
| 116 std::copy(data, data + margin * num_channels_, it); | |
| 117 std::copy(data + margin * num_channels_, data + num_frames * num_channels_, | |
| 118 audio_buffer_.begin()); | |
| 119 buffer_end_index_ = num_frames - margin; | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 bool AudioRepetitionDetector::Equal(const float* frame, | |
| 124 int look_back_frames) const { | |
| 125 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 126 const size_t look_back_index = | |
| 127 (buffer_end_index_ + buffer_size_frames_ - look_back_frames) % | |
| 128 buffer_size_frames_ ; | |
| 129 auto it = audio_buffer_.begin() + look_back_index * num_channels_; | |
| 130 for (size_t channel = 0; channel < num_channels_; ++channel, ++frame, ++it) { | |
| 131 if (*frame != *it) | |
| 132 return false; | |
| 133 } | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 bool AudioRepetitionDetector::IsZero(const float* frame, | |
| 138 size_t num_channels) const { | |
| 139 for (size_t channel = 0; channel < num_channels; ++channel, ++frame) { | |
| 140 if (*frame != 0) | |
| 141 return false; | |
| 142 } | |
| 143 return true; | |
| 144 } | |
| 145 | |
| 146 void AudioRepetitionDetector::Detect(const float* data, size_t num_frames, | |
| 147 size_t num_channels, int sample_rate) { | |
| 148 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 149 DCHECK_GT(states_.size(), 0ul); | |
| 150 if (num_channels != num_channels_ || sample_rate != sample_rate_) | |
| 151 Reset(num_channels, sample_rate); | |
| 152 | |
| 153 // The maximum number of frames |audio_buffer_| can take in is |max_frames_|. | |
| 154 // Therefore, input data with larger frames needs be divided into trunks. | |
|
ajm
2015/09/30 00:23:43
s/trunks/chunks
| |
| 155 while (num_frames > max_frames_) { | |
| 156 Detect(data, max_frames_, num_channels, sample_rate); | |
| 157 data += max_frames_ * num_channels; | |
| 158 num_frames -= max_frames_; | |
| 159 } | |
| 160 | |
| 161 if (num_frames == 0) | |
| 162 return; | |
| 163 | |
| 164 AddFramesToBuffer(data, num_frames); | |
| 165 | |
| 166 for (size_t idx = num_frames; idx > 0; --idx, data += num_channels) { | |
| 167 for (auto state : states_) { | |
| 168 // Look back position depends on the sample rate. It is rounded down to | |
| 169 // the closest integer. | |
| 170 const size_t look_back_frames = | |
| 171 state->look_back_ms() * sample_rate_ / 1000; | |
| 172 // Equal(data, offset) checks if |data| equals the audio frame located | |
| 173 // |offset| frames from the end of buffer. Now a full frame has been | |
| 174 // inserted to the buffer, and thus |offset| should compensate for it. | |
| 175 if (Equal(data, look_back_frames + idx)) { | |
| 176 if (!state->reported()) { | |
| 177 state->Increment(IsZero(data, num_channels)); | |
| 178 if (state->HasValidReport(sample_rate)) { | |
| 179 ReportRepetition(state->id()); | |
| 180 state->set_reported(true); | |
| 181 } | |
| 182 } | |
| 183 } else { | |
| 184 state->Reset(); | |
| 185 } | |
| 186 } | |
| 187 } | |
| 188 } | |
| 189 | |
| 190 void AudioRepetitionDetector::ReportRepetition(int id) { | |
| 191 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 192 UMA_HISTOGRAM_CUSTOM_ENUMERATION( | |
| 193 "Media.AudioCapturerRepetition", id, ids_); | |
| 194 } | |
| 195 | |
| 196 } // namespace content | |
| OLD | NEW |