summaryrefslogtreecommitdiffstats
path: root/third_party/libwebrtc/modules/audio_processing/transient/voice_probability_delay_unit.cc
blob: 27b2b42b38ae93096916a34b4d3e7f443dfeac6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
 *  Copyright (c) 2022 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/audio_processing/transient/voice_probability_delay_unit.h"

#include <array>

#include "rtc_base/checks.h"

namespace webrtc {

VoiceProbabilityDelayUnit::VoiceProbabilityDelayUnit(int delay_num_samples,
                                                     int sample_rate_hz) {
  Initialize(delay_num_samples, sample_rate_hz);
}

void VoiceProbabilityDelayUnit::Initialize(int delay_num_samples,
                                           int sample_rate_hz) {
  RTC_DCHECK_GE(delay_num_samples, 0);
  RTC_DCHECK_LE(delay_num_samples, sample_rate_hz / 50)
      << "The implementation does not support delays greater than 20 ms.";
  int frame_size = rtc::CheckedDivExact(sample_rate_hz, 100);  // 10 ms.
  if (delay_num_samples <= frame_size) {
    weights_[0] = 0.0f;
    weights_[1] = static_cast<float>(delay_num_samples) / frame_size;
    weights_[2] =
        static_cast<float>(frame_size - delay_num_samples) / frame_size;
  } else {
    delay_num_samples -= frame_size;
    weights_[0] = static_cast<float>(delay_num_samples) / frame_size;
    weights_[1] =
        static_cast<float>(frame_size - delay_num_samples) / frame_size;
    weights_[2] = 0.0f;
  }

  // Resets the delay unit.
  last_probabilities_.fill(0.0f);
}

float VoiceProbabilityDelayUnit::Delay(float voice_probability) {
  float weighted_probability = weights_[0] * last_probabilities_[0] +
                               weights_[1] * last_probabilities_[1] +
                               weights_[2] * voice_probability;
  last_probabilities_[0] = last_probabilities_[1];
  last_probabilities_[1] = voice_probability;
  return weighted_probability;
}

}  // namespace webrtc