xref: /aosp_15_r20/frameworks/av/media/libeffects/loudness/aidl/LoudnessEnhancerContext.cpp (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "LoudnessEnhancerContext"
18 
19 #include <Utils.h>
20 
21 #include "LoudnessEnhancerContext.h"
22 
23 namespace aidl::android::hardware::audio::effect {
24 
LoudnessEnhancerContext(int statusDepth,const Parameter::Common & common)25 LoudnessEnhancerContext::LoudnessEnhancerContext(int statusDepth, const Parameter::Common& common)
26     : EffectContext(statusDepth, common) {
27     init_params();
28 }
29 
enable()30 RetCode LoudnessEnhancerContext::enable() {
31     if (mState != LOUDNESS_ENHANCER_STATE_INITIALIZED) {
32         return RetCode::ERROR_EFFECT_LIB_ERROR;
33     }
34     mState = LOUDNESS_ENHANCER_STATE_ACTIVE;
35     return RetCode::SUCCESS;
36 }
37 
disable()38 RetCode LoudnessEnhancerContext::disable() {
39     if (mState != LOUDNESS_ENHANCER_STATE_ACTIVE) {
40         return RetCode::ERROR_EFFECT_LIB_ERROR;
41     }
42     mState = LOUDNESS_ENHANCER_STATE_INITIALIZED;
43     return RetCode::SUCCESS;
44 }
45 
setLeGain(int gainMb)46 RetCode LoudnessEnhancerContext::setLeGain(int gainMb) {
47     float targetAmp = pow(10, gainMb / 2000.0f);  // mB to linear amplification
48     if (mCompressor != nullptr) {
49         // Get samplingRate from input
50         mCompressor->Initialize(targetAmp, mCommon.input.base.sampleRate);
51     }
52     mGain = gainMb;
53     return RetCode::SUCCESS;
54 }
55 
process(float * in,float * out,int samples)56 IEffect::Status LoudnessEnhancerContext::process(float* in, float* out, int samples) {
57     IEffect::Status status = {EX_NULL_POINTER, 0, 0};
58     RETURN_VALUE_IF(!in, status, "nullInput");
59     RETURN_VALUE_IF(!out, status, "nullOutput");
60     status = {EX_ILLEGAL_STATE, 0, 0};
61     RETURN_VALUE_IF(getInputFrameSize() != getOutputFrameSize(), status, "FrameSizeMismatch");
62     auto frameSize = getInputFrameSize();
63     RETURN_VALUE_IF(0 == frameSize, status, "zeroFrameSize");
64 
65     status = {STATUS_INVALID_OPERATION, 0, 0};
66     RETURN_VALUE_IF(mState != LOUDNESS_ENHANCER_STATE_ACTIVE, status, "stateNotActive");
67 
68     // PcmType is always expected to be Float 32 bit.
69     constexpr float scale = 1 << 15;  // power of 2 is lossless conversion to int16_t range
70     constexpr float inverseScale = 1.f / scale;
71     const float inputAmp = pow(10, mGain / 2000.0f) * scale;
72     float leftSample, rightSample;
73 
74     if (mCompressor != nullptr) {
75         for (int inIdx = 0; inIdx < samples; inIdx += 2) {
76             // makeup gain is applied on the input of the compressor
77             leftSample = inputAmp * in[inIdx];
78             rightSample = inputAmp * in[inIdx + 1];
79             mCompressor->Compress(&leftSample, &rightSample);
80             in[inIdx] = leftSample * inverseScale;
81             in[inIdx + 1] = rightSample * inverseScale;
82         }
83     } else {
84         for (int inIdx = 0; inIdx < samples; inIdx += 2) {
85             leftSample = inputAmp * in[inIdx];
86             rightSample = inputAmp * in[inIdx + 1];
87             in[inIdx] = leftSample * inverseScale;
88             in[inIdx + 1] = rightSample * inverseScale;
89         }
90     }
91     bool accumulate = false;
92     if (in != out) {
93         for (int i = 0; i < samples; i++) {
94             if (accumulate) {
95                 out[i] += in[i];
96             } else {
97                 out[i] = in[i];
98             }
99         }
100     }
101     return {STATUS_OK, samples, samples};
102 }
103 
init_params()104 void LoudnessEnhancerContext::init_params() {
105     int channelCount = ::aidl::android::hardware::audio::common::getChannelCount(
106             mCommon.input.base.channelMask);
107     LOG_ALWAYS_FATAL_IF(channelCount != 2, "channel count %d not supported", channelCount);
108 
109     mGain = LOUDNESS_ENHANCER_DEFAULT_TARGET_GAIN_MB;
110     float targetAmp = pow(10, mGain / 2000.0f);  // mB to linear amplification
111     LOG(VERBOSE) << __func__ << "Target gain = " << mGain << "mB <=> factor = " << targetAmp;
112 
113     mCompressor = std::make_unique<le_fx::AdaptiveDynamicRangeCompression>();
114     mCompressor->Initialize(targetAmp, mCommon.input.base.sampleRate);
115     mState = LOUDNESS_ENHANCER_STATE_INITIALIZED;
116 }
117 
118 }  // namespace aidl::android::hardware::audio::effect
119