• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
reset()46 RetCode LoudnessEnhancerContext::reset() {
47     const float targetAmp = pow(10, mGain / 2000.0f);  // mB to linear amplification
48     LOG(VERBOSE) << __func__ << "Target gain = " << mGain << "mB <=> factor = " << targetAmp;
49     mCompressor->Initialize(targetAmp, mCommon.input.base.sampleRate);
50     return RetCode::SUCCESS;
51 }
52 
setLeGain(int gainMb)53 RetCode LoudnessEnhancerContext::setLeGain(int gainMb) {
54     if (mCompressor != nullptr) {
55         const float targetAmp = pow(10, gainMb / 2000.f);  // mB to linear amplification
56         mCompressor->set_target_gain(targetAmp);
57     }
58     mGain = gainMb;
59     return RetCode::SUCCESS;
60 }
61 
process(float * in,float * out,int samples)62 IEffect::Status LoudnessEnhancerContext::process(float* in, float* out, int samples) {
63     IEffect::Status status = {EX_NULL_POINTER, 0, 0};
64     RETURN_VALUE_IF(!in, status, "nullInput");
65     RETURN_VALUE_IF(!out, status, "nullOutput");
66     status = {EX_ILLEGAL_STATE, 0, 0};
67     RETURN_VALUE_IF(getInputFrameSize() != getOutputFrameSize(), status, "FrameSizeMismatch");
68     auto frameSize = getInputFrameSize();
69     RETURN_VALUE_IF(0 == frameSize, status, "zeroFrameSize");
70 
71     status = {STATUS_INVALID_OPERATION, 0, 0};
72     RETURN_VALUE_IF(mState != LOUDNESS_ENHANCER_STATE_ACTIVE, status, "stateNotActive");
73 
74     // PcmType is always expected to be Float 32 bit.
75     constexpr float scale = 1 << 15;  // power of 2 is lossless conversion to int16_t range
76     constexpr float inverseScale = 1.f / scale;
77     const float inputAmp = pow(10, mGain / 2000.0f) * scale;
78     if (mCompressor != nullptr) {
79         const size_t channelCount = aidl::android::hardware::audio::common::getChannelCount(
80                 mCommon.input.base.channelMask);
81         const size_t frameCount = samples / channelCount;
82         mCompressor->Compress(channelCount, inputAmp, inverseScale, in, frameCount);
83     }
84     if (in != out) {
85         // nit: update Compress() to write to out.
86         memcpy(out, in, samples * sizeof(float));
87     }
88     return {STATUS_OK, samples, samples};
89 }
90 
init_params()91 void LoudnessEnhancerContext::init_params() {
92     mGain = LOUDNESS_ENHANCER_DEFAULT_TARGET_GAIN_MB;
93     float targetAmp = pow(10, mGain / 2000.0f);  // mB to linear amplification
94     LOG(VERBOSE) << __func__ << "Target gain = " << mGain << "mB <=> factor = " << targetAmp;
95 
96     mCompressor = std::make_unique<le_fx::AdaptiveDynamicRangeCompression>();
97     mCompressor->Initialize(targetAmp, mCommon.input.base.sampleRate);
98     mState = LOUDNESS_ENHANCER_STATE_INITIALIZED;
99 }
100 
101 }  // namespace aidl::android::hardware::audio::effect
102