| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
#ifndef MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_ |
| 12 |
#define MODULES_AUDIO_PROCESSING_INCLUDE_AUDIO_PROCESSING_H_ |
| 13 |
|
| 14 |
|
| 15 |
#ifndef _USE_MATH_DEFINES |
| 16 |
#define _USE_MATH_DEFINES |
| 17 |
#endif |
| 18 |
|
| 19 |
#include <math.h> |
| 20 |
#include <stddef.h> |
| 21 |
#include <stdio.h> |
| 22 |
#include <string.h> |
| 23 |
|
| 24 |
#include <vector> |
| 25 |
|
| 26 |
#include "absl/types/optional.h" |
| 27 |
#include "api/array_view.h" |
| 28 |
#include "api/audio/echo_canceller3_config.h" |
| 29 |
#include "api/audio/echo_control.h" |
| 30 |
#include "api/scoped_refptr.h" |
| 31 |
#include "modules/audio_processing/include/audio_processing_statistics.h" |
| 32 |
#include "modules/audio_processing/include/config.h" |
| 33 |
#include "rtc_base/arraysize.h" |
| 34 |
#include "rtc_base/constructor_magic.h" |
| 35 |
#include "rtc_base/ref_count.h" |
| 36 |
#include "rtc_base/system/file_wrapper.h" |
| 37 |
#include "rtc_base/system/rtc_export.h" |
| 38 |
|
| 39 |
namespace rtc { |
| 40 |
class TaskQueue; |
| 41 |
} |
| 42 |
|
| 43 |
namespace webrtc { |
| 44 |
|
| 45 |
class AecDump; |
| 46 |
class AudioBuffer; |
| 47 |
|
| 48 |
class StreamConfig; |
| 49 |
class ProcessingConfig; |
| 50 |
|
| 51 |
class EchoDetector; |
| 52 |
class CustomAudioAnalyzer; |
| 53 |
class CustomProcessing; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
#if defined(WEBRTC_CHROMIUM_BUILD) |
| 62 |
static const int kAgcStartupMinVolume = 85; |
| 63 |
#else |
| 64 |
static const int kAgcStartupMinVolume = 0; |
| 65 |
#endif |
| 66 |
static constexpr int kClippedLevelMin = 70; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
struct ExperimentalAgc { |
| 72 |
ExperimentalAgc() = default; |
| 73 |
explicit ExperimentalAgc(bool enabled) : enabled(enabled) {} |
| 74 |
ExperimentalAgc(bool enabled, int startup_min_volume) |
| 75 |
: enabled(enabled), startup_min_volume(startup_min_volume) {} |
| 76 |
static const ConfigOptionID identifier = ConfigOptionID::kExperimentalAgc; |
| 77 |
bool enabled = true; |
| 78 |
int startup_min_volume = kAgcStartupMinVolume; |
| 79 |
|
| 80 |
int clipped_level_min = kClippedLevelMin; |
| 81 |
bool digital_adaptive_disabled = false; |
| 82 |
}; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
struct ExperimentalNs { |
| 91 |
ExperimentalNs() : enabled(false) {} |
| 92 |
explicit ExperimentalNs(bool enabled) : enabled(enabled) {} |
| 93 |
static const ConfigOptionID identifier = ConfigOptionID::kExperimentalNs; |
| 94 |
bool enabled; |
| 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 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
class RTC_EXPORT AudioProcessing : public rtc::RefCountInterface { |
| 178 |
public: |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
struct RTC_EXPORT Config { |
| 194 |
|
| 195 |
|
| 196 |
struct RTC_EXPORT Pipeline { |
| 197 |
|
| 198 |
|
| 199 |
int maximum_internal_processing_rate = 48000; |
| 200 |
|
| 201 |
bool multi_channel_render = false; |
| 202 |
|
| 203 |
|
| 204 |
bool multi_channel_capture = false; |
| 205 |
} pipeline; |
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
struct PreAmplifier { |
| 212 |
bool enabled = false; |
| 213 |
float fixed_gain_factor = 1.0f; |
| 214 |
} pre_amplifier; |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
struct CaptureLevelAdjustment { |
| 219 |
bool operator==(const CaptureLevelAdjustment& rhs) const; |
| 220 |
bool operator!=(const CaptureLevelAdjustment& rhs) const { |
| 221 |
return !(*this == rhs); |
| 222 |
} |
| 223 |
bool enabled = false; |
| 224 |
|
| 225 |
float pre_gain_factor = 1.0f; |
| 226 |
|
| 227 |
float post_gain_factor = 1.0f; |
| 228 |
struct AnalogMicGainEmulation { |
| 229 |
bool operator==(const AnalogMicGainEmulation& rhs) const; |
| 230 |
bool operator!=(const AnalogMicGainEmulation& rhs) const { |
| 231 |
return !(*this == rhs); |
| 232 |
} |
| 233 |
bool enabled = false; |
| 234 |
|
| 235 |
|
| 236 |
int initial_level = 255; |
| 237 |
} analog_mic_gain_emulation; |
| 238 |
} capture_level_adjustment; |
| 239 |
|
| 240 |
struct HighPassFilter { |
| 241 |
bool enabled = false; |
| 242 |
bool apply_in_full_band = true; |
| 243 |
} high_pass_filter; |
| 244 |
|
| 245 |
struct EchoCanceller { |
| 246 |
bool enabled = false; |
| 247 |
bool mobile_mode = false; |
| 248 |
bool export_linear_aec_output = false; |
| 249 |
|
| 250 |
|
| 251 |
bool enforce_high_pass_filtering = true; |
| 252 |
} echo_canceller; |
| 253 |
|
| 254 |
|
| 255 |
struct NoiseSuppression { |
| 256 |
bool enabled = false; |
| 257 |
enum Level { kLow, kModerate, kHigh, kVeryHigh }; |
| 258 |
Level level = kModerate; |
| 259 |
bool analyze_linear_aec_output_when_available = false; |
| 260 |
} noise_suppression; |
| 261 |
|
| 262 |
|
| 263 |
struct TransientSuppression { |
| 264 |
bool enabled = false; |
| 265 |
} transient_suppression; |
| 266 |
|
| 267 |
|
| 268 |
struct VoiceDetection { |
| 269 |
bool enabled = false; |
| 270 |
} voice_detection; |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
struct GainController1 { |
| 279 |
bool operator==(const GainController1& rhs) const; |
| 280 |
bool operator!=(const GainController1& rhs) const { |
| 281 |
return !(*this == rhs); |
| 282 |
} |
| 283 |
|
| 284 |
bool enabled = false; |
| 285 |
enum Mode { |
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
kAdaptiveAnalog, |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
kAdaptiveDigital, |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
kFixedDigital |
| 308 |
}; |
| 309 |
Mode mode = kAdaptiveAnalog; |
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
int target_level_dbfs = 3; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
int compression_gain_db = 9; |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
bool enable_limiter = true; |
| 324 |
|
| 325 |
|
| 326 |
int analog_level_minimum = 0; |
| 327 |
int analog_level_maximum = 255; |
| 328 |
|
| 329 |
|
| 330 |
struct AnalogGainController { |
| 331 |
bool enabled = true; |
| 332 |
int startup_min_volume = kAgcStartupMinVolume; |
| 333 |
|
| 334 |
|
| 335 |
int clipped_level_min = kClippedLevelMin; |
| 336 |
bool enable_digital_adaptive = true; |
| 337 |
} analog_gain_controller; |
| 338 |
} gain_controller1; |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
struct GainController2 { |
| 347 |
bool operator==(const GainController2& rhs) const; |
| 348 |
bool operator!=(const GainController2& rhs) const { |
| 349 |
return !(*this == rhs); |
| 350 |
} |
| 351 |
|
| 352 |
|
| 353 |
enum LevelEstimator { kRms, kPeak }; |
| 354 |
enum NoiseEstimator { kStationaryNoise, kNoiseFloor }; |
| 355 |
bool enabled = false; |
| 356 |
struct FixedDigital { |
| 357 |
float gain_db = 0.0f; |
| 358 |
} fixed_digital; |
| 359 |
struct AdaptiveDigital { |
| 360 |
bool enabled = false; |
| 361 |
NoiseEstimator noise_estimator = kNoiseFloor; |
| 362 |
int vad_reset_period_ms = 1500; |
| 363 |
int adjacent_speech_frames_threshold = 12; |
| 364 |
float max_gain_change_db_per_second = 3.0f; |
| 365 |
float max_output_noise_level_dbfs = -50.0f; |
| 366 |
bool sse2_allowed = true; |
| 367 |
bool avx2_allowed = true; |
| 368 |
bool neon_allowed = true; |
| 369 |
|
| 370 |
float vad_probability_attack = 1.0f; |
| 371 |
LevelEstimator level_estimator = kRms; |
| 372 |
int level_estimator_adjacent_speech_frames_threshold = 12; |
| 373 |
bool use_saturation_protector = true; |
| 374 |
float initial_saturation_margin_db = 25.0f; |
| 375 |
float extra_saturation_margin_db = 5.0f; |
| 376 |
int gain_applier_adjacent_speech_frames_threshold = 12; |
| 377 |
} adaptive_digital; |
| 378 |
} gain_controller2; |
| 379 |
|
| 380 |
struct ResidualEchoDetector { |
| 381 |
bool enabled = true; |
| 382 |
} residual_echo_detector; |
| 383 |
|
| 384 |
|
| 385 |
struct LevelEstimation { |
| 386 |
bool enabled = false; |
| 387 |
} level_estimation; |
| 388 |
|
| 389 |
std::string ToString() const; |
| 390 |
}; |
| 391 |
|
| 392 |
|
| 393 |
enum ChannelLayout { |
| 394 |
kMono, |
| 395 |
|
| 396 |
kStereo, |
| 397 |
|
| 398 |
kMonoAndKeyboard, |
| 399 |
|
| 400 |
kStereoAndKeyboard |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
class RuntimeSetting { |
| 406 |
public: |
| 407 |
enum class Type { |
| 408 |
kNotSpecified, |
| 409 |
kCapturePreGain, |
| 410 |
kCaptureCompressionGain, |
| 411 |
kCaptureFixedPostGain, |
| 412 |
kPlayoutVolumeChange, |
| 413 |
kCustomRenderProcessingRuntimeSetting, |
| 414 |
kPlayoutAudioDeviceChange, |
| 415 |
kCapturePostGain, |
| 416 |
kCaptureOutputUsed |
| 417 |
}; |
| 418 |
|
| 419 |
|
| 420 |
struct PlayoutAudioDeviceInfo { |
| 421 |
int id; |
| 422 |
int max_volume; |
| 423 |
}; |
| 424 |
|
| 425 |
RuntimeSetting() : type_(Type::kNotSpecified), value_(0.0f) {} |
| 426 |
~RuntimeSetting() = default; |
| 427 |
|
| 428 |
static RuntimeSetting CreateCapturePreGain(float gain) { |
| 429 |
return {Type::kCapturePreGain, gain}; |
| 430 |
} |
| 431 |
|
| 432 |
static RuntimeSetting CreateCapturePostGain(float gain) { |
| 433 |
return {Type::kCapturePostGain, gain}; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
static RuntimeSetting CreateCompressionGainDb(int gain_db) { |
| 439 |
RTC_DCHECK_GE(gain_db, 0); |
| 440 |
RTC_DCHECK_LE(gain_db, 90); |
| 441 |
return {Type::kCaptureCompressionGain, static_cast<float>(gain_db)}; |
| 442 |
} |
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
static RuntimeSetting CreateCaptureFixedPostGain(float gain_db) { |
| 447 |
RTC_DCHECK_GE(gain_db, 0.0f); |
| 448 |
RTC_DCHECK_LE(gain_db, 90.0f); |
| 449 |
return {Type::kCaptureFixedPostGain, gain_db}; |
| 450 |
} |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
static RuntimeSetting CreatePlayoutAudioDeviceChange( |
| 455 |
PlayoutAudioDeviceInfo audio_device) { |
| 456 |
return {Type::kPlayoutAudioDeviceChange, audio_device}; |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
static RuntimeSetting CreatePlayoutVolumeChange(int volume) { |
| 462 |
return {Type::kPlayoutVolumeChange, volume}; |
| 463 |
} |
| 464 |
|
| 465 |
static RuntimeSetting CreateCustomRenderSetting(float payload) { |
| 466 |
return {Type::kCustomRenderProcessingRuntimeSetting, payload}; |
| 467 |
} |
| 468 |
|
| 469 |
static RuntimeSetting CreateCaptureOutputUsedSetting( |
| 470 |
bool capture_output_used) { |
| 471 |
return {Type::kCaptureOutputUsed, capture_output_used}; |
| 472 |
} |
| 473 |
|
| 474 |
Type type() const { return type_; } |
| 475 |
|
| 476 |
|
| 477 |
void GetFloat(float* value) const { |
| 478 |
RTC_DCHECK(value); |
| 479 |
*value = value_.float_value; |
| 480 |
} |
| 481 |
void GetInt(int* value) const { |
| 482 |
RTC_DCHECK(value); |
| 483 |
*value = value_.int_value; |
| 484 |
} |
| 485 |
void GetBool(bool* value) const { |
| 486 |
RTC_DCHECK(value); |
| 487 |
*value = value_.bool_value; |
| 488 |
} |
| 489 |
void GetPlayoutAudioDeviceInfo(PlayoutAudioDeviceInfo* value) const { |
| 490 |
RTC_DCHECK(value); |
| 491 |
*value = value_.playout_audio_device_info; |
| 492 |
} |
| 493 |
|
| 494 |
private: |
| 495 |
RuntimeSetting(Type id, float value) : type_(id), value_(value) {} |
| 496 |
RuntimeSetting(Type id, int value) : type_(id), value_(value) {} |
| 497 |
RuntimeSetting(Type id, PlayoutAudioDeviceInfo value) |
| 498 |
: type_(id), value_(value) {} |
| 499 |
Type type_; |
| 500 |
union U { |
| 501 |
U() {} |
| 502 |
U(int value) : int_value(value) {} |
| 503 |
U(float value) : float_value(value) {} |
| 504 |
U(PlayoutAudioDeviceInfo value) : playout_audio_device_info(value) {} |
| 505 |
float float_value; |
| 506 |
int int_value; |
| 507 |
bool bool_value; |
| 508 |
PlayoutAudioDeviceInfo playout_audio_device_info; |
| 509 |
} value_; |
| 510 |
}; |
| 511 |
|
| 512 |
~AudioProcessing() override {} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
virtual int Initialize() = 0; |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
virtual int Initialize(const ProcessingConfig& processing_config) = 0; |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
virtual int Initialize(int capture_input_sample_rate_hz, |
| 541 |
int capture_output_sample_rate_hz, |
| 542 |
int render_sample_rate_hz, |
| 543 |
ChannelLayout capture_input_layout, |
| 544 |
ChannelLayout capture_output_layout, |
| 545 |
ChannelLayout render_input_layout) = 0; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
virtual void ApplyConfig(const Config& config) = 0; |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
virtual int proc_sample_rate_hz() const = 0; |
| 554 |
virtual int proc_split_sample_rate_hz() const = 0; |
| 555 |
virtual size_t num_input_channels() const = 0; |
| 556 |
virtual size_t num_proc_channels() const = 0; |
| 557 |
virtual size_t num_output_channels() const = 0; |
| 558 |
virtual size_t num_reverse_channels() const = 0; |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
virtual void set_output_will_be_muted(bool muted) = 0; |
| 566 |
|
| 567 |
|
| 568 |
virtual void SetRuntimeSetting(RuntimeSetting setting) = 0; |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
virtual bool PostRuntimeSetting(RuntimeSetting setting) = 0; |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
virtual int ProcessStream(const int16_t* const src, |
| 578 |
const StreamConfig& input_config, |
| 579 |
const StreamConfig& output_config, |
| 580 |
int16_t* const dest) = 0; |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
virtual int ProcessStream(const float* const* src, |
| 590 |
const StreamConfig& input_config, |
| 591 |
const StreamConfig& output_config, |
| 592 |
float* const* dest) = 0; |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
virtual int ProcessReverseStream(const int16_t* const src, |
| 598 |
const StreamConfig& input_config, |
| 599 |
const StreamConfig& output_config, |
| 600 |
int16_t* const dest) = 0; |
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
virtual int ProcessReverseStream(const float* const* src, |
| 605 |
const StreamConfig& input_config, |
| 606 |
const StreamConfig& output_config, |
| 607 |
float* const* dest) = 0; |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
virtual int AnalyzeReverseStream(const float* const* data, |
| 613 |
const StreamConfig& reverse_config) = 0; |
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
virtual bool GetLinearAecOutput( |
| 620 |
rtc::ArrayView<std::array<float, 160>> linear_output) const = 0; |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
virtual void set_stream_analog_level(int level) = 0; |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
virtual int recommended_stream_analog_level() const = 0; |
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
virtual int set_stream_delay_ms(int delay) = 0; |
| 646 |
virtual int stream_delay_ms() const = 0; |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
virtual void set_stream_key_pressed(bool key_pressed) = 0; |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
virtual bool CreateAndAttachAecDump(const std::string& file_name, |
| 662 |
int64_t max_log_size_bytes, |
| 663 |
rtc::TaskQueue* worker_queue) = 0; |
| 664 |
virtual bool CreateAndAttachAecDump(FILE* handle, |
| 665 |
int64_t max_log_size_bytes, |
| 666 |
rtc::TaskQueue* worker_queue) = 0; |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
virtual void AttachAecDump(std::unique_ptr<AecDump> aec_dump) = 0; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
virtual void DetachAecDump() = 0; |
| 682 |
|
| 683 |
|
| 684 |
virtual AudioProcessingStats GetStatistics() = 0; |
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
virtual AudioProcessingStats GetStatistics(bool has_remote_tracks) = 0; |
| 691 |
|
| 692 |
|
| 693 |
virtual AudioProcessing::Config GetConfig() const = 0; |
| 694 |
|
| 695 |
enum Error { |
| 696 |
|
| 697 |
kNoError = 0, |
| 698 |
kUnspecifiedError = -1, |
| 699 |
kCreationFailedError = -2, |
| 700 |
kUnsupportedComponentError = -3, |
| 701 |
kUnsupportedFunctionError = -4, |
| 702 |
kNullPointerError = -5, |
| 703 |
kBadParameterError = -6, |
| 704 |
kBadSampleRateError = -7, |
| 705 |
kBadDataLengthError = -8, |
| 706 |
kBadNumberChannelsError = -9, |
| 707 |
kFileError = -10, |
| 708 |
kStreamParameterNotSetError = -11, |
| 709 |
kNotEnabledError = -12, |
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
kBadStreamParameterWarning = -13 |
| 715 |
}; |
| 716 |
|
| 717 |
|
| 718 |
enum NativeRate { |
| 719 |
kSampleRate8kHz = 8000, |
| 720 |
kSampleRate16kHz = 16000, |
| 721 |
kSampleRate32kHz = 32000, |
| 722 |
kSampleRate48kHz = 48000 |
| 723 |
}; |
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
static constexpr int kNativeSampleRatesHz[4] = { |
| 729 |
kSampleRate8kHz, kSampleRate16kHz, kSampleRate32kHz, kSampleRate48kHz}; |
| 730 |
static constexpr size_t kNumNativeSampleRates = |
| 731 |
arraysize(kNativeSampleRatesHz); |
| 732 |
static constexpr int kMaxNativeSampleRateHz = |
| 733 |
kNativeSampleRatesHz[kNumNativeSampleRates - 1]; |
| 734 |
|
| 735 |
static constexpr int kChunkSizeMs = 10; |
| 736 |
}; |
| 737 |
|
| 738 |
class RTC_EXPORT AudioProcessingBuilder { |
| 739 |
public: |
| 740 |
AudioProcessingBuilder(); |
| 741 |
~AudioProcessingBuilder(); |
| 742 |
|
| 743 |
AudioProcessingBuilder& SetEchoControlFactory( |
| 744 |
std::unique_ptr<EchoControlFactory> echo_control_factory) { |
| 745 |
echo_control_factory_ = std::move(echo_control_factory); |
| 746 |
return *this; |
| 747 |
} |
| 748 |
|
| 749 |
AudioProcessingBuilder& SetCapturePostProcessing( |
| 750 |
std::unique_ptr<CustomProcessing> capture_post_processing) { |
| 751 |
capture_post_processing_ = std::move(capture_post_processing); |
| 752 |
return *this; |
| 753 |
} |
| 754 |
|
| 755 |
AudioProcessingBuilder& SetRenderPreProcessing( |
| 756 |
std::unique_ptr<CustomProcessing> render_pre_processing) { |
| 757 |
render_pre_processing_ = std::move(render_pre_processing); |
| 758 |
return *this; |
| 759 |
} |
| 760 |
|
| 761 |
AudioProcessingBuilder& SetEchoDetector( |
| 762 |
rtc::scoped_refptr<EchoDetector> echo_detector) { |
| 763 |
echo_detector_ = std::move(echo_detector); |
| 764 |
return *this; |
| 765 |
} |
| 766 |
|
| 767 |
AudioProcessingBuilder& SetCaptureAnalyzer( |
| 768 |
std::unique_ptr<CustomAudioAnalyzer> capture_analyzer) { |
| 769 |
capture_analyzer_ = std::move(capture_analyzer); |
| 770 |
return *this; |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
AudioProcessing* Create(); |
| 775 |
AudioProcessing* Create(const webrtc::Config& config); |
| 776 |
|
| 777 |
private: |
| 778 |
std::unique_ptr<EchoControlFactory> echo_control_factory_; |
| 779 |
std::unique_ptr<CustomProcessing> capture_post_processing_; |
| 780 |
std::unique_ptr<CustomProcessing> render_pre_processing_; |
| 781 |
rtc::scoped_refptr<EchoDetector> echo_detector_; |
| 782 |
std::unique_ptr<CustomAudioAnalyzer> capture_analyzer_; |
| 783 |
RTC_DISALLOW_COPY_AND_ASSIGN(AudioProcessingBuilder); |
| 784 |
}; |
| 785 |
|
| 786 |
class StreamConfig { |
| 787 |
public: |
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
StreamConfig(int sample_rate_hz = 0, |
| 801 |
size_t num_channels = 0, |
| 802 |
bool has_keyboard = false) |
| 803 |
: sample_rate_hz_(sample_rate_hz), |
| 804 |
num_channels_(num_channels), |
| 805 |
has_keyboard_(has_keyboard), |
| 806 |
num_frames_(calculate_frames(sample_rate_hz)) {} |
| 807 |
|
| 808 |
void set_sample_rate_hz(int value) { |
| 809 |
sample_rate_hz_ = value; |
| 810 |
num_frames_ = calculate_frames(value); |
| 811 |
} |
| 812 |
void set_num_channels(size_t value) { num_channels_ = value; } |
| 813 |
void set_has_keyboard(bool value) { has_keyboard_ = value; } |
| 814 |
|
| 815 |
int sample_rate_hz() const { return sample_rate_hz_; } |
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
size_t num_channels() const { return num_channels_; } |
| 820 |
|
| 821 |
bool has_keyboard() const { return has_keyboard_; } |
| 822 |
size_t num_frames() const { return num_frames_; } |
| 823 |
size_t num_samples() const { return num_channels_ * num_frames_; } |
| 824 |
|
| 825 |
bool operator==(const StreamConfig& other) const { |
| 826 |
return sample_rate_hz_ == other.sample_rate_hz_ && |
| 827 |
num_channels_ == other.num_channels_ && |
| 828 |
has_keyboard_ == other.has_keyboard_; |
| 829 |
} |
| 830 |
|
| 831 |
bool operator!=(const StreamConfig& other) const { return !(*this == other); } |
| 832 |
|
| 833 |
private: |
| 834 |
static size_t calculate_frames(int sample_rate_hz) { |
| 835 |
return static_cast<size_t>(AudioProcessing::kChunkSizeMs * sample_rate_hz / |
| 836 |
1000); |
| 837 |
} |
| 838 |
|
| 839 |
int sample_rate_hz_; |
| 840 |
size_t num_channels_; |
| 841 |
bool has_keyboard_; |
| 842 |
size_t num_frames_; |
| 843 |
}; |
| 844 |
|
| 845 |
class ProcessingConfig { |
| 846 |
public: |
| 847 |
enum StreamName { |
| 848 |
kInputStream, |
| 849 |
kOutputStream, |
| 850 |
kReverseInputStream, |
| 851 |
kReverseOutputStream, |
| 852 |
kNumStreamNames, |
| 853 |
}; |
| 854 |
|
| 855 |
const StreamConfig& input_stream() const { |
| 856 |
return streams[StreamName::kInputStream]; |
| 857 |
} |
| 858 |
const StreamConfig& output_stream() const { |
| 859 |
return streams[StreamName::kOutputStream]; |
| 860 |
} |
| 861 |
const StreamConfig& reverse_input_stream() const { |
| 862 |
return streams[StreamName::kReverseInputStream]; |
| 863 |
} |
| 864 |
const StreamConfig& reverse_output_stream() const { |
| 865 |
return streams[StreamName::kReverseOutputStream]; |
| 866 |
} |
| 867 |
|
| 868 |
StreamConfig& input_stream() { return streams[StreamName::kInputStream]; } |
| 869 |
StreamConfig& output_stream() { return streams[StreamName::kOutputStream]; } |
| 870 |
StreamConfig& reverse_input_stream() { |
| 871 |
return streams[StreamName::kReverseInputStream]; |
| 872 |
} |
| 873 |
StreamConfig& reverse_output_stream() { |
| 874 |
return streams[StreamName::kReverseOutputStream]; |
| 875 |
} |
| 876 |
|
| 877 |
bool operator==(const ProcessingConfig& other) const { |
| 878 |
for (int i = 0; i < StreamName::kNumStreamNames; ++i) { |
| 879 |
if (this->streams[i] != other.streams[i]) { |
| 880 |
return false; |
| 881 |
} |
| 882 |
} |
| 883 |
return true; |
| 884 |
} |
| 885 |
|
| 886 |
bool operator!=(const ProcessingConfig& other) const { |
| 887 |
return !(*this == other); |
| 888 |
} |
| 889 |
|
| 890 |
StreamConfig streams[StreamName::kNumStreamNames]; |
| 891 |
}; |
| 892 |
|
| 893 |
|
| 894 |
class CustomAudioAnalyzer { |
| 895 |
public: |
| 896 |
|
| 897 |
virtual void Initialize(int sample_rate_hz, int num_channels) = 0; |
| 898 |
|
| 899 |
virtual void Analyze(const AudioBuffer* audio) = 0; |
| 900 |
|
| 901 |
virtual std::string ToString() const = 0; |
| 902 |
|
| 903 |
virtual ~CustomAudioAnalyzer() {} |
| 904 |
}; |
| 905 |
|
| 906 |
|
| 907 |
class CustomProcessing { |
| 908 |
public: |
| 909 |
|
| 910 |
virtual void Initialize(int sample_rate_hz, int num_channels) = 0; |
| 911 |
|
| 912 |
virtual void Process(AudioBuffer* audio) = 0; |
| 913 |
|
| 914 |
virtual std::string ToString() const = 0; |
| 915 |
|
| 916 |
|
| 917 |
virtual void SetRuntimeSetting(AudioProcessing::RuntimeSetting setting); |
| 918 |
|
| 919 |
virtual ~CustomProcessing() {} |
| 920 |
}; |
| 921 |
|
| 922 |
|
| 923 |
class EchoDetector : public rtc::RefCountInterface { |
| 924 |
public: |
| 925 |
|
| 926 |
virtual void Initialize(int capture_sample_rate_hz, |
| 927 |
int num_capture_channels, |
| 928 |
int render_sample_rate_hz, |
| 929 |
int num_render_channels) = 0; |
| 930 |
|
| 931 |
|
| 932 |
virtual void AnalyzeRenderAudio(rtc::ArrayView<const float> render_audio) = 0; |
| 933 |
|
| 934 |
|
| 935 |
virtual void AnalyzeCaptureAudio( |
| 936 |
rtc::ArrayView<const float> capture_audio) = 0; |
| 937 |
|
| 938 |
|
| 939 |
static void PackRenderAudioBuffer(AudioBuffer* audio, |
| 940 |
std::vector<float>* packed_buffer); |
| 941 |
|
| 942 |
struct Metrics { |
| 943 |
absl::optional<double> echo_likelihood; |
| 944 |
absl::optional<double> echo_likelihood_recent_max; |
| 945 |
}; |
| 946 |
|
| 947 |
|
| 948 |
virtual Metrics GetMetrics() const = 0; |
| 949 |
}; |
| 950 |
|
| 951 |
} |
| 952 |
|
| 953 |
#endif |