xref: /aosp_15_r20/frameworks/wilhelm/src/itf/IAutomaticGainControl.cpp (revision bebae9c0e76121f8312ccb50385c080b3a0b023c)
1*bebae9c0SAndroid Build Coastguard Worker /*
2*bebae9c0SAndroid Build Coastguard Worker  * Copyright (C) 2014 The Android Open Source Project
3*bebae9c0SAndroid Build Coastguard Worker  *
4*bebae9c0SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*bebae9c0SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*bebae9c0SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*bebae9c0SAndroid Build Coastguard Worker  *
8*bebae9c0SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*bebae9c0SAndroid Build Coastguard Worker  *
10*bebae9c0SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*bebae9c0SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*bebae9c0SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*bebae9c0SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*bebae9c0SAndroid Build Coastguard Worker  * limitations under the License.
15*bebae9c0SAndroid Build Coastguard Worker  */
16*bebae9c0SAndroid Build Coastguard Worker 
17*bebae9c0SAndroid Build Coastguard Worker /* Automatic Gain Control implementation */
18*bebae9c0SAndroid Build Coastguard Worker #include "sles_allinclusive.h"
19*bebae9c0SAndroid Build Coastguard Worker 
20*bebae9c0SAndroid Build Coastguard Worker #include <system/audio_effects/effect_agc.h>
21*bebae9c0SAndroid Build Coastguard Worker 
22*bebae9c0SAndroid Build Coastguard Worker /**
23*bebae9c0SAndroid Build Coastguard Worker  * returns true if this interface is not associated with an initialized AGC effect
24*bebae9c0SAndroid Build Coastguard Worker  */
NO_AUTOGAIN(IAndroidAutomaticGainControl * v)25*bebae9c0SAndroid Build Coastguard Worker static inline bool NO_AUTOGAIN(IAndroidAutomaticGainControl* v) {
26*bebae9c0SAndroid Build Coastguard Worker     return (v->mAGCEffect == 0);
27*bebae9c0SAndroid Build Coastguard Worker }
28*bebae9c0SAndroid Build Coastguard Worker 
IAndroidAutomaticGainControl_SetEnabled(SLAndroidAutomaticGainControlItf self,SLboolean enabled)29*bebae9c0SAndroid Build Coastguard Worker SLresult IAndroidAutomaticGainControl_SetEnabled(SLAndroidAutomaticGainControlItf self,
30*bebae9c0SAndroid Build Coastguard Worker                                                  SLboolean enabled)
31*bebae9c0SAndroid Build Coastguard Worker {
32*bebae9c0SAndroid Build Coastguard Worker     SL_ENTER_INTERFACE
33*bebae9c0SAndroid Build Coastguard Worker 
34*bebae9c0SAndroid Build Coastguard Worker     IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
35*bebae9c0SAndroid Build Coastguard Worker     interface_lock_exclusive(thiz);
36*bebae9c0SAndroid Build Coastguard Worker     thiz->mEnabled = (SLboolean) enabled;
37*bebae9c0SAndroid Build Coastguard Worker     if (NO_AUTOGAIN(thiz)) {
38*bebae9c0SAndroid Build Coastguard Worker         result = SL_RESULT_CONTROL_LOST;
39*bebae9c0SAndroid Build Coastguard Worker     } else {
40*bebae9c0SAndroid Build Coastguard Worker         android::status_t status = thiz->mAGCEffect->setEnabled((bool) thiz->mEnabled);
41*bebae9c0SAndroid Build Coastguard Worker         result = android_fx_statusToResult(status);
42*bebae9c0SAndroid Build Coastguard Worker     }
43*bebae9c0SAndroid Build Coastguard Worker     interface_unlock_exclusive(thiz);
44*bebae9c0SAndroid Build Coastguard Worker 
45*bebae9c0SAndroid Build Coastguard Worker     SL_LEAVE_INTERFACE
46*bebae9c0SAndroid Build Coastguard Worker }
47*bebae9c0SAndroid Build Coastguard Worker 
IAndroidAutomaticGainControl_IsEnabled(SLAndroidAutomaticGainControlItf self,SLboolean * pEnabled)48*bebae9c0SAndroid Build Coastguard Worker SLresult IAndroidAutomaticGainControl_IsEnabled(SLAndroidAutomaticGainControlItf self,
49*bebae9c0SAndroid Build Coastguard Worker                                                 SLboolean *pEnabled)
50*bebae9c0SAndroid Build Coastguard Worker {
51*bebae9c0SAndroid Build Coastguard Worker     SL_ENTER_INTERFACE
52*bebae9c0SAndroid Build Coastguard Worker 
53*bebae9c0SAndroid Build Coastguard Worker     if (NULL == pEnabled) {
54*bebae9c0SAndroid Build Coastguard Worker         result = SL_RESULT_PARAMETER_INVALID;
55*bebae9c0SAndroid Build Coastguard Worker     } else {
56*bebae9c0SAndroid Build Coastguard Worker         IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
57*bebae9c0SAndroid Build Coastguard Worker         interface_lock_exclusive(thiz);
58*bebae9c0SAndroid Build Coastguard Worker         if (NO_AUTOGAIN(thiz)) {
59*bebae9c0SAndroid Build Coastguard Worker             result = SL_RESULT_CONTROL_LOST;
60*bebae9c0SAndroid Build Coastguard Worker         } else {
61*bebae9c0SAndroid Build Coastguard Worker             *pEnabled = (SLboolean) thiz->mAGCEffect->getEnabled();
62*bebae9c0SAndroid Build Coastguard Worker             result = SL_RESULT_SUCCESS;
63*bebae9c0SAndroid Build Coastguard Worker         }
64*bebae9c0SAndroid Build Coastguard Worker         interface_unlock_exclusive(thiz);
65*bebae9c0SAndroid Build Coastguard Worker     }
66*bebae9c0SAndroid Build Coastguard Worker     SL_LEAVE_INTERFACE
67*bebae9c0SAndroid Build Coastguard Worker }
68*bebae9c0SAndroid Build Coastguard Worker 
69*bebae9c0SAndroid Build Coastguard Worker static const struct SLAndroidAutomaticGainControlItf_ IAndroidAutomaticGainControl_Itf = {
70*bebae9c0SAndroid Build Coastguard Worker     IAndroidAutomaticGainControl_SetEnabled,
71*bebae9c0SAndroid Build Coastguard Worker     IAndroidAutomaticGainControl_IsEnabled
72*bebae9c0SAndroid Build Coastguard Worker };
73*bebae9c0SAndroid Build Coastguard Worker 
IAndroidAutomaticGainControl_init(void * self)74*bebae9c0SAndroid Build Coastguard Worker void IAndroidAutomaticGainControl_init(void *self)
75*bebae9c0SAndroid Build Coastguard Worker {
76*bebae9c0SAndroid Build Coastguard Worker     IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
77*bebae9c0SAndroid Build Coastguard Worker     thiz->mItf = &IAndroidAutomaticGainControl_Itf;
78*bebae9c0SAndroid Build Coastguard Worker     thiz->mEnabled = SL_BOOLEAN_FALSE;
79*bebae9c0SAndroid Build Coastguard Worker     memset(&thiz->mAGCDescriptor, 0, sizeof(effect_descriptor_t));
80*bebae9c0SAndroid Build Coastguard Worker     // placement new (explicit constructor)
81*bebae9c0SAndroid Build Coastguard Worker     (void) new (&thiz->mAGCEffect) android::sp<android::AudioEffect>();
82*bebae9c0SAndroid Build Coastguard Worker }
83*bebae9c0SAndroid Build Coastguard Worker 
IAndroidAutomaticGainControl_deinit(void * self)84*bebae9c0SAndroid Build Coastguard Worker void IAndroidAutomaticGainControl_deinit(void *self)
85*bebae9c0SAndroid Build Coastguard Worker {
86*bebae9c0SAndroid Build Coastguard Worker     IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
87*bebae9c0SAndroid Build Coastguard Worker     // explicit destructor
88*bebae9c0SAndroid Build Coastguard Worker     thiz->mAGCEffect.~sp();
89*bebae9c0SAndroid Build Coastguard Worker }
90*bebae9c0SAndroid Build Coastguard Worker 
IAndroidAutomaticGainControl_Expose(void * self)91*bebae9c0SAndroid Build Coastguard Worker bool IAndroidAutomaticGainControl_Expose(void *self)
92*bebae9c0SAndroid Build Coastguard Worker {
93*bebae9c0SAndroid Build Coastguard Worker     IAndroidAutomaticGainControl *thiz = (IAndroidAutomaticGainControl *) self;
94*bebae9c0SAndroid Build Coastguard Worker     if (!android_fx_initEffectDescriptor(SL_IID_ANDROIDAUTOMATICGAINCONTROL,
95*bebae9c0SAndroid Build Coastguard Worker                                          &thiz->mAGCDescriptor)) {
96*bebae9c0SAndroid Build Coastguard Worker         SL_LOGE("Automatic Gain Control initialization failed.");
97*bebae9c0SAndroid Build Coastguard Worker         return false;
98*bebae9c0SAndroid Build Coastguard Worker     }
99*bebae9c0SAndroid Build Coastguard Worker     return true;
100*bebae9c0SAndroid Build Coastguard Worker }
101