Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9c47719b authored by Ram Mohan's avatar Ram Mohan
Browse files

Add utils for checking range limits of effect config params

Bug: 272195502
Test: Build

Change-Id: I1ff5af7d25e9410c58f323b6d12dbe6764e40073
parent 45322523
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -41,6 +41,20 @@ cc_library {
    ],
}

cc_library {
    name: "libaudioaidlranges",
    host_supported: true,
    vendor_available: true,
    static_libs: [
        "android.hardware.audio.effect-V1-ndk",
    ],
    export_include_dirs: ["include"],
    header_libs: ["libaudioaidl_headers"],
    srcs: [
        "EffectRangeSpecific.cpp",
    ],
}

cc_test {
    name: "libaudioaidlcommon_test",
    host_supported: true,
+161 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <aidl/android/hardware/audio/effect/DynamicsProcessing.h>
#include <aidl/android/hardware/audio/effect/Range.h>

#include "EffectRangeSpecific.h"
#include "effect-impl/EffectRange.h"

namespace aidl::android::hardware::audio::effect {

namespace DynamicsProcessingRanges {

static bool isInputGainConfigInRange(const std::vector<DynamicsProcessing::InputGain>& cfgs,
                                     const DynamicsProcessing::InputGain& min,
                                     const DynamicsProcessing::InputGain& max) {
    auto func = [](const DynamicsProcessing::InputGain& arg) {
        return std::make_tuple(arg.channel, arg.gainDb);
    };
    return isTupleInRange(cfgs, min, max, func);
}

static bool isLimiterConfigInRange(const std::vector<DynamicsProcessing::LimiterConfig>& cfgs,
                                   const DynamicsProcessing::LimiterConfig& min,
                                   const DynamicsProcessing::LimiterConfig& max) {
    auto func = [](const DynamicsProcessing::LimiterConfig& arg) {
        return std::make_tuple(arg.channel, arg.enable, arg.linkGroup, arg.attackTimeMs,
                               arg.releaseTimeMs, arg.ratio, arg.thresholdDb, arg.postGainDb);
    };
    return isTupleInRange(cfgs, min, max, func);
}

static bool isMbcBandConfigInRange(const std::vector<DynamicsProcessing::MbcBandConfig>& cfgs,
                                   const DynamicsProcessing::MbcBandConfig& min,
                                   const DynamicsProcessing::MbcBandConfig& max) {
    auto func = [](const DynamicsProcessing::MbcBandConfig& arg) {
        return std::make_tuple(arg.channel, arg.band, arg.enable, arg.cutoffFrequencyHz,
                               arg.attackTimeMs, arg.releaseTimeMs, arg.ratio, arg.thresholdDb,
                               arg.kneeWidthDb, arg.noiseGateThresholdDb, arg.expanderRatio,
                               arg.preGainDb, arg.postGainDb);
    };
    return isTupleInRange(cfgs, min, max, func);
}

static bool isEqBandConfigInRange(const std::vector<DynamicsProcessing::EqBandConfig>& cfgs,
                                  const DynamicsProcessing::EqBandConfig& min,
                                  const DynamicsProcessing::EqBandConfig& max) {
    auto func = [](const DynamicsProcessing::EqBandConfig& arg) {
        return std::make_tuple(arg.channel, arg.band, arg.enable, arg.cutoffFrequencyHz,
                               arg.gainDb);
    };
    return isTupleInRange(cfgs, min, max, func);
}

static bool isChannelConfigInRange(const std::vector<DynamicsProcessing::ChannelConfig>& cfgs,
                                   const DynamicsProcessing::ChannelConfig& min,
                                   const DynamicsProcessing::ChannelConfig& max) {
    auto func = [](const DynamicsProcessing::ChannelConfig& arg) {
        return std::make_tuple(arg.channel, arg.enable);
    };
    return isTupleInRange(cfgs, min, max, func);
}

static bool isEngineConfigInRange(const DynamicsProcessing::EngineArchitecture& cfg,
                                  const DynamicsProcessing::EngineArchitecture& min,
                                  const DynamicsProcessing::EngineArchitecture& max) {
    auto func = [](const DynamicsProcessing::EngineArchitecture& arg) {
        return std::make_tuple(arg.resolutionPreference, arg.preferredProcessingDurationMs,
                               arg.preEqStage.inUse, arg.preEqStage.bandCount,
                               arg.postEqStage.inUse, arg.postEqStage.bandCount, arg.mbcStage.inUse,
                               arg.mbcStage.bandCount, arg.limiterInUse);
    };
    return isTupleInRange(func(cfg), func(min), func(max));
}

static int locateMinMaxForTag(DynamicsProcessing::Tag tag,
                              const std::vector<Range::DynamicsProcessingRange>& ranges) {
    for (int i = 0; i < (int)ranges.size(); i++) {
        if (tag == ranges[i].min.getTag() && tag == ranges[i].max.getTag()) {
            return i;
        }
    }
    return -1;
}

bool isParamInRange(const DynamicsProcessing& dp,
                    const std::vector<Range::DynamicsProcessingRange>& ranges) {
    auto tag = dp.getTag();
    int i = locateMinMaxForTag(tag, ranges);
    if (i == -1) return true;

    switch (tag) {
        case DynamicsProcessing::engineArchitecture: {
            return isEngineConfigInRange(
                    dp.get<DynamicsProcessing::engineArchitecture>(),
                    ranges[i].min.get<DynamicsProcessing::engineArchitecture>(),
                    ranges[i].max.get<DynamicsProcessing::engineArchitecture>());
        }
        case DynamicsProcessing::preEq: {
            return isChannelConfigInRange(dp.get<DynamicsProcessing::preEq>(),
                                          ranges[i].min.get<DynamicsProcessing::preEq>()[0],
                                          ranges[i].max.get<DynamicsProcessing::preEq>()[0]);
        }
        case DynamicsProcessing::postEq: {
            return isChannelConfigInRange(dp.get<DynamicsProcessing::postEq>(),
                                          ranges[i].min.get<DynamicsProcessing::postEq>()[0],
                                          ranges[i].max.get<DynamicsProcessing::postEq>()[0]);
        }
        case DynamicsProcessing::mbc: {
            return isChannelConfigInRange(dp.get<DynamicsProcessing::mbc>(),
                                          ranges[i].min.get<DynamicsProcessing::mbc>()[0],
                                          ranges[i].max.get<DynamicsProcessing::mbc>()[0]);
        }
        case DynamicsProcessing::preEqBand: {
            return isEqBandConfigInRange(dp.get<DynamicsProcessing::preEqBand>(),
                                         ranges[i].min.get<DynamicsProcessing::preEqBand>()[0],
                                         ranges[i].max.get<DynamicsProcessing::preEqBand>()[0]);
        }
        case DynamicsProcessing::postEqBand: {
            return isEqBandConfigInRange(dp.get<DynamicsProcessing::postEqBand>(),
                                         ranges[i].min.get<DynamicsProcessing::postEqBand>()[0],
                                         ranges[i].max.get<DynamicsProcessing::postEqBand>()[0]);
        }
        case DynamicsProcessing::mbcBand: {
            return isMbcBandConfigInRange(dp.get<DynamicsProcessing::mbcBand>(),
                                          ranges[i].min.get<DynamicsProcessing::mbcBand>()[0],
                                          ranges[i].max.get<DynamicsProcessing::mbcBand>()[0]);
        }
        case DynamicsProcessing::limiter: {
            return isLimiterConfigInRange(dp.get<DynamicsProcessing::limiter>(),
                                          ranges[i].min.get<DynamicsProcessing::limiter>()[0],
                                          ranges[i].max.get<DynamicsProcessing::limiter>()[0]);
        }
        case DynamicsProcessing::inputGain: {
            return isInputGainConfigInRange(dp.get<DynamicsProcessing::inputGain>(),
                                            ranges[i].min.get<DynamicsProcessing::inputGain>()[0],
                                            ranges[i].max.get<DynamicsProcessing::inputGain>()[0]);
        }
        default: {
            return true;
        }
    }
    return true;
}

}  // namespace DynamicsProcessingRanges

}  // namespace aidl::android::hardware::audio::effect
 No newline at end of file
+28 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

namespace aidl::android::hardware::audio::effect {

namespace DynamicsProcessingRanges {

bool isParamInRange(const DynamicsProcessing& dp,
                    const std::vector<Range::DynamicsProcessingRange>& ranges);

}  // namespace DynamicsProcessingRanges

}  // namespace aidl::android::hardware::audio::effect
 No newline at end of file
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <algorithm>
#include <tuple>
#include <utility>
#include <vector>

namespace aidl::android::hardware::audio::effect {

template <typename T>
bool isInRange(const T& value, const T& low, const T& high) {
    return (value >= low) && (value <= high);
}

template <typename T, std::size_t... Is>
bool isTupleInRange(const T& test, const T& min, const T& max, std::index_sequence<Is...>) {
    return (isInRange(std::get<Is>(test), std::get<Is>(min), std::get<Is>(max)) && ...);
}

template <typename T, std::size_t TupSize = std::tuple_size_v<T>>
bool isTupleInRange(const T& test, const T& min, const T& max) {
    return isTupleInRange(test, min, max, std::make_index_sequence<TupSize>{});
}

template <typename T, typename F>
bool isTupleInRange(const std::vector<T>& cfgs, const T& min, const T& max, const F& func) {
    auto minT = func(min), maxT = func(max);
    return std::all_of(cfgs.cbegin(), cfgs.cend(),
                       [&](const T& cfg) { return isTupleInRange(func(cfg), minT, maxT); });
}

}  // namespace aidl::android::hardware::audio::effect