OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #ifndef WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ | 11 #ifndef WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ |
12 #define WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ | 12 #define WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ |
13 | 13 |
14 #include "webrtc/base/analytics/exp_filter.h" | |
15 #include "webrtc/base/constructormagic.h" | 14 #include "webrtc/base/constructormagic.h" |
16 #include "webrtc/base/optional.h" | 15 #include "webrtc/base/optional.h" |
17 #include "webrtc/system_wrappers/include/clock.h" | 16 #include "webrtc/system_wrappers/include/clock.h" |
18 | 17 |
19 namespace webrtc { | 18 namespace webrtc { |
20 | 19 |
21 class SmoothingFilter { | 20 class SmoothingFilter { |
22 public: | 21 public: |
23 virtual ~SmoothingFilter() = default; | 22 virtual ~SmoothingFilter() = default; |
24 virtual void AddSample(float sample) = 0; | 23 virtual void AddSample(float sample) = 0; |
25 virtual rtc::Optional<float> GetAverage() const = 0; | 24 virtual rtc::Optional<float> GetAverage() const = 0; |
26 virtual void SetTimeConstantMs(int time_constant_ms) = 0; | 25 virtual void SetTimeConstantMs(int time_constant_ms) = 0; |
27 }; | 26 }; |
28 | 27 |
29 // SmoothingFilterImpl applies an exponential filter | 28 // SmoothingFilterImpl applies an exponential filter |
30 // alpha = exp(-sample_interval / time_constant); | 29 // alpha = exp(-1.0 / time_constant_ms); |
31 // y[t] = alpha * y[t-1] + (1 - alpha) * sample; | 30 // y[t] = alpha * y[t-1] + (1 - alpha) * sample; |
| 31 // This implies a sample rate of 1000 Hz, i.e., 1 sample / ms. |
| 32 // But SmoothingFilterImpl allows sparse samples. All missing samples will be |
| 33 // assumed to equal the last received sample. |
32 class SmoothingFilterImpl final : public SmoothingFilter { | 34 class SmoothingFilterImpl final : public SmoothingFilter { |
33 public: | 35 public: |
34 // |time_constant_ms| is the time constant for the exponential filter. | 36 // |init_time_constant_ms| is initialization time. It defines a period |
35 SmoothingFilterImpl(int time_constant_ms, const Clock* clock); | 37 // starting from the arriving time of the first sample. During this period, |
| 38 // the exponential filter uses a varying time constant so that a smaller |
| 39 // time constant will be applied to the earlier samples. This is to allow the |
| 40 // the filter to adapt to earlier samples quickly. After the initialization |
| 41 // period, the time constant will be set to |init_time_constant_ms| first |
| 42 // and can be changed through |SetTimeConstantMs|. |
| 43 SmoothingFilterImpl(int init_time_constant_ms, const Clock* clock); |
| 44 ~SmoothingFilterImpl() override; |
36 | 45 |
37 void AddSample(float sample) override; | 46 void AddSample(float sample) override; |
38 rtc::Optional<float> GetAverage() const override; | 47 rtc::Optional<float> GetAverage() const override; |
39 void SetTimeConstantMs(int time_constant_ms) override; | 48 void SetTimeConstantMs(int time_constant_ms) override; |
40 | 49 |
| 50 // Methods used for unittests. |
| 51 float alpha() const { return alpha_; } |
| 52 |
41 private: | 53 private: |
42 int time_constant_ms_; | 54 void UpdateAlpha(int time_constant_ms); |
| 55 float ExtrapolateLastSample(int64_t time_ms) const; |
| 56 |
| 57 const int init_time_constant_ms_; |
43 const Clock* const clock_; | 58 const Clock* const clock_; |
44 | 59 |
45 bool first_sample_received_; | |
46 bool initialized_; | 60 bool initialized_; |
47 int64_t first_sample_time_ms_; | 61 rtc::Optional<int64_t> first_sample_time_ms_; |
48 int64_t last_sample_time_ms_; | 62 int64_t last_sample_time_ms_; |
49 rtc::ExpFilter filter_; | 63 float last_sample_; |
| 64 float alpha_; |
| 65 float state_; |
50 | 66 |
51 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SmoothingFilterImpl); | 67 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SmoothingFilterImpl); |
52 }; | 68 }; |
53 | 69 |
54 } // namespace webrtc | 70 } // namespace webrtc |
55 | 71 |
56 #endif // WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ | 72 #endif // WEBRTC_COMMON_AUDIO_SMOOTHING_FILTER_H_ |
OLD | NEW |