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> |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 17 | #include <vector> |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 18 | |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 19 | #include "webrtc/base/checks.h" |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 20 | #include "webrtc/modules/audio_processing/agc/loudness_histogram.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 21 | #include "webrtc/modules/audio_processing/agc/utility.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 22 | #include "webrtc/modules/include/module_common_types.h" |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 23 | |
| 24 | namespace webrtc { |
| 25 | namespace { |
| 26 | |
| 27 | const int kDefaultLevelDbfs = -18; |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 28 | const int kNumAnalysisFrames = 100; |
| 29 | const double kActivityThreshold = 0.3; |
| 30 | |
| 31 | } // namespace |
| 32 | |
| 33 | Agc::Agc() |
| 34 | : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)), |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 35 | target_level_dbfs_(kDefaultLevelDbfs), |
peah | bbe4233 | 2016-06-08 06:42:02 -0700 | [diff] [blame] | 36 | histogram_(LoudnessHistogram::Create(kNumAnalysisFrames)), |
| 37 | inactive_histogram_(LoudnessHistogram::Create()) {} |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 38 | |
| 39 | Agc::~Agc() {} |
| 40 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 41 | float Agc::AnalyzePreproc(const int16_t* audio, size_t length) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 42 | RTC_DCHECK_GT(length, 0u); |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 43 | size_t num_clipped = 0; |
| 44 | for (size_t i = 0; i < length; ++i) { |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 45 | if (audio[i] == 32767 || audio[i] == -32768) |
| 46 | ++num_clipped; |
| 47 | } |
| 48 | return 1.0f * num_clipped / length; |
| 49 | } |
| 50 | |
Peter Kasting | dce40cf | 2015-08-24 14:52:23 -0700 | [diff] [blame] | 51 | int Agc::Process(const int16_t* audio, size_t length, int sample_rate_hz) { |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 52 | vad_.ProcessChunk(audio, length, sample_rate_hz); |
| 53 | const std::vector<double>& rms = vad_.chunkwise_rms(); |
| 54 | const std::vector<double>& probabilities = |
| 55 | vad_.chunkwise_voice_probabilities(); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 56 | RTC_DCHECK_EQ(rms.size(), probabilities.size()); |
aluebs | ecf6b81 | 2015-06-25 12:28:48 -0700 | [diff] [blame] | 57 | for (size_t i = 0; i < rms.size(); ++i) { |
| 58 | histogram_->Update(rms[i], probabilities[i]); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 59 | } |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | bool Agc::GetRmsErrorDb(int* error) { |
| 64 | if (!error) { |
kwiberg | 9e2be5f | 2016-09-14 05:23:22 -0700 | [diff] [blame] | 65 | RTC_NOTREACHED(); |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (histogram_->num_updates() < kNumAnalysisFrames) { |
| 70 | // We haven't yet received enough frames. |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) { |
| 75 | // We are likely in an inactive segment. |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | double loudness = Linear2Loudness(histogram_->CurrentRms()); |
| 80 | *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5); |
| 81 | histogram_->Reset(); |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void Agc::Reset() { |
| 86 | histogram_->Reset(); |
| 87 | } |
| 88 | |
| 89 | int Agc::set_target_level_dbfs(int level) { |
| 90 | // TODO(turajs): just some arbitrary sanity check. We can come up with better |
| 91 | // limits. The upper limit should be chosen such that the risk of clipping is |
| 92 | // low. The lower limit should not result in a too quiet signal. |
| 93 | if (level >= 0 || level <= -100) |
| 94 | return -1; |
| 95 | target_level_dbfs_ = level; |
| 96 | target_level_loudness_ = Dbfs2Loudness(level); |
| 97 | return 0; |
| 98 | } |
| 99 | |
kwiberg | 8cf8898 | 2016-08-26 14:50:38 -0700 | [diff] [blame] | 100 | int Agc::target_level_dbfs() const { |
| 101 | return target_level_dbfs_; |
| 102 | } |
| 103 | |
| 104 | float Agc::voice_probability() const { |
| 105 | return vad_.last_voice_probability(); |
| 106 | } |
| 107 | |
pbos@webrtc.org | 788acd1 | 2014-12-15 09:41:24 +0000 | [diff] [blame] | 108 | } // namespace webrtc |