| Index: content/renderer/media/audio_repetition_detector.h
|
| diff --git a/content/renderer/media/audio_repetition_detector.h b/content/renderer/media/audio_repetition_detector.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..15949810921295e3b5a5b60b2014ad7f2e52faf7
|
| --- /dev/null
|
| +++ b/content/renderer/media/audio_repetition_detector.h
|
| @@ -0,0 +1,141 @@
|
| +// Copyright 2015 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#ifndef CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_
|
| +#define CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/memory/scoped_vector.h"
|
| +#include "base/threading/thread_checker.h"
|
| +#include "content/common/content_export.h"
|
| +
|
| +namespace content {
|
| +
|
| +
|
| +// AudioRepetitionDetector detects bit-exact audio repetitions of registered
|
| +// patterns. A repetition pattern includes a look back time and a minimum
|
| +// duration. The detector buffers the audio signal and checks equality of each
|
| +// input sample against the samples at the look back positions of all registered
|
| +// patterns, and counts the duration of any consecutive equality.
|
| +class CONTENT_EXPORT AudioRepetitionDetector {
|
| + public:
|
| + AudioRepetitionDetector();
|
| + virtual ~AudioRepetitionDetector();
|
| +
|
| + // Pattern is used for registering repetition patterns.
|
| + // A pattern includes an |id| and two time-related descriptors |look_back_ms|
|
| + // and |min_length_ms|, which are counted in milliseconds.
|
| + struct Pattern {
|
| + int id;
|
| + int look_back_ms;
|
| + int min_length_ms;
|
| + };
|
| +
|
| + // Detect repetition in |data|. A UMA report is generated upon finding
|
| + // a repetition. |sample_rate| is measured in Hz.
|
| + void Detect(const float* data, size_t num_frames, size_t num_channels,
|
| + int sample_rate);
|
| +
|
| + private:
|
| + friend class AudioRepetitionDetectorForTest; // For testing.
|
| +
|
| + // A state is used by the detector to keep track of a consecutive repetition,
|
| + // whether the samples in a repetition are all zeros, and whether a repetition
|
| + // has been reported.
|
| + class State {
|
| + public:
|
| + State(const Pattern& pattern);
|
| +
|
| + bool reported() const { return reported_; }
|
| + void set_reported(bool reported) { reported_ = reported; }
|
| +
|
| + // Increase |count_frames_| by 1, and |zero| indidates whether the added
|
| + // audio frame is zero.
|
| + void Increment(bool zero);
|
| +
|
| + // Check whether their is a valid repetition report. |sample_rate| is
|
| + // measured in Hz.
|
| + bool HasValidReport(int sample_rate) const;
|
| +
|
| + void Reset();
|
| +
|
| + int id() const { return pattern_.id; }
|
| +
|
| + int look_back_ms() const { return pattern_.look_back_ms; }
|
| +
|
| + private:
|
| + // The repetition pattern this state keeps track of.
|
| + const Pattern pattern_;
|
| +
|
| + // counter of frames in a consecutive repetition.
|
| + size_t count_frames_;
|
| +
|
| + // whether a repetition contains only zeros.
|
| + bool all_zero_;
|
| +
|
| + // whether a repetition has been reported. This is to make sure that a
|
| + // repetition that contains multiple cycles will be reported at as early as
|
| + // the first cycle and only once.
|
| + bool reported_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(State);
|
| + };
|
| +
|
| + void RegisterRepetitionPatterns(const Pattern* patterns,
|
| + size_t num_patterns);
|
| +
|
| + // Reset |audio_buffer_| when number of channels or sample rate (Hz) changes.
|
| + void Reset(size_t num_channels, int sample_rate);
|
| +
|
| + // Add frames (interleaved if stereo) to |audio_buffer_|.
|
| + void AddFramesToBuffer(const float* data, size_t num_frames);
|
| +
|
| + // Determine if an audio frame (samples interleaved if stereo) is identical to
|
| + // |audio_buffer_| at a look back position.
|
| + bool Equal(const float* frame, int look_back_samples) const;
|
| +
|
| + // Determine if an audio frame (samples interleaved if stereo) is zero.
|
| + bool IsZero(const float* frame, size_t num_channels) const;
|
| +
|
| + // Action when found repetition.
|
| + virtual void ReportRepetition(int id);
|
| +
|
| + // Used to DCHECK that we are called on the correct thread.
|
| + base::ThreadChecker thread_checker_;
|
| +
|
| + ScopedVector<State> states_;
|
| +
|
| + // The Ids of all registered patterns. This defines the range of values for
|
| + // the UMA report.
|
| + std::vector<int> ids_;
|
| +
|
| + // Ring buffer to store input audio.
|
| + std::vector<float> audio_buffer_;
|
| +
|
| + // Maximum look back time of all registered patterns. This defines the size of
|
| + // |audio_buffer_|
|
| + int max_look_back_ms_;
|
| +
|
| + // Number of audio channels in buffer.
|
| + size_t num_channels_;
|
| +
|
| + // Sample rate in Hz.
|
| + int sample_rate_;
|
| +
|
| + // Number of frames in |audio_buffer|.
|
| + size_t buffer_size_frames_;
|
| +
|
| + // The index of the last frame in |audio_buffer|.
|
| + size_t buffer_end_index_;
|
| +
|
| + // The maximum frames |audio_buffer_| can take in each time.
|
| + size_t max_frames_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector);
|
| +};
|
| +
|
| +} // namespace content
|
| +
|
| +#endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_
|
|
|