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

Side by Side Diff: content/renderer/media/audio_repetition_detector.h

Issue 1357013006: Add detection for repeated audio in capturing. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: decoupling uma reporting from detection Created 5 years, 2 months 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 #ifndef CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_
6 #define CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_
7
8 #include <vector>
9
10 #include "base/memory/scoped_vector.h"
11 #include "base/threading/thread_checker.h"
12 #include "content/common/content_export.h"
13
14 namespace content {
15
16 // AudioRepetitionDetector detects bit-exact audio repetitions of registered
17 // patterns. A repetition pattern is defined by a look back time. The detector
18 // buffers the audio signal and checks equality of each input sample against the
19 // samples at the look back positions of all registered patterns, and counts the
20 // duration of any consecutive equality.
21 // All methods should be called from the same thread. However, we allow the
22 // construction and destruction be made from a separate thread.
23
24 class CONTENT_EXPORT AudioRepetitionDetector {
25 public:
26 // |min_length_ms| is the minimum duration (in milliseconds) of repetitions
27 // that count.
28 // |max_frames| is the maximum number of audio frames that will be provided to
29 // |Detect()| each time. Input longer than |max_frames| won't cause any
30 // problem, and will only affect computational efficiency.
31 // |look_back_times| is a list of look back times (in millisecond) for the
32 // detector to keep track. |num_look_back| is the size of the list.
33 AudioRepetitionDetector(int min_length_ms, size_t max_frames,
34 const int* look_back_times, size_t num_look_back);
35
36 virtual ~AudioRepetitionDetector();
37
38 // Detect repetition in |data|. |sample_rate| is measured in Hz.
39 void Detect(const float* data, size_t num_frames, size_t num_channels,
40 int sample_rate);
41
42 private:
43 friend class AudioRepetitionDetectorForTest;
44
45 // A state is used by the detector to keep track of a consecutive repetition,
46 // whether the samples in a repetition are all zeros, and whether a repetition
47 // has been reported.
48 class State {
49 public:
50 explicit State(int look_back_ms);
51
52 int look_back_ms() const { return look_back_ms_; };
53 size_t count_frames() const { return count_frames_; }
54 bool all_zero() const { return all_zero_; }
55 bool reported() const { return reported_; }
56 void set_reported(bool reported) { reported_ = reported; }
57
58 // Increase |count_frames_| by 1, and |zero| indidates whether the added
59 // audio frame is zero.
60 void Increment(bool zero);
61
62 void Reset();
63
64 private:
65 // Look back time of the repetition pattern this state keeps track of.
66 const int look_back_ms_;
67
68 // Counter of frames in a consecutive repetition.
69 size_t count_frames_;
70
71 // Whether a repetition contains only zeros.
72 bool all_zero_;
73
74 // |reported_| tells whether a repetition has been reported. This is to make
75 // sure that a repetition with a long duration will be reported as early as
76 // being detected but no more than one time.
77 bool reported_;
78
79 DISALLOW_COPY_AND_ASSIGN(State);
80 };
81
82 void RegisterLookbackTime(int look_back_ms);
83
84 // Reset |audio_buffer_| when number of channels or sample rate (Hz) changes.
85 void Reset(size_t num_channels, int sample_rate);
86
87 // Add frames (interleaved if stereo) to |audio_buffer_|.
88 void AddFramesToBuffer(const float* data, size_t num_frames);
89
90 // Determine if an audio frame (samples interleaved if stereo) is identical to
91 // |audio_buffer_| at a look back position.
92 bool Equal(const float* frame, int look_back_samples) const;
93
94 // Determine if an audio frame (samples interleaved if stereo) is zero.
95 bool IsZero(const float* frame, size_t num_channels) const;
96
97 // Check whether the state contains a valid repetition report.
98 bool HasValidReport(const State* state) const;
99
100 // Action when a repetition is found. |look_back_ms| provides the look back
101 // time of the detected repetition.
102 virtual void ReportRepetition(int look_back_ms) = 0;
103
104 // Used to DCHECK that we are called on the correct thread. Ctor/dtor
105 // should be called on one thread. The rest can be called on another.
106 base::ThreadChecker main_thread_checker_;
107 base::ThreadChecker processing_thread_checker_;
108
109 ScopedVector<State> states_;
110
111 // Ring buffer to store input audio.
112 std::vector<float> audio_buffer_;
113
114 // Maximum look back time of all registered repetitions. This defines the size
115 // of |audio_buffer_|
116 int max_look_back_ms_;
117
118 // The shortest length for repetitions.
119 const int min_length_ms_;
120
121 // Number of audio channels in buffer.
122 size_t num_channels_;
123
124 // Sample rate in Hz.
125 int sample_rate_;
126
127 // Number of frames in |audio_buffer|.
128 size_t buffer_size_frames_;
129
130 // The index of the last frame in |audio_buffer|.
131 size_t buffer_end_index_;
132
133 // The maximum frames |audio_buffer_| can take in each time.
134 const size_t max_frames_;
135
136 DISALLOW_COPY_AND_ASSIGN(AudioRepetitionDetector);
137 };
138
139 } // namespace content
140
141 #endif // CONTENT_RENDERER_MEDIA_AUDIO_REPETITION_DETECTOR_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698