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

Commit 63dc1bed authored by Treehugger Robot's avatar Treehugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Libeffects : Refactor bundle and add missing functionalities" am:...

Merge "Libeffects : Refactor bundle and add missing functionalities" am: 7b7472f6 am: 6f66a3bd am: 52d1c9f7

Original change: https://android-review.googlesource.com/c/platform/frameworks/av/+/2337405



Change-Id: I4c14151efa162fe50a35b1e6f0d3ba397632a53d
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents 1b5fbab4 52d1c9f7
Loading
Loading
Loading
Loading
+201 −20
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@

#include "BundleContext.h"
#include "BundleTypes.h"
#include "math.h"

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

@@ -67,38 +68,151 @@ void BundleContext::deInit() {
}

RetCode BundleContext::enable() {
    if (mEnabled) return RetCode::ERROR_ILLEGAL_PARAMETER;
    switch (mType) {
        case lvm::BundleEffectType::EQUALIZER:
            LOG(DEBUG) << __func__ << " enable bundle EQ";
            if (mSamplesToExitCountEq <= 0) mNumberEffectsEnabled++;
            mSamplesToExitCountEq = (mSamplesPerSecond * 0.1);
            mEffectInDrain &= ~(1 << int(lvm::BundleEffectType::EQUALIZER));
            break;
        default:
            // Add handling for other effects
            break;
    }
    mEnabled = true;
    return enableOperatingMode();
}

RetCode BundleContext::enableOperatingMode() {
    LVM_ControlParams_t params;
    {
        std::lock_guard lg(mMutex);
        RETURN_VALUE_IF(LVM_SUCCESS != LVM_GetControlParameters(mInstance, &params),
                        RetCode::ERROR_EFFECT_LIB_ERROR, "failGetControlParams");
        if (mType == lvm::BundleEffectType::EQUALIZER) {
        switch (mType) {
            case lvm::BundleEffectType::EQUALIZER:
                LOG(DEBUG) << __func__ << " enable bundle EQ";
                params.EQNB_OperatingMode = LVM_EQNB_ON;
                break;
            default:
                // Add handling for other effects
                break;
        }
        RETURN_VALUE_IF(LVM_SUCCESS != LVM_SetControlParameters(mInstance, &params),
                        RetCode::ERROR_EFFECT_LIB_ERROR, "failSetControlParams");
    }
    mEnabled = true;
    // LvmEffect_limitLevel(pContext);
    return RetCode::SUCCESS;
    return limitLevel();
}

RetCode BundleContext::disable() {
    if (!mEnabled) return RetCode::ERROR_ILLEGAL_PARAMETER;
    switch (mType) {
        case lvm::BundleEffectType::EQUALIZER:
            LOG(DEBUG) << __func__ << " disable bundle EQ";
            mEffectInDrain |= 1 << int(lvm::BundleEffectType::EQUALIZER);
            break;
        default:
            // Add handling for other effects
            break;
    }
    mEnabled = false;
    return disableOperatingMode();
}

RetCode BundleContext::disableOperatingMode() {
    LVM_ControlParams_t params;
    {
        std::lock_guard lg(mMutex);
        RETURN_VALUE_IF(LVM_SUCCESS != LVM_GetControlParameters(mInstance, &params),
                        RetCode::ERROR_EFFECT_LIB_ERROR, "failGetControlParams");
        if (mType == lvm::BundleEffectType::EQUALIZER) {
        switch (mType) {
            case lvm::BundleEffectType::EQUALIZER:
                LOG(DEBUG) << __func__ << " disable bundle EQ";
                params.EQNB_OperatingMode = LVM_EQNB_OFF;
                break;
            default:
                // Add handling for other effects
                break;
        }
        RETURN_VALUE_IF(LVM_SUCCESS != LVM_SetControlParameters(mInstance, &params),
                        RetCode::ERROR_EFFECT_LIB_ERROR, "failSetControlParams");
    }
    mEnabled = false;
    // LvmEffect_limitLevel(pContext);
    return limitLevel();
}

RetCode BundleContext::limitLevel() {
    int gainCorrection = 0;
    // Count the energy contribution per band for EQ and BassBoost only if they are active.
    float energyContribution = 0;
    float energyCross = 0;
    float energyBassBoost = 0;
    float crossCorrection = 0;
    LVM_ControlParams_t params;
    {
        std::lock_guard lg(mMutex);
        RETURN_VALUE_IF(LVM_SUCCESS != LVM_GetControlParameters(mInstance, &params),
                        RetCode::ERROR_EFFECT_LIB_ERROR, " getControlParamFailed");

        bool eqEnabled = params.EQNB_OperatingMode == LVM_EQNB_ON;

        if (eqEnabled) {
            for (int i = 0; i < lvm::MAX_NUM_BANDS; i++) {
                float bandFactor = mBandGaindB[i] / 15.0;
                float bandCoefficient = lvm::kBandEnergyCoefficient[i];
                float bandEnergy = bandFactor * bandCoefficient * bandCoefficient;
                if (bandEnergy > 0) energyContribution += bandEnergy;
            }

            // cross EQ coefficients
            float bandFactorSum = 0;
            for (int i = 0; i < lvm::MAX_NUM_BANDS - 1; i++) {
                float bandFactor1 = mBandGaindB[i] / 15.0;
                float bandFactor2 = mBandGaindB[i + 1] / 15.0;

                if (bandFactor1 > 0 && bandFactor2 > 0) {
                    float crossEnergy =
                            bandFactor1 * bandFactor2 * lvm::kBandEnergyCrossCoefficient[i];
                    bandFactorSum += bandFactor1 * bandFactor2;

                    if (crossEnergy > 0) energyCross += crossEnergy;
                }
            }
            bandFactorSum -= 1.0;
            if (bandFactorSum > 0) crossCorrection = bandFactorSum * 0.7;
        }

        double totalEnergyEstimation =
                sqrt(energyContribution + energyCross + energyBassBoost) - crossCorrection;
        LOG(INFO) << " TOTAL energy estimation: " << totalEnergyEstimation << " dB";

        // roundoff
        int maxLevelRound = (int)(totalEnergyEstimation + 0.99);
        if (maxLevelRound + mLevelSaved > 0) {
            gainCorrection = maxLevelRound + mLevelSaved;
        }

        params.VC_EffectLevel = mLevelSaved - gainCorrection;
        if (params.VC_EffectLevel < -96) {
            params.VC_EffectLevel = -96;
        }
        LOG(INFO) << "\tVol: " << mLevelSaved << ", GainCorrection: " << gainCorrection
                  << ", Actual vol: " << params.VC_EffectLevel;

        /* Activate the initial settings */
        RETURN_VALUE_IF(LVM_SUCCESS != LVM_SetControlParameters(mInstance, &params),
                        RetCode::ERROR_EFFECT_LIB_ERROR, " setControlParamFailed");

        if (mFirstVolume) {
            RETURN_VALUE_IF(LVM_SUCCESS != LVM_SetVolumeNoSmoothing(mInstance, &params),
                            RetCode::ERROR_EFFECT_LIB_ERROR, " setVolumeNoSmoothingFailed");
            LOG(INFO) << "\tLVM_VOLUME: Disabling Smoothing for first volume change to remove "
                         "spikes/clicks";
            mFirstVolume = false;
        }
    }

    return RetCode::SUCCESS;
}

@@ -339,22 +453,89 @@ LVM_HeadroomBandDef_t *BundleContext::getDefaultEqualizerHeadroomBanDefs() {

IEffect::Status BundleContext::lvmProcess(float* in, float* out, int samples) {
    IEffect::Status status = {EX_NULL_POINTER, 0, 0};
    RETURN_VALUE_IF(!in, status, "nullInput");
    RETURN_VALUE_IF(!out, status, "nullOutput");
    status = {EX_ILLEGAL_STATE, 0, 0};
    int64_t inputFrameCount = getCommon().input.frameCount;
    int64_t outputFrameCount = getCommon().output.frameCount;
    RETURN_VALUE_IF(inputFrameCount != outputFrameCount, status, "FrameCountMismatch");
    int isDataAvailable = true;

    auto frameSize = getInputFrameSize();
    RETURN_VALUE_IF(0== frameSize, status, "nullContext");
    RETURN_VALUE_IF(0 == frameSize, status, "zeroFrameSize");

    LOG(DEBUG) << __func__ << " start processing";
    if ((mEffectProcessCalled & 1 << int(mType)) != 0) {
        const int undrainedEffects = mEffectInDrain & ~mEffectProcessCalled;
        if ((undrainedEffects & 1 << int(lvm::BundleEffectType::EQUALIZER)) != 0) {
            LOG(DEBUG) << "Draining EQUALIZER";
            mSamplesToExitCountEq = 0;
            --mNumberEffectsEnabled;
            mEffectInDrain &= ~(1 << int(lvm::BundleEffectType::EQUALIZER));
        }
    }
    mEffectProcessCalled |= 1 << int(mType);
    if (!mEnabled) {
        switch (mType) {
            case lvm::BundleEffectType::EQUALIZER:
                if (mSamplesToExitCountEq > 0) {
                    mSamplesToExitCountEq -= samples;
                }
                if (mSamplesToExitCountEq <= 0) {
                    isDataAvailable = false;
                    if ((mEffectInDrain & 1 << int(lvm::BundleEffectType::EQUALIZER)) != 0) {
                        mNumberEffectsEnabled--;
                        mEffectInDrain &= ~(1 << int(lvm::BundleEffectType::EQUALIZER));
                    }
                    LOG(DEBUG) << "Effect_process() this is the last frame for EQUALIZER";
                }
                break;
            default:
                // Add handling for other effects
                break;
        }
    }
    if (isDataAvailable) {
        mNumberEffectsCalled++;
    }
    bool accumulate = false;
    if (mNumberEffectsCalled >= mNumberEffectsEnabled) {
        // We expect the # effects called to be equal to # effects enabled in sequence (including
        // draining effects).  Warn if this is not the case due to inconsistent calls.
        ALOGW_IF(mNumberEffectsCalled > mNumberEffectsEnabled,
                 "%s Number of effects called %d is greater than number of effects enabled %d",
                 __func__, mNumberEffectsCalled, mNumberEffectsEnabled);
        mEffectProcessCalled = 0;  // reset our consistency check.
        if (!isDataAvailable) {
            LOG(DEBUG) << "Effect_process() processing last frame";
        }
        mNumberEffectsCalled = 0;
        LVM_UINT16 frames = samples * sizeof(float) / frameSize;
        float* outTmp = (accumulate ? getWorkBuffer() : out);
        /* Process the samples */
        LVM_ReturnStatus_en lvmStatus;
        {
            std::lock_guard lg(mMutex);
        lvmStatus = LVM_Process(mInstance, in, out, frames, 0);
    }

            lvmStatus = LVM_Process(mInstance, in, outTmp, frames, 0);
            if (lvmStatus != LVM_SUCCESS) {
                LOG(ERROR) << __func__ << lvmStatus;
                return {EX_UNSUPPORTED_OPERATION, 0, 0};
            }
            if (accumulate) {
                for (int i = 0; i < samples; i++) {
                    out[i] += outTmp[i];
                }
            }
        }
    } else {
        for (int i = 0; i < samples; i++) {
            if (accumulate) {
                out[i] += in[i];
            } else {
                out[i] = in[i];
            }
        }
    }
    LOG(DEBUG) << __func__ << " done processing";
    return {STATUS_OK, samples, samples};
}
+5 −0
Original line number Diff line number Diff line
@@ -43,7 +43,9 @@ class BundleContext final : public EffectContext {
    lvm::BundleEffectType getBundleType() const { return mType; }

    RetCode enable();
    RetCode enableOperatingMode();
    RetCode disable();
    RetCode disableOperatingMode();

    void setSampleRate (const int sampleRate) { mSampleRate = sampleRate; }
    int getSampleRate() const { return mSampleRate; }
@@ -65,6 +67,8 @@ class BundleContext final : public EffectContext {

    IEffect::Status lvmProcess(float* in, float* out, int samples);

    IEffect::Status processEffect(float* in, float* out, int sampleToProcess);

  private:
    std::mutex mMutex;
    const lvm::BundleEffectType mType;
@@ -106,6 +110,7 @@ class BundleContext final : public EffectContext {

    void initControlParameter(LVM_ControlParams_t& params) const;
    void initHeadroomParameter(LVM_HeadroomParams_t& params) const;
    RetCode limitLevel();
    int16_t VolToDb(uint32_t vol) const;
    LVM_INT16 LVC_ToDB_s32Tos16(LVM_INT32 Lin_fix) const;
    RetCode updateControlParameter(const std::vector<Equalizer::BandLevel>& bandLevels);
+3 −1
Original line number Diff line number Diff line
@@ -70,6 +70,8 @@ static const std::vector<Equalizer::Preset> kEqPresets = {
static const Equalizer::Capability kEqCap = {.bandFrequencies = kEqBandFrequency,
                                             .presets = kEqPresets};

static const std::string kEqualizerEffectName = "EqualizerBundle";

static const Descriptor kEqualizerDesc = {
        .common = {.id = {.type = kEqualizerTypeUUID,
                          .uuid = kEqualizerBundleImplUUID,
@@ -77,7 +79,7 @@ static const Descriptor kEqualizerDesc = {
                   .flags = {.type = Flags::Type::INSERT,
                             .insert = Flags::Insert::FIRST,
                             .volume = Flags::Volume::CTRL},
                   .name = "EqualizerBundle",
                   .name = kEqualizerEffectName,
                   .implementor = "NXP Software Ltd."},
        .capability = Capability::make<Capability::equalizer>(kEqCap)};

+34 −17
Original line number Diff line number Diff line
@@ -31,14 +31,18 @@

using aidl::android::hardware::audio::effect::Descriptor;
using aidl::android::hardware::audio::effect::EffectBundleAidl;
using aidl::android::hardware::audio::effect::kEqualizerBundleImplUUID;
using aidl::android::hardware::audio::effect::IEffect;
using aidl::android::hardware::audio::effect::kEqualizerBundleImplUUID;
using aidl::android::hardware::audio::effect::State;
using aidl::android::media::audio::common::AudioUuid;

bool isUuidSupported(const AudioUuid* uuid) {
    return *uuid == kEqualizerBundleImplUUID;
}

extern "C" binder_exception_t createEffect(const AudioUuid* uuid,
                                           std::shared_ptr<IEffect>* instanceSpp) {
    if (uuid == nullptr || *uuid != kEqualizerBundleImplUUID) {
    if (uuid == nullptr || !isUuidSupported(uuid)) {
        LOG(ERROR) << __func__ << "uuid not supported";
        return EX_ILLEGAL_ARGUMENT;
    }
@@ -68,6 +72,7 @@ EffectBundleAidl::EffectBundleAidl(const AudioUuid& uuid) {
    if (uuid == kEqualizerBundleImplUUID) {
        mType = lvm::BundleEffectType::EQUALIZER;
        mDescriptor = &lvm::kEqualizerDesc;
        mEffectName = &lvm::kEqualizerEffectName;
    } else {
        // TODO: add other bundle effect types here.
        LOG(ERROR) << __func__ << uuid.toString() << " not supported yet!";
@@ -124,52 +129,62 @@ ndk::ScopedAStatus EffectBundleAidl::setParameterCommon(const Parameter& param)

ndk::ScopedAStatus EffectBundleAidl::setParameterSpecific(const Parameter::Specific& specific) {
    LOG(DEBUG) << __func__ << " specific " << specific.toString();
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");

    auto tag = specific.getTag();
    RETURN_IF(tag != Parameter::Specific::equalizer, EX_ILLEGAL_ARGUMENT,
    switch (tag) {
        case Parameter::Specific::equalizer:
            return setParameterEqualizer(specific);
        default:
            LOG(ERROR) << __func__ << " unsupported tag " << toString(tag);
            return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
                                                                    "specificParamNotSupported");
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
    }
}

ndk::ScopedAStatus EffectBundleAidl::setParameterEqualizer(const Parameter::Specific& specific) {
    auto& eq = specific.get<Parameter::Specific::equalizer>();
    auto eqTag = eq.getTag();
    switch (eqTag) {
        case Equalizer::preset:
            RETURN_IF(mContext->setEqualizerPreset(eq.get<Equalizer::preset>()) != RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "setBandLevelsFailed");
            break;
            return ndk::ScopedAStatus::ok();
        case Equalizer::bandLevels:
            RETURN_IF(mContext->setEqualizerBandLevels(eq.get<Equalizer::bandLevels>()) !=
                              RetCode::SUCCESS,
                      EX_ILLEGAL_ARGUMENT, "setBandLevelsFailed");
            break;
            return ndk::ScopedAStatus::ok();
        default:
            LOG(ERROR) << __func__ << " unsupported parameter " << specific.toString();
            return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
                                                                    "eqTagNotSupported");
    }
    return ndk::ScopedAStatus::ok();
}

ndk::ScopedAStatus EffectBundleAidl::getParameterSpecific(const Parameter::Id& id,
                                                          Parameter::Specific* specific) {
    RETURN_IF(!specific, EX_NULL_POINTER, "nullPtr");
    auto tag = id.getTag();
    RETURN_IF(Parameter::Id::equalizerTag != tag, EX_ILLEGAL_ARGUMENT, "wrongIdTag");
    auto eqId = id.get<Parameter::Id::equalizerTag>();
    auto eqIdTag = eqId.getTag();
    switch (eqIdTag) {
        case Equalizer::Id::commonTag:
            return getParameterEqualizer(eqId.get<Equalizer::Id::commonTag>(), specific);

    switch (tag) {
        case Parameter::Id::equalizerTag:
            return getParameterEqualizer(id.get<Parameter::Id::equalizerTag>(), specific);
        default:
            LOG(ERROR) << __func__ << " tag " << toString(eqIdTag) << " not supported";
            LOG(ERROR) << __func__ << " unsupported tag: " << toString(tag);
            return ndk::ScopedAStatus::fromExceptionCodeWithMessage(EX_ILLEGAL_ARGUMENT,
                                                                    "EqualizerTagNotSupported");
                                                                    "wrongIdTag");
    }
}

ndk::ScopedAStatus EffectBundleAidl::getParameterEqualizer(const Equalizer::Tag& tag,
ndk::ScopedAStatus EffectBundleAidl::getParameterEqualizer(const Equalizer::Id& id,
                                                           Parameter::Specific* specific) {
    RETURN_IF(id.getTag() != Equalizer::Id::commonTag, EX_ILLEGAL_ARGUMENT,
              "EqualizerTagNotSupported");
    RETURN_IF(!mContext, EX_NULL_POINTER, "nullContext");
    Equalizer eqParam;

    auto tag = id.get<Equalizer::Id::commonTag>();
    switch (tag) {
        case Equalizer::bandLevels: {
            eqParam.set<Equalizer::bandLevels>(mContext->getEqualizerBandLevels());
@@ -237,6 +252,8 @@ ndk::ScopedAStatus EffectBundleAidl::commandImpl(CommandId command) {

// Processing method running in EffectWorker thread.
IEffect::Status EffectBundleAidl::effectProcessImpl(float* in, float* out, int sampleToProcess) {
    IEffect::Status status = {EX_NULL_POINTER, 0, 0};
    RETURN_VALUE_IF(!mContext, status, "nullContext");
    return mContext->lvmProcess(in, out, sampleToProcess);
}

+5 −2
Original line number Diff line number Diff line
@@ -50,15 +50,18 @@ class EffectBundleAidl final : public EffectImpl {

    ndk::ScopedAStatus commandImpl(CommandId command) override;

    std::string getEffectName() override { return "EqualizerBundle"; }
    std::string getEffectName() override { return *mEffectName; }

  private:
    std::shared_ptr<BundleContext> mContext;
    const Descriptor* mDescriptor;
    const std::string* mEffectName;
    lvm::BundleEffectType mType = lvm::BundleEffectType::EQUALIZER;

    IEffect::Status status(binder_status_t status, size_t consumed, size_t produced);
    ndk::ScopedAStatus getParameterEqualizer(const Equalizer::Tag& tag,

    ndk::ScopedAStatus setParameterEqualizer(const Parameter::Specific& specific);
    ndk::ScopedAStatus getParameterEqualizer(const Equalizer::Id& id,
                                             Parameter::Specific* specific);
};