blob: 4dc17d50b5b071d469550744e6ccbc534d4c4e56 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_
12#define MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_
ekm030249d2015-06-15 13:02:24 -070013
14#include <complex>
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080015#include <vector>
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.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080024template <typename T>
Alejandro Luebs32348192016-02-17 20:04:19 -080025class PowerEstimator {
ekm030249d2015-06-15 13:02:24 -070026 public:
Alejandro Luebs32348192016-02-17 20:04:19 -080027 // 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);
ekm030249d2015-06-15 13:02:24 -070030
Alejandro Luebs32348192016-02-17 20:04:19 -080031 // Add a new data point to the series.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080032 void Step(const T* data);
ekm030249d2015-06-15 13:02:24 -070033
Alejandro Luebs32348192016-02-17 20:04:19 -080034 // The current power array.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080035 const std::vector<float>& power() { return power_; };
ekm030249d2015-06-15 13:02:24 -070036
37 private:
Alejandro Luebs32348192016-02-17 20:04:19 -080038 // The current power array.
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080039 std::vector<float> power_;
ekm030249d2015-06-15 13:02:24 -070040
ekm030249d2015-06-15 13:02:24 -070041 const float decay_;
ekm030249d2015-06-15 13:02:24 -070042};
43
Alejandro Luebs32348192016-02-17 20:04:19 -080044// Helper class for smoothing gain changes. On each application step, the
ekm030249d2015-06-15 13:02:24 -070045// currently used gains are changed towards a set of settable target gains,
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080046// constrained by a limit on the relative changes.
ekm030249d2015-06-15 13:02:24 -070047class GainApplier {
48 public:
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080049 GainApplier(size_t freqs, float relative_change_limit);
ekm030249d2015-06-15 13:02:24 -070050
Alejandro Luebs37062ed2016-09-06 11:25:40 -070051 ~GainApplier();
52
ekm030249d2015-06-15 13:02:24 -070053 // 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.
aluebs0a007592016-02-26 17:17:38 -080059 float* target() { return target_.data(); }
ekm030249d2015-06-15 13:02:24 -070060
61 private:
Peter Kastingdce40cf2015-08-24 14:52:23 -070062 const size_t num_freqs_;
Alejandro Luebs18fcbcf2016-02-22 15:57:38 -080063 const float relative_change_limit_;
aluebs0a007592016-02-26 17:17:38 -080064 std::vector<float> target_;
65 std::vector<float> current_;
ekm030249d2015-06-15 13:02:24 -070066};
67
Alejandro Luebsef009252016-09-20 14:51:56 -070068// Helper class to delay a signal by an integer number of samples.
69class DelayBuffer {
70 public:
71 DelayBuffer(size_t delay, size_t num_channels);
72
73 ~DelayBuffer();
74
75 void Delay(float* const* data, size_t length);
76
77 private:
78 std::vector<std::vector<float>> buffer_;
79 size_t read_index_;
80};
81
ekm030249d2015-06-15 13:02:24 -070082} // namespace intelligibility
83
84} // namespace webrtc
85
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020086#endif // MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_