minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef COMMON_AUDIO_SMOOTHING_FILTER_H_ |
| 12 | #define COMMON_AUDIO_SMOOTHING_FILTER_H_ |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 13 | |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 14 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 15 | #include "rtc_base/constructormagic.h" |
| 16 | #include "system_wrappers/include/clock.h" |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | class SmoothingFilter { |
| 21 | public: |
| 22 | virtual ~SmoothingFilter() = default; |
| 23 | virtual void AddSample(float sample) = 0; |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 24 | virtual absl::optional<float> GetAverage() = 0; |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 25 | virtual bool SetTimeConstantMs(int time_constant_ms) = 0; |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
| 28 | // SmoothingFilterImpl applies an exponential filter |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 29 | // alpha = exp(-1.0 / time_constant_ms); |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 30 | // y[t] = alpha * y[t-1] + (1 - alpha) * sample; |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 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. |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 34 | class SmoothingFilterImpl final : public SmoothingFilter { |
| 35 | public: |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 36 | // |init_time_ms| is initialization time. It defines a period starting from |
| 37 | // the arriving time of the first sample. During this period, the exponential |
| 38 | // filter uses a varying time constant so that a smaller time constant will be |
| 39 | // applied to the earlier samples. This is to allow the the filter to adapt to |
| 40 | // earlier samples quickly. After the initialization period, the time constant |
| 41 | // will be set to |init_time_ms| first and can be changed through |
| 42 | // |SetTimeConstantMs|. |
michaelt | 92aef17 | 2017-04-18 00:11:48 -0700 | [diff] [blame] | 43 | explicit SmoothingFilterImpl(int init_time_ms); |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 44 | ~SmoothingFilterImpl() override; |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 45 | |
| 46 | void AddSample(float sample) override; |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 47 | absl::optional<float> GetAverage() override; |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 48 | bool SetTimeConstantMs(int time_constant_ms) override; |
| 49 | |
| 50 | // Methods used for unittests. |
| 51 | float alpha() const { return alpha_; } |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 52 | |
| 53 | private: |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 54 | void UpdateAlpha(int time_constant_ms); |
| 55 | void ExtrapolateLastSample(int64_t time_ms); |
| 56 | |
| 57 | const int init_time_ms_; |
| 58 | const float init_factor_; |
| 59 | const float init_const_; |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 60 | |
Danil Chapovalov | 196100e | 2018-06-21 10:17:24 +0200 | [diff] [blame] | 61 | absl::optional<int64_t> init_end_time_ms_; |
minyue | 301fc4a | 2016-12-13 06:52:56 -0800 | [diff] [blame] | 62 | float last_sample_; |
| 63 | float alpha_; |
| 64 | float state_; |
| 65 | int64_t last_state_time_ms_; |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 66 | |
minyue | 69b627d | 2016-11-24 11:01:09 -0800 | [diff] [blame] | 67 | RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(SmoothingFilterImpl); |
minyue | 3548357 | 2016-09-20 23:13:08 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | } // namespace webrtc |
| 71 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 72 | #endif // COMMON_AUDIO_SMOOTHING_FILTER_H_ |