1/*
2 *  Copyright (c) 2013 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#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
13
14#include "webrtc/base/scoped_ptr.h"
15#include "webrtc/modules/audio_processing/agc/agc.h"
16
17namespace webrtc {
18
19class AudioFrame;
20class DebugFile;
21class GainControl;
22
23// Callbacks that need to be injected into AgcManagerDirect to read and control
24// the volume values. This is done to remove the VoiceEngine dependency in
25// AgcManagerDirect.
26// TODO(aluebs): Remove VolumeCallbacks.
27class VolumeCallbacks {
28 public:
29  virtual ~VolumeCallbacks() {}
30  virtual void SetMicVolume(int volume) = 0;
31  virtual int GetMicVolume() = 0;
32};
33
34// Direct interface to use AGC to set volume and compression values.
35// AudioProcessing uses this interface directly to integrate the callback-less
36// AGC.
37//
38// This class is not thread-safe.
39class AgcManagerDirect final {
40 public:
41  // AgcManagerDirect will configure GainControl internally. The user is
42  // responsible for processing the audio using it after the call to Process.
43  // The operating range of startup_min_level is [12, 255] and any input value
44  // outside that range will be clamped.
45  AgcManagerDirect(GainControl* gctrl,
46                   VolumeCallbacks* volume_callbacks,
47                   int startup_min_level);
48  // Dependency injection for testing. Don't delete |agc| as the memory is owned
49  // by the manager.
50  AgcManagerDirect(Agc* agc,
51                   GainControl* gctrl,
52                   VolumeCallbacks* volume_callbacks,
53                   int startup_min_level);
54  ~AgcManagerDirect();
55
56  int Initialize();
57  void AnalyzePreProcess(int16_t* audio,
58                         int num_channels,
59                         size_t samples_per_channel);
60  void Process(const int16_t* audio, size_t length, int sample_rate_hz);
61
62  // Call when the capture stream has been muted/unmuted. This causes the
63  // manager to disregard all incoming audio; chances are good it's background
64  // noise to which we'd like to avoid adapting.
65  void SetCaptureMuted(bool muted);
66  bool capture_muted() { return capture_muted_; }
67
68  float voice_probability();
69
70 private:
71  // Sets a new microphone level, after first checking that it hasn't been
72  // updated by the user, in which case no action is taken.
73  void SetLevel(int new_level);
74
75  // Set the maximum level the AGC is allowed to apply. Also updates the
76  // maximum compression gain to compensate. The level must be at least
77  // |kClippedLevelMin|.
78  void SetMaxLevel(int level);
79
80  int CheckVolumeAndReset();
81  void UpdateGain();
82  void UpdateCompressor();
83
84  rtc::scoped_ptr<Agc> agc_;
85  GainControl* gctrl_;
86  VolumeCallbacks* volume_callbacks_;
87
88  int frames_since_clipped_;
89  int level_;
90  int max_level_;
91  int max_compression_gain_;
92  int target_compression_;
93  int compression_;
94  float compression_accumulator_;
95  bool capture_muted_;
96  bool check_volume_on_next_process_;
97  bool startup_;
98  int startup_min_level_;
99
100  rtc::scoped_ptr<DebugFile> file_preproc_;
101  rtc::scoped_ptr<DebugFile> file_postproc_;
102
103  RTC_DISALLOW_COPY_AND_ASSIGN(AgcManagerDirect);
104};
105
106}  // namespace webrtc
107
108#endif  // WEBRTC_MODULES_AUDIO_PROCESSING_AGC_AGC_MANAGER_DIRECT_H_
109