OLD | NEW |
| (Empty) |
1 // Copyright (c) 2013 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 "media/audio/audio_silence_detector.h" | |
6 | |
7 #include "base/float_util.h" | |
8 #include "base/time/time.h" | |
9 #include "media/base/audio_bus.h" | |
10 | |
11 using base::AtomicRefCountDec; | |
12 using base::AtomicRefCountInc; | |
13 using base::AtomicRefCountIsOne; | |
14 using base::AtomicRefCountIsZero; | |
15 | |
16 namespace media { | |
17 | |
18 AudioSilenceDetector::AudioSilenceDetector( | |
19 int sample_rate, | |
20 const base::TimeDelta& questionable_silence_period, | |
21 float indistinguishable_silence_threshold) | |
22 : polling_period_(questionable_silence_period), | |
23 frames_before_observing_silence_( | |
24 sample_rate * questionable_silence_period.InSecondsF()), | |
25 silence_threshold_(indistinguishable_silence_threshold), | |
26 frames_silent_so_far_(frames_before_observing_silence_), | |
27 observing_silence_(1), | |
28 was_audible_(false) { | |
29 } | |
30 | |
31 AudioSilenceDetector::~AudioSilenceDetector() { | |
32 DCHECK(thread_checker_.CalledOnValidThread()); | |
33 // Note: If active, ~RepeatingTimer() will StopAndAbandon(). | |
34 } | |
35 | |
36 void AudioSilenceDetector::Start(const AudibleCallback& callback) { | |
37 DCHECK(thread_checker_.CalledOnValidThread()); | |
38 DCHECK(notify_is_audible_.is_null()); | |
39 DCHECK(!callback.is_null()); | |
40 | |
41 notify_is_audible_ = callback; | |
42 was_audible_ = AtomicRefCountIsZero(&observing_silence_); | |
43 notify_is_audible_.Run(was_audible_); | |
44 poll_timer_.Start( | |
45 FROM_HERE, polling_period_, | |
46 this, &AudioSilenceDetector::MaybeInvokeAudibleCallback); | |
47 } | |
48 | |
49 void AudioSilenceDetector::Stop(bool notify_ending_in_silence) { | |
50 DCHECK(thread_checker_.CalledOnValidThread()); | |
51 DCHECK(!notify_is_audible_.is_null()); | |
52 | |
53 poll_timer_.Stop(); | |
54 if (notify_ending_in_silence) | |
55 notify_is_audible_.Run(false); | |
56 notify_is_audible_.Reset(); | |
57 } | |
58 | |
59 void AudioSilenceDetector::Scan(const AudioBus* buffer, int frames) { | |
60 // Determine whether the frames just read are probably silence. If enough | |
61 // frames of silence have been observed, flip the |observing_silence_| | |
62 // boolean, which will be read by another thread. | |
63 if (ProbablyContainsSilence(buffer, frames)) { | |
64 // Note: Prevent indefinite incrementing of |frames_silent_so_far_|, to | |
65 // avoid eventual integer overflow. | |
66 if (frames_silent_so_far_ < frames_before_observing_silence_) { | |
67 frames_silent_so_far_ += frames; | |
68 if (frames_silent_so_far_ >= frames_before_observing_silence_) { | |
69 DCHECK(AtomicRefCountIsZero(&observing_silence_)); | |
70 AtomicRefCountInc(&observing_silence_); | |
71 } | |
72 } | |
73 } else { | |
74 if (frames_silent_so_far_ >= frames_before_observing_silence_) { | |
75 DCHECK(AtomicRefCountIsOne(&observing_silence_)); | |
76 AtomicRefCountDec(&observing_silence_); | |
77 } | |
78 frames_silent_so_far_ = 0; | |
79 } | |
80 } | |
81 | |
82 void AudioSilenceDetector::MaybeInvokeAudibleCallback() { | |
83 DCHECK(thread_checker_.CalledOnValidThread()); | |
84 | |
85 const bool is_now_audible = AtomicRefCountIsZero(&observing_silence_); | |
86 if (was_audible_ && !is_now_audible) | |
87 notify_is_audible_.Run(was_audible_ = false); | |
88 else if (!was_audible_ && is_now_audible) | |
89 notify_is_audible_.Run(was_audible_ = true); | |
90 } | |
91 | |
92 bool AudioSilenceDetector::ProbablyContainsSilence(const AudioBus* buffer, | |
93 int num_frames) { | |
94 if (!buffer) | |
95 return true; | |
96 DCHECK_LE(num_frames, buffer->frames()); | |
97 if (buffer->frames() <= 0) | |
98 return true; | |
99 | |
100 // Scan the data in each channel. If any one channel contains sound whose | |
101 // range of values exceeds |silence_threshold_|, return false immediately. | |
102 for (int i = 0; i < buffer->channels(); ++i) { | |
103 // Examine the 1st, 2nd (+1), 4th (+2), 7th (+3), 11th (+4), etc. samples, | |
104 // checking whether |silence_threshold_| has been breached each time. For | |
105 // typical AudioBus sizes, this algorithm will in the worst case examine | |
106 // fewer than 10% of the samples. | |
107 // | |
108 // Note that there *is* a heavy bias in sampling at the beginning of the | |
109 // channels, but that doesn't matter. The buffer sizes are simply too | |
110 // small. For example, it is commonplace to use 128-sample buffers, which | |
111 // represents ~3 ms of audio at 44.1 kHz; and this means that frequencies | |
112 // below ~350 Hz will span more than one buffer to make a full cycle. In | |
113 // all, the algorithm here is meant to be dirt-simple math that isn't | |
114 // susceptible to periodic bias within a single buffer. | |
115 const float* p = buffer->channel(i); | |
116 const float* const end_of_samples = p + num_frames; | |
117 int skip = 1; | |
118 float min_value = *p; | |
119 float max_value = *p; | |
120 for (p += skip; p < end_of_samples; ++skip, p += skip) { | |
121 DCHECK(base::IsFinite(*p)); | |
122 if (*p < min_value) | |
123 min_value = *p; | |
124 else if (max_value < *p) | |
125 max_value = *p; | |
126 if ((max_value - min_value) > silence_threshold_) | |
127 return false; | |
128 } | |
129 } | |
130 | |
131 return true; | |
132 } | |
133 | |
134 } // namespace media | |
OLD | NEW |