1d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org/*
2d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *
4d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  Use of this source code is governed by a BSD-style license
5d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  that can be found in the LICENSE file in the root of the source
6d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  tree. An additional intellectual property rights grant can be found
7d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  in the file PATENTS.  All contributing project authors may
8d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org *  be found in the AUTHORS file in the root of the source tree.
9d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org */
10d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
119c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/normal.h"
12d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
1312dc1a38ca54a000e4fecfbc6d41138b895c9ca5pbos@webrtc.org#include <string.h>  // memset, memcpy
1412dc1a38ca54a000e4fecfbc6d41138b895c9ca5pbos@webrtc.org
15d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include <algorithm>  // min
16d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
17d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org#include "webrtc/common_audio/signal_processing/include/signal_processing_library.h"
18e04a93bcf5e1b608c798a6a3148224b8035f0119kwiberg@webrtc.org#include "webrtc/modules/audio_coding/codecs/audio_decoder.h"
193c652b67468d182bd36aee4c31557621be50cc92kjellander@webrtc.org#include "webrtc/modules/audio_coding/codecs/cng/webrtc_cng.h"
209c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/audio_multi_vector.h"
219c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/background_noise.h"
229c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/decoder_database.h"
239c55f0f957534144d2b8a64154f0a479249b34behenrik.lundin@webrtc.org#include "webrtc/modules/audio_coding/neteq/expand.h"
24d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
25d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgnamespace webrtc {
26d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
27d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.orgint Normal::Process(const int16_t* input,
28d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                    size_t length,
29d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                    Modes last_mode,
30d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                    int16_t* external_mute_factor_array,
31fd11bbfb56b42f82e18a744a414325db7a56013fhenrik.lundin@webrtc.org                    AudioMultiVector* output) {
32d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (length == 0) {
33d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Nothing to process.
34d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    output->Clear();
35362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org    return static_cast<int>(length);
36d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
37d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
38d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  assert(output->Empty());
39d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Output should be empty at this point.
40ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  if (length % output->Channels() != 0) {
41ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org    // The length does not match the number of channels.
42ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org    output->Clear();
43ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org    return 0;
44ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org  }
45d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  output->PushBackInterleaved(input, length);
46d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  int16_t* signal = &(*output)[0][0];
47d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
48dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting  const int fs_mult = fs_hz_ / 8000;
49d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  assert(fs_mult > 0);
50d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // fs_shift = log2(fs_mult), rounded down.
51d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Note that |fs_shift| is not "exact" for 48 kHz.
52d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // TODO(hlundin): Investigate this further.
53dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting  const int fs_shift = 30 - WebRtcSpl_NormW32(fs_mult);
54d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
55d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // Check if last RecOut call resulted in an Expand. If so, we have to take
56d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  // care of some cross-fading and unmuting.
57d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  if (last_mode == kModeExpand) {
58d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Generate interpolation data using Expand.
59d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // First, set Expand parameters to appropriate values.
60d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    expand_->SetParametersForNormalAfterExpand();
61d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
62d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Call Expand.
63fd11bbfb56b42f82e18a744a414325db7a56013fhenrik.lundin@webrtc.org    AudioMultiVector expanded(output->Channels());
64d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    expand_->Process(&expanded);
65d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    expand_->Reset();
66d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
67d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    for (size_t channel_ix = 0; channel_ix < output->Channels(); ++channel_ix) {
68d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // Adjust muting factor (main muting factor times expand muting factor).
69d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      external_mute_factor_array[channel_ix] = static_cast<int16_t>(
70600587d5ac96c270612716ab76d813e0c275f98cbjornv@webrtc.org          (external_mute_factor_array[channel_ix] *
71600587d5ac96c270612716ab76d813e0c275f98cbjornv@webrtc.org          expand_->MuteFactor(channel_ix)) >> 14);
72d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
73d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      int16_t* signal = &(*output)[channel_ix][0];
74d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      size_t length_per_channel = length / output->Channels();
75d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // Find largest absolute value in new data.
76dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting      int16_t decoded_max =
77dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting          WebRtcSpl_MaxAbsValueW16(signal, length_per_channel);
78d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // Adjust muting factor if needed (to BGN level).
79dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting      size_t energy_length =
80dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting          std::min(static_cast<size_t>(fs_mult * 64), length_per_channel);
81d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      int scaling = 6 + fs_shift
82d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org          - WebRtcSpl_NormW32(decoded_max * decoded_max);
83d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      scaling = std::max(scaling, 0);  // |scaling| should always be >= 0.
84d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      int32_t energy = WebRtcSpl_DotProductWithScale(signal, signal,
85d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org                                                     energy_length, scaling);
86f045e4da43e671ae511aa1d9b6ef2968256a745dPeter Kasting      int32_t scaled_energy_length =
87f045e4da43e671ae511aa1d9b6ef2968256a745dPeter Kasting          static_cast<int32_t>(energy_length >> scaling);
88f045e4da43e671ae511aa1d9b6ef2968256a745dPeter Kasting      if (scaled_energy_length > 0) {
89f045e4da43e671ae511aa1d9b6ef2968256a745dPeter Kasting        energy = energy / scaled_energy_length;
90ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org      } else {
91ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org        energy = 0;
92ee0fb187a583b0c66b2c9fc8571411dca510ce7bhenrik.lundin@webrtc.org      }
93d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
94d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      int mute_factor;
95d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      if ((energy != 0) &&
96d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org          (energy > background_noise_.Energy(channel_ix))) {
97d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Normalize new frame energy to 15 bits.
98d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        scaling = WebRtcSpl_NormW32(energy) - 16;
99d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // We want background_noise_.energy() / energy in Q14.
100d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        int32_t bgn_energy =
101d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org            background_noise_.Energy(channel_ix) << (scaling+14);
102b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        int16_t energy_scaled = static_cast<int16_t>(energy << scaling);
103b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        int32_t ratio = WebRtcSpl_DivW32W16(bgn_energy, energy_scaled);
104b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        mute_factor = WebRtcSpl_SqrtFloor(ratio << 14);
105d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      } else {
106d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        mute_factor = 16384;  // 1.0 in Q14.
107d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      }
108d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      if (mute_factor > external_mute_factor_array[channel_ix]) {
109b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        external_mute_factor_array[channel_ix] =
110b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting            static_cast<int16_t>(std::min(mute_factor, 16384));
111d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      }
112d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
113d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14).
114dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting      int increment = 64 / fs_mult;
115d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      for (size_t i = 0; i < length_per_channel; i++) {
116d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Scale with mute factor.
117d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        assert(channel_ix < output->Channels());
118d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        assert(i < output->Size());
119d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        int32_t scaled_signal = (*output)[channel_ix][i] *
120d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org            external_mute_factor_array[channel_ix];
121d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Shift 14 with proper rounding.
122b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        (*output)[channel_ix][i] =
123b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting            static_cast<int16_t>((scaled_signal + 8192) >> 14);
124d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Increase mute_factor towards 16384.
125b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min(
126b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting            external_mute_factor_array[channel_ix] + increment, 16384));
127d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      }
128d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
129d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // Interpolate the expanded data into the new vector.
130d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
131d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      assert(fs_shift < 3);  // Will always be 0, 1, or, 2.
132d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      increment = 4 >> fs_shift;
133d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      int fraction = increment;
134dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting      for (size_t i = 0; i < static_cast<size_t>(8 * fs_mult); i++) {
135d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8
136d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // now for legacy bit-exactness.
137d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        assert(channel_ix < output->Channels());
138d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        assert(i < output->Size());
139d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        (*output)[channel_ix][i] =
140b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting            static_cast<int16_t>((fraction * (*output)[channel_ix][i] +
141b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting                (32 - fraction) * expanded[channel_ix][i] + 8) >> 5);
142d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        fraction += increment;
143d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      }
144d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
145d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  } else if (last_mode == kModeRfc3389Cng) {
146d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    assert(output->Channels() == 1);  // Not adapted for multi-channel yet.
147dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting    static const size_t kCngLength = 32;
148d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    int16_t cng_output[kCngLength];
149d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Reset mute factor and start up fresh.
150d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    external_mute_factor_array[0] = 16384;
151d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    AudioDecoder* cng_decoder = decoder_database_->GetActiveCngDecoder();
152d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
153d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    if (cng_decoder) {
154d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // Generate long enough for 32kHz.
1558b2058e73354e7f02cc44bc212fa58a52bb376cbkwiberg@webrtc.org      if (WebRtcCng_Generate(cng_decoder->CngDecoderInstance(), cng_output,
1568b2058e73354e7f02cc44bc212fa58a52bb376cbkwiberg@webrtc.org                             kCngLength, 0) < 0) {
157d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Error returned; set return vector to all zeros.
158d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        memset(cng_output, 0, sizeof(cng_output));
159d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      }
160d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    } else {
161d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // If no CNG instance is defined, just copy from the decoded data.
162d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // (This will result in interpolating the decoded with itself.)
163d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      memcpy(cng_output, signal, fs_mult * 8 * sizeof(int16_t));
164d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
165d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Interpolate the CNG into the new vector.
166d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // (NB/WB/SWB32/SWB48 8/16/32/48 samples.)
167d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    assert(fs_shift < 3);  // Will always be 0, 1, or, 2.
168d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    int16_t increment = 4 >> fs_shift;
169d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    int16_t fraction = increment;
170dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting    for (size_t i = 0; i < static_cast<size_t>(8 * fs_mult); i++) {
171d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // TODO(hlundin): Add 16 instead of 8 for correct rounding. Keeping 8 now
172d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      // for legacy bit-exactness.
173d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      signal[i] =
174d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org          (fraction * signal[i] + (32 - fraction) * cng_output[i] + 8) >> 5;
175d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      fraction += increment;
176d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
177d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  } else if (external_mute_factor_array[0] < 16384) {
178d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // Previous was neither of Expand, FadeToBGN or RFC3389_CNG, but we are
179d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // still ramping up from previous muting.
180d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    // If muted increase by 0.64 for every 20 ms (NB/WB 0.0040/0.0020 in Q14).
181dce40cf804019a9898b6ab8d8262466b697c56e0Peter Kasting    int increment = 64 / fs_mult;
182d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    size_t length_per_channel = length / output->Channels();
183d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    for (size_t i = 0; i < length_per_channel; i++) {
184d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      for (size_t channel_ix = 0; channel_ix < output->Channels();
185d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org          ++channel_ix) {
186d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Scale with mute factor.
187d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        assert(channel_ix < output->Channels());
188d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        assert(i < output->Size());
189d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        int32_t scaled_signal = (*output)[channel_ix][i] *
190d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org            external_mute_factor_array[channel_ix];
191d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Shift 14 with proper rounding.
192b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        (*output)[channel_ix][i] =
193b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting            static_cast<int16_t>((scaled_signal + 8192) >> 14);
194d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org        // Increase mute_factor towards 16384.
195b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting        external_mute_factor_array[channel_ix] = static_cast<int16_t>(std::min(
196b7e5054414ff524f9db81dab7917729b8c4c8bcbPeter Kasting            16384, external_mute_factor_array[channel_ix] + increment));
197d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org      }
198d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org    }
199d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org  }
200d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
201362a55e7b0852a7be95f0d627321503258152551turaj@webrtc.org  return static_cast<int>(length);
202d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}
203d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org
204d94659dc279b86376c1a6470dc326fd342caaa93henrik.lundin@webrtc.org}  // namespace webrtc
205