blob: 2bf0791d8544e7e0bf6786f10c9bf64186d0fdbb [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>
15
ekmdb4fecf2015-06-22 17:49:08 -070016#include "webrtc/base/scoped_ptr.h"
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.
25class 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.
32 void Step(const std::complex<float>* data);
ekm030249d2015-06-15 13:02:24 -070033
Alejandro Luebs32348192016-02-17 20:04:19 -080034 // The current power array.
35 const float* Power();
ekm030249d2015-06-15 13:02:24 -070036
37 private:
ekmdb4fecf2015-06-22 17:49:08 -070038 // TODO(ekmeyerson): Switch the following running means
39 // and histories from rtc::scoped_ptr to std::vector.
ekmdb4fecf2015-06-22 17:49:08 -070040 rtc::scoped_ptr<std::complex<float>[]> running_mean_sq_;
ekm030249d2015-06-15 13:02:24 -070041
Alejandro Luebs32348192016-02-17 20:04:19 -080042 // The current magnitude array.
43 rtc::scoped_ptr<float[]> magnitude_;
44 // The current power array.
45 rtc::scoped_ptr<float[]> power_;
ekm030249d2015-06-15 13:02:24 -070046
Peter Kastingdce40cf2015-08-24 14:52:23 -070047 const size_t num_freqs_;
ekm030249d2015-06-15 13:02:24 -070048 const float decay_;
ekm030249d2015-06-15 13:02:24 -070049};
50
Alejandro Luebs32348192016-02-17 20:04:19 -080051// Helper class for smoothing gain changes. On each application step, the
ekm030249d2015-06-15 13:02:24 -070052// currently used gains are changed towards a set of settable target gains,
53// constrained by a limit on the magnitude of the changes.
54class GainApplier {
55 public:
Peter Kastingdce40cf2015-08-24 14:52:23 -070056 GainApplier(size_t freqs, float change_limit);
ekm030249d2015-06-15 13:02:24 -070057
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.
ekmdb4fecf2015-06-22 17:49:08 -070064 float* target() const { return target_.get(); }
ekm030249d2015-06-15 13:02:24 -070065
66 private:
Peter Kastingdce40cf2015-08-24 14:52:23 -070067 const size_t num_freqs_;
ekm030249d2015-06-15 13:02:24 -070068 const float change_limit_;
ekmdb4fecf2015-06-22 17:49:08 -070069 rtc::scoped_ptr<float[]> target_;
70 rtc::scoped_ptr<float[]> current_;
ekm030249d2015-06-15 13:02:24 -070071};
72
73} // namespace intelligibility
74
75} // namespace webrtc
76
77#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INTELLIGIBILITY_INTELLIGIBILITY_UTILS_H_