pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
| 11 | #include "webrtc/modules/audio_processing/agc/agc.h" |
| 12 | |
| 13 | #include <cmath> |
| 14 | #include <cstdlib> |
| 15 | |
| 16 | #include <algorithm> |
| 17 | |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 18 | #include "webrtc/common_audio/resampler/include/resampler.h" |
| 19 | #include "webrtc/modules/audio_processing/agc/agc_audio_proc.h" |
| 20 | #include "webrtc/modules/audio_processing/agc/common.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/agc/histogram.h" |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 22 | #include "webrtc/modules/audio_processing/agc/pitch_based_vad.h" |
| 23 | #include "webrtc/modules/audio_processing/agc/standalone_vad.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 24 | #include "webrtc/modules/audio_processing/agc/utility.h" |
| 25 | #include "webrtc/modules/interface/module_common_types.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 26 | |
| 27 | namespace webrtc { |
| 28 | namespace { |
| 29 | |
| 30 | const int kDefaultLevelDbfs = -18; |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 31 | const double kDefaultVoiceValue = 1.0; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 32 | const int kNumAnalysisFrames = 100; |
| 33 | const double kActivityThreshold = 0.3; |
| 34 | |
| 35 | } // namespace |
| 36 | |
| 37 | Agc::Agc() |
| 38 | : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)), |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 39 | last_voice_probability_(kDefaultVoiceValue), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 40 | target_level_dbfs_(kDefaultLevelDbfs), |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 41 | standalone_vad_enabled_(true), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 42 | histogram_(Histogram::Create(kNumAnalysisFrames)), |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 43 | inactive_histogram_(Histogram::Create()), |
| 44 | audio_processing_(new AgcAudioProc()), |
| 45 | pitch_based_vad_(new PitchBasedVad()), |
| 46 | standalone_vad_(StandaloneVad::Create()), |
| 47 | // Initialize to the most common resampling situation. |
| 48 | resampler_(new Resampler(32000, kSampleRateHz, 1)) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | Agc::~Agc() {} |
| 52 | |
| 53 | float Agc::AnalyzePreproc(const int16_t* audio, int length) { |
| 54 | assert(length > 0); |
| 55 | int num_clipped = 0; |
| 56 | for (int i = 0; i < length; ++i) { |
| 57 | if (audio[i] == 32767 || audio[i] == -32768) |
| 58 | ++num_clipped; |
| 59 | } |
| 60 | return 1.0f * num_clipped / length; |
| 61 | } |
| 62 | |
| 63 | int Agc::Process(const int16_t* audio, int length, int sample_rate_hz) { |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 64 | assert(length == sample_rate_hz / 100); |
| 65 | if (sample_rate_hz > 32000) { |
| 66 | return -1; |
| 67 | } |
| 68 | // Resample to the required rate. |
| 69 | int16_t resampled[kLength10Ms]; |
| 70 | const int16_t* resampled_ptr = audio; |
| 71 | if (sample_rate_hz != kSampleRateHz) { |
| 72 | if (resampler_->ResetIfNeeded(sample_rate_hz, kSampleRateHz, 1) != 0) { |
| 73 | return -1; |
| 74 | } |
| 75 | resampler_->Push(audio, length, resampled, kLength10Ms, length); |
| 76 | resampled_ptr = resampled; |
| 77 | } |
| 78 | assert(length == kLength10Ms); |
| 79 | |
| 80 | if (standalone_vad_enabled_) { |
| 81 | if (standalone_vad_->AddAudio(resampled_ptr, length) != 0) |
| 82 | return -1; |
| 83 | } |
| 84 | |
| 85 | AudioFeatures features; |
| 86 | audio_processing_->ExtractFeatures(resampled_ptr, length, &features); |
| 87 | if (features.num_frames > 0) { |
| 88 | if (features.silence) { |
| 89 | // The other features are invalid, so update the histogram with an |
| 90 | // arbitrary low value. |
| 91 | for (int n = 0; n < features.num_frames; ++n) |
| 92 | histogram_->Update(features.rms[n], 0.01); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | // Initialize to 0.5 which is a neutral value for combining probabilities, |
| 97 | // in case the standalone-VAD is not enabled. |
| 98 | double p_combined[] = {0.5, 0.5, 0.5, 0.5}; |
| 99 | static_assert(sizeof(p_combined) / sizeof(p_combined[0]) == kMaxNumFrames, |
| 100 | "combined probability incorrect size"); |
| 101 | if (standalone_vad_enabled_) { |
| 102 | if (standalone_vad_->GetActivity(p_combined, kMaxNumFrames) < 0) |
| 103 | return -1; |
| 104 | } |
| 105 | // If any other VAD is enabled it must be combined before calling the |
| 106 | // pitch-based VAD. |
| 107 | if (pitch_based_vad_->VoicingProbability(features, p_combined) < 0) |
| 108 | return -1; |
| 109 | for (int n = 0; n < features.num_frames; n++) { |
| 110 | histogram_->Update(features.rms[n], p_combined[n]); |
| 111 | last_voice_probability_ = p_combined[n]; |
| 112 | } |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 113 | } |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | bool Agc::GetRmsErrorDb(int* error) { |
| 118 | if (!error) { |
| 119 | assert(false); |
| 120 | return false; |
| 121 | } |
| 122 | |
| 123 | if (histogram_->num_updates() < kNumAnalysisFrames) { |
| 124 | // We haven't yet received enough frames. |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) { |
| 129 | // We are likely in an inactive segment. |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | double loudness = Linear2Loudness(histogram_->CurrentRms()); |
| 134 | *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5); |
| 135 | histogram_->Reset(); |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | void Agc::Reset() { |
| 140 | histogram_->Reset(); |
| 141 | } |
| 142 | |
| 143 | int Agc::set_target_level_dbfs(int level) { |
| 144 | // TODO(turajs): just some arbitrary sanity check. We can come up with better |
| 145 | // limits. The upper limit should be chosen such that the risk of clipping is |
| 146 | // low. The lower limit should not result in a too quiet signal. |
| 147 | if (level >= 0 || level <= -100) |
| 148 | return -1; |
| 149 | target_level_dbfs_ = level; |
| 150 | target_level_loudness_ = Dbfs2Loudness(level); |
| 151 | return 0; |
| 152 | } |
| 153 | |
Bjorn Volcker | 51c7cbb | 2015-06-25 08:46:02 +0200 | [diff] [blame] | 154 | void Agc::EnableStandaloneVad(bool enable) { |
| 155 | standalone_vad_enabled_ = enable; |
| 156 | } |
| 157 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 158 | } // namespace webrtc |