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

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

Powered by Google App Engine
This is Rietveld 408576698