From 504bd59422283ebfa6275108804197edeeef908e Mon Sep 17 00:00:00 2001 From: Alessio Bazzica Date: Thu, 1 Dec 2022 13:26:26 +0100 Subject: [PATCH] Expose APM pipeline parameter to select downmixing method Bug: b/257289030 Change-Id: I03a666a15752d7a1125af2f8d3edae530493618c Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/285473 Reviewed-by: Jesus de Vicente Pena Commit-Queue: Alessio Bazzica Cr-Commit-Position: refs/heads/main@{#38789} --- .../audio_processing/audio_processing_impl.cc | 21 ++++++++++++++++++- .../include/audio_processing.h | 9 ++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/modules/audio_processing/audio_processing_impl.cc b/modules/audio_processing/audio_processing_impl.cc index 52f2fcb14c..2cb48588d8 100644 --- a/modules/audio_processing/audio_processing_impl.cc +++ b/modules/audio_processing/audio_processing_impl.cc @@ -442,6 +442,19 @@ const AudioProcessing::Config AdjustConfig( return adjusted_config; } +using DownmixMethod = AudioProcessing::Config::Pipeline::DownmixMethod; + +void SetDownmixMethod(AudioBuffer& buffer, DownmixMethod method) { + switch (method) { + case DownmixMethod::kAverageChannels: + buffer.set_downmixing_by_averaging(); + break; + case DownmixMethod::kUseFirstChannel: + buffer.set_downmixing_to_specific_channel(/*channel=*/0); + break; + } +} + constexpr int kUnspecifiedDataDumpInputVolume = -100; } // namespace @@ -689,6 +702,8 @@ void AudioProcessingImpl::InitializeLocked() { formats_.api_format.output_stream().num_channels(), formats_.api_format.output_stream().sample_rate_hz(), formats_.api_format.output_stream().num_channels())); + SetDownmixMethod(*capture_.capture_audio, + config_.pipeline.capture_downmix_method); if (capture_nonlocked_.capture_processing_format.sample_rate_hz() < formats_.api_format.output_stream().sample_rate_hz() && @@ -700,6 +715,8 @@ void AudioProcessingImpl::InitializeLocked() { formats_.api_format.output_stream().num_channels(), formats_.api_format.output_stream().sample_rate_hz(), formats_.api_format.output_stream().num_channels())); + SetDownmixMethod(*capture_.capture_fullband_audio, + config_.pipeline.capture_downmix_method); } else { capture_.capture_fullband_audio.reset(); } @@ -821,7 +838,9 @@ void AudioProcessingImpl::ApplyConfig(const AudioProcessing::Config& config) { config_.pipeline.multi_channel_capture != adjusted_config.pipeline.multi_channel_capture || config_.pipeline.maximum_internal_processing_rate != - adjusted_config.pipeline.maximum_internal_processing_rate; + adjusted_config.pipeline.maximum_internal_processing_rate || + config_.pipeline.capture_downmix_method != + adjusted_config.pipeline.capture_downmix_method; const bool aec_config_changed = config_.echo_canceller.enabled != diff --git a/modules/audio_processing/include/audio_processing.h b/modules/audio_processing/include/audio_processing.h index f3600b3233..9935167750 100644 --- a/modules/audio_processing/include/audio_processing.h +++ b/modules/audio_processing/include/audio_processing.h @@ -146,6 +146,12 @@ class RTC_EXPORT AudioProcessing : public rtc::RefCountInterface { struct RTC_EXPORT Config { // Sets the properties of the audio processing pipeline. struct RTC_EXPORT Pipeline { + // Ways to downmix a multi-channel track to mono. + enum class DownmixMethod { + kAverageChannels, // Average across channels. + kUseFirstChannel // Use the first channel. + }; + // Maximum allowed processing rate used internally. May only be set to // 32000 or 48000 and any differing values will be treated as 48000. int maximum_internal_processing_rate = 48000; @@ -154,6 +160,9 @@ class RTC_EXPORT AudioProcessing : public rtc::RefCountInterface { // Allow multi-channel processing of capture audio when AEC3 is active // or a custom AEC is injected.. bool multi_channel_capture = false; + // Indicates how to downmix multi-channel capture audio to mono (when + // needed). + DownmixMethod capture_downmix_method = DownmixMethod::kAverageChannels; } pipeline; // Enabled the pre-amplifier. It amplifies the capture signal