1 /*
2  *  Copyright (c) 2017 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 MODULES_AUDIO_PROCESSING_AEC3_ECHO_REMOVER_METRICS_H_
12 #define MODULES_AUDIO_PROCESSING_AEC3_ECHO_REMOVER_METRICS_H_
13 
14 #include <array>
15 
16 #include "modules/audio_processing/aec3/aec3_common.h"
17 #include "modules/audio_processing/aec3/aec_state.h"
18 #include "rtc_base/constructor_magic.h"
19 
20 namespace webrtc {
21 
22 // Handles the reporting of metrics for the echo remover.
23 class EchoRemoverMetrics {
24  public:
25   struct DbMetric {
26     DbMetric();
27     DbMetric(float sum_value, float floor_value, float ceil_value);
28     void Update(float value);
29     void UpdateInstant(float value);
30     float sum_value;
31     float floor_value;
32     float ceil_value;
33   };
34 
35   EchoRemoverMetrics();
36 
37   // Updates the metric with new data.
38   void Update(
39       const AecState& aec_state,
40       const std::array<float, kFftLengthBy2Plus1>& comfort_noise_spectrum,
41       const std::array<float, kFftLengthBy2Plus1>& suppressor_gain);
42 
43   // Returns true if the metrics have just been reported, otherwise false.
MetricsReported()44   bool MetricsReported() { return metrics_reported_; }
45 
46  private:
47   // Resets the metrics.
48   void ResetMetrics();
49 
50   int block_counter_ = 0;
51   std::array<DbMetric, 2> erl_;
52   DbMetric erl_time_domain_;
53   std::array<DbMetric, 2> erle_;
54   DbMetric erle_time_domain_;
55   int active_render_count_ = 0;
56   bool saturated_capture_ = false;
57   bool metrics_reported_ = false;
58 
59   RTC_DISALLOW_COPY_AND_ASSIGN(EchoRemoverMetrics);
60 };
61 
62 namespace aec3 {
63 
64 // Updates a banded metric of type DbMetric with the values in the supplied
65 // array.
66 void UpdateDbMetric(const std::array<float, kFftLengthBy2Plus1>& value,
67                     std::array<EchoRemoverMetrics::DbMetric, 2>* statistic);
68 
69 // Transforms a DbMetric from the linear domain into the logarithmic domain.
70 int TransformDbMetricForReporting(bool negate,
71                                   float min_value,
72                                   float max_value,
73                                   float offset,
74                                   float scaling,
75                                   float value);
76 
77 }  // namespace aec3
78 
79 }  // namespace webrtc
80 
81 #endif  // MODULES_AUDIO_PROCESSING_AEC3_ECHO_REMOVER_METRICS_H_
82