blob: 3805a0cd1556c4a5da1dd266a200740516f9f020 [file] [log] [blame]
ekm030249d2015-06-15 13:02:24 -07001/*
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>
kwiberg88788ad2016-02-19 07:04:49 -080015#include <memory>
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080016#include <vector>
ekm030249d2015-06-15 13:02:24 -070017
18namespace webrtc {
19
20namespace intelligibility {
21
Alejandro Luebs32348192016-02-17 20:04:19 -080022// 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.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080025template <typename T>
Alejandro Luebs32348192016-02-17 20:04:19 -080026class PowerEstimator {
ekm030249d2015-06-15 13:02:24 -070027 public:
Alejandro Luebs32348192016-02-17 20:04:19 -080028 // Construct an instance for the given input array length (|freqs|), with the
29 // appropriate parameters. |decay| is the forgetting factor.
30 PowerEstimator(size_t freqs, float decay);
ekm030249d2015-06-15 13:02:24 -070031
Alejandro Luebs32348192016-02-17 20:04:19 -080032 // Add a new data point to the series.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080033 void Step(const T* data);
ekm030249d2015-06-15 13:02:24 -070034
Alejandro Luebs32348192016-02-17 20:04:19 -080035 // The current power array.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080036 const std::vector<float>& power() { return power_; };
ekm030249d2015-06-15 13:02:24 -070037
38 private:
Alejandro Luebs32348192016-02-17 20:04:19 -080039 // The current power array.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080040 std::vector<float> power_;
ekm030249d2015-06-15 13:02:24 -070041
ekm030249d2015-06-15 13:02:24 -070042 const float decay_;
ekm030249d2015-06-15 13:02:24 -070043};
44
Alejandro Luebs32348192016-02-17 20:04:19 -080045// Helper class for smoothing gain changes. On each application step, the
ekm030249d2015-06-15 13:02:24 -070046// currently used gains are changed towards a set of settable target gains,
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080047// constrained by a limit on the relative changes.
ekm030249d2015-06-15 13:02:24 -070048class GainApplier {
49 public:
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080050 GainApplier(size_t freqs, float relative_change_limit);
ekm030249d2015-06-15 13:02:24 -070051
52 // Copy |in_block| to |out_block|, multiplied by the current set of gains,
53 // and step the current set of gains towards the target set.
54 void Apply(const std::complex<float>* in_block,
55 std::complex<float>* out_block);
56
57 // Return the current target gain set. Modify this array to set the targets.
ekmdb4fecf2015-06-22 17:49:08 -070058 float* target() const { return target_.get(); }
ekm030249d2015-06-15 13:02:24 -070059
60 private:
Peter Kastingdce40cf2015-08-24 14:52:23 -070061 const size_t num_freqs_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080062 const float relative_change_limit_;
kwiberg88788ad2016-02-19 07:04:49 -080063 std::unique_ptr<float[]> target_;
64 std::unique_ptr<float[]> current_;
ekm030249d2015-06-15 13:02:24 -070065};
66
67} // namespace intelligibility
68
69} // namespace webrtc
70
71#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_