blob: 8858cff74cba814609e45ecc0478d4023a9f2751 [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>
ekm030249d2015-06-15 13:02:24 -070016
17namespace webrtc {
18
19namespace intelligibility {
20
Alejandro Luebs32348192016-02-17 20:04:19 -080021// 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.
24class PowerEstimator {
ekm030249d2015-06-15 13:02:24 -070025 public:
Alejandro Luebs32348192016-02-17 20:04:19 -080026 // Construct an instance for the given input array length (|freqs|), with the
27 // appropriate parameters. |decay| is the forgetting factor.
28 PowerEstimator(size_t freqs, float decay);
ekm030249d2015-06-15 13:02:24 -070029
Alejandro Luebs32348192016-02-17 20:04:19 -080030 // Add a new data point to the series.
31 void Step(const std::complex<float>* data);
ekm030249d2015-06-15 13:02:24 -070032
Alejandro Luebs32348192016-02-17 20:04:19 -080033 // The current power array.
34 const float* Power();
ekm030249d2015-06-15 13:02:24 -070035
36 private:
ekmdb4fecf2015-06-22 17:49:08 -070037 // TODO(ekmeyerson): Switch the following running means
kwiberg88788ad2016-02-19 07:04:49 -080038 // and histories from std::unique_ptr to std::vector.
39 std::unique_ptr<std::complex<float>[]> running_mean_sq_;
ekm030249d2015-06-15 13:02:24 -070040
Alejandro Luebs32348192016-02-17 20:04:19 -080041 // The current magnitude array.
kwiberg88788ad2016-02-19 07:04:49 -080042 std::unique_ptr<float[]> magnitude_;
Alejandro Luebs32348192016-02-17 20:04:19 -080043 // The current power array.
kwiberg88788ad2016-02-19 07:04:49 -080044 std::unique_ptr<float[]> power_;
ekm030249d2015-06-15 13:02:24 -070045
Peter Kastingdce40cf2015-08-24 14:52:23 -070046 const size_t num_freqs_;
ekm030249d2015-06-15 13:02:24 -070047 const float decay_;
ekm030249d2015-06-15 13:02:24 -070048};
49
Alejandro Luebs32348192016-02-17 20:04:19 -080050// Helper class for smoothing gain changes. On each application step, the
ekm030249d2015-06-15 13:02:24 -070051// currently used gains are changed towards a set of settable target gains,
52// constrained by a limit on the magnitude of the changes.
53class GainApplier {
54 public:
Peter Kastingdce40cf2015-08-24 14:52:23 -070055 GainApplier(size_t freqs, float change_limit);
ekm030249d2015-06-15 13:02:24 -070056
57 // Copy |in_block| to |out_block|, multiplied by the current set of gains,
58 // and step the current set of gains towards the target set.
59 void Apply(const std::complex<float>* in_block,
60 std::complex<float>* out_block);
61
62 // Return the current target gain set. Modify this array to set the targets.
ekmdb4fecf2015-06-22 17:49:08 -070063 float* target() const { return target_.get(); }
ekm030249d2015-06-15 13:02:24 -070064
65 private:
Peter Kastingdce40cf2015-08-24 14:52:23 -070066 const size_t num_freqs_;
ekm030249d2015-06-15 13:02:24 -070067 const float change_limit_;
kwiberg88788ad2016-02-19 07:04:49 -080068 std::unique_ptr<float[]> target_;
69 std::unique_ptr<float[]> current_;
ekm030249d2015-06-15 13:02:24 -070070};
71
72} // namespace intelligibility
73
74} // namespace webrtc
75
76#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_