• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         if (NO_NOISESUPPRESS(thiz)) {
58             result = SL_RESULT_CONTROL_LOST;
59         } else {
60             *pEnabled = (SLboolean) thiz->mNSEffect->getEnabled();
61             result = SL_RESULT_SUCCESS;
62         }
63         interface_unlock_exclusive(thiz);
64     }
65     SL_LEAVE_INTERFACE
66 }
67 
IAndroidNoiseSuppression_IsAvailable(SLAndroidNoiseSuppressionItf self,SLboolean * pEnabled)68 SLresult IAndroidNoiseSuppression_IsAvailable(SLAndroidNoiseSuppressionItf self,
69                                               SLboolean *pEnabled)
70 {
71     SL_ENTER_INTERFACE
72 
73     *pEnabled = false;
74 
75     uint32_t numEffects = 0;
76     int ret = EffectQueryNumberEffects(&numEffects);
77     if (ret != 0) {
78         ALOGE("IAndroidNoiseSuppression_IsAvailable() error %d querying number of effects", ret);
79         result = SL_RESULT_FEATURE_UNSUPPORTED;
80    } else {
81         ALOGV("EffectQueryNumberEffects() numEffects=%d", numEffects);
82 
83         effect_descriptor_t fxDesc;
84         for (uint32_t i = 0 ; i < numEffects ; i++) {
85             if (EffectQueryEffect(i, &fxDesc) == 0) {
86                 ALOGV("effect %d is called %s", i, fxDesc.name);
87                 if (memcmp(&fxDesc.type, SL_IID_ANDROIDNOISESUPPRESSION,
88                            sizeof(effect_uuid_t)) == 0) {
89                     ALOGI("found effect \"%s\" from %s", fxDesc.name, fxDesc.implementor);
90                     *pEnabled = true;
91                     break;
92                 }
93             }
94         }
95         result = SL_RESULT_SUCCESS;
96     }
97     SL_LEAVE_INTERFACE
98 }
99 
100 static const struct SLAndroidNoiseSuppressionItf_ IAndroidNoiseSuppression_Itf = {
101     IAndroidNoiseSuppression_SetEnabled,
102     IAndroidNoiseSuppression_IsEnabled,
103     IAndroidNoiseSuppression_IsAvailable
104 };
105 
IAndroidNoiseSuppression_init(void * self)106 void IAndroidNoiseSuppression_init(void *self)
107 {
108     IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
109     thiz->mItf = &IAndroidNoiseSuppression_Itf;
110     thiz->mEnabled = SL_BOOLEAN_FALSE;
111     memset(&thiz->mNSDescriptor, 0, sizeof(effect_descriptor_t));
112     // placement new (explicit constructor)
113     (void) new (&thiz->mNSEffect) android::sp<android::AudioEffect>();
114 }
115 
IAndroidNoiseSuppression_deinit(void * self)116 void IAndroidNoiseSuppression_deinit(void *self)
117 {
118     IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
119     // explicit destructor
120     thiz->mNSEffect.~sp();
121 }
122 
IAndroidNoiseSuppression_Expose(void * self)123 bool IAndroidNoiseSuppression_Expose(void *self)
124 {
125     IAndroidNoiseSuppression *thiz = (IAndroidNoiseSuppression *) self;
126     if (!android_fx_initEffectDescriptor(SL_IID_ANDROIDNOISESUPPRESSION, &thiz->mNSDescriptor)) {
127         SL_LOGE("Noise Suppression initialization failed.");
128         return false;
129     }
130     return true;
131 }
132