File: agc.cc

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (161 lines) | stat: -rw-r--r-- 5,221 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 *  Copyright (c) 2012 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 "webrtc/modules/audio_processing/agc/agc.h"

#include <cmath>
#include <cstdlib>

#include <algorithm>

#include "webrtc/common_audio/resampler/include/resampler.h"
#include "webrtc/modules/audio_processing/agc/agc_audio_proc.h"
#include "webrtc/modules/audio_processing/agc/common.h"
#include "webrtc/modules/audio_processing/agc/histogram.h"
#include "webrtc/modules/audio_processing/agc/pitch_based_vad.h"
#include "webrtc/modules/audio_processing/agc/standalone_vad.h"
#include "webrtc/modules/audio_processing/agc/utility.h"
#include "webrtc/modules/interface/module_common_types.h"
#include "webrtc/system_wrappers/interface/compile_assert.h"

namespace webrtc {
namespace {

const int kDefaultLevelDbfs = -18;
const double kDefaultVoiceValue = 1.0;
const int kNumAnalysisFrames = 100;
const double kActivityThreshold = 0.3;

}  // namespace

Agc::Agc()
    : target_level_loudness_(Dbfs2Loudness(kDefaultLevelDbfs)),
      last_voice_probability_(kDefaultVoiceValue),
      target_level_dbfs_(kDefaultLevelDbfs),
      standalone_vad_enabled_(true),
      histogram_(Histogram::Create(kNumAnalysisFrames)),
      inactive_histogram_(Histogram::Create()),
      audio_processing_(new AgcAudioProc()),
      pitch_based_vad_(new PitchBasedVad()),
      standalone_vad_(StandaloneVad::Create()),
      // Initialize to the most common resampling situation.
      resampler_(new Resampler(32000, kSampleRateHz, kResamplerSynchronous)) {
  }

Agc::~Agc() {}

float Agc::AnalyzePreproc(const int16_t* audio, int length) {
  assert(length > 0);
  int num_clipped = 0;
  for (int i = 0; i < length; ++i) {
    if (audio[i] == 32767 || audio[i] == -32768)
      ++num_clipped;
  }
  return 1.0f * num_clipped / length;
}

int Agc::Process(const int16_t* audio, int length, int sample_rate_hz) {
  assert(length == sample_rate_hz / 100);
  if (sample_rate_hz > 32000) {
    return -1;
  }
  // Resample to the required rate.
  int16_t resampled[kLength10Ms];
  const int16_t* resampled_ptr = audio;
  if (sample_rate_hz != kSampleRateHz) {
    if (resampler_->ResetIfNeeded(sample_rate_hz,
                                  kSampleRateHz,
                                  kResamplerSynchronous) != 0) {
      return -1;
    }
    resampler_->Push(audio, length, resampled, kLength10Ms, length);
    resampled_ptr = resampled;
  }
  assert(length == kLength10Ms);

  if (standalone_vad_enabled_) {
    if (standalone_vad_->AddAudio(resampled_ptr, length) != 0)
      return -1;
  }

  AudioFeatures features;
  audio_processing_->ExtractFeatures(resampled_ptr, length, &features);
  if (features.num_frames > 0) {
    if (features.silence) {
      // The other features are invalid, so update the histogram with an
      // arbitrary low value.
      for (int n = 0; n < features.num_frames; ++n)
        histogram_->Update(features.rms[n], 0.01);
      return 0;
    }

    // Initialize to 0.5 which is a neutral value for combining probabilities,
    // in case the standalone-VAD is not enabled.
    double p_combined[] = {0.5, 0.5, 0.5, 0.5};
    COMPILE_ASSERT(sizeof(p_combined) / sizeof(p_combined[0]) == kMaxNumFrames,
                   combined_probability_incorrect_size);
    if (standalone_vad_enabled_) {
      if (standalone_vad_->GetActivity(p_combined, kMaxNumFrames) < 0)
        return -1;
    }
    // If any other VAD is enabled it must be combined before calling the
    // pitch-based VAD.
    if (pitch_based_vad_->VoicingProbability(features, p_combined) < 0)
      return -1;
    for (int n = 0; n < features.num_frames; n++) {
      histogram_->Update(features.rms[n], p_combined[n]);
      last_voice_probability_ = p_combined[n];
    }
  }
  return 0;
}

bool Agc::GetRmsErrorDb(int* error) {
  if (!error) {
    assert(false);
    return false;
  }

  if (histogram_->num_updates() < kNumAnalysisFrames) {
    // We haven't yet received enough frames.
    return false;
  }

  if (histogram_->AudioContent() < kNumAnalysisFrames * kActivityThreshold) {
    // We are likely in an inactive segment.
    return false;
  }

  double loudness = Linear2Loudness(histogram_->CurrentRms());
  *error = std::floor(Loudness2Db(target_level_loudness_ - loudness) + 0.5);
  histogram_->Reset();
  return true;
}

void Agc::Reset() {
  histogram_->Reset();
}

int Agc::set_target_level_dbfs(int level) {
  // TODO(turajs): just some arbitrary sanity check. We can come up with better
  // limits. The upper limit should be chosen such that the risk of clipping is
  // low. The lower limit should not result in a too quiet signal.
  if (level >= 0 || level <= -100)
    return -1;
  target_level_dbfs_ = level;
  target_level_loudness_ = Dbfs2Loudness(level);
  return 0;
}

void Agc::EnableStandaloneVad(bool enable) {
  standalone_vad_enabled_ = enable;
}

}  // namespace webrtc