blob: fd91bfafbf384d036c2a8606268ee9ccad610250 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
andrew@webrtc.org648af742012-02-08 01:57:29 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +000011#ifndef WEBRTC_MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_
12#define WEBRTC_MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +000014#include <stddef.h> // size_t
henrikg@webrtc.org863b5362013-12-06 16:05:17 +000015#include <stdio.h> // FILE
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +000016#include <vector>
ajm@google.com22e65152011-07-18 18:03:01 +000017
xians@webrtc.orge46bc772014-10-10 08:36:56 +000018#include "webrtc/base/platform_file.h"
andrew@webrtc.org61e596f2013-07-25 18:28:29 +000019#include "webrtc/common.h"
aluebs@webrtc.org1d883942015-03-05 20:38:21 +000020#include "webrtc/modules/audio_processing/beamformer/array_util.h"
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +000021#include "webrtc/typedefs.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +000023struct AecCore;
24
niklase@google.com470e71d2011-07-07 08:21:25 +000025namespace webrtc {
26
27class AudioFrame;
Michael Graczykdfa36052015-03-25 16:37:27 -070028
29template<typename T>
30class Beamformer;
31
Michael Graczyk86c6d332015-07-23 11:41:39 -070032class StreamConfig;
33class ProcessingConfig;
34
niklase@google.com470e71d2011-07-07 08:21:25 +000035class EchoCancellation;
36class EchoControlMobile;
37class GainControl;
38class HighPassFilter;
39class LevelEstimator;
40class NoiseSuppression;
41class VoiceDetection;
42
Henrik Lundin441f6342015-06-09 16:03:13 +020043// Use to enable the extended filter mode in the AEC, along with robustness
44// measures around the reported system delays. It comes with a significant
45// increase in AEC complexity, but is much more robust to unreliable reported
46// delays.
andrew@webrtc.org6b1e2192013-09-25 23:46:20 +000047//
48// Detailed changes to the algorithm:
49// - The filter length is changed from 48 to 128 ms. This comes with tuning of
50// several parameters: i) filter adaptation stepsize and error threshold;
51// ii) non-linear processing smoothing and overdrive.
52// - Option to ignore the reported delays on platforms which we deem
53// sufficiently unreliable. See WEBRTC_UNTRUSTED_DELAY in echo_cancellation.c.
54// - Faster startup times by removing the excessive "startup phase" processing
55// of reported delays.
56// - Much more conservative adjustments to the far-end read pointer. We smooth
57// the delay difference more heavily, and back off from the difference more.
58// Adjustments force a readaptation of the filter, so they should be avoided
59// except when really necessary.
Henrik Lundin441f6342015-06-09 16:03:13 +020060struct ExtendedFilter {
61 ExtendedFilter() : enabled(false) {}
62 explicit ExtendedFilter(bool enabled) : enabled(enabled) {}
63 bool enabled;
64};
andrew@webrtc.org6b1e2192013-09-25 23:46:20 +000065
henrik.lundin366e9522015-07-03 00:50:05 -070066// Enables delay-agnostic echo cancellation. This feature relies on internally
67// estimated delays between the process and reverse streams, thus not relying
68// on reported system delays. This configuration only applies to
69// EchoCancellation and not EchoControlMobile. It can be set in the constructor
70// or using AudioProcessing::SetExtraOptions().
henrik.lundin0f133b92015-07-02 00:17:55 -070071struct DelayAgnostic {
72 DelayAgnostic() : enabled(false) {}
73 explicit DelayAgnostic(bool enabled) : enabled(enabled) {}
74 bool enabled;
75};
bjornv@webrtc.org3f830722014-06-11 04:48:11 +000076
Bjorn Volckeradc46c42015-04-15 11:42:40 +020077// Use to enable experimental gain control (AGC). At startup the experimental
78// AGC moves the microphone volume up to |startup_min_volume| if the current
79// microphone volume is set too low. The value is clamped to its operating range
80// [12, 255]. Here, 255 maps to 100%.
81//
82// Must be provided through AudioProcessing::Create(Confg&).
Bjorn Volckerfb494512015-04-22 06:39:58 +020083#if defined(WEBRTC_CHROMIUM_BUILD)
Bjorn Volckeradc46c42015-04-15 11:42:40 +020084static const int kAgcStartupMinVolume = 85;
Bjorn Volckerfb494512015-04-22 06:39:58 +020085#else
86static const int kAgcStartupMinVolume = 0;
87#endif // defined(WEBRTC_CHROMIUM_BUILD)
andrew@webrtc.orgc7c7a532014-01-29 04:57:25 +000088struct ExperimentalAgc {
Bjorn Volckeradc46c42015-04-15 11:42:40 +020089 ExperimentalAgc() : enabled(true), startup_min_volume(kAgcStartupMinVolume) {}
Michael Graczyk86c6d332015-07-23 11:41:39 -070090 explicit ExperimentalAgc(bool enabled)
Bjorn Volckeradc46c42015-04-15 11:42:40 +020091 : enabled(enabled), startup_min_volume(kAgcStartupMinVolume) {}
92 ExperimentalAgc(bool enabled, int startup_min_volume)
93 : enabled(enabled), startup_min_volume(startup_min_volume) {}
andrew@webrtc.org6b1e2192013-09-25 23:46:20 +000094 bool enabled;
Bjorn Volckeradc46c42015-04-15 11:42:40 +020095 int startup_min_volume;
andrew@webrtc.org6b1e2192013-09-25 23:46:20 +000096};
97
aluebs@webrtc.org9825afc2014-06-30 17:39:53 +000098// Use to enable experimental noise suppression. It can be set in the
99// constructor or using AudioProcessing::SetExtraOptions().
100struct ExperimentalNs {
101 ExperimentalNs() : enabled(false) {}
102 explicit ExperimentalNs(bool enabled) : enabled(enabled) {}
103 bool enabled;
104};
105
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000106// Use to enable beamforming. Must be provided through the constructor. It will
107// have no impact if used with AudioProcessing::SetExtraOptions().
108struct Beamforming {
eblima894ad942015-07-03 08:34:33 -0700109 Beamforming()
110 : enabled(false),
111 array_geometry() {}
aluebs@webrtc.orgfb7a0392015-01-05 21:58:58 +0000112 Beamforming(bool enabled, const std::vector<Point>& array_geometry)
113 : enabled(enabled),
114 array_geometry(array_geometry) {}
115 const bool enabled;
116 const std::vector<Point> array_geometry;
aluebs@webrtc.orgae643ce2014-12-19 19:57:34 +0000117};
118
ekmeyerson60d9b332015-08-14 10:35:55 -0700119// Use to enable intelligibility enhancer in audio processing. Must be provided
120// though the constructor. It will have no impact if used with
121// AudioProcessing::SetExtraOptions().
122//
123// Note: If enabled and the reverse stream has more than one output channel,
124// the reverse stream will become an upmixed mono signal.
125struct Intelligibility {
126 Intelligibility() : enabled(false) {}
127 explicit Intelligibility(bool enabled) : enabled(enabled) {}
128 bool enabled;
129};
130
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +0000131static const int kAudioProcMaxNativeSampleRateHz = 32000;
132
niklase@google.com470e71d2011-07-07 08:21:25 +0000133// The Audio Processing Module (APM) provides a collection of voice processing
134// components designed for real-time communications software.
135//
136// APM operates on two audio streams on a frame-by-frame basis. Frames of the
137// primary stream, on which all processing is applied, are passed to
138// |ProcessStream()|. Frames of the reverse direction stream, which are used for
139// analysis by some components, are passed to |AnalyzeReverseStream()|. On the
140// client-side, this will typically be the near-end (capture) and far-end
141// (render) streams, respectively. APM should be placed in the signal chain as
142// close to the audio hardware abstraction layer (HAL) as possible.
143//
144// On the server-side, the reverse stream will normally not be used, with
145// processing occurring on each incoming stream.
146//
147// Component interfaces follow a similar pattern and are accessed through
148// corresponding getters in APM. All components are disabled at create-time,
149// with default settings that are recommended for most situations. New settings
150// can be applied without enabling a component. Enabling a component triggers
151// memory allocation and initialization to allow it to start processing the
152// streams.
153//
154// Thread safety is provided with the following assumptions to reduce locking
155// overhead:
156// 1. The stream getters and setters are called from the same thread as
157// ProcessStream(). More precisely, stream functions are never called
158// concurrently with ProcessStream().
159// 2. Parameter getters are never called concurrently with the corresponding
160// setter.
161//
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000162// APM accepts only linear PCM audio data in chunks of 10 ms. The int16
163// interfaces use interleaved data, while the float interfaces use deinterleaved
164// data.
niklase@google.com470e71d2011-07-07 08:21:25 +0000165//
166// Usage example, omitting error checking:
167// AudioProcessing* apm = AudioProcessing::Create(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168//
169// apm->high_pass_filter()->Enable(true);
170//
171// apm->echo_cancellation()->enable_drift_compensation(false);
172// apm->echo_cancellation()->Enable(true);
173//
174// apm->noise_reduction()->set_level(kHighSuppression);
175// apm->noise_reduction()->Enable(true);
176//
177// apm->gain_control()->set_analog_level_limits(0, 255);
178// apm->gain_control()->set_mode(kAdaptiveAnalog);
179// apm->gain_control()->Enable(true);
180//
181// apm->voice_detection()->Enable(true);
182//
183// // Start a voice call...
184//
185// // ... Render frame arrives bound for the audio HAL ...
186// apm->AnalyzeReverseStream(render_frame);
187//
188// // ... Capture frame arrives from the audio HAL ...
189// // Call required set_stream_ functions.
190// apm->set_stream_delay_ms(delay_ms);
191// apm->gain_control()->set_stream_analog_level(analog_level);
192//
193// apm->ProcessStream(capture_frame);
194//
195// // Call required stream_ functions.
196// analog_level = apm->gain_control()->stream_analog_level();
197// has_voice = apm->stream_has_voice();
198//
199// // Repeate render and capture processing for the duration of the call...
200// // Start a new call...
201// apm->Initialize();
202//
203// // Close the application...
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +0000204// delete apm;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205//
andrew@webrtc.orgf92aaff2014-02-15 04:22:49 +0000206class AudioProcessing {
niklase@google.com470e71d2011-07-07 08:21:25 +0000207 public:
Michael Graczyk86c6d332015-07-23 11:41:39 -0700208 // TODO(mgraczyk): Remove once all methods that use ChannelLayout are gone.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000209 enum ChannelLayout {
210 kMono,
211 // Left, right.
212 kStereo,
213 // Mono, keyboard mic.
214 kMonoAndKeyboard,
215 // Left, right, keyboard mic.
216 kStereoAndKeyboard
217 };
218
andrew@webrtc.org54744912014-02-05 06:30:29 +0000219 // Creates an APM instance. Use one instance for every primary audio stream
220 // requiring processing. On the client-side, this would typically be one
221 // instance for the near-end stream, and additional instances for each far-end
222 // stream which requires processing. On the server-side, this would typically
223 // be one instance for every incoming stream.
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000224 static AudioProcessing* Create();
andrew@webrtc.org54744912014-02-05 06:30:29 +0000225 // Allows passing in an optional configuration at create-time.
andrew@webrtc.orge84978f2014-01-25 02:09:06 +0000226 static AudioProcessing* Create(const Config& config);
aluebs@webrtc.orgd82f55d2015-01-15 18:07:21 +0000227 // Only for testing.
mgraczyk@chromium.org0f663de2015-03-13 00:13:32 +0000228 static AudioProcessing* Create(const Config& config,
Michael Graczykdfa36052015-03-25 16:37:27 -0700229 Beamformer<float>* beamformer);
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000230 virtual ~AudioProcessing() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000231
niklase@google.com470e71d2011-07-07 08:21:25 +0000232 // Initializes internal states, while retaining all user settings. This
233 // should be called before beginning to process a new audio stream. However,
234 // it is not necessary to call before processing the first stream after
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000235 // creation.
236 //
237 // It is also not necessary to call if the audio parameters (sample
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000238 // rate and number of channels) have changed. Passing updated parameters
239 // directly to |ProcessStream()| and |AnalyzeReverseStream()| is permissible.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000240 // If the parameters are known at init-time though, they may be provided.
niklase@google.com470e71d2011-07-07 08:21:25 +0000241 virtual int Initialize() = 0;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000242
243 // The int16 interfaces require:
244 // - only |NativeRate|s be used
245 // - that the input, output and reverse rates must match
Michael Graczyk86c6d332015-07-23 11:41:39 -0700246 // - that |processing_config.output_stream()| matches
247 // |processing_config.input_stream()|.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000248 //
Michael Graczyk86c6d332015-07-23 11:41:39 -0700249 // The float interfaces accept arbitrary rates and support differing input and
250 // output layouts, but the output must have either one channel or the same
251 // number of channels as the input.
252 virtual int Initialize(const ProcessingConfig& processing_config) = 0;
253
254 // Initialize with unpacked parameters. See Initialize() above for details.
255 //
256 // TODO(mgraczyk): Remove once clients are updated to use the new interface.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000257 virtual int Initialize(int input_sample_rate_hz,
258 int output_sample_rate_hz,
andrew@webrtc.orga8b97372014-03-10 22:26:12 +0000259 int reverse_sample_rate_hz,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000260 ChannelLayout input_layout,
261 ChannelLayout output_layout,
262 ChannelLayout reverse_layout) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000263
andrew@webrtc.org61e596f2013-07-25 18:28:29 +0000264 // Pass down additional options which don't have explicit setters. This
265 // ensures the options are applied immediately.
266 virtual void SetExtraOptions(const Config& config) = 0;
267
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000268 // DEPRECATED.
269 // TODO(ajm): Remove after Chromium has upgraded to using Initialize().
niklase@google.com470e71d2011-07-07 08:21:25 +0000270 virtual int set_sample_rate_hz(int rate) = 0;
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000271 // TODO(ajm): Remove after voice engine no longer requires it to resample
272 // the reverse stream to the forward rate.
273 virtual int input_sample_rate_hz() const = 0;
andrew@webrtc.org46b31b12014-04-23 03:33:54 +0000274 // TODO(ajm): Remove after Chromium no longer depends on it.
275 virtual int sample_rate_hz() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000277 // TODO(ajm): Only intended for internal use. Make private and friend the
278 // necessary classes?
279 virtual int proc_sample_rate_hz() const = 0;
280 virtual int proc_split_sample_rate_hz() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 virtual int num_input_channels() const = 0;
282 virtual int num_output_channels() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000283 virtual int num_reverse_channels() const = 0;
284
andrew@webrtc.org17342e52014-02-12 22:28:31 +0000285 // Set to true when the output of AudioProcessing will be muted or in some
286 // other way not used. Ideally, the captured audio would still be processed,
287 // but some components may change behavior based on this information.
288 // Default false.
289 virtual void set_output_will_be_muted(bool muted) = 0;
290 virtual bool output_will_be_muted() const = 0;
291
niklase@google.com470e71d2011-07-07 08:21:25 +0000292 // Processes a 10 ms |frame| of the primary audio stream. On the client-side,
293 // this is the near-end (or captured) audio.
294 //
295 // If needed for enabled functionality, any function with the set_stream_ tag
296 // must be called prior to processing the current frame. Any getter function
297 // with the stream_ tag which is needed should be called after processing.
298 //
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000299 // The |sample_rate_hz_|, |num_channels_|, and |samples_per_channel_|
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000300 // members of |frame| must be valid. If changed from the previous call to this
301 // method, it will trigger an initialization.
niklase@google.com470e71d2011-07-07 08:21:25 +0000302 virtual int ProcessStream(AudioFrame* frame) = 0;
303
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000304 // Accepts deinterleaved float audio with the range [-1, 1]. Each element
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000305 // of |src| points to a channel buffer, arranged according to
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000306 // |input_layout|. At output, the channels will be arranged according to
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000307 // |output_layout| at |output_sample_rate_hz| in |dest|.
308 //
Michael Graczyk86c6d332015-07-23 11:41:39 -0700309 // The output layout must have one channel or as many channels as the input.
310 // |src| and |dest| may use the same memory, if desired.
311 //
312 // TODO(mgraczyk): Remove once clients are updated to use the new interface.
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000313 virtual int ProcessStream(const float* const* src,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000314 int samples_per_channel,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000315 int input_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000316 ChannelLayout input_layout,
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000317 int output_sample_rate_hz,
318 ChannelLayout output_layout,
319 float* const* dest) = 0;
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000320
Michael Graczyk86c6d332015-07-23 11:41:39 -0700321 // Accepts deinterleaved float audio with the range [-1, 1]. Each element of
322 // |src| points to a channel buffer, arranged according to |input_stream|. At
323 // output, the channels will be arranged according to |output_stream| in
324 // |dest|.
325 //
326 // The output must have one channel or as many channels as the input. |src|
327 // and |dest| may use the same memory, if desired.
328 virtual int ProcessStream(const float* const* src,
329 const StreamConfig& input_config,
330 const StreamConfig& output_config,
331 float* const* dest) = 0;
332
niklase@google.com470e71d2011-07-07 08:21:25 +0000333 // Analyzes a 10 ms |frame| of the reverse direction audio stream. The frame
334 // will not be modified. On the client-side, this is the far-end (or to be
335 // rendered) audio.
336 //
337 // It is only necessary to provide this if echo processing is enabled, as the
338 // reverse stream forms the echo reference signal. It is recommended, but not
339 // necessary, to provide if gain control is enabled. On the server-side this
340 // typically will not be used. If you're not sure what to pass in here,
341 // chances are you don't need to use it.
342 //
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000343 // The |sample_rate_hz_|, |num_channels_|, and |samples_per_channel_|
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000344 // members of |frame| must be valid. |sample_rate_hz_| must correspond to
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000345 // |input_sample_rate_hz()|
niklase@google.com470e71d2011-07-07 08:21:25 +0000346 //
347 // TODO(ajm): add const to input; requires an implementation fix.
ekmeyerson60d9b332015-08-14 10:35:55 -0700348 // DEPRECATED: Use |ProcessReverseStream| instead.
349 // TODO(ekm): Remove once all users have updated to |ProcessReverseStream|.
niklase@google.com470e71d2011-07-07 08:21:25 +0000350 virtual int AnalyzeReverseStream(AudioFrame* frame) = 0;
351
ekmeyerson60d9b332015-08-14 10:35:55 -0700352 // Same as |AnalyzeReverseStream|, but may modify |frame| if intelligibility
353 // is enabled.
354 virtual int ProcessReverseStream(AudioFrame* frame) = 0;
355
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000356 // Accepts deinterleaved float audio with the range [-1, 1]. Each element
357 // of |data| points to a channel buffer, arranged according to |layout|.
Michael Graczyk86c6d332015-07-23 11:41:39 -0700358 // TODO(mgraczyk): Remove once clients are updated to use the new interface.
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000359 virtual int AnalyzeReverseStream(const float* const* data,
360 int samples_per_channel,
ekmeyerson60d9b332015-08-14 10:35:55 -0700361 int rev_sample_rate_hz,
andrew@webrtc.org17e40642014-03-04 20:58:13 +0000362 ChannelLayout layout) = 0;
363
Michael Graczyk86c6d332015-07-23 11:41:39 -0700364 // Accepts deinterleaved float audio with the range [-1, 1]. Each element of
365 // |data| points to a channel buffer, arranged according to |reverse_config|.
ekmeyerson60d9b332015-08-14 10:35:55 -0700366 virtual int ProcessReverseStream(const float* const* src,
367 const StreamConfig& reverse_input_config,
368 const StreamConfig& reverse_output_config,
369 float* const* dest) = 0;
Michael Graczyk86c6d332015-07-23 11:41:39 -0700370
niklase@google.com470e71d2011-07-07 08:21:25 +0000371 // This must be called if and only if echo processing is enabled.
372 //
373 // Sets the |delay| in ms between AnalyzeReverseStream() receiving a far-end
374 // frame and ProcessStream() receiving a near-end frame containing the
375 // corresponding echo. On the client-side this can be expressed as
376 // delay = (t_render - t_analyze) + (t_process - t_capture)
377 // where,
378 // - t_analyze is the time a frame is passed to AnalyzeReverseStream() and
379 // t_render is the time the first sample of the same frame is rendered by
380 // the audio hardware.
381 // - t_capture is the time the first sample of a frame is captured by the
382 // audio hardware and t_pull is the time the same frame is passed to
383 // ProcessStream().
384 virtual int set_stream_delay_ms(int delay) = 0;
385 virtual int stream_delay_ms() const = 0;
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000386 virtual bool was_stream_delay_set() const = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387
andrew@webrtc.org75dd2882014-02-11 20:52:30 +0000388 // Call to signal that a key press occurred (true) or did not occur (false)
389 // with this chunk of audio.
390 virtual void set_stream_key_pressed(bool key_pressed) = 0;
391 virtual bool stream_key_pressed() const = 0;
392
andrew@webrtc.org6f9f8172012-03-06 19:03:39 +0000393 // Sets a delay |offset| in ms to add to the values passed in through
394 // set_stream_delay_ms(). May be positive or negative.
395 //
396 // Note that this could cause an otherwise valid value passed to
397 // set_stream_delay_ms() to return an error.
398 virtual void set_delay_offset_ms(int offset) = 0;
399 virtual int delay_offset_ms() const = 0;
400
niklase@google.com470e71d2011-07-07 08:21:25 +0000401 // Starts recording debugging information to a file specified by |filename|,
402 // a NULL-terminated string. If there is an ongoing recording, the old file
403 // will be closed, and recording will continue in the newly specified file.
404 // An already existing file will be overwritten without warning.
andrew@webrtc.org5ae19de2011-12-13 22:59:33 +0000405 static const size_t kMaxFilenameSize = 1024;
niklase@google.com470e71d2011-07-07 08:21:25 +0000406 virtual int StartDebugRecording(const char filename[kMaxFilenameSize]) = 0;
407
henrikg@webrtc.org863b5362013-12-06 16:05:17 +0000408 // Same as above but uses an existing file handle. Takes ownership
409 // of |handle| and closes it at StopDebugRecording().
410 virtual int StartDebugRecording(FILE* handle) = 0;
411
xians@webrtc.orge46bc772014-10-10 08:36:56 +0000412 // Same as above but uses an existing PlatformFile handle. Takes ownership
413 // of |handle| and closes it at StopDebugRecording().
414 // TODO(xians): Make this interface pure virtual.
415 virtual int StartDebugRecordingForPlatformFile(rtc::PlatformFile handle) {
416 return -1;
417 }
418
niklase@google.com470e71d2011-07-07 08:21:25 +0000419 // Stops recording debugging information, and closes the file. Recording
420 // cannot be resumed in the same file (without overwriting it).
421 virtual int StopDebugRecording() = 0;
422
Bjorn Volcker4e7aa432015-07-07 11:50:05 +0200423 // Use to send UMA histograms at end of a call. Note that all histogram
424 // specific member variables are reset.
425 virtual void UpdateHistogramsOnCallEnd() = 0;
426
niklase@google.com470e71d2011-07-07 08:21:25 +0000427 // These provide access to the component interfaces and should never return
428 // NULL. The pointers will be valid for the lifetime of the APM instance.
429 // The memory for these objects is entirely managed internally.
430 virtual EchoCancellation* echo_cancellation() const = 0;
431 virtual EchoControlMobile* echo_control_mobile() const = 0;
432 virtual GainControl* gain_control() const = 0;
433 virtual HighPassFilter* high_pass_filter() const = 0;
434 virtual LevelEstimator* level_estimator() const = 0;
435 virtual NoiseSuppression* noise_suppression() const = 0;
436 virtual VoiceDetection* voice_detection() const = 0;
437
438 struct Statistic {
439 int instant; // Instantaneous value.
440 int average; // Long-term average.
441 int maximum; // Long-term maximum.
442 int minimum; // Long-term minimum.
443 };
444
andrew@webrtc.org648af742012-02-08 01:57:29 +0000445 enum Error {
446 // Fatal errors.
niklase@google.com470e71d2011-07-07 08:21:25 +0000447 kNoError = 0,
448 kUnspecifiedError = -1,
449 kCreationFailedError = -2,
450 kUnsupportedComponentError = -3,
451 kUnsupportedFunctionError = -4,
452 kNullPointerError = -5,
453 kBadParameterError = -6,
454 kBadSampleRateError = -7,
455 kBadDataLengthError = -8,
456 kBadNumberChannelsError = -9,
457 kFileError = -10,
458 kStreamParameterNotSetError = -11,
andrew@webrtc.org648af742012-02-08 01:57:29 +0000459 kNotEnabledError = -12,
niklase@google.com470e71d2011-07-07 08:21:25 +0000460
andrew@webrtc.org648af742012-02-08 01:57:29 +0000461 // Warnings are non-fatal.
niklase@google.com470e71d2011-07-07 08:21:25 +0000462 // This results when a set_stream_ parameter is out of range. Processing
463 // will continue, but the parameter may have been truncated.
andrew@webrtc.org648af742012-02-08 01:57:29 +0000464 kBadStreamParameterWarning = -13
niklase@google.com470e71d2011-07-07 08:21:25 +0000465 };
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000466
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000467 enum NativeRate {
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000468 kSampleRate8kHz = 8000,
469 kSampleRate16kHz = 16000,
aluebs@webrtc.org087da132014-11-17 23:01:23 +0000470 kSampleRate32kHz = 32000,
471 kSampleRate48kHz = 48000
andrew@webrtc.org56e4a052014-02-27 22:23:17 +0000472 };
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000473
474 static const int kChunkSizeMs = 10;
niklase@google.com470e71d2011-07-07 08:21:25 +0000475};
476
Michael Graczyk86c6d332015-07-23 11:41:39 -0700477class StreamConfig {
478 public:
479 // sample_rate_hz: The sampling rate of the stream.
480 //
481 // num_channels: The number of audio channels in the stream, excluding the
482 // keyboard channel if it is present. When passing a
483 // StreamConfig with an array of arrays T*[N],
484 //
485 // N == {num_channels + 1 if has_keyboard
486 // {num_channels if !has_keyboard
487 //
488 // has_keyboard: True if the stream has a keyboard channel. When has_keyboard
489 // is true, the last channel in any corresponding list of
490 // channels is the keyboard channel.
491 StreamConfig(int sample_rate_hz = 0,
492 int num_channels = 0,
493 bool has_keyboard = false)
494 : sample_rate_hz_(sample_rate_hz),
495 num_channels_(num_channels),
496 has_keyboard_(has_keyboard),
497 num_frames_(calculate_frames(sample_rate_hz)) {}
498
499 void set_sample_rate_hz(int value) {
500 sample_rate_hz_ = value;
501 num_frames_ = calculate_frames(value);
502 }
503 void set_num_channels(int value) { num_channels_ = value; }
504 void set_has_keyboard(bool value) { has_keyboard_ = value; }
505
506 int sample_rate_hz() const { return sample_rate_hz_; }
507
508 // The number of channels in the stream, not including the keyboard channel if
509 // present.
510 int num_channels() const { return num_channels_; }
511
512 bool has_keyboard() const { return has_keyboard_; }
513 int num_frames() const { return num_frames_; }
ekmeyerson60d9b332015-08-14 10:35:55 -0700514 int num_samples() const { return num_channels_ * num_frames_; }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700515
516 bool operator==(const StreamConfig& other) const {
517 return sample_rate_hz_ == other.sample_rate_hz_ &&
518 num_channels_ == other.num_channels_ &&
519 has_keyboard_ == other.has_keyboard_;
520 }
521
522 bool operator!=(const StreamConfig& other) const { return !(*this == other); }
523
524 private:
525 static int calculate_frames(int sample_rate_hz) {
526 return AudioProcessing::kChunkSizeMs * sample_rate_hz / 1000;
527 }
528
529 int sample_rate_hz_;
530 int num_channels_;
531 bool has_keyboard_;
532 int num_frames_;
533};
534
535class ProcessingConfig {
536 public:
537 enum StreamName {
538 kInputStream,
539 kOutputStream,
ekmeyerson60d9b332015-08-14 10:35:55 -0700540 kReverseInputStream,
541 kReverseOutputStream,
Michael Graczyk86c6d332015-07-23 11:41:39 -0700542 kNumStreamNames,
543 };
544
545 const StreamConfig& input_stream() const {
546 return streams[StreamName::kInputStream];
547 }
548 const StreamConfig& output_stream() const {
549 return streams[StreamName::kOutputStream];
550 }
ekmeyerson60d9b332015-08-14 10:35:55 -0700551 const StreamConfig& reverse_input_stream() const {
552 return streams[StreamName::kReverseInputStream];
553 }
554 const StreamConfig& reverse_output_stream() const {
555 return streams[StreamName::kReverseOutputStream];
Michael Graczyk86c6d332015-07-23 11:41:39 -0700556 }
557
558 StreamConfig& input_stream() { return streams[StreamName::kInputStream]; }
559 StreamConfig& output_stream() { return streams[StreamName::kOutputStream]; }
ekmeyerson60d9b332015-08-14 10:35:55 -0700560 StreamConfig& reverse_input_stream() {
561 return streams[StreamName::kReverseInputStream];
562 }
563 StreamConfig& reverse_output_stream() {
564 return streams[StreamName::kReverseOutputStream];
565 }
Michael Graczyk86c6d332015-07-23 11:41:39 -0700566
567 bool operator==(const ProcessingConfig& other) const {
568 for (int i = 0; i < StreamName::kNumStreamNames; ++i) {
569 if (this->streams[i] != other.streams[i]) {
570 return false;
571 }
572 }
573 return true;
574 }
575
576 bool operator!=(const ProcessingConfig& other) const {
577 return !(*this == other);
578 }
579
580 StreamConfig streams[StreamName::kNumStreamNames];
581};
582
niklase@google.com470e71d2011-07-07 08:21:25 +0000583// The acoustic echo cancellation (AEC) component provides better performance
584// than AECM but also requires more processing power and is dependent on delay
585// stability and reporting accuracy. As such it is well-suited and recommended
586// for PC and IP phone applications.
587//
588// Not recommended to be enabled on the server-side.
589class EchoCancellation {
590 public:
591 // EchoCancellation and EchoControlMobile may not be enabled simultaneously.
592 // Enabling one will disable the other.
593 virtual int Enable(bool enable) = 0;
594 virtual bool is_enabled() const = 0;
595
596 // Differences in clock speed on the primary and reverse streams can impact
597 // the AEC performance. On the client-side, this could be seen when different
598 // render and capture devices are used, particularly with webcams.
599 //
600 // This enables a compensation mechanism, and requires that
andrew@webrtc.orgddbb8a22014-04-22 21:00:04 +0000601 // set_stream_drift_samples() be called.
niklase@google.com470e71d2011-07-07 08:21:25 +0000602 virtual int enable_drift_compensation(bool enable) = 0;
603 virtual bool is_drift_compensation_enabled() const = 0;
604
niklase@google.com470e71d2011-07-07 08:21:25 +0000605 // Sets the difference between the number of samples rendered and captured by
606 // the audio devices since the last call to |ProcessStream()|. Must be called
andrew@webrtc.org6be1e932013-03-01 18:47:28 +0000607 // if drift compensation is enabled, prior to |ProcessStream()|.
608 virtual void set_stream_drift_samples(int drift) = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000609 virtual int stream_drift_samples() const = 0;
610
611 enum SuppressionLevel {
612 kLowSuppression,
613 kModerateSuppression,
614 kHighSuppression
615 };
616
617 // Sets the aggressiveness of the suppressor. A higher level trades off
618 // double-talk performance for increased echo suppression.
619 virtual int set_suppression_level(SuppressionLevel level) = 0;
620 virtual SuppressionLevel suppression_level() const = 0;
621
622 // Returns false if the current frame almost certainly contains no echo
623 // and true if it _might_ contain echo.
624 virtual bool stream_has_echo() const = 0;
625
626 // Enables the computation of various echo metrics. These are obtained
627 // through |GetMetrics()|.
628 virtual int enable_metrics(bool enable) = 0;
629 virtual bool are_metrics_enabled() const = 0;
630
631 // Each statistic is reported in dB.
632 // P_far: Far-end (render) signal power.
633 // P_echo: Near-end (capture) echo signal power.
634 // P_out: Signal power at the output of the AEC.
635 // P_a: Internal signal power at the point before the AEC's non-linear
636 // processor.
637 struct Metrics {
638 // RERL = ERL + ERLE
639 AudioProcessing::Statistic residual_echo_return_loss;
640
641 // ERL = 10log_10(P_far / P_echo)
642 AudioProcessing::Statistic echo_return_loss;
643
644 // ERLE = 10log_10(P_echo / P_out)
645 AudioProcessing::Statistic echo_return_loss_enhancement;
646
647 // (Pre non-linear processing suppression) A_NLP = 10log_10(P_echo / P_a)
648 AudioProcessing::Statistic a_nlp;
649 };
650
651 // TODO(ajm): discuss the metrics update period.
652 virtual int GetMetrics(Metrics* metrics) = 0;
653
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000654 // Enables computation and logging of delay values. Statistics are obtained
655 // through |GetDelayMetrics()|.
656 virtual int enable_delay_logging(bool enable) = 0;
657 virtual bool is_delay_logging_enabled() const = 0;
658
659 // The delay metrics consists of the delay |median| and the delay standard
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000660 // deviation |std|. It also consists of the fraction of delay estimates
661 // |fraction_poor_delays| that can make the echo cancellation perform poorly.
662 // The values are aggregated until the first call to |GetDelayMetrics()| and
663 // afterwards aggregated and updated every second.
664 // Note that if there are several clients pulling metrics from
665 // |GetDelayMetrics()| during a session the first call from any of them will
666 // change to one second aggregation window for all.
667 // TODO(bjornv): Deprecated, remove.
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000668 virtual int GetDelayMetrics(int* median, int* std) = 0;
bjornv@webrtc.orgb1786db2015-02-03 06:06:26 +0000669 virtual int GetDelayMetrics(int* median, int* std,
670 float* fraction_poor_delays) = 0;
bjornv@google.com1ba3dbe2011-10-03 08:18:10 +0000671
bjornv@webrtc.org91d11b32013-03-05 16:53:09 +0000672 // Returns a pointer to the low level AEC component. In case of multiple
673 // channels, the pointer to the first one is returned. A NULL pointer is
674 // returned when the AEC component is disabled or has not been initialized
675 // successfully.
676 virtual struct AecCore* aec_core() const = 0;
677
niklase@google.com470e71d2011-07-07 08:21:25 +0000678 protected:
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000679 virtual ~EchoCancellation() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000680};
681
682// The acoustic echo control for mobile (AECM) component is a low complexity
683// robust option intended for use on mobile devices.
684//
685// Not recommended to be enabled on the server-side.
686class EchoControlMobile {
687 public:
688 // EchoCancellation and EchoControlMobile may not be enabled simultaneously.
689 // Enabling one will disable the other.
690 virtual int Enable(bool enable) = 0;
691 virtual bool is_enabled() const = 0;
692
693 // Recommended settings for particular audio routes. In general, the louder
694 // the echo is expected to be, the higher this value should be set. The
695 // preferred setting may vary from device to device.
696 enum RoutingMode {
697 kQuietEarpieceOrHeadset,
698 kEarpiece,
699 kLoudEarpiece,
700 kSpeakerphone,
701 kLoudSpeakerphone
702 };
703
704 // Sets echo control appropriate for the audio routing |mode| on the device.
705 // It can and should be updated during a call if the audio routing changes.
706 virtual int set_routing_mode(RoutingMode mode) = 0;
707 virtual RoutingMode routing_mode() const = 0;
708
709 // Comfort noise replaces suppressed background noise to maintain a
710 // consistent signal level.
711 virtual int enable_comfort_noise(bool enable) = 0;
712 virtual bool is_comfort_noise_enabled() const = 0;
713
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000714 // A typical use case is to initialize the component with an echo path from a
ajm@google.com22e65152011-07-18 18:03:01 +0000715 // previous call. The echo path is retrieved using |GetEchoPath()|, typically
716 // at the end of a call. The data can then be stored for later use as an
717 // initializer before the next call, using |SetEchoPath()|.
718 //
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000719 // Controlling the echo path this way requires the data |size_bytes| to match
720 // the internal echo path size. This size can be acquired using
721 // |echo_path_size_bytes()|. |SetEchoPath()| causes an entire reset, worth
ajm@google.com22e65152011-07-18 18:03:01 +0000722 // noting if it is to be called during an ongoing call.
723 //
724 // It is possible that version incompatibilities may result in a stored echo
725 // path of the incorrect size. In this case, the stored path should be
726 // discarded.
727 virtual int SetEchoPath(const void* echo_path, size_t size_bytes) = 0;
728 virtual int GetEchoPath(void* echo_path, size_t size_bytes) const = 0;
729
730 // The returned path size is guaranteed not to change for the lifetime of
731 // the application.
732 static size_t echo_path_size_bytes();
bjornv@google.comc4b939c2011-07-13 08:09:56 +0000733
niklase@google.com470e71d2011-07-07 08:21:25 +0000734 protected:
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000735 virtual ~EchoControlMobile() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000736};
737
738// The automatic gain control (AGC) component brings the signal to an
739// appropriate range. This is done by applying a digital gain directly and, in
740// the analog mode, prescribing an analog gain to be applied at the audio HAL.
741//
742// Recommended to be enabled on the client-side.
743class GainControl {
744 public:
745 virtual int Enable(bool enable) = 0;
746 virtual bool is_enabled() const = 0;
747
748 // When an analog mode is set, this must be called prior to |ProcessStream()|
749 // to pass the current analog level from the audio HAL. Must be within the
750 // range provided to |set_analog_level_limits()|.
751 virtual int set_stream_analog_level(int level) = 0;
752
753 // When an analog mode is set, this should be called after |ProcessStream()|
754 // to obtain the recommended new analog level for the audio HAL. It is the
755 // users responsibility to apply this level.
756 virtual int stream_analog_level() = 0;
757
758 enum Mode {
759 // Adaptive mode intended for use if an analog volume control is available
760 // on the capture device. It will require the user to provide coupling
761 // between the OS mixer controls and AGC through the |stream_analog_level()|
762 // functions.
763 //
764 // It consists of an analog gain prescription for the audio device and a
765 // digital compression stage.
766 kAdaptiveAnalog,
767
768 // Adaptive mode intended for situations in which an analog volume control
769 // is unavailable. It operates in a similar fashion to the adaptive analog
770 // mode, but with scaling instead applied in the digital domain. As with
771 // the analog mode, it additionally uses a digital compression stage.
772 kAdaptiveDigital,
773
774 // Fixed mode which enables only the digital compression stage also used by
775 // the two adaptive modes.
776 //
777 // It is distinguished from the adaptive modes by considering only a
778 // short time-window of the input signal. It applies a fixed gain through
779 // most of the input level range, and compresses (gradually reduces gain
780 // with increasing level) the input signal at higher levels. This mode is
781 // preferred on embedded devices where the capture signal level is
782 // predictable, so that a known gain can be applied.
783 kFixedDigital
784 };
785
786 virtual int set_mode(Mode mode) = 0;
787 virtual Mode mode() const = 0;
788
789 // Sets the target peak |level| (or envelope) of the AGC in dBFs (decibels
790 // from digital full-scale). The convention is to use positive values. For
791 // instance, passing in a value of 3 corresponds to -3 dBFs, or a target
792 // level 3 dB below full-scale. Limited to [0, 31].
793 //
794 // TODO(ajm): use a negative value here instead, if/when VoE will similarly
795 // update its interface.
796 virtual int set_target_level_dbfs(int level) = 0;
797 virtual int target_level_dbfs() const = 0;
798
799 // Sets the maximum |gain| the digital compression stage may apply, in dB. A
800 // higher number corresponds to greater compression, while a value of 0 will
801 // leave the signal uncompressed. Limited to [0, 90].
802 virtual int set_compression_gain_db(int gain) = 0;
803 virtual int compression_gain_db() const = 0;
804
805 // When enabled, the compression stage will hard limit the signal to the
806 // target level. Otherwise, the signal will be compressed but not limited
807 // above the target level.
808 virtual int enable_limiter(bool enable) = 0;
809 virtual bool is_limiter_enabled() const = 0;
810
811 // Sets the |minimum| and |maximum| analog levels of the audio capture device.
812 // Must be set if and only if an analog mode is used. Limited to [0, 65535].
813 virtual int set_analog_level_limits(int minimum,
814 int maximum) = 0;
815 virtual int analog_level_minimum() const = 0;
816 virtual int analog_level_maximum() const = 0;
817
818 // Returns true if the AGC has detected a saturation event (period where the
819 // signal reaches digital full-scale) in the current frame and the analog
820 // level cannot be reduced.
821 //
822 // This could be used as an indicator to reduce or disable analog mic gain at
823 // the audio HAL.
824 virtual bool stream_is_saturated() const = 0;
825
826 protected:
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000827 virtual ~GainControl() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000828};
829
830// A filtering component which removes DC offset and low-frequency noise.
831// Recommended to be enabled on the client-side.
832class HighPassFilter {
833 public:
834 virtual int Enable(bool enable) = 0;
835 virtual bool is_enabled() const = 0;
836
837 protected:
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000838 virtual ~HighPassFilter() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000839};
840
841// An estimation component used to retrieve level metrics.
842class LevelEstimator {
843 public:
844 virtual int Enable(bool enable) = 0;
845 virtual bool is_enabled() const = 0;
846
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000847 // Returns the root mean square (RMS) level in dBFs (decibels from digital
848 // full-scale), or alternately dBov. It is computed over all primary stream
849 // frames since the last call to RMS(). The returned value is positive but
850 // should be interpreted as negative. It is constrained to [0, 127].
851 //
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000852 // The computation follows: https://tools.ietf.org/html/rfc6465
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000853 // with the intent that it can provide the RTP audio level indication.
854 //
855 // Frames passed to ProcessStream() with an |_energy| of zero are considered
856 // to have been muted. The RMS of the frame will be interpreted as -127.
857 virtual int RMS() = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000858
859 protected:
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000860 virtual ~LevelEstimator() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000861};
862
863// The noise suppression (NS) component attempts to remove noise while
864// retaining speech. Recommended to be enabled on the client-side.
865//
866// Recommended to be enabled on the client-side.
867class NoiseSuppression {
868 public:
869 virtual int Enable(bool enable) = 0;
870 virtual bool is_enabled() const = 0;
871
872 // Determines the aggressiveness of the suppression. Increasing the level
873 // will reduce the noise level at the expense of a higher speech distortion.
874 enum Level {
875 kLow,
876 kModerate,
877 kHigh,
878 kVeryHigh
879 };
880
881 virtual int set_level(Level level) = 0;
882 virtual Level level() const = 0;
883
bjornv@webrtc.org08329f42012-07-12 21:00:43 +0000884 // Returns the internally computed prior speech probability of current frame
885 // averaged over output channels. This is not supported in fixed point, for
886 // which |kUnsupportedFunctionError| is returned.
887 virtual float speech_probability() const = 0;
888
niklase@google.com470e71d2011-07-07 08:21:25 +0000889 protected:
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000890 virtual ~NoiseSuppression() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000891};
892
893// The voice activity detection (VAD) component analyzes the stream to
894// determine if voice is present. A facility is also provided to pass in an
895// external VAD decision.
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000896//
897// In addition to |stream_has_voice()| the VAD decision is provided through the
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000898// |AudioFrame| passed to |ProcessStream()|. The |vad_activity_| member will be
andrew@webrtc.orged083d42011-09-19 15:28:51 +0000899// modified to reflect the current decision.
niklase@google.com470e71d2011-07-07 08:21:25 +0000900class VoiceDetection {
901 public:
902 virtual int Enable(bool enable) = 0;
903 virtual bool is_enabled() const = 0;
904
905 // Returns true if voice is detected in the current frame. Should be called
906 // after |ProcessStream()|.
907 virtual bool stream_has_voice() const = 0;
908
909 // Some of the APM functionality requires a VAD decision. In the case that
910 // a decision is externally available for the current frame, it can be passed
911 // in here, before |ProcessStream()| is called.
912 //
913 // VoiceDetection does _not_ need to be enabled to use this. If it happens to
914 // be enabled, detection will be skipped for any frame in which an external
915 // VAD decision is provided.
916 virtual int set_stream_has_voice(bool has_voice) = 0;
917
918 // Specifies the likelihood that a frame will be declared to contain voice.
919 // A higher value makes it more likely that speech will not be clipped, at
920 // the expense of more noise being detected as voice.
921 enum Likelihood {
922 kVeryLowLikelihood,
923 kLowLikelihood,
924 kModerateLikelihood,
925 kHighLikelihood
926 };
927
928 virtual int set_likelihood(Likelihood likelihood) = 0;
929 virtual Likelihood likelihood() const = 0;
930
931 // Sets the |size| of the frames in ms on which the VAD will operate. Larger
932 // frames will improve detection accuracy, but reduce the frequency of
933 // updates.
934 //
935 // This does not impact the size of frames passed to |ProcessStream()|.
936 virtual int set_frame_size_ms(int size) = 0;
937 virtual int frame_size_ms() const = 0;
938
939 protected:
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000940 virtual ~VoiceDetection() {}
niklase@google.com470e71d2011-07-07 08:21:25 +0000941};
942} // namespace webrtc
943
andrew@webrtc.orgd72b3d62012-11-15 21:46:06 +0000944#endif // WEBRTC_MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_