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> |
| 15 | |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 16 | #include "webrtc/base/scoped_ptr.h" |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 17 | |
| 18 | namespace webrtc { |
| 19 | |
| 20 | namespace intelligibility { |
| 21 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 22 | // Internal helper for computing the power of a stream of arrays. |
| 23 | // The result is an array of power per position: the i-th power is the power of |
| 24 | // the stream of data on the i-th positions in the input arrays. |
| 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. |
| 32 | void Step(const std::complex<float>* 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. |
| 35 | const float* Power(); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 36 | |
| 37 | private: |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 38 | // TODO(ekmeyerson): Switch the following running means |
| 39 | // and histories from rtc::scoped_ptr to std::vector. |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 40 | rtc::scoped_ptr<std::complex<float>[]> running_mean_sq_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 41 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 42 | // The current magnitude array. |
| 43 | rtc::scoped_ptr<float[]> magnitude_; |
| 44 | // The current power array. |
| 45 | rtc::scoped_ptr<float[]> power_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 46 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 47 | const size_t num_freqs_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 48 | const float decay_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 49 | }; |
| 50 | |
Alejandro Luebs | 3234819 | 2016-02-17 20:04:19 -0800 | [diff] [blame] | 51 | // Helper class for smoothing gain changes. On each application step, the |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 52 | // currently used gains are changed towards a set of settable target gains, |
| 53 | // constrained by a limit on the magnitude of the changes. |
| 54 | class GainApplier { |
| 55 | public: |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 56 | GainApplier(size_t freqs, float change_limit); |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 57 | |
| 58 | // Copy |in_block| to |out_block|, multiplied by the current set of gains, |
| 59 | // and step the current set of gains towards the target set. |
| 60 | void Apply(const std::complex<float>* in_block, |
| 61 | std::complex<float>* out_block); |
| 62 | |
| 63 | // Return the current target gain set. Modify this array to set the targets. |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 64 | float* target() const { return target_.get(); } |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 65 | |
| 66 | private: |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 67 | const size_t num_freqs_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 68 | const float change_limit_; |
ekm | db4fecf | 2015-06-22 17:49:08 -0700 | [diff] [blame] | 69 | rtc::scoped_ptr<float[]> target_; |
| 70 | rtc::scoped_ptr<float[]> current_; |
ekm | 030249d | 2015-06-15 13:02:24 -0700 | [diff] [blame] | 71 | }; |
| 72 | |
| 73 | } // namespace intelligibility |
| 74 | |
| 75 | } // namespace webrtc |
| 76 | |
| 77 | #endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_ |