1 /*
2  * Copyright (C) 2014 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 /* Automatic Gain Control implementation */
18 #include "sles_allinclusive.h"
19 
20 #include <media/EffectsFactoryApi.h>
21 
22 #include <audio_effects/effect_ns.h>
23 /**
24  * returns true if this interface is not associated with an initialized Noise Suppression effect
25  */
NO_NOISESUPPRESS(IAndroidNoiseSuppression * v)26 static inline bool NO_NOISESUPPRESS(IAndroidNoiseSuppression* v) {
27     return (v->mNSEffect == 0);
28 }
29 
IAndroidNoiseSuppression_SetEnabled(SLAndroidNoiseSuppressionItf self,SLboolean enabled)30 SLresult IAndroidNoiseSuppression_SetEnabled(SLAndroidNoiseSuppressionItf self, SLboolean enabled)
31 {
32     SL_ENTER_INTERFACE
33 
34     IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
35     interface_lock_exclusive(thiz);
36     thiz->mEnabled = (SLboolean) enabled;
37     if (NO_NOISESUPPRESS(thiz)) {
38         result = SL_RESULT_CONTROL_LOST;
39     } else {
40         android::status_t status = thiz->mNSEffect->setEnabled((bool) thiz->mEnabled);
41         result = android_fx_statusToResult(status);
42     }
43     interface_unlock_exclusive(thiz);
44 
45     SL_LEAVE_INTERFACE
46 }
47 
IAndroidNoiseSuppression_IsEnabled(SLAndroidNoiseSuppressionItf self,SLboolean * pEnabled)48 SLresult IAndroidNoiseSuppression_IsEnabled(SLAndroidNoiseSuppressionItf self, SLboolean *pEnabled)
49 {
50     SL_ENTER_INTERFACE
51 
52     if (NULL == pEnabled) {
53         result = SL_RESULT_PARAMETER_INVALID;
54     } else {
55         IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
56         interface_lock_exclusive(thiz);
57         SLboolean enabled = thiz->mEnabled;
58         if (NO_NOISESUPPRESS(thiz)) {
59             result = SL_RESULT_CONTROL_LOST;
60         } else {
61             *pEnabled = (SLboolean) thiz->mNSEffect->getEnabled();
62             result = SL_RESULT_SUCCESS;
63         }
64         interface_unlock_exclusive(thiz);
65     }
66     SL_LEAVE_INTERFACE
67 }
68 
IAndroidNoiseSuppression_IsAvailable(SLAndroidNoiseSuppressionItf self,SLboolean * pEnabled)69 SLresult IAndroidNoiseSuppression_IsAvailable(SLAndroidNoiseSuppressionItf self,
70                                               SLboolean *pEnabled)
71 {
72     SL_ENTER_INTERFACE
73 
74     *pEnabled = false;
75 
76     uint32_t numEffects = 0;
77     int ret = EffectQueryNumberEffects(&numEffects);
78     if (ret != 0) {
79         ALOGE("IAndroidNoiseSuppression_IsAvailable() error %d querying number of effects", ret);
80         result = SL_RESULT_FEATURE_UNSUPPORTED;
81    } else {
82         ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
83 
84         effect_descriptor_t fxDesc;
85         for (uint32_t i = 0 ; i < numEffects ; i++) {
86             if (EffectQueryEffect(i, &fxDesc) == 0) {
87                 ALOGV("effect %d is called %s", i, fxDesc.name);
88                 if (memcmp(&fxDesc.type, SL_IID_ANDROIDNOISESUPPRESSION,
89                            sizeof(effect_uuid_t)) == 0) {
90                     ALOGI("found effect \"%s\" from %s", fxDesc.name, fxDesc.implementor);
91                     *pEnabled = true;
92                     break;
93                 }
94             }
95         }
96         result = SL_RESULT_SUCCESS;
97     }
98     SL_LEAVE_INTERFACE
99 }
100 
101 static const struct SLAndroidNoiseSuppressionItf_ IAndroidNoiseSuppression_Itf = {
102     IAndroidNoiseSuppression_SetEnabled,
103     IAndroidNoiseSuppression_IsEnabled,
104     IAndroidNoiseSuppression_IsAvailable
105 };
106 
IAndroidNoiseSuppression_init(void * self)107 void IAndroidNoiseSuppression_init(void *self)
108 {
109     IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
110     thiz->mItf = &IAndroidNoiseSuppression_Itf;
111     thiz->mEnabled = SL_BOOLEAN_FALSE;
112     memset(&thiz->mNSDescriptor, 0, sizeof(effect_descriptor_t));
113     // placement new (explicit constructor)
114     (void) new (&thiz->mNSEffect) android::sp<android::AudioEffect>();
115 }
116 
IAndroidNoiseSuppression_deinit(void * self)117 void IAndroidNoiseSuppression_deinit(void *self)
118 {
119     IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
120     // explicit destructor
121     thiz->mNSEffect.~sp();
122 }
123 
IAndroidNoiseSuppression_Expose(void * self)124 bool IAndroidNoiseSuppression_Expose(void *self)
125 {
126     IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
127     if (!android_fx_initEffectDescriptor(SL_IID_ANDROIDNOISESUPPRESSION, &thiz->mNSDescriptor)) {
128         SL_LOGE("Noise Suppression initialization failed.");
129         return false;
130     }
131     return true;
132 }
133