1 /*
2 * Copyright (c) 2019 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 #include "modules/audio_processing/aec3/alignment_mixer.h"
11
12 #include <algorithm>
13
14 #include "rtc_base/checks.h"
15
16 namespace webrtc {
17 namespace {
18
ChooseMixingVariant(bool downmix,bool adaptive_selection,int num_channels)19 AlignmentMixer::MixingVariant ChooseMixingVariant(bool downmix,
20 bool adaptive_selection,
21 int num_channels) {
22 RTC_DCHECK(!(adaptive_selection && downmix));
23 RTC_DCHECK_LT(0, num_channels);
24
25 if (num_channels == 1) {
26 return AlignmentMixer::MixingVariant::kFixed;
27 }
28 if (downmix) {
29 return AlignmentMixer::MixingVariant::kDownmix;
30 }
31 if (adaptive_selection) {
32 return AlignmentMixer::MixingVariant::kAdaptive;
33 }
34 return AlignmentMixer::MixingVariant::kFixed;
35 }
36
37 } // namespace
38
AlignmentMixer(size_t num_channels,const EchoCanceller3Config::Delay::AlignmentMixing & config)39 AlignmentMixer::AlignmentMixer(
40 size_t num_channels,
41 const EchoCanceller3Config::Delay::AlignmentMixing& config)
42 : AlignmentMixer(num_channels,
43 config.downmix,
44 config.adaptive_selection,
45 config.activity_power_threshold,
46 config.prefer_first_two_channels) {}
47
AlignmentMixer(size_t num_channels,bool downmix,bool adaptive_selection,float activity_power_threshold,bool prefer_first_two_channels)48 AlignmentMixer::AlignmentMixer(size_t num_channels,
49 bool downmix,
50 bool adaptive_selection,
51 float activity_power_threshold,
52 bool prefer_first_two_channels)
53 : num_channels_(num_channels),
54 one_by_num_channels_(1.f / num_channels_),
55 excitation_energy_threshold_(kBlockSize * activity_power_threshold),
56 prefer_first_two_channels_(prefer_first_two_channels),
57 selection_variant_(
58 ChooseMixingVariant(downmix, adaptive_selection, num_channels_)) {
59 if (selection_variant_ == MixingVariant::kAdaptive) {
60 std::fill(strong_block_counters_.begin(), strong_block_counters_.end(), 0);
61 cumulative_energies_.resize(num_channels_);
62 std::fill(cumulative_energies_.begin(), cumulative_energies_.end(), 0.f);
63 }
64 }
65
ProduceOutput(const Block & x,rtc::ArrayView<float,kBlockSize> y)66 void AlignmentMixer::ProduceOutput(const Block& x,
67 rtc::ArrayView<float, kBlockSize> y) {
68 RTC_DCHECK_EQ(x.NumChannels(), num_channels_);
69
70 if (selection_variant_ == MixingVariant::kDownmix) {
71 Downmix(x, y);
72 return;
73 }
74
75 int ch = selection_variant_ == MixingVariant::kFixed ? 0 : SelectChannel(x);
76
77 RTC_DCHECK_GT(x.NumChannels(), ch);
78 std::copy(x.begin(/*band=*/0, ch), x.end(/*band=*/0, ch), y.begin());
79 }
80
Downmix(const Block & x,rtc::ArrayView<float,kBlockSize> y) const81 void AlignmentMixer::Downmix(const Block& x,
82 rtc::ArrayView<float, kBlockSize> y) const {
83 RTC_DCHECK_EQ(x.NumChannels(), num_channels_);
84 RTC_DCHECK_GE(num_channels_, 2);
85 std::memcpy(&y[0], x.View(/*band=*/0, /*channel=*/0).data(),
86 kBlockSize * sizeof(y[0]));
87 for (size_t ch = 1; ch < num_channels_; ++ch) {
88 const auto x_ch = x.View(/*band=*/0, ch);
89 for (size_t i = 0; i < kBlockSize; ++i) {
90 y[i] += x_ch[i];
91 }
92 }
93
94 for (size_t i = 0; i < kBlockSize; ++i) {
95 y[i] *= one_by_num_channels_;
96 }
97 }
98
SelectChannel(const Block & x)99 int AlignmentMixer::SelectChannel(const Block& x) {
100 RTC_DCHECK_EQ(x.NumChannels(), num_channels_);
101 RTC_DCHECK_GE(num_channels_, 2);
102 RTC_DCHECK_EQ(cumulative_energies_.size(), num_channels_);
103
104 constexpr size_t kBlocksToChooseLeftOrRight =
105 static_cast<size_t>(0.5f * kNumBlocksPerSecond);
106 const bool good_signal_in_left_or_right =
107 prefer_first_two_channels_ &&
108 (strong_block_counters_[0] > kBlocksToChooseLeftOrRight ||
109 strong_block_counters_[1] > kBlocksToChooseLeftOrRight);
110
111 const int num_ch_to_analyze =
112 good_signal_in_left_or_right ? 2 : num_channels_;
113
114 constexpr int kNumBlocksBeforeEnergySmoothing = 60 * kNumBlocksPerSecond;
115 ++block_counter_;
116
117 for (int ch = 0; ch < num_ch_to_analyze; ++ch) {
118 float x2_sum = 0.f;
119 rtc::ArrayView<const float, kBlockSize> x_ch = x.View(/*band=*/0, ch);
120 for (size_t i = 0; i < kBlockSize; ++i) {
121 x2_sum += x_ch[i] * x_ch[i];
122 }
123
124 if (ch < 2 && x2_sum > excitation_energy_threshold_) {
125 ++strong_block_counters_[ch];
126 }
127
128 if (block_counter_ <= kNumBlocksBeforeEnergySmoothing) {
129 cumulative_energies_[ch] += x2_sum;
130 } else {
131 constexpr float kSmoothing = 1.f / (10 * kNumBlocksPerSecond);
132 cumulative_energies_[ch] +=
133 kSmoothing * (x2_sum - cumulative_energies_[ch]);
134 }
135 }
136
137 // Normalize the energies to allow the energy computations to from now be
138 // based on smoothing.
139 if (block_counter_ == kNumBlocksBeforeEnergySmoothing) {
140 constexpr float kOneByNumBlocksBeforeEnergySmoothing =
141 1.f / kNumBlocksBeforeEnergySmoothing;
142 for (int ch = 0; ch < num_ch_to_analyze; ++ch) {
143 cumulative_energies_[ch] *= kOneByNumBlocksBeforeEnergySmoothing;
144 }
145 }
146
147 int strongest_ch = 0;
148 for (int ch = 0; ch < num_ch_to_analyze; ++ch) {
149 if (cumulative_energies_[ch] > cumulative_energies_[strongest_ch]) {
150 strongest_ch = ch;
151 }
152 }
153
154 if ((good_signal_in_left_or_right && selected_channel_ > 1) ||
155 cumulative_energies_[strongest_ch] >
156 2.f * cumulative_energies_[selected_channel_]) {
157 selected_channel_ = strongest_ch;
158 }
159
160 return selected_channel_;
161 }
162
163 } // namespace webrtc
164