chromium/third_party/webrtc/modules/audio_processing/agc2/input_volume_controller_unittest.cc

/*
 *  Copyright (c) 2013 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 "modules/audio_processing/agc2/input_volume_controller.h"

#include <algorithm>
#include <fstream>
#include <limits>
#include <string>
#include <vector>

#include "rtc_base/numerics/safe_minmax.h"
#include "rtc_base/strings/string_builder.h"
#include "system_wrappers/include/metrics.h"
#include "test/field_trial.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/testsupport/file_utils.h"

_;
AtLeast;
DoAll;
Return;
SetArgPointee;

namespace webrtc {
namespace {

constexpr int kSampleRateHz =;
constexpr int kNumChannels =;
constexpr int kInitialInputVolume =;
constexpr int kClippedMin =;  // Arbitrary, but different from the default.
constexpr float kAboveClippedThreshold =;
constexpr int kMinMicLevel =;
constexpr int kClippedLevelStep =;
constexpr float kClippedRatioThreshold =;
constexpr int kClippedWaitFrames =;
constexpr float kHighSpeechProbability =;
constexpr float kLowSpeechProbability =;
constexpr float kSpeechLevel =;
constexpr float kSpeechProbabilityThreshold =;
constexpr float kSpeechRatioThreshold =;

constexpr float kMinSample =;
constexpr float kMaxSample =;

ClippingPredictorConfig;

InputVolumeControllerConfig;

constexpr ClippingPredictorConfig kDefaultClippingPredictorConfig{};

std::unique_ptr<InputVolumeController> CreateInputVolumeController(
    int clipped_level_step = kClippedLevelStep,
    float clipped_ratio_threshold = kClippedRatioThreshold,
    int clipped_wait_frames = kClippedWaitFrames,
    bool enable_clipping_predictor = false,
    int update_input_volume_wait_frames = 0) {}

// (Over)writes `samples_value` for the samples in `audio_buffer`.
// When `clipped_ratio`, a value in [0, 1], is greater than 0, the corresponding
// fraction of the frame is set to a full scale value to simulate clipping.
void WriteAudioBufferSamples(float samples_value,
                             float clipped_ratio,
                             AudioBuffer& audio_buffer) {}

// (Over)writes samples in `audio_buffer`. Alternates samples `samples_value`
// and zero.
void WriteAlternatingAudioBufferSamples(float samples_value,
                                        AudioBuffer& audio_buffer) {}

// Reads a given number of 10 ms chunks from a PCM file and feeds them to
// `InputVolumeController`.
class SpeechSamplesReader {};

// Runs the MonoInputVolumeControl processing sequence following the API
// contract. Returns the updated recommended input volume.
float UpdateRecommendedInputVolume(MonoInputVolumeController& mono_controller,
                                   int applied_input_volume,
                                   float speech_probability,
                                   absl::optional<float> rms_error_dbfs) {}

}  // namespace

// TODO(bugs.webrtc.org/12874): Use constexpr struct with designated
// initializers once fixed.
constexpr InputVolumeControllerConfig GetInputVolumeControllerTestConfig() {}

// Helper class that provides an `InputVolumeController` instance with an
// `AudioBuffer` instance and `CallAgcSequence()`, a helper method that runs the
// `InputVolumeController` instance on the `AudioBuffer` one by sticking to the
// API contract.
class InputVolumeControllerTestHelper {};

class InputVolumeControllerChannelSampleRateTest
    : public ::testing::TestWithParam<std::tuple<int, int>> {};

TEST_P(InputVolumeControllerChannelSampleRateTest, CheckIsAlive) {}

INSTANTIATE_TEST_SUITE_P();

class InputVolumeControllerParametrizedTest
    : public ::testing::TestWithParam<int> {};

TEST_P(InputVolumeControllerParametrizedTest,
       StartupMinVolumeConfigurationRespectedWhenAppliedInputVolumeAboveMin) {}

TEST_P(
    InputVolumeControllerParametrizedTest,
    StartupMinVolumeConfigurationRespectedWhenAppliedInputVolumeMaybeBelowMin) {}

TEST_P(InputVolumeControllerParametrizedTest,
       StartupMinVolumeRespectedWhenAppliedVolumeNonZero) {}

TEST_P(InputVolumeControllerParametrizedTest,
       MinVolumeRepeatedlyRespectedWhenAppliedVolumeNonZero) {}

TEST_P(InputVolumeControllerParametrizedTest,
       StartupMinVolumeRespectedOnceWhenAppliedVolumeZero) {}

TEST_P(InputVolumeControllerParametrizedTest, MicVolumeResponseToRmsError) {}

TEST_P(InputVolumeControllerParametrizedTest, MicVolumeIsLimited) {}

TEST_P(InputVolumeControllerParametrizedTest, NoActionWhileMuted) {}

TEST_P(InputVolumeControllerParametrizedTest,
       UnmutingChecksVolumeWithoutRaising) {}

TEST_P(InputVolumeControllerParametrizedTest, UnmutingRaisesTooLowVolume) {}

TEST_P(InputVolumeControllerParametrizedTest,
       ManualLevelChangeResultsInNoSetMicCall) {}

TEST_P(InputVolumeControllerParametrizedTest,
       RecoveryAfterManualLevelChangeFromMax) {}

// Checks that the minimum input volume is enforced during the upward adjustment
// of the input volume.
TEST_P(InputVolumeControllerParametrizedTest,
       EnforceMinInputVolumeDuringUpwardsAdjustment) {}

// Checks that, when the min mic level override is specified, AGC immediately
// applies the minimum mic level after the mic level is manually set below the
// minimum gain to enforce.
TEST_P(InputVolumeControllerParametrizedTest,
       RecoveryAfterManualLevelChangeBelowMin) {}

TEST_P(InputVolumeControllerParametrizedTest, NoClippingHasNoImpact) {}

TEST_P(InputVolumeControllerParametrizedTest,
       ClippingUnderThresholdHasNoImpact) {}

TEST_P(InputVolumeControllerParametrizedTest, ClippingLowersVolume) {}

TEST_P(InputVolumeControllerParametrizedTest,
       WaitingPeriodBetweenClippingChecks) {}

TEST_P(InputVolumeControllerParametrizedTest, ClippingLoweringIsLimited) {}

TEST_P(InputVolumeControllerParametrizedTest,
       ClippingMaxIsRespectedWhenEqualToLevel) {}

TEST_P(InputVolumeControllerParametrizedTest,
       ClippingMaxIsRespectedWhenHigherThanLevel) {}

TEST_P(InputVolumeControllerParametrizedTest, UserCanRaiseVolumeAfterClipping) {}

TEST_P(InputVolumeControllerParametrizedTest,
       ClippingDoesNotPullLowVolumeBackUp) {}

TEST_P(InputVolumeControllerParametrizedTest, TakesNoActionOnZeroMicVolume) {}

TEST_P(InputVolumeControllerParametrizedTest, ClippingDetectionLowersVolume) {}

// TODO(bugs.webrtc.org/12774): Test the bahavior of `clipped_level_step`.
// TODO(bugs.webrtc.org/12774): Test the bahavior of `clipped_ratio_threshold`.
// TODO(bugs.webrtc.org/12774): Test the bahavior of `clipped_wait_frames`.
// Verifies that configurable clipping parameters are initialized as intended.
TEST_P(InputVolumeControllerParametrizedTest, ClippingParametersVerified) {}

TEST_P(InputVolumeControllerParametrizedTest,
       DisableClippingPredictorDisablesClippingPredictor) {}

TEST_P(InputVolumeControllerParametrizedTest,
       EnableClippingPredictorEnablesClippingPredictor) {}

TEST_P(InputVolumeControllerParametrizedTest,
       DisableClippingPredictorDoesNotLowerVolume) {}

// TODO(bugs.webrtc.org/7494): Split into several smaller tests.
TEST_P(InputVolumeControllerParametrizedTest,
       UsedClippingPredictionsProduceLowerAnalogLevels) {}

// Checks that passing an empty speech level has no effect on the input volume.
TEST_P(InputVolumeControllerParametrizedTest, EmptyRmsErrorHasNoEffect) {}

// Checks that the recommended input volume is not updated unless enough
// frames have been processed after the previous update.
TEST(InputVolumeControllerTest, UpdateInputVolumeWaitFramesIsEffective) {}

INSTANTIATE_TEST_SUITE_P();

TEST(InputVolumeControllerTest,
     MinInputVolumeEnforcedWithClippingWhenAboveClippedLevelMin) {}

TEST(InputVolumeControllerTest,
     ClippedlevelMinEnforcedWithClippingWhenAboveMinInputVolume) {}

TEST(InputVolumeControllerTest, SpeechRatioThresholdIsEffective) {}

TEST(InputVolumeControllerTest, SpeechProbabilityThresholdIsEffective) {}

TEST(InputVolumeControllerTest,
     DoNotLogRecommendedInputVolumeOnChangeToMatchTarget) {}

TEST(InputVolumeControllerTest,
     LogRecommendedInputVolumeOnUpwardChangeToMatchTarget) {}

TEST(InputVolumeControllerTest,
     LogRecommendedInputVolumeOnDownwardChangeToMatchTarget) {}

TEST(MonoInputVolumeControllerTest, CheckHandleClippingLowersVolume) {}

TEST(MonoInputVolumeControllerTest,
     CheckProcessNegativeRmsErrorDecreasesInputVolume) {}

TEST(MonoInputVolumeControllerTest,
     CheckProcessPositiveRmsErrorIncreasesInputVolume) {}

TEST(MonoInputVolumeControllerTest,
     CheckProcessNegativeRmsErrorDecreasesInputVolumeWithLimit) {}

TEST(MonoInputVolumeControllerTest,
     CheckProcessPositiveRmsErrorIncreasesInputVolumeWithLimit) {}

TEST(MonoInputVolumeControllerTest,
     CheckProcessRmsErrorDecreasesInputVolumeRepeatedly) {}

TEST(MonoInputVolumeControllerTest,
     CheckProcessPositiveRmsErrorIncreasesInputVolumeRepeatedly) {}

TEST(MonoInputVolumeControllerTest, CheckClippedLevelMinIsEffective) {}

TEST(MonoInputVolumeControllerTest, CheckMinMicLevelIsEffective) {}

TEST(MonoInputVolumeControllerTest,
     CheckUpdateInputVolumeWaitFramesIsEffective) {}

TEST(MonoInputVolumeControllerTest,
     CheckSpeechProbabilityThresholdIsEffective) {}

TEST(MonoInputVolumeControllerTest, CheckSpeechRatioThresholdIsEffective) {}

TEST(MonoInputVolumeControllerTest,
     CheckProcessEmptyRmsErrorDoesNotLowerVolume) {}

}  // namespace webrtc