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