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

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

Powered by Google App Engine
This is Rietveld 408576698