ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014 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 | |
| 11 | #ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |
| 12 | #define WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |
| 13 | |
| 14 | #include <complex> |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 15 | #include <vector> |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 16 | |
| 17 | namespace webrtc { |
| 18 | |
| 19 | namespace intelligibility { |
| 20 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 21 | // Internal helper for computing the power of a stream of arrays. |
| 22 | // The result is an array of power per position: the i-th power is the power of |
| 23 | // the stream of data on the i-th positions in the input arrays. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 24 | template <typename T> |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 25 | class PowerEstimator { |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 26 | public: |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 27 | // Construct an instance for the given input array length (|freqs|), with the |
| 28 | // appropriate parameters. |decay| is the forgetting factor. |
| 29 | PowerEstimator(size_t freqs, float decay); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 30 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 31 | // Add a new data point to the series. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 32 | void Step(const T* data); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 33 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 34 | // The current power array. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 35 | const std::vector<float>& power() { return power_; }; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 36 | |
| 37 | private: |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 38 | // The current power array. |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 39 | std::vector<float> power_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 40 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 41 | const float decay_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 42 | }; |
| 43 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 44 | // Helper class for smoothing gain changes. On each application step, the |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 45 | // currently used gains are changed towards a set of settable target gains, |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 46 | // constrained by a limit on the relative changes. |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 47 | class GainApplier { |
| 48 | public: |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 49 | GainApplier(size_t freqs, float relative_change_limit); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 50 | |
Alejandro Luebs | 37062ed | 2016-09-06 11:25:40 -0700 | [diff] [blame^] | 51 | ~GainApplier(); |
| 52 | |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 53 | // Copy |in_block| to |out_block|, multiplied by the current set of gains, |
| 54 | // and step the current set of gains towards the target set. |
| 55 | void Apply(const std::complex<float>* in_block, |
| 56 | std::complex<float>* out_block); |
| 57 | |
| 58 | // Return the current target gain set. Modify this array to set the targets. |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 59 | float* target() { return target_.data(); } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 60 | |
| 61 | private: |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 62 | const size_t num_freqs_; |
Alejandro Luebs | 18fcbcf | 2016-02-22 15:57:38 -0800 | [diff] [blame] | 63 | const float relative_change_limit_; |
aluebs | 0a00759 | 2016-02-26 17:17:38 -0800 | [diff] [blame] | 64 | std::vector<float> target_; |
| 65 | std::vector<float> current_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
| 68 | } // namespace intelligibility |
| 69 | |
| 70 | } // namespace webrtc |
| 71 | |
| 72 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |