blob: 68e60dc66ae5176b29acd0477d1cd40544e3eee6 [file] [log] [blame]
pbos@webrtc.org788acd12014-12-15 09:41:24 +00001/*
2 * Copyright (c) 2012 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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "modules/audio_processing/vad/pitch_based_vad.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000012
pbos@webrtc.org788acd12014-12-15 09:41:24 +000013#include <string.h>
14
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "modules/audio_processing/vad/common.h"
16#include "modules/audio_processing/vad/noise_gmm_tables.h"
Yves Gerey665174f2018-06-19 15:03:05 +020017#include "modules/audio_processing/vad/vad_circular_buffer.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/audio_processing/vad/voice_gmm_tables.h"
pbos@webrtc.org788acd12014-12-15 09:41:24 +000019
20namespace webrtc {
21
kwiberg@webrtc.org2ebfac52015-01-14 10:51:54 +000022static_assert(kNoiseGmmDim == kVoiceGmmDim,
23 "noise and voice gmm dimension not equal");
pbos@webrtc.org788acd12014-12-15 09:41:24 +000024
25// These values should match MATLAB counterparts for unit-tests to pass.
26static const int kPosteriorHistorySize = 500; // 5 sec of 10 ms frames.
27static const double kInitialPriorProbability = 0.3;
28static const int kTransientWidthThreshold = 7;
29static const double kLowProbabilityThreshold = 0.2;
30
31static double LimitProbability(double p) {
32 const double kLimHigh = 0.99;
33 const double kLimLow = 0.01;
34
35 if (p > kLimHigh)
36 p = kLimHigh;
37 else if (p < kLimLow)
38 p = kLimLow;
39 return p;
40}
41
42PitchBasedVad::PitchBasedVad()
43 : p_prior_(kInitialPriorProbability),
aluebsecf6b812015-06-25 12:28:48 -070044 circular_buffer_(VadCircularBuffer::Create(kPosteriorHistorySize)) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000045 // Setup noise GMM.
46 noise_gmm_.dimension = kNoiseGmmDim;
47 noise_gmm_.num_mixtures = kNoiseGmmNumMixtures;
48 noise_gmm_.weight = kNoiseGmmWeights;
49 noise_gmm_.mean = &kNoiseGmmMean[0][0];
50 noise_gmm_.covar_inverse = &kNoiseGmmCovarInverse[0][0][0];
51
52 // Setup voice GMM.
53 voice_gmm_.dimension = kVoiceGmmDim;
54 voice_gmm_.num_mixtures = kVoiceGmmNumMixtures;
55 voice_gmm_.weight = kVoiceGmmWeights;
56 voice_gmm_.mean = &kVoiceGmmMean[0][0];
57 voice_gmm_.covar_inverse = &kVoiceGmmCovarInverse[0][0][0];
58}
59
Yves Gerey665174f2018-06-19 15:03:05 +020060PitchBasedVad::~PitchBasedVad() {}
pbos@webrtc.org788acd12014-12-15 09:41:24 +000061
62int PitchBasedVad::VoicingProbability(const AudioFeatures& features,
63 double* p_combined) {
64 double p;
65 double gmm_features[3];
66 double pdf_features_given_voice;
67 double pdf_features_given_noise;
68 // These limits are the same in matlab implementation 'VoicingProbGMM().'
69 const double kLimLowLogPitchGain = -2.0;
70 const double kLimHighLogPitchGain = -0.9;
71 const double kLimLowSpectralPeak = 200;
72 const double kLimHighSpectralPeak = 2000;
73 const double kEps = 1e-12;
Peter Kastingdce40cf2015-08-24 14:52:23 -070074 for (size_t n = 0; n < features.num_frames; n++) {
pbos@webrtc.org788acd12014-12-15 09:41:24 +000075 gmm_features[0] = features.log_pitch_gain[n];
76 gmm_features[1] = features.spectral_peak[n];
77 gmm_features[2] = features.pitch_lag_hz[n];
78
79 pdf_features_given_voice = EvaluateGmm(gmm_features, voice_gmm_);
80 pdf_features_given_noise = EvaluateGmm(gmm_features, noise_gmm_);
81
82 if (features.spectral_peak[n] < kLimLowSpectralPeak ||
83 features.spectral_peak[n] > kLimHighSpectralPeak ||
84 features.log_pitch_gain[n] < kLimLowLogPitchGain) {
85 pdf_features_given_voice = kEps * pdf_features_given_noise;
86 } else if (features.log_pitch_gain[n] > kLimHighLogPitchGain) {
87 pdf_features_given_noise = kEps * pdf_features_given_voice;
88 }
89
aluebsecf6b812015-06-25 12:28:48 -070090 p = p_prior_ * pdf_features_given_voice /
91 (pdf_features_given_voice * p_prior_ +
92 pdf_features_given_noise * (1 - p_prior_));
pbos@webrtc.org788acd12014-12-15 09:41:24 +000093
94 p = LimitProbability(p);
95
96 // Combine pitch-based probability with standalone probability, before
97 // updating prior probabilities.
98 double prod_active = p * p_combined[n];
99 double prod_inactive = (1 - p) * (1 - p_combined[n]);
100 p_combined[n] = prod_active / (prod_active + prod_inactive);
101
102 if (UpdatePrior(p_combined[n]) < 0)
103 return -1;
104 // Limit prior probability. With a zero prior probability the posterior
105 // probability is always zero.
106 p_prior_ = LimitProbability(p_prior_);
107 }
108 return 0;
109}
110
111int PitchBasedVad::UpdatePrior(double p) {
112 circular_buffer_->Insert(p);
113 if (circular_buffer_->RemoveTransient(kTransientWidthThreshold,
114 kLowProbabilityThreshold) < 0)
115 return -1;
116 p_prior_ = circular_buffer_->Mean();
117 return 0;
118}
119
120} // namespace webrtc